TrieHash.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 TrieHash.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #if !defined(ETH_EMSCRIPTEN)
  19. #include "TrieHash.h"
  20. #include "TrieCommon.h"
  21. #include "TrieDB.h" // @TODO replace ASAP!
  22. #include "SHA3.h"
  23. using namespace std;
  24. using namespace dev;
  25. namespace dev
  26. {
  27. /*/
  28. #define APPEND_CHILD appendData
  29. /*/
  30. #define APPEND_CHILD appendRaw
  31. /**/
  32. #define ENABLE_DEBUG_PRINT 0
  33. #if ENABLE_DEBUG_PRINT
  34. bool g_hashDebug = false;
  35. #endif
  36. void hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream& _rlp);
  37. void hash256rlp(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream& _rlp)
  38. {
  39. #if ENABLE_DEBUG_PRINT
  40. static std::string s_indent;
  41. if (_preLen)
  42. s_indent += " ";
  43. #endif
  44. if (_begin == _end)
  45. _rlp << ""; // NULL
  46. else if (std::next(_begin) == _end)
  47. {
  48. // only one left - terminate with the pair.
  49. _rlp.appendList(2) << hexPrefixEncode(_begin->first, true, _preLen) << _begin->second;
  50. #if ENABLE_DEBUG_PRINT
  51. if (g_hashDebug)
  52. std::cerr << s_indent << toHex(bytesConstRef(_begin->first.data() + _preLen, _begin->first.size() - _preLen), 1) << ": " << _begin->second << " = " << sha3(_rlp.out()) << std::endl;
  53. #endif
  54. }
  55. else
  56. {
  57. // find the number of common prefix nibbles shared
  58. // i.e. the minimum number of nibbles shared at the beginning between the first hex string and each successive.
  59. unsigned sharedPre = (unsigned)-1;
  60. unsigned c = 0;
  61. for (auto i = std::next(_begin); i != _end && sharedPre; ++i, ++c)
  62. {
  63. unsigned x = std::min(sharedPre, std::min((unsigned)_begin->first.size(), (unsigned)i->first.size()));
  64. unsigned shared = _preLen;
  65. for (; shared < x && _begin->first[shared] == i->first[shared]; ++shared) {}
  66. sharedPre = std::min(shared, sharedPre);
  67. }
  68. if (sharedPre > _preLen)
  69. {
  70. // if they all have the same next nibble, we also want a pair.
  71. #if ENABLE_DEBUG_PRINT
  72. if (g_hashDebug)
  73. std::cerr << s_indent << toHex(bytesConstRef(_begin->first.data() + _preLen, sharedPre), 1) << ": " << std::endl;
  74. #endif
  75. _rlp.appendList(2) << hexPrefixEncode(_begin->first, false, _preLen, (int)sharedPre);
  76. hash256aux(_s, _begin, _end, (unsigned)sharedPre, _rlp);
  77. #if ENABLE_DEBUG_PRINT
  78. if (g_hashDebug)
  79. std::cerr << s_indent << "= " << hex << sha3(_rlp.out()) << dec << std::endl;
  80. #endif
  81. }
  82. else
  83. {
  84. // otherwise enumerate all 16+1 entries.
  85. _rlp.appendList(17);
  86. auto b = _begin;
  87. if (_preLen == b->first.size())
  88. {
  89. #if ENABLE_DEBUG_PRINT
  90. if (g_hashDebug)
  91. std::cerr << s_indent << "@: " << b->second << std::endl;
  92. #endif
  93. ++b;
  94. }
  95. for (auto i = 0; i < 16; ++i)
  96. {
  97. auto n = b;
  98. for (; n != _end && n->first[_preLen] == i; ++n) {}
  99. if (b == n)
  100. _rlp << "";
  101. else
  102. {
  103. #if ENABLE_DEBUG_PRINT
  104. if (g_hashDebug)
  105. std::cerr << s_indent << std::hex << i << ": " << std::dec << std::endl;
  106. #endif
  107. hash256aux(_s, b, n, _preLen + 1, _rlp);
  108. }
  109. b = n;
  110. }
  111. if (_preLen == _begin->first.size())
  112. _rlp << _begin->second;
  113. else
  114. _rlp << "";
  115. #if ENABLE_DEBUG_PRINT
  116. if (g_hashDebug)
  117. std::cerr << s_indent << "= " << hex << sha3(_rlp.out()) << dec << std::endl;
  118. #endif
  119. }
  120. }
  121. #if ENABLE_DEBUG_PRINT
  122. if (_preLen)
  123. s_indent.resize(s_indent.size() - 2);
  124. #endif
  125. }
  126. void hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen, RLPStream& _rlp)
  127. {
  128. RLPStream rlp;
  129. hash256rlp(_s, _begin, _end, _preLen, rlp);
  130. if (rlp.out().size() < 32)
  131. {
  132. // RECURSIVE RLP
  133. #if ENABLE_DEBUG_PRINT
  134. cerr << "[INLINE: " << dec << rlp.out().size() << " < 32]" << endl;
  135. #endif
  136. _rlp.APPEND_CHILD(rlp.out());
  137. }
  138. else
  139. {
  140. #if ENABLE_DEBUG_PRINT
  141. cerr << "[HASH: " << dec << rlp.out().size() << " >= 32]" << endl;
  142. #endif
  143. _rlp << sha3(rlp.out());
  144. }
  145. }
  146. bytes rlp256(BytesMap const& _s)
  147. {
  148. // build patricia tree.
  149. if (_s.empty())
  150. return rlp("");
  151. HexMap hexMap;
  152. for (auto i = _s.rbegin(); i != _s.rend(); ++i)
  153. hexMap[asNibbles(bytesConstRef(&i->first))] = i->second;
  154. RLPStream s;
  155. hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s);
  156. return s.out();
  157. }
  158. h256 hash256(BytesMap const& _s)
  159. {
  160. return sha3(rlp256(_s));
  161. }
  162. h256 orderedTrieRoot(std::vector<bytes> const& _data)
  163. {
  164. BytesMap m;
  165. unsigned j = 0;
  166. for (auto i: _data)
  167. m[rlp(j++)] = i;
  168. return hash256(m);
  169. }
  170. h256 orderedTrieRoot(std::vector<bytesConstRef> const& _data)
  171. {
  172. BytesMap m;
  173. unsigned j = 0;
  174. for (auto i: _data)
  175. m[rlp(j++)] = i.toBytes();
  176. return hash256(m);
  177. }
  178. }
  179. #endif // ETH_EMSCRIPTEN