From a762781343ae85d65d495ffebb865e8744d8ab76 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 18 Jul 2013 10:23:57 -0700 Subject: [PATCH] Convert to beast UnitTest --- Builds/VisualStudio2012/RippleD.vcxproj | 6 -- .../VisualStudio2012/RippleD.vcxproj.filters | 3 - TODO.txt | 2 + modules/ripple_app/ripple_app.cpp | 1 - src/cpp/ripple/ripple_ProofOfWork.cpp | 3 +- src/cpp/ripple/ripple_ProofOfWorkFactory.cpp | 69 +++++++++++++++++++ .../ripple_ProofOfWorkFactoryUnitTests.cpp | 61 ---------------- 7 files changed, 73 insertions(+), 72 deletions(-) diff --git a/Builds/VisualStudio2012/RippleD.vcxproj b/Builds/VisualStudio2012/RippleD.vcxproj index b741d0fa69..f4e259fdc9 100644 --- a/Builds/VisualStudio2012/RippleD.vcxproj +++ b/Builds/VisualStudio2012/RippleD.vcxproj @@ -820,12 +820,6 @@ true true - - true - true - true - true - true true diff --git a/Builds/VisualStudio2012/RippleD.vcxproj.filters b/Builds/VisualStudio2012/RippleD.vcxproj.filters index 964828c715..a2d81919ff 100644 --- a/Builds/VisualStudio2012/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2012/RippleD.vcxproj.filters @@ -594,9 +594,6 @@ [1] Ripple\ripple_app\_misc - - [1] Ripple\ripple_app\_misc - [1] Ripple\ripple_app\_misc diff --git a/TODO.txt b/TODO.txt index 91f07811af..38c8cdd5b9 100644 --- a/TODO.txt +++ b/TODO.txt @@ -3,6 +3,8 @@ RIPPLE TODO -------------------------------------------------------------------------------- Vinnie's Short List (Changes day to day) +- Memory NodeStore::Backend for unit tests +- Improved Mutex to track deadlocks - Convert some Ripple boost unit tests to Beast. - Eliminate new technical in NodeStore::Backend - Work on KeyvaDB diff --git a/modules/ripple_app/ripple_app.cpp b/modules/ripple_app/ripple_app.cpp index 305bd0c829..1d5bb112e0 100644 --- a/modules/ripple_app/ripple_app.cpp +++ b/modules/ripple_app/ripple_app.cpp @@ -431,7 +431,6 @@ static DH* handleTmpDh (SSL* ssl, int is_export, int iKeyLength) #include "ledger/LedgerUnitTests.cpp" #include "src/cpp/ripple/ripple_SHAMapUnitTests.cpp" #include "src/cpp/ripple/ripple_SHAMapSyncUnitTests.cpp" -#include "src/cpp/ripple/ripple_ProofOfWorkFactoryUnitTests.cpp" // Requires ProofOfWorkFactory.h #include "src/cpp/ripple/ripple_SerializedTransactionUnitTests.cpp" //------------------------------------------------------------------------------ diff --git a/src/cpp/ripple/ripple_ProofOfWork.cpp b/src/cpp/ripple/ripple_ProofOfWork.cpp index 03f407f7e7..3a6f7a0839 100644 --- a/src/cpp/ripple/ripple_ProofOfWork.cpp +++ b/src/cpp/ripple/ripple_ProofOfWork.cpp @@ -177,4 +177,5 @@ bool ProofOfWork::validateToken (const std::string& strToken) return boost::regex_match (strToken, smMatch, reToken); } -// vim:ts=4 +//------------------------------------------------------------------------------ + diff --git a/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp b/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp index 0cd7c776dd..c749b287cf 100644 --- a/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp +++ b/src/cpp/ripple/ripple_ProofOfWorkFactory.cpp @@ -231,3 +231,72 @@ IProofOfWorkFactory* IProofOfWorkFactory::New () return new ProofOfWorkFactory; } +//------------------------------------------------------------------------------ + +class ProofOfWorkTests : public UnitTest +{ +public: + ProofOfWorkTests () : UnitTest ("ProofOfWork") + { + } + + void runTest () + { + using namespace ripple; + + ProofOfWorkFactory gen; + ProofOfWork pow = gen.getProof (); + + String s; + + s << "solve difficulty " << String (pow.getDifficulty ()); + beginTest ("solve"); + + uint256 solution = pow.solve (16777216); + + expect (! solution.isZero (), "Should be solved"); + + expect (pow.checkSolution (solution), "Should be checked"); + + // Why is this emitted? + //WriteLog (lsDEBUG, ProofOfWork) << "A bad nonce error is expected"; + + POWResult r = gen.checkProof (pow.getToken (), uint256 ()); + + expect (r == powBADNONCE, "Should show bad nonce for empty solution"); + + expect (gen.checkProof (pow.getToken (), solution) == powOK, "Solution should check with issuer"); + + //WriteLog (lsDEBUG, ProofOfWork) << "A reused nonce error is expected"; + + expect (gen.checkProof (pow.getToken (), solution) == powREUSED, "Reuse solution should be detected"); + + #ifdef SOLVE_POWS + + for (int i = 0; i < 12; ++i) + { + gen.setDifficulty (i); + ProofOfWork pow = gen.getProof (); + WriteLog (lsINFO, ProofOfWork) << "Level: " << i << ", Estimated difficulty: " << pow.getDifficulty (); + uint256 solution = pow.solve (131072); + + if (solution.isZero ()) + { + //WriteLog (lsINFO, ProofOfWork) << "Giving up"; + } + else + { + //WriteLog (lsINFO, ProofOfWork) << "Solution found"; + + if (gen.checkProof (pow.getToken (), solution) != powOK) + { + //WriteLog (lsFATAL, ProofOfWork) << "Solution fails"; + } + } + } + + #endif + } +}; + +static ProofOfWorkTests proofOfWorkTests; diff --git a/src/cpp/ripple/ripple_ProofOfWorkFactoryUnitTests.cpp b/src/cpp/ripple/ripple_ProofOfWorkFactoryUnitTests.cpp index e96eccd485..121e1743be 100644 --- a/src/cpp/ripple/ripple_ProofOfWorkFactoryUnitTests.cpp +++ b/src/cpp/ripple/ripple_ProofOfWorkFactoryUnitTests.cpp @@ -4,64 +4,3 @@ */ //============================================================================== -BOOST_AUTO_TEST_SUITE (ProofOfWork_suite) - -BOOST_AUTO_TEST_CASE ( ProofOfWork_test ) -{ - using namespace ripple; - - ProofOfWorkFactory gen; - ProofOfWork pow = gen.getProof (); - WriteLog (lsINFO, ProofOfWork) << "Estimated difficulty: " << pow.getDifficulty (); - uint256 solution = pow.solve (16777216); - - if (solution.isZero ()) - BOOST_FAIL ("Unable to solve proof of work"); - - if (!pow.checkSolution (solution)) - BOOST_FAIL ("Solution did not check"); - - WriteLog (lsDEBUG, ProofOfWork) << "A bad nonce error is expected"; - POWResult r = gen.checkProof (pow.getToken (), uint256 ()); - - if (r != powBADNONCE) - { - Log (lsFATAL) << "POWResult = " << static_cast (r); - BOOST_FAIL ("Empty solution didn't show bad nonce"); - } - - if (gen.checkProof (pow.getToken (), solution) != powOK) - BOOST_FAIL ("Solution did not check with issuer"); - - WriteLog (lsDEBUG, ProofOfWork) << "A reused nonce error is expected"; - - if (gen.checkProof (pow.getToken (), solution) != powREUSED) - BOOST_FAIL ("Reuse solution not detected"); - -#ifdef SOLVE_POWS - - for (int i = 0; i < 12; ++i) - { - gen.setDifficulty (i); - ProofOfWork pow = gen.getProof (); - WriteLog (lsINFO, ProofOfWork) << "Level: " << i << ", Estimated difficulty: " << pow.getDifficulty (); - uint256 solution = pow.solve (131072); - - if (solution.isZero ()) - WriteLog (lsINFO, ProofOfWork) << "Giving up"; - else - { - WriteLog (lsINFO, ProofOfWork) << "Solution found"; - - if (gen.checkProof (pow.getToken (), solution) != powOK) - { - WriteLog (lsFATAL, ProofOfWork) << "Solution fails"; - } - } - } - -#endif - -} - -BOOST_AUTO_TEST_SUITE_END ()