dagger.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file dagger.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. * Dashimoto test functions.
  18. */
  19. #include <fstream>
  20. #include <json_spirit/JsonSpiritHeaders.h>
  21. #include <libdevcore/CommonIO.h>
  22. #include <libethashseal/Ethash.h>
  23. #include <libethashseal/EthashAux.h>
  24. #include <boost/test/unit_test.hpp>
  25. #include <test/TestHelper.h>
  26. using namespace std;
  27. using namespace dev;
  28. using namespace dev::eth;
  29. using namespace dev::test;
  30. namespace js = json_spirit;
  31. using dev::operator <<;
  32. BOOST_FIXTURE_TEST_SUITE(DashimotoTests, TestOutputHelper)
  33. BOOST_AUTO_TEST_CASE(basic_test)
  34. {
  35. string testPath = test::getTestPath();
  36. testPath += "/PoWTests";
  37. cnote << "Testing Proof of Work...";
  38. js::mValue v;
  39. string s = contentsString(testPath + "/ethash_tests.json");
  40. BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'ethash_tests.json' is empty. Have you cloned the 'tests' repo branch develop?");
  41. js::read_string(s, v);
  42. for (auto& i: v.get_obj())
  43. {
  44. cnote << i.first;
  45. js::mObject& o = i.second.get_obj();
  46. vector<pair<string, string>> ss;
  47. BlockHeader header(fromHex(o["header"].get_str()), HeaderData);
  48. h256 headerHash(o["header_hash"].get_str());
  49. Nonce nonce(o["nonce"].get_str());
  50. BOOST_REQUIRE_EQUAL(headerHash, header.hash(WithoutSeal));
  51. BOOST_REQUIRE_EQUAL(nonce, Ethash::nonce(header));
  52. unsigned cacheSize(o["cache_size"].get_int());
  53. h256 cacheHash(o["cache_hash"].get_str());
  54. BOOST_REQUIRE_EQUAL(EthashAux::get()->light(Ethash::seedHash(header))->size, cacheSize);
  55. BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(Ethash::seedHash(header))->data()), cacheHash);
  56. #if TEST_FULL
  57. unsigned fullSize(o["full_size"].get_int());
  58. h256 fullHash(o["full_hash"].get_str());
  59. BOOST_REQUIRE_EQUAL(EthashAux::get()->full(Ethash::seedHash(header))->size(), fullSize);
  60. BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(Ethash::seedHash(header))->data()), fullHash);
  61. #endif
  62. h256 result(o["result"].get_str());
  63. EthashProofOfWork::Result r = EthashAux::eval(Ethash::seedHash(header), header.hash(WithoutSeal), Ethash::nonce(header));
  64. BOOST_REQUIRE_EQUAL(r.value, result);
  65. BOOST_REQUIRE_EQUAL(r.mixHash, Ethash::mixHash(header));
  66. }
  67. }
  68. BOOST_AUTO_TEST_SUITE_END()