Ethereum  PoC-8
The C++ Implementation of Ethereum
TrieCommon.h
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 #pragma once
23 
24 #include "Common.h"
25 #include "RLP.h"
26 
27 namespace dev
28 {
29 extern const h256 EmptyTrie;
30 
31 inline byte nibble(bytesConstRef _data, unsigned _i)
32 {
33  return (_i & 1) ? (_data[_i / 2] & 15) : (_data[_i / 2] >> 4);
34 }
35 
38 inline unsigned sharedNibbles(bytesConstRef _first, unsigned _beginFirst, unsigned _endFirst, bytesConstRef _second, unsigned _beginSecond, unsigned _endSecond)
39 {
40  unsigned ret = 0;
41  while (_beginFirst < _endFirst && _beginSecond < _endSecond && nibble(_first, _beginFirst) == nibble(_second, _beginSecond))
42  {
43  ++_beginFirst;
44  ++_beginSecond;
45  ++ret;
46  }
47  return ret;
48 }
49 
54 {
56  unsigned offset;
57 
58  NibbleSlice(bytesConstRef _data = bytesConstRef(), unsigned _offset = 0): data(_data), offset(_offset) {}
59  byte operator[](unsigned _index) const { return nibble(data, offset + _index); }
60  unsigned size() const { return data.size() * 2 - offset; }
61  bool empty() const { return !size(); }
62  NibbleSlice mid(unsigned _index) const { return NibbleSlice(data, offset + _index); }
63  void clear() { data.reset(); offset = 0; }
64 
66  bool contains(NibbleSlice _k) const { return shared(_k) == _k.size(); }
68  unsigned shared(NibbleSlice _k) const { return sharedNibbles(data, offset, offset + size(), _k.data, _k.offset, _k.offset + _k.size()); }
74  bool isEarlierThan(NibbleSlice _k) const
75  {
76  unsigned i = 0;
77  for (; i < _k.size() && i < size(); ++i)
78  if (operator[](i) < _k[i]) // Byte is lower - we're earlier..
79  return true;
80  else if (operator[](i) > _k[i]) // Byte is higher - we're not earlier.
81  return false;
82  if (i >= _k.size()) // Ran past the end of the prefix - we're == for the entire prefix - we're not earlier.
83  return false;
84  return true; // Ran out before the prefix had finished - we're earlier.
85  }
86  bool operator==(NibbleSlice _k) const { return _k.size() == size() && shared(_k) == _k.size(); }
87  bool operator!=(NibbleSlice _s) const { return !operator==(_s); }
88 };
89 
90 inline std::ostream& operator<<(std::ostream& _out, NibbleSlice const& _m)
91 {
92  for (unsigned i = 0; i < _m.size(); ++i)
93  _out << std::hex << (int)_m[i] << std::dec;
94  return _out;
95 }
96 
97 inline bool isLeaf(RLP const& _twoItem)
98 {
99  assert(_twoItem.isList() && _twoItem.itemCount() == 2);
100  auto pl = _twoItem[0].payload();
101  return (pl[0] & 0x20) != 0;
102 }
103 
105 {
106  if (!_hpe.size())
107  return NibbleSlice(_hpe, 0);
108  if (_hpe[0] & 0x10)
109  return NibbleSlice(_hpe, 1);
110  else
111  return NibbleSlice(_hpe, 2);
112 }
113 
114 inline NibbleSlice keyOf(RLP const& _twoItem)
115 {
116  return keyOf(_twoItem[0].payload());
117 }
118 
119 byte uniqueInUse(RLP const& _orig, byte except);
120 std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf = false, int _begin = 0, int _end = -1);
121 std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset);
122 std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf);
123 
124 inline std::string hexPrefixEncode(NibbleSlice _s, bool _leaf, int _begin = 0, int _end = -1)
125 {
126  return hexPrefixEncode(_s.data, _leaf, _begin, _end, _s.offset);
127 }
128 
129 inline std::string hexPrefixEncode(NibbleSlice _s1, NibbleSlice _s2, bool _leaf)
130 {
131  return hexPrefixEncode(_s1.data, _s1.offset, _s2.data, _s2.offset, _leaf);
132 }
133 
134 }
dev::NibbleSlice::clear
void clear()
Definition: TrieCommon.h:63
dev::EmptyTrie
h256 const EmptyTrie
Definition: TrieCommon.cpp:28
dev::sharedNibbles
unsigned sharedNibbles(bytesConstRef _first, unsigned _beginFirst, unsigned _endFirst, bytesConstRef _second, unsigned _beginSecond, unsigned _endSecond)
Definition: TrieCommon.h:38
dev::vector_ref< byte const >
dev::NibbleSlice::shared
unsigned shared(NibbleSlice _k) const
Definition: TrieCommon.h:68
dev::NibbleSlice::operator[]
byte operator[](unsigned _index) const
Definition: TrieCommon.h:59
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::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::nibble
byte nibble(bytesConstRef _data, unsigned _i)
Definition: TrieCommon.h:31
dev::RLP::isList
bool isList() const
List value.
Definition: RLP.h:95
dev::NibbleSlice::operator==
bool operator==(NibbleSlice _k) const
Definition: TrieCommon.h:86
dev::NibbleSlice::operator!=
bool operator!=(NibbleSlice _s) const
Definition: TrieCommon.h:87
dev::operator<<
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:77
Common.h
dev::NibbleSlice::contains
bool contains(NibbleSlice _k) const
Definition: TrieCommon.h:66
dev::keyOf
NibbleSlice keyOf(bytesConstRef _hpe)
Definition: TrieCommon.h:104
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::NibbleSlice::isEarlierThan
bool isEarlierThan(NibbleSlice _k) const
Determine if we, a full key, are situated prior to a particular key-prefix.
Definition: TrieCommon.h:74
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
dev::NibbleSlice::NibbleSlice
NibbleSlice(bytesConstRef _data=bytesConstRef(), unsigned _offset=0)
Definition: TrieCommon.h:58
dev::vector_ref::size
size_t size() const
Definition: vector_ref.h:53
dev::RLP::payload
bytesConstRef payload() const
Definition: RLP.h:308
dev::NibbleSlice
Definition: TrieCommon.h:54
dev::NibbleSlice::offset
unsigned offset
Definition: TrieCommon.h:56
dev::RLP::itemCount
size_t itemCount() const
Definition: RLP.h:101
dev::isLeaf
bool isLeaf(RLP const &_twoItem)
Definition: TrieCommon.h:97
dev
Definition: Address.cpp:21
dev::NibbleSlice::mid
NibbleSlice mid(unsigned _index) const
Definition: TrieCommon.h:62
dev::vector_ref::reset
void reset()
Definition: vector_ref.h:102
dev::NibbleSlice::empty
bool empty() const
Definition: TrieCommon.h:61
dev::RLP
Definition: RLP.h:48
RLP.h
dev::NibbleSlice::size
unsigned size() const
Definition: TrieCommon.h:60
dev::NibbleSlice::data
bytesConstRef data
Definition: TrieCommon.h:55