32 #define ENABLE_DEBUG_PRINT 0 49 void putRLP(
RLPStream& _parentStream)
const;
51 #if ENABLE_DEBUG_PRINT 52 void debugPrint(std::string
const& _indent =
"")
const { std::cerr << std::hex <<
hash256() <<
":" << std::dec << std::endl; debugPrintBody(_indent); }
61 virtual void makeRLP(
RLPStream& _intoStream)
const = 0;
63 #if ENABLE_DEBUG_PRINT 64 virtual void debugPrintBody(std::string
const& _indent =
"")
const = 0;
73 static const std::string c_nullString;
88 memset(m_nodes.data(), 0,
sizeof(
MemTrieNode*) * 16);
93 memset(m_nodes.data(), 0,
sizeof(
MemTrieNode*) * 16);
99 memset(m_nodes.data(), 0,
sizeof(
MemTrieNode*) * 16);
106 for (
auto i: m_nodes)
110 #if ENABLE_DEBUG_PRINT 111 virtual void debugPrintBody(std::string
const& _indent)
const 115 std::cerr << _indent <<
"@: " << m_value << std::endl;
116 for (
auto i = 0; i < 16; ++i)
119 std::cerr << _indent << std::hex << i <<
": " << std::dec;
120 m_nodes[i]->debugPrint(_indent +
" ");
125 virtual std::string
const& at(
bytesConstRef _key)
const override;
128 virtual void makeRLP(
RLPStream& _parentStream)
const override;
132 byte activeBranch()
const;
145 #if ENABLE_DEBUG_PRINT 146 virtual void debugPrintBody(std::string
const& _indent)
const 149 std::cerr << _indent;
151 std::cerr <<
toHex(m_ext, 1) <<
": ";
154 std::cerr << m_value << std::endl;
161 virtual void makeRLP(
RLPStream& _parentStream)
const override;
175 #if ENABLE_DEBUG_PRINT 176 virtual void debugPrintBody(std::string
const& _indent)
const 178 std::cerr << _indent <<
toHex(m_ext, 1) <<
": ";
179 m_next->debugPrint(_indent +
" ");
186 virtual void makeRLP(
RLPStream& _parentStream)
const override;
194 void MemTrieNode::putRLP(
RLPStream& _parentStream)
const 198 if (s.
out().size() < 32)
199 _parentStream.APPEND_CHILD(s.
out());
204 void TrieBranchNode::makeRLP(
RLPStream& _intoStream)
const 207 for (
auto i: m_nodes)
209 i->putRLP(_intoStream);
212 _intoStream << m_value;
215 void TrieLeafNode::makeRLP(
RLPStream& _intoStream)
const 220 void TrieInfixNode::makeRLP(
RLPStream& _intoStream)
const 225 m_next->putRLP(_intoStream);
251 else if (m_nodes[_key[0]] !=
nullptr)
252 return m_nodes[_key[0]]->
at(_key.
cropped(1));
263 if (!m_nodes[_key[0]])
264 m_nodes[_key[0]] =
new TrieLeafNode(_key.cropped(1), _value);
266 m_nodes[_key[0]] = m_nodes[_key[0]]->
insert(_key.cropped(1), _value);
279 else if (m_nodes[_key[0]] !=
nullptr)
281 m_nodes[_key[0]] = m_nodes[_key[0]]->remove(_key.cropped(1));
290 byte n = activeBranch();
292 if (n == (
byte)-1 && m_value.size())
299 else if (n < 16 && m_value.empty())
302 if (
auto b = dynamic_cast<TrieBranchNode*>(m_nodes[n]))
305 m_nodes[n] =
nullptr;
315 m_nodes[n] =
nullptr;
324 byte TrieBranchNode::activeBranch()
const 327 for (
int i = 0; i < 16; ++i)
328 if (m_nodes[i] !=
nullptr)
344 m_next = m_next->insert(_key.
cropped(m_ext.size()), _value);
370 ret->
insert(_key, _value);
381 m_next = m_next->remove(_key.
cropped(m_ext.size()));
382 if (
auto p = dynamic_cast<TrieExtNode*>(m_next))
385 m_ext.reserve(m_ext.size() + p->m_ext.size());
386 for (
auto i: p->m_ext)
415 auto n = MemTrieNode::newBranch(_key, _value,
bytesConstRef(&m_ext), m_value);
446 void MemTrie::debugPrint()
448 #if ENABLE_DEBUG_PRINT 450 m_root->debugPrint();
454 std::string
const& MemTrie::at(std::string
const& _key)
const 462 void MemTrie::insert(std::string
const& _key, std::string
const& _value)
470 void MemTrie::remove(std::string
const& _key)
475 m_root = m_root->remove(&
h);
unsigned commonPrefix(T const &_t, _U const &_u)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
virtual MemTrieNode * insert(bytesConstRef _key, std::string const &_value) override
virtual ~TrieBranchNode()
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
void hash256(RaIter first, RaIter last, OutIter first2, OutIter last2)
TrieExtNode(bytesConstRef _bytes)
TrieBranchNode(std::string const &_value)
std::string hexPrefixEncode(bytes const &_hexVector, bool _leaf, int _begin, int _end)
bytes const & out() const
Read the byte stream.
virtual MemTrieNode * insert(bytesConstRef _key, std::string const &_value) override
void trimFront(T &_t, unsigned _elements)
Trims a given number of elements from the front of a collection.
std::hash for asio::adress
assert(len-trim+(2 *lenIndices)<=WIDTH)
vector_ref< _T > cropped(size_t _begin, size_t _count) const
std::array< MemTrieNode *, 16 > m_nodes
TrieBranchNode(byte _i1, MemTrieNode *_n1, std::string const &_value=std::string())
h256 hash256() const
256-bit hash of the node - this is a SHA-3/256 hash of the RLP of the node.
TrieLeafNode(bytesConstRef _key, std::string const &_value)
bool contains(T const &_t, V const &_v)
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
RLPStream & appendList(size_t _items)
Appends a list.
bool contains(bytesConstRef _key) const
bytes asNibbles(bytesConstRef const &_s)
virtual std::string const & at(bytesConstRef _key) const override
TrieInfixNode(bytesConstRef _key, MemTrieNode *_next)
void pushFront(T &_t, _U _e)
Pushes an element on to the front of a collection.
bool contains(bytesConstRef _key) const
TrieBranchNode(byte _i1, MemTrieNode *_n1, byte _i2, MemTrieNode *_n2)
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
virtual std::string const & at(bytesConstRef _key) const =0
Class for writing to an RLP bytestream.
virtual std::string const & at(bytesConstRef _key) const override