You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
4.0 KiB

  1. Merkle
  2. ======
  3. For an overview of Merkle trees, see
  4. `wikipedia <https://en.wikipedia.org/wiki/Merkle_tree>`__.
  5. There are two types of Merkle trees used in Tendermint.
  6. - **IAVL+ Tree**: An immutable self-balancing binary
  7. tree for persistent application state
  8. - **Simple Tree**: A simple compact binary tree for
  9. a static list of items
  10. IAVL+ Tree
  11. ----------
  12. The purpose of this data structure is to provide persistent storage for
  13. key-value pairs (e.g. account state, name-registrar data, and
  14. per-contract data) such that a deterministic merkle root hash can be
  15. computed. The tree is balanced using a variant of the `AVL
  16. algorithm <http://en.wikipedia.org/wiki/AVL_tree>`__ so all operations
  17. are O(log(n)).
  18. Nodes of this tree are immutable and indexed by its hash. Thus any node
  19. serves as an immutable snapshot which lets us stage uncommitted
  20. transactions from the mempool cheaply, and we can instantly roll back to
  21. the last committed state to process transactions of a newly committed
  22. block (which may not be the same set of transactions as those from the
  23. mempool).
  24. In an AVL tree, the heights of the two child subtrees of any node differ
  25. by at most one. Whenever this condition is violated upon an update, the
  26. tree is rebalanced by creating O(log(n)) new nodes that point to
  27. unmodified nodes of the old tree. In the original AVL algorithm, inner
  28. nodes can also hold key-value pairs. The AVL+ algorithm (note the plus)
  29. modifies the AVL algorithm to keep all values on leaf nodes, while only
  30. using branch-nodes to store keys. This simplifies the algorithm while
  31. minimizing the size of merkle proofs
  32. In Ethereum, the analog is the `Patricia
  33. trie <http://en.wikipedia.org/wiki/Radix_tree>`__. There are tradeoffs.
  34. Keys do not need to be hashed prior to insertion in IAVL+ trees, so this
  35. provides faster iteration in the key space which may benefit some
  36. applications. The logic is simpler to implement, requiring only two
  37. types of nodes -- inner nodes and leaf nodes. The IAVL+ tree is a binary
  38. tree, so merkle proofs are much shorter than the base 16 Patricia trie.
  39. On the other hand, while IAVL+ trees provide a deterministic merkle root
  40. hash, it depends on the order of updates. In practice this shouldn't be
  41. a problem, since you can efficiently encode the tree structure when
  42. serializing the tree contents.
  43. Simple Tree
  44. -----------
  45. For merkelizing smaller static lists, use the Simple Tree. The
  46. transactions and validation signatures of a block are hashed using this
  47. simple merkle tree logic.
  48. If the number of items is not a power of two, the tree will not be full
  49. and some leaf nodes will be at different levels. Simple Tree tries to
  50. keep both sides of the tree the same size, but the left side may be one
  51. greater.
  52. ::
  53. Simple Tree with 6 items Simple Tree with 7 items
  54. * *
  55. / \ / \
  56. / \ / \
  57. / \ / \
  58. / \ / \
  59. * * * *
  60. / \ / \ / \ / \
  61. / \ / \ / \ / \
  62. / \ / \ / \ / \
  63. * h2 * h5 * * * h6
  64. / \ / \ / \ / \ / \
  65. h0 h1 h3 h4 h0 h1 h2 h3 h4 h5
  66. Simple Tree with Dictionaries
  67. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. The Simple Tree is used to merkelize a list of items, so to merkelize a
  69. (short) dictionary of key-value pairs, encode the dictionary as an
  70. ordered list of ``KVPair`` structs. The block hash is such a hash
  71. derived from all the fields of the block ``Header``. The state hash is
  72. similarly derived.