Browse Source

More testing on new Trie format.

cl-refactor
Gav Wood 11 years ago
parent
commit
5fbf9c752b
  1. 14
      libethereum/RLP.cpp
  2. 1
      libethereum/RLP.h
  3. 28
      libethereum/Trie.cpp
  4. 13
      test/main.cpp

14
libethereum/RLP.cpp

@ -125,6 +125,18 @@ RLPStream& RLPStream::append(std::string const& _s)
return *this;
}
RLPStream& RLPStream::appendString(bytes const& _s)
{
if (_s.size() < 0x38)
m_out.push_back(_s.size() | 0x40);
else
pushCount(_s.size(), 0x40);
uint os = m_out.size();
m_out.resize(os + _s.size());
memcpy(m_out.data() + os, _s.data(), _s.size());
return *this;
}
RLPStream& RLPStream::appendRaw(bytes const& _s)
{
uint os = m_out.size();
@ -202,7 +214,7 @@ std::ostream& operator<<(std::ostream& _out, eth::RLP _d)
else if (_d.isInt())
_out << std::showbase << std::hex << std::nouppercase << _d.toBigInt(RLP::LaisezFaire);
else if (_d.isString())
_out << eth::escaped(_d.toString(), true);
_out << eth::escaped(_d.toString(), false);
else if (_d.isList())
{
_out << "[";

1
libethereum/RLP.h

@ -278,6 +278,7 @@ public:
RLPStream& append(std::string const& _s);
RLPStream& appendList(uint _count);
RLPStream& appendRaw(bytes const& _rlp);
RLPStream& appendString(bytes const& _rlp);
/// Shift operators for appending data items.
RLPStream& operator<<(uint _i) { return append(_i); }

28
libethereum/Trie.cpp

@ -29,6 +29,12 @@ namespace eth
#define ENABLE_DEBUG_PRINT 0
/*/
#define APPEND_CHILD appendString
/*/
#define APPEND_CHILD appendRaw
/**/
#if ENABLE_DEBUG_PRINT
bool g_hashDebug = false;
#endif
@ -111,7 +117,7 @@ void hash256rlp(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_i
hash256aux(_s, _begin, _end, sharedPre, _rlp);
#if ENABLE_DEBUG_PRINT
if (g_hashDebug)
std::cerr << s_indent << "= " << sha3(_rlp.out()) << std::endl;
std::cerr << s_indent << "= " << hex << sha3(_rlp.out()) << std::endl;
#endif
}
else
@ -150,7 +156,7 @@ void hash256rlp(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_i
#if ENABLE_DEBUG_PRINT
if (g_hashDebug)
std::cerr << s_indent << "= " << sha3(_rlp.out()) << std::endl;
std::cerr << s_indent << "= " << hex << sha3(_rlp.out()) << std::endl;
#endif
}
}
@ -165,9 +171,20 @@ void hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_i
RLPStream rlp;
hash256rlp(_s, _begin, _end, _preLen, rlp);
if (rlp.out().size() < 32)
_rlp.appendRaw(rlp.out());
{
// RECURSIVE RLP
#if ENABLE_DEBUG_PRINT
cerr << "[INLINE: " << dec << rlp.out().size() << " < 32]" << endl;
#endif
_rlp.APPEND_CHILD(rlp.out());
}
else
{
#if ENABLE_DEBUG_PRINT
cerr << "[HASH: " << dec << rlp.out().size() << " >= 32]" << endl;
#endif
_rlp << toCompactBigEndianString(sha3(rlp.out()));
}
}
u256 hash256(StringMap const& _s)
@ -208,7 +225,7 @@ public:
void putRLP(RLPStream& _parentStream) const;
#if ENABLE_DEBUG_PRINT
void debugPrint(std::string const& _indent = "") const { std::cerr << std::hex << sha3() << ":" << std::endl; debugPrintBody(_indent); }
void debugPrint(std::string const& _indent = "") const { std::cerr << std::hex << hash256() << ":" << std::endl; debugPrintBody(_indent); }
#endif
/// 256-bit hash of the node - this is a SHA-3/256 hash of the RLP of the node.
@ -217,7 +234,6 @@ public:
void mark() { m_hash256 = 0; }
protected:
void submitRLP(RLPStream& _rlpStream, bytes const& _rlpFragment) const;
virtual void makeRLP(RLPStream& _intoStream) const = 0;
#if ENABLE_DEBUG_PRINT
@ -356,7 +372,7 @@ void TrieNode::putRLP(RLPStream& _parentStream) const
RLPStream s;
makeRLP(s);
if (s.out().size() < 32)
_parentStream.appendRaw(s.out());
_parentStream.APPEND_CHILD(s.out());
else
_parentStream << toCompactBigEndianString(eth::sha3(s.out()));
}

13
test/main.cpp

@ -93,9 +93,20 @@ int main()
*/
{
Trie t;
cout << hex << hash256(StringMap({})) << endl;
t.insert("dog", "puppy");
cout << hex << t.hash256() << endl;
cout << RLP(t.rlp()) << endl;
}
{
Trie t;
t.insert("bed", "d");
t.insert("be", "e");
cout << hex << t.hash256() << endl;
cout << RLP(t.rlp()) << endl;
}
{
cout << hex << hash256({{"dog", "puppy"}, {"doe", "reindeer"}}) << endl;
Trie t;
t.insert("dog", "puppy");
t.insert("doe", "reindeer");
cout << hex << t.hash256() << endl;

Loading…
Cancel
Save