From f6cd7ab36d610dc1ed6c0f95cc97e96282ae68d1 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Wed, 5 Aug 2015 09:43:04 +0200 Subject: [PATCH] add difficutly tests --- libethcore/BlockInfo.h | 1 + test/libethcore/difficulty.cpp | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 test/libethcore/difficulty.cpp diff --git a/libethcore/BlockInfo.h b/libethcore/BlockInfo.h index 389d0a8ef..d37b70302 100644 --- a/libethcore/BlockInfo.h +++ b/libethcore/BlockInfo.h @@ -128,6 +128,7 @@ public: void setCoinbaseAddress(Address const& _v) { m_coinbaseAddress = _v; noteDirty(); } void setRoots(h256 const& _t, h256 const& _r, h256 const& _u, h256 const& _s) { m_transactionsRoot = _t; m_receiptsRoot = _r; m_stateRoot = _s; m_sha3Uncles = _u; noteDirty(); } void setGasUsed(u256 const& _v) { m_gasUsed = _v; noteDirty(); } + void setNumber(u256 const& _v) { m_number = _v; noteDirty(); } void setGasLimit(u256 const& _v) { m_gasLimit = _v; noteDirty(); } void setExtraData(bytes const& _v) { m_extraData = _v; noteDirty(); } void setLogBloom(LogBloom const& _v) { m_logBloom = _v; noteDirty(); } diff --git a/test/libethcore/difficulty.cpp b/test/libethcore/difficulty.cpp new file mode 100644 index 000000000..6defcd51d --- /dev/null +++ b/test/libethcore/difficulty.cpp @@ -0,0 +1,62 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file difficulty.cpp + * @author Christoph Jentzsch + * @date 2015 + * difficulty calculation tests. + */ + + +#include +#include +#include + +using namespace std; +using namespace dev; +using namespace dev::eth; + +namespace js = json_spirit; + +BOOST_AUTO_TEST_SUITE(DifficultyTests) + +BOOST_AUTO_TEST_CASE(difficultyTests) +{ + string testPath = test::getTestPath(); + testPath += "/BasicTests"; + + js::mValue v; + string s = contentsString(testPath + "/difficulty.json"); + BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'difficulty.json' is empty. Have you cloned the 'tests' repo branch develop?"); + js::read_string(s, v); + + for (auto& i: v.get_obj()) + { + js::mObject o = i.second.get_obj(); + cnote << "Difficulty test: " << i.first; + BlockInfo parent; + parent.setTimestamp(test::toInt(o["parentTimestamp"])); + parent.setDifficulty(test::toInt(o["parentDifficulty"])); + + BlockInfo current; + current.setTimestamp(test::toInt(o["currentTimestamp"])); + current.setNumber(test::toInt(o["currentBlockNumber"])); + + BOOST_CHECK_EQUAL(current.calculateDifficulty(parent), test::toInt(o["currentDifficulty"])); + } +} + +BOOST_AUTO_TEST_SUITE_END()