Browse Source

orderedTrieRoot testing

cl-refactor
arkpar 10 years ago
parent
commit
4e358001ac
  1. 14
      libdevcore/CommonData.cpp
  2. 3
      libdevcore/CommonData.h
  3. 55
      libdevcrypto/TrieHash.cpp
  4. 10
      libdevcrypto/TrieHash.h
  5. 12
      libethcore/BlockInfo.cpp
  6. 3
      test/libdevcrypto/trie.cpp

14
libdevcore/CommonData.cpp

@ -115,19 +115,7 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw)
return ret;
}
bytes dev::asNibbles(bytes const& _s)
{
std::vector<uint8_t> ret;
ret.reserve(_s.size() * 2);
for (auto i: _s)
{
ret.push_back(i / 16);
ret.push_back(i % 16);
}
return ret;
}
bytes dev::asNibbles(std::string const& _s)
bytes dev::asNibbles(bytesConstRef const& _s)
{
std::vector<uint8_t> ret;
ret.reserve(_s.size() * 2);

3
libdevcore/CommonData.h

@ -95,8 +95,7 @@ inline bytes asBytes(std::string const& _b)
/// Converts a string into the big-endian base-16 stream of integers (NOT ASCII).
/// @example asNibbles("A")[0] == 4 && asNibbles("A")[1] == 1
bytes asNibbles(std::string const& _s);
bytes asNibbles(bytes const& _s);
bytes asNibbles(bytesConstRef const& _s);
// Big-endian to/from host endian conversion functions.

55
libdevcrypto/TrieHash.cpp

@ -165,7 +165,7 @@ bytes rlp256(BytesMap const& _s)
return rlp("");
HexMap hexMap;
for (auto i = _s.rbegin(); i != _s.rend(); ++i)
hexMap[asNibbles(i->first)] = i->second;
hexMap[asNibbles(bytesConstRef(&i->first))] = i->second;
RLPStream s;
hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s);
return s.out();
@ -176,61 +176,22 @@ h256 hash256(BytesMap const& _s)
return sha3(rlp256(_s));
}
h256 hash256(StringMap const& _s)
{
// build patricia tree.
if (_s.empty())
return sha3(rlp(""));
HexMap hexMap;
for (auto i = _s.rbegin(); i != _s.rend(); ++i)
hexMap[asNibbles(i->first)] = bytes(i->second.begin(), i->second.end());
RLPStream s;
hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s);
return sha3(s.out());
}
bytes rlp256(StringMap const& _s)
{
// build patricia tree.
if (_s.empty())
return rlp("");
HexMap hexMap;
for (auto i = _s.rbegin(); i != _s.rend(); ++i)
hexMap[asNibbles(i->first)] = bytes(i->second.begin(), i->second.end());
RLPStream s;
hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s);
return s.out();
}
/*h256 orderedTrieRoot(std::vector<bytes> const& _data)
h256 orderedTrieRoot(std::vector<bytes> const& _data)
{
StringMap m;
BytesMap m;
unsigned j = 0;
for (auto i: _data)
m[asString(rlp(j++))] = asString(i);
m[rlp(j++)] = i;
return hash256(m);
}*/
h256 orderedTrieRoot(std::vector<bytesConstRef> const& _data)
{
MemoryDB db;
GenericTrieDB<MemoryDB> t(&db);
t.init();
unsigned j = 0;
for (auto i: _data)
t.insert(rlp(j++), i.toBytes());
return t.root();
}
h256 orderedTrieRoot(std::vector<bytes> const& _data)
h256 orderedTrieRoot(std::vector<bytesConstRef> const& _data)
{
MemoryDB db;
GenericTrieDB<MemoryDB> t(&db);
t.init();
BytesMap m;
unsigned j = 0;
for (auto i: _data)
t.insert(rlp(j++), i);
return t.root();
m[rlp(j++)] = i.toBytes();
return hash256(m);
}
}

10
libdevcrypto/TrieHash.h

@ -29,18 +29,16 @@ namespace dev
bytes rlp256(BytesMap const& _s);
h256 hash256(BytesMap const& _s);
bytes rlp256(StringMap const& _s);
h256 hash256(StringMap const& _s);
/*h256 orderedTrieRoot(std::vector<bytes> const& _data);
h256 orderedTrieRoot(std::vector<bytes> const& _data);
template <class T, class U> inline h256 trieRootOver(unsigned _itemCount, T const& _getKey, U const& _getValue)
{
StringMap m;
BytesMap m;
for (unsigned i = 0; i < _itemCount; ++i)
m[asString(_getKey(i))] = asString(_getValue(i));
m[_getKey(i)] = _getValue(i);
return hash256(m);
}*/
}
h256 orderedTrieRoot(std::vector<bytesConstRef> const& _data);
h256 orderedTrieRoot(std::vector<bytes> const& _data);

12
libethcore/BlockInfo.cpp

@ -191,22 +191,12 @@ void BlockInfo::populate(bytesConstRef _block, Strictness _s, h256 const& _h)
struct BlockInfoDiagnosticsChannel: public LogChannel { static const char* name() { return EthBlue "" EthWhite ""; } static const int verbosity = 9; };
template <class T, class U> h256 trieRootOver(unsigned _itemCount, T const& _getKey, U const& _getValue)
{
MemoryDB db;
GenericTrieDB<MemoryDB> t(&db);
t.init();
for (unsigned i = 0; i < _itemCount; ++i)
t.insert(_getKey(i), _getValue(i));
return t.root();
}
void BlockInfo::verifyInternals(bytesConstRef _block) const
{
RLP root(_block);
auto txList = root[1];
auto expectedRoot = trieRootOver(txList.itemCount(), [&](unsigned i){ return rlp(i); }, [&](unsigned i){ return txList[i].data(); });
auto expectedRoot = trieRootOver(txList.itemCount(), [&](unsigned i){ return rlp(i); }, [&](unsigned i){ return txList[i].data().toBytes(); });
clog(BlockInfoDiagnosticsChannel) << "Expected trie root:" << toString(expectedRoot);
if (transactionsRoot != expectedRoot)

3
test/libdevcrypto/trie.cpp

@ -296,7 +296,6 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered)
h256 stringMapHash256(StringMap const& _s)
{
//return hash256(_s);
BytesMap bytesMap;
for (auto const& _v: _s)
bytesMap.insert(std::make_pair(bytes(_v.first.begin(), _v.first.end()), bytes(_v.second.begin(), _v.second.end())));
@ -305,7 +304,6 @@ h256 stringMapHash256(StringMap const& _s)
bytes stringMapRlp256(StringMap const& _s)
{
//return rlp256(_s);
BytesMap bytesMap;
for (auto const& _v: _s)
bytesMap.insert(std::make_pair(bytes(_v.first.begin(), _v.first.end()), bytes(_v.second.begin(), _v.second.end())));
@ -407,7 +405,6 @@ BOOST_AUTO_TEST_CASE(moreTrieTests)
cout << d;
cout << m;
cout << d.root() << endl;
cout << hash256(s) << endl;
cout << stringMapHash256(s) << endl;
BOOST_REQUIRE(d.check(true));

Loading…
Cancel
Save