Fabcoin Core  0.16.2
P2P Digital Currency
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 
30 inline byte nibble(bytesConstRef _data, unsigned _i)
31 {
32  return (_i & 1) ? (_data[_i / 2] & 15) : (_data[_i / 2] >> 4);
33 }
34 
37 inline unsigned sharedNibbles(bytesConstRef _first, unsigned _beginFirst, unsigned _endFirst, bytesConstRef _second, unsigned _beginSecond, unsigned _endSecond)
38 {
39  unsigned ret = 0;
40  while (_beginFirst < _endFirst && _beginSecond < _endSecond && nibble(_first, _beginFirst) == nibble(_second, _beginSecond))
41  {
42  ++_beginFirst;
43  ++_beginSecond;
44  ++ret;
45  }
46  return ret;
47 }
48 
53 {
55  unsigned offset;
56 
57  NibbleSlice(bytesConstRef _data = bytesConstRef(), unsigned _offset = 0): data(_data), offset(_offset) {}
58  byte operator[](unsigned _index) const { return nibble(data, offset + _index); }
59  unsigned size() const { return data.size() * 2 - offset; }
60  bool empty() const { return !size(); }
61  NibbleSlice mid(unsigned _index) const { return NibbleSlice(data, offset + _index); }
62  void clear() { data.reset(); offset = 0; }
63 
65  bool contains(NibbleSlice _k) const { return shared(_k) == _k.size(); }
67  unsigned shared(NibbleSlice _k) const { return sharedNibbles(data, offset, offset + size(), _k.data, _k.offset, _k.offset + _k.size()); }
73  bool isEarlierThan(NibbleSlice _k) const
74  {
75  unsigned i = 0;
76  for (; i < _k.size() && i < size(); ++i)
77  if (operator[](i) < _k[i]) // Byte is lower - we're earlier..
78  return true;
79  else if (operator[](i) > _k[i]) // Byte is higher - we're not earlier.
80  return false;
81  if (i >= _k.size()) // Ran past the end of the prefix - we're == for the entire prefix - we're not earlier.
82  return false;
83  return true; // Ran out before the prefix had finished - we're earlier.
84  }
85  bool operator==(NibbleSlice _k) const { return _k.size() == size() && shared(_k) == _k.size(); }
86  bool operator!=(NibbleSlice _s) const { return !operator==(_s); }
87 };
88 
89 inline std::ostream& operator<<(std::ostream& _out, NibbleSlice const& _m)
90 {
91  for (unsigned i = 0; i < _m.size(); ++i)
92  _out << std::hex << (int)_m[i] << std::dec;
93  return _out;
94 }
95 
96 inline bool isLeaf(RLP const& _twoItem)
97 {
98  assert(_twoItem.isList() && _twoItem.itemCount() == 2);
99  auto pl = _twoItem[0].payload();
100  return (pl[0] & 0x20) != 0;
101 }
102 
104 {
105  if (!_hpe.size())
106  return NibbleSlice(_hpe, 0);
107  if (_hpe[0] & 0x10)
108  return NibbleSlice(_hpe, 1);
109  else
110  return NibbleSlice(_hpe, 2);
111 }
112 
113 inline NibbleSlice keyOf(RLP const& _twoItem)
114 {
115  return keyOf(_twoItem[0].payload());
116 }
117 
118 byte uniqueInUse(RLP const& _orig, byte except);
119 std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf = false, int _begin = 0, int _end = -1);
120 std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset);
121 std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf);
122 
123 inline std::string hexPrefixEncode(NibbleSlice _s, bool _leaf, int _begin = 0, int _end = -1)
124 {
125  return hexPrefixEncode(_s.data, _leaf, _begin, _end, _s.offset);
126 }
127 
128 inline std::string hexPrefixEncode(NibbleSlice _s1, NibbleSlice _s2, bool _leaf)
129 {
130  return hexPrefixEncode(_s1.data, _s1.offset, _s2.data, _s2.offset, _leaf);
131 }
132 
133 }
bool operator!=(NibbleSlice _s) const
Definition: TrieCommon.h:86
byte nibble(bytesConstRef _data, unsigned _i)
Definition: TrieCommon.h:30
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint8_t byte
Definition: Common.h:57
bool operator==(NibbleSlice _k) const
Definition: TrieCommon.h:85
bool isList() const
List value.
Definition: RLP.h:112
size_t itemCount() const
Definition: RLP.h:118
byte operator[](unsigned _index) const
Definition: TrieCommon.h:58
std::string hexPrefixEncode(bytes const &_hexVector, bool _leaf, int _begin, int _end)
Definition: TrieCommon.cpp:43
unsigned size() const
Definition: TrieCommon.h:59
assert(len-trim+(2 *lenIndices)<=WIDTH)
bool isLeaf(RLP const &_twoItem)
Definition: TrieCommon.h:96
unsigned sharedNibbles(bytesConstRef _first, unsigned _beginFirst, unsigned _endFirst, bytesConstRef _second, unsigned _beginSecond, unsigned _endSecond)
Interprets _first and _second as vectors of nibbles and returns the length of the longest common pref...
Definition: TrieCommon.h:37
bool empty() const
Definition: TrieCommon.h:60
NibbleSlice keyOf(bytesConstRef _hpe)
Definition: TrieCommon.h:103
NibbleSlice mid(unsigned _index) const
Definition: TrieCommon.h:61
bytesConstRef payload() const
Definition: RLP.h:321
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
unsigned offset
Definition: TrieCommon.h:55
unsigned shared(NibbleSlice _k) const
Definition: TrieCommon.h:67
size_t size() const
Definition: vector_ref.h:55
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:80
bytesConstRef data
Definition: TrieCommon.h:54
Nibble-based view on a bytesConstRef.
Definition: TrieCommon.h:52
byte uniqueInUse(RLP const &_orig, byte except)
Definition: TrieCommon.cpp:113
NibbleSlice(bytesConstRef _data=bytesConstRef(), unsigned _offset=0)
Definition: TrieCommon.h:57
bool isEarlierThan(NibbleSlice _k) const
Determine if we, a full key, are situated prior to a particular key-prefix.
Definition: TrieCommon.h:73
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
bool contains(NibbleSlice _k) const
Definition: TrieCommon.h:65