TrieCommon.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file TrieCommon.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include "Common.h"
  20. #include "RLP.h"
  21. namespace dev
  22. {
  23. inline byte nibble(bytesConstRef _data, unsigned _i)
  24. {
  25. return (_i & 1) ? (_data[_i / 2] & 15) : (_data[_i / 2] >> 4);
  26. }
  27. /// Interprets @a _first and @a _second as vectors of nibbles and returns the length of the longest common
  28. /// prefix of _first[_beginFirst..._endFirst] and _second[_beginSecond..._endSecond].
  29. inline unsigned sharedNibbles(bytesConstRef _first, unsigned _beginFirst, unsigned _endFirst, bytesConstRef _second, unsigned _beginSecond, unsigned _endSecond)
  30. {
  31. unsigned ret = 0;
  32. while (_beginFirst < _endFirst && _beginSecond < _endSecond && nibble(_first, _beginFirst) == nibble(_second, _beginSecond))
  33. {
  34. ++_beginFirst;
  35. ++_beginSecond;
  36. ++ret;
  37. }
  38. return ret;
  39. }
  40. /**
  41. * Nibble-based view on a bytesConstRef.
  42. */
  43. struct NibbleSlice
  44. {
  45. bytesConstRef data;
  46. unsigned offset;
  47. NibbleSlice(bytesConstRef _data = bytesConstRef(), unsigned _offset = 0): data(_data), offset(_offset) {}
  48. byte operator[](unsigned _index) const { return nibble(data, offset + _index); }
  49. unsigned size() const { return data.size() * 2 - offset; }
  50. bool empty() const { return !size(); }
  51. NibbleSlice mid(unsigned _index) const { return NibbleSlice(data, offset + _index); }
  52. void clear() { data.reset(); offset = 0; }
  53. /// @returns true iff _k is a prefix of this.
  54. bool contains(NibbleSlice _k) const { return shared(_k) == _k.size(); }
  55. /// @returns the number of shared nibbles at the beginning of this and _k.
  56. unsigned shared(NibbleSlice _k) const { return sharedNibbles(data, offset, offset + size(), _k.data, _k.offset, _k.offset + _k.size()); }
  57. /**
  58. * @brief Determine if we, a full key, are situated prior to a particular key-prefix.
  59. * @param _k The prefix.
  60. * @return true if we are strictly prior to the prefix.
  61. */
  62. bool isEarlierThan(NibbleSlice _k) const
  63. {
  64. unsigned i = 0;
  65. for (; i < _k.size() && i < size(); ++i)
  66. if (operator[](i) < _k[i]) // Byte is lower - we're earlier..
  67. return true;
  68. else if (operator[](i) > _k[i]) // Byte is higher - we're not earlier.
  69. return false;
  70. if (i >= _k.size()) // Ran past the end of the prefix - we're == for the entire prefix - we're not earlier.
  71. return false;
  72. return true; // Ran out before the prefix had finished - we're earlier.
  73. }
  74. bool operator==(NibbleSlice _k) const { return _k.size() == size() && shared(_k) == _k.size(); }
  75. bool operator!=(NibbleSlice _s) const { return !operator==(_s); }
  76. };
  77. inline std::ostream& operator<<(std::ostream& _out, NibbleSlice const& _m)
  78. {
  79. for (unsigned i = 0; i < _m.size(); ++i)
  80. _out << std::hex << (int)_m[i] << std::dec;
  81. return _out;
  82. }
  83. inline bool isLeaf(RLP const& _twoItem)
  84. {
  85. assert(_twoItem.isList() && _twoItem.itemCount() == 2);
  86. auto pl = _twoItem[0].payload();
  87. return (pl[0] & 0x20) != 0;
  88. }
  89. inline NibbleSlice keyOf(bytesConstRef _hpe)
  90. {
  91. if (!_hpe.size())
  92. return NibbleSlice(_hpe, 0);
  93. if (_hpe[0] & 0x10)
  94. return NibbleSlice(_hpe, 1);
  95. else
  96. return NibbleSlice(_hpe, 2);
  97. }
  98. inline NibbleSlice keyOf(RLP const& _twoItem)
  99. {
  100. return keyOf(_twoItem[0].payload());
  101. }
  102. byte uniqueInUse(RLP const& _orig, byte except);
  103. std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf = false, int _begin = 0, int _end = -1);
  104. std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset);
  105. std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf);
  106. inline std::string hexPrefixEncode(NibbleSlice _s, bool _leaf, int _begin = 0, int _end = -1)
  107. {
  108. return hexPrefixEncode(_s.data, _leaf, _begin, _end, _s.offset);
  109. }
  110. inline std::string hexPrefixEncode(NibbleSlice _s1, NibbleSlice _s2, bool _leaf)
  111. {
  112. return hexPrefixEncode(_s1.data, _s1.offset, _s2.data, _s2.offset, _leaf);
  113. }
  114. }