from trie import HexaryTrie

"""
Explanation: Understanding the use of the Merkle-Patricia tree in Ethereum
Ethereum innovated when designing their blockchain, in many ways when compared to Bitcoin. One of them was my enhancing the kind of Merkle tree used within the blocks and how they are used.
In Ethereum, in contrast to the Bitcoim blockchain, they use three Merkle trees inside each block:
* one for transactions
* another for receipts (data showing the effects of transactions)
* and the third for the State
<img src="ethblockchain_full.png" width=50% />
In this noteboook we will explore using python code how all this merkling works. For that you will need to install the trie package maintained byt the folks at Ethereum:
pip install --user trie
End of explanation
"""


t = HexaryTrie(db={})
t.root_hash

"""
Explanation: Let's start by creating an empty tree (or trie):
End of explanation
"""


t[b'my-key'] = b'some-value'

t[b'my-key']

b'another-key' in t

t[b'another-key']  = b'another-value'

b'another-key' in t

t.root_hash

"""
Explanation: The Hexary trie stores both keys and values, like a dictionary, and can be accessed like a dictionary.
End of explanation
"""


[m for m in dir(t) if not m.startswith('_')]

t.set(b'my-key2',b'second value')

t.exists(b'my-key2')

t.get(b'my-key2')

t.

t.db

"""
Explanation: But it also has a simple API with methods you can call in the tree
End of explanation
"""


