Ethereum  PoC-8
The C++ Implementation of Ethereum
TrieCommon.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 */
22 #include "TrieCommon.h"
23 #include "SHA3.h"
24 
25 namespace dev
26 {
27 
28 h256 const EmptyTrie = sha3(rlp(""));
29 
30 /*
31  * Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1
32  * NOTE: the "termination marker" and "leaf-node" specifier are completely equivalent.
33  * [0,0,1,2,3,4,5] 0x10012345
34  * [0,1,2,3,4,5] 0x00012345
35  * [1,2,3,4,5] 0x112345
36  * [0,0,1,2,3,4] 0x00001234
37  * [0,1,2,3,4] 0x101234
38  * [1,2,3,4] 0x001234
39  * [0,0,1,2,3,4,5,T] 0x30012345
40  * [0,0,1,2,3,4,T] 0x20001234
41  * [0,1,2,3,4,5,T] 0x20012345
42  * [1,2,3,4,5,T] 0x312345
43  * [1,2,3,4,T] 0x201234
44  */
45 
46 std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf, int _begin, int _end)
47 {
48  unsigned begin = _begin;
49  unsigned end = _end < 0 ? _hexVector.size() + 1 + _end : _end;
50  bool odd = ((end - begin) % 2) != 0;
51 
52  std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
53  if (odd)
54  {
55  ret[0] |= _hexVector[begin];
56  ++begin;
57  }
58  for (unsigned i = begin; i < end; i += 2)
59  ret += _hexVector[i] * 16 + _hexVector[i + 1];
60  return ret;
61 }
62 
63 std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset)
64 {
65  unsigned begin = _beginNibble + _offset;
66  unsigned end = (_endNibble < 0 ? ((int)(_data.size() * 2 - _offset) + 1) + _endNibble : _endNibble) + _offset;
67  bool odd = (end - begin) & 1;
68 
69  std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
70  ret.reserve((end - begin) / 2 + 1);
71 
72  unsigned d = odd ? 1 : 2;
73  for (auto i = begin; i < end; ++i, ++d)
74  {
75  byte n = nibble(_data, i);
76  if (d & 1) // odd
77  ret.back() |= n; // or the nibble onto the back
78  else
79  ret.push_back(n << 4); // push the nibble on to the back << 4
80  }
81  return ret;
82 }
83 
84 std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf)
85 {
86  unsigned begin1 = _o1;
87  unsigned end1 = _d1.size() * 2;
88  unsigned begin2 = _o2;
89  unsigned end2 = _d2.size() * 2;
90 
91  bool odd = (end1 - begin1 + end2 - begin2) & 1;
92 
93  std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
94  ret.reserve((end1 - begin1 + end2 - begin2) / 2 + 1);
95 
96  unsigned d = odd ? 1 : 2;
97  for (auto i = begin1; i < end1; ++i, ++d)
98  {
99  byte n = nibble(_d1, i);
100  if (d & 1) // odd
101  ret.back() |= n; // or the nibble onto the back
102  else
103  ret.push_back(n << 4); // push the nibble on to the back << 4
104  }
105  for (auto i = begin2; i < end2; ++i, ++d)
106  {
107  byte n = nibble(_d2, i);
108  if (d & 1) // odd
109  ret.back() |= n; // or the nibble onto the back
110  else
111  ret.push_back(n << 4); // push the nibble on to the back << 4
112  }
113  return ret;
114 }
115 
116 byte uniqueInUse(RLP const& _orig, byte except)
117 {
118  byte used = 255;
119  for (unsigned i = 0; i < 17; ++i)
120  if (i != except && !_orig[i].isEmpty())
121  {
122  if (used == 255)
123  used = (byte)i;
124  else
125  return 255;
126  }
127  return used;
128 }
129 
130 }
dev::EmptyTrie
h256 const EmptyTrie
Definition: TrieCommon.cpp:28
dev::vector_ref
Definition: vector_ref.h:22
byte
uint8_t byte
Definition: Common.h:57
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
dev::hexPrefixEncode
std::string hexPrefixEncode(bytes const &_hexVector, bool _leaf, int _begin, int _end)
Definition: TrieCommon.cpp:46
dev::uniqueInUse
byte uniqueInUse(RLP const &_orig, byte except)
Definition: TrieCommon.cpp:116
dev::nibble
byte nibble(bytesConstRef _data, unsigned _i)
Definition: TrieCommon.h:31
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::bytes
std::vector< byte > bytes
Definition: Common.h:72
SHA3.h
dev::vector_ref::size
size_t size() const
Definition: vector_ref.h:53
dev
Definition: Address.cpp:21
dev::RLP
Definition: RLP.h:48