From fc9ad9eb5edf79944bb0c64f96f678c0ab5b394f Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 2 Mar 2015 14:34:45 +0100 Subject: [PATCH 1/3] add secure trie tests --- test/trie.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/test/trie.cpp b/test/trie.cpp index bdb188651..8a2598211 100644 --- a/test/trie.cpp +++ b/test/trie.cpp @@ -196,6 +196,48 @@ BOOST_AUTO_TEST_CASE(trie_test_anyorder) } } +BOOST_AUTO_TEST_CASE(trie_test_anyorder_secureTrie) +{ + string testPath = test::getTestPath(); + + testPath += "/TrieTests"; + + cnote << "Testing Trie..."; + js::mValue v; + string s = asString(contents(testPath + "/trieanyorder_secureTrie.json")); + BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trieanyorder.json' is empty. Have you cloned the 'tests' repo branch develop?"); + js::read_string(s, v); + for (auto& i: v.get_obj()) + { + cnote << i.first; + js::mObject& o = i.second.get_obj(); + vector> ss; + for (auto i: o["in"].get_obj()) + { + ss.push_back(make_pair(i.first, i.second.get_str())); + if (!ss.back().first.find("0x")) + ss.back().first = asString(fromHex(ss.back().first.substr(2))); + if (!ss.back().second.find("0x")) + ss.back().second = asString(fromHex(ss.back().second.substr(2))); + } + for (unsigned j = 0; j < min(1000u, dev::test::fac((unsigned)ss.size())); ++j) + { + next_permutation(ss.begin(), ss.end()); + MemoryDB m; + SecureGenericTrieDB t(&m); + t.init(); + BOOST_REQUIRE(t.check(true)); + for (auto const& k: ss) + { + t.insert(k.first, k.second); + BOOST_REQUIRE(t.check(true)); + } + BOOST_REQUIRE(!o["root"].is_null()); + BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray())); + } + } +} + BOOST_AUTO_TEST_CASE(trie_tests_ordered) { string testPath = test::getTestPath(); @@ -277,6 +319,69 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered) } } +BOOST_AUTO_TEST_CASE(trie_tests_ordered_secureTrie) +{ + string testPath = test::getTestPath(); + + testPath += "/TrieTests"; + + cnote << "Testing Trie..."; + js::mValue v; + string s = asString(contents(testPath + "/trietest_secureTrie.json")); + BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?"); + js::read_string(s, v); + + for (auto& i: v.get_obj()) + { + cnote << i.first; + js::mObject& o = i.second.get_obj(); + vector> ss; + vector keysToBeDeleted; + for (auto& i: o["in"].get_array()) + { + vector values; + for (auto& s: i.get_array()) + { + if (s.type() == json_spirit::str_type) + values.push_back(s.get_str()); + else if (s.type() == json_spirit::null_type) + { + // mark entry for deletion + values.push_back(""); + if (!values[0].find("0x")) + values[0] = asString(fromHex(values[0].substr(2))); + keysToBeDeleted.push_back(values[0]); + } + else + BOOST_FAIL("Bad type (expected string)"); + } + + BOOST_REQUIRE(values.size() == 2); + ss.push_back(make_pair(values[0], values[1])); + if (!ss.back().first.find("0x")) + ss.back().first = asString(fromHex(ss.back().first.substr(2))); + if (!ss.back().second.find("0x")) + ss.back().second = asString(fromHex(ss.back().second.substr(2))); + } + + MemoryDB m; + SecureGenericTrieDB t(&m); + t.init(); + BOOST_REQUIRE(t.check(true)); + for (auto const& k: ss) + { + if (find(keysToBeDeleted.begin(), keysToBeDeleted.end(), k.first) != keysToBeDeleted.end() && k.second.empty()) + t.remove(k.first); + else + t.insert(k.first, k.second); + BOOST_REQUIRE(t.check(true)); + } + + BOOST_REQUIRE(!o["root"].is_null()); + BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray())); + } +} + inline h256 stringMapHash256(StringMap const& _s) { return hash256(_s); From f572da381d53684f26c4615ff5a28a112400e04b Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Fri, 6 Mar 2015 01:13:45 +0100 Subject: [PATCH 2/3] merge --- test/trie.cpp | 105 -------------------------------------------------- 1 file changed, 105 deletions(-) diff --git a/test/trie.cpp b/test/trie.cpp index 8a2598211..bdb188651 100644 --- a/test/trie.cpp +++ b/test/trie.cpp @@ -196,48 +196,6 @@ BOOST_AUTO_TEST_CASE(trie_test_anyorder) } } -BOOST_AUTO_TEST_CASE(trie_test_anyorder_secureTrie) -{ - string testPath = test::getTestPath(); - - testPath += "/TrieTests"; - - cnote << "Testing Trie..."; - js::mValue v; - string s = asString(contents(testPath + "/trieanyorder_secureTrie.json")); - BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trieanyorder.json' is empty. Have you cloned the 'tests' repo branch develop?"); - js::read_string(s, v); - for (auto& i: v.get_obj()) - { - cnote << i.first; - js::mObject& o = i.second.get_obj(); - vector> ss; - for (auto i: o["in"].get_obj()) - { - ss.push_back(make_pair(i.first, i.second.get_str())); - if (!ss.back().first.find("0x")) - ss.back().first = asString(fromHex(ss.back().first.substr(2))); - if (!ss.back().second.find("0x")) - ss.back().second = asString(fromHex(ss.back().second.substr(2))); - } - for (unsigned j = 0; j < min(1000u, dev::test::fac((unsigned)ss.size())); ++j) - { - next_permutation(ss.begin(), ss.end()); - MemoryDB m; - SecureGenericTrieDB t(&m); - t.init(); - BOOST_REQUIRE(t.check(true)); - for (auto const& k: ss) - { - t.insert(k.first, k.second); - BOOST_REQUIRE(t.check(true)); - } - BOOST_REQUIRE(!o["root"].is_null()); - BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray())); - } - } -} - BOOST_AUTO_TEST_CASE(trie_tests_ordered) { string testPath = test::getTestPath(); @@ -319,69 +277,6 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered) } } -BOOST_AUTO_TEST_CASE(trie_tests_ordered_secureTrie) -{ - string testPath = test::getTestPath(); - - testPath += "/TrieTests"; - - cnote << "Testing Trie..."; - js::mValue v; - string s = asString(contents(testPath + "/trietest_secureTrie.json")); - BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?"); - js::read_string(s, v); - - for (auto& i: v.get_obj()) - { - cnote << i.first; - js::mObject& o = i.second.get_obj(); - vector> ss; - vector keysToBeDeleted; - for (auto& i: o["in"].get_array()) - { - vector values; - for (auto& s: i.get_array()) - { - if (s.type() == json_spirit::str_type) - values.push_back(s.get_str()); - else if (s.type() == json_spirit::null_type) - { - // mark entry for deletion - values.push_back(""); - if (!values[0].find("0x")) - values[0] = asString(fromHex(values[0].substr(2))); - keysToBeDeleted.push_back(values[0]); - } - else - BOOST_FAIL("Bad type (expected string)"); - } - - BOOST_REQUIRE(values.size() == 2); - ss.push_back(make_pair(values[0], values[1])); - if (!ss.back().first.find("0x")) - ss.back().first = asString(fromHex(ss.back().first.substr(2))); - if (!ss.back().second.find("0x")) - ss.back().second = asString(fromHex(ss.back().second.substr(2))); - } - - MemoryDB m; - SecureGenericTrieDB t(&m); - t.init(); - BOOST_REQUIRE(t.check(true)); - for (auto const& k: ss) - { - if (find(keysToBeDeleted.begin(), keysToBeDeleted.end(), k.first) != keysToBeDeleted.end() && k.second.empty()) - t.remove(k.first); - else - t.insert(k.first, k.second); - BOOST_REQUIRE(t.check(true)); - } - - BOOST_REQUIRE(!o["root"].is_null()); - BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray())); - } -} - inline h256 stringMapHash256(StringMap const& _s) { return hash256(_s); From a63893ede973f2ab4ff2771ea12ce9497c48d593 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Fri, 6 Mar 2015 01:15:12 +0100 Subject: [PATCH 3/3] check for minGasLimit and minDifficulty when constructing block --- libethcore/BlockInfo.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index 270f96b07..3b4878a7d 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -125,10 +125,16 @@ void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce) BOOST_THROW_EXCEPTION(InvalidBlockNonce(headerHash(WithoutNonce), nonce, difficulty)); if (gasUsed > gasLimit) - BOOST_THROW_EXCEPTION(TooMuchGasUsed()); + BOOST_THROW_EXCEPTION(TooMuchGasUsed() << RequirementError(bigint(gasLimit), bigint(gasUsed)) ); + + if (difficulty < c_minimumDifficulty) + BOOST_THROW_EXCEPTION(InvalidDifficulty() << RequirementError(bigint(c_minimumDifficulty), bigint(difficulty)) ); + + if (gasLimit < c_minGasLimit) + BOOST_THROW_EXCEPTION(InvalidGasLimit() << RequirementError(bigint(c_minGasLimit), bigint(gasLimit)) ); if (number && extraData.size() > c_maximumExtraDataSize) - BOOST_THROW_EXCEPTION(ExtraDataTooBig()); + BOOST_THROW_EXCEPTION(ExtraDataTooBig() << RequirementError(bigint(c_maximumExtraDataSize), bigint(extraData.size()))); } void BlockInfo::populate(bytesConstRef _block, bool _checkNonce) @@ -207,7 +213,7 @@ void BlockInfo::verifyParent(BlockInfo const& _parent) const { // Check difficulty is correct given the two timestamps. if (difficulty != calculateDifficulty(_parent)) - BOOST_THROW_EXCEPTION(InvalidDifficulty()); + BOOST_THROW_EXCEPTION(InvalidDifficulty() << RequirementError((bigint)calculateDifficulty(_parent), (bigint)difficulty)); if (gasLimit < _parent.gasLimit * (c_gasLimitBoundDivisor - 1) / c_gasLimitBoundDivisor || gasLimit > _parent.gasLimit * (c_gasLimitBoundDivisor + 1) / c_gasLimitBoundDivisor)