Ethereum  PoC-8
The C++ Implementation of Ethereum
TrieHash.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "TrieHash.h"
19 #include "TrieCommon.h"
20 #include "TrieDB.h" // @TODO replace ASAP!
21 
22 namespace dev
23 {
24 
25 void hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream& _rlp);
26 
27 void hash256rlp(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream& _rlp)
28 {
29  if (_begin == _end)
30  _rlp << ""; // NULL
31  else if (std::next(_begin) == _end)
32  {
33  // only one left - terminate with the pair.
34  _rlp.appendList(2) << hexPrefixEncode(_begin->first, true, _preLen) << _begin->second;
35  }
36  else
37  {
38  // find the number of common prefix nibbles shared
39  // i.e. the minimum number of nibbles shared at the beginning between the first hex string and each successive.
40  unsigned sharedPre = (unsigned)-1;
41  unsigned c = 0;
42  for (auto i = std::next(_begin); i != _end && sharedPre; ++i, ++c)
43  {
44  unsigned x = std::min(sharedPre, std::min((unsigned)_begin->first.size(), (unsigned)i->first.size()));
45  unsigned shared = _preLen;
46  for (; shared < x && _begin->first[shared] == i->first[shared]; ++shared) {}
47  sharedPre = std::min(shared, sharedPre);
48  }
49  if (sharedPre > _preLen)
50  {
51  // if they all have the same next nibble, we also want a pair.
52  _rlp.appendList(2) << hexPrefixEncode(_begin->first, false, _preLen, (int)sharedPre);
53  hash256aux(_s, _begin, _end, (unsigned)sharedPre, _rlp);
54  }
55  else
56  {
57  // otherwise enumerate all 16+1 entries.
58  _rlp.appendList(17);
59  auto b = _begin;
60  if (_preLen == b->first.size())
61  ++b;
62  for (auto i = 0; i < 16; ++i)
63  {
64  auto n = b;
65  for (; n != _end && n->first[_preLen] == i; ++n) {}
66  if (b == n)
67  _rlp << "";
68  else
69  hash256aux(_s, b, n, _preLen + 1, _rlp);
70  b = n;
71  }
72  if (_preLen == _begin->first.size())
73  _rlp << _begin->second;
74  else
75  _rlp << "";
76 
77  }
78  }
79 }
80 
81 void hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream& _rlp)
82 {
83  RLPStream rlp;
84  hash256rlp(_s, _begin, _end, _preLen, rlp);
85  if (rlp.out().size() < 32)
86  {
87  // RECURSIVE RLP
88  _rlp.appendRaw(rlp.out());
89  }
90  else
91  _rlp << sha3(rlp.out());
92 }
93 
94 bytes rlp256(BytesMap const& _s)
95 {
96  // build patricia tree.
97  if (_s.empty())
98  return rlp("");
99  HexMap hexMap;
100  for (auto i = _s.rbegin(); i != _s.rend(); ++i)
101  hexMap[asNibbles(bytesConstRef(&i->first))] = i->second;
102  RLPStream s;
103  hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s);
104  return s.out();
105 }
106 
108 {
109  return sha3(rlp256(_s));
110 }
111 
112 h256 orderedTrieRoot(std::vector<bytes> const& _data)
113 {
114  BytesMap m;
115  unsigned j = 0;
116  for (auto i: _data)
117  m[rlp(j++)] = i;
118  return hash256(m);
119 }
120 
121 h256 orderedTrieRoot(std::vector<bytesConstRef> const& _data)
122 {
123  BytesMap m;
124  unsigned j = 0;
125  for (auto i: _data)
126  m[rlp(j++)] = i.toBytes();
127  return hash256(m);
128 }
129 
130 }
dev::HexMap
std::map< bytes, bytes > HexMap
Definition: Common.h:136
dev::asNibbles
bytes asNibbles(bytesConstRef const &_s)
Definition: CommonData.cpp:111
dev::BytesMap
std::map< bytes, bytes > BytesMap
Definition: Common.h:134
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
dev::hash256rlp
void hash256rlp(HexMap const &_s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream &_rlp)
Definition: TrieHash.cpp:27
dev::hexPrefixEncode
std::string hexPrefixEncode(bytes const &_hexVector, bool _leaf, int _begin, int _end)
Definition: TrieCommon.cpp:46
dev::orderedTrieRoot
h256 orderedTrieRoot(std::vector< bytes > const &_data)
Definition: TrieHash.cpp:112
dev::RLPStream::out
bytes const & out() const
Read the byte stream.
Definition: RLP.h:419
TrieCommon.h
dev::rlp
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:453
dev::FixedHash< 32 >
dev::rlp256
bytes rlp256(BytesMap const &_s)
Definition: TrieHash.cpp:94
dev::hash256
h256 hash256(BytesMap const &_s)
Definition: TrieHash.cpp:107
TrieDB.h
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
TrieHash.h
dev::RLPStream
Class for writing to an RLP bytestream.
Definition: RLP.h:370
dev::hash256aux
void hash256aux(HexMap const &_s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream &_rlp)
Definition: TrieHash.cpp:81
dev
Definition: Address.cpp:21
dev::RLPStream::appendRaw
RLPStream & appendRaw(bytesConstRef _rlp, size_t _itemCount=1)
Appends raw (pre-serialised) RLP data. Use with caution.
Definition: RLP.cpp:222
dev::RLPStream::appendList
RLPStream & appendList(size_t _items)
Appends a list.
Definition: RLP.cpp:268