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:
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
In [3]:
from trie import HexaryTrie
Let's start by creating an empty tree (or trie):
In [5]:
t = HexaryTrie(db={})
t.root_hash
Out[5]:
The Hexary trie stores both keys and values, like a dictionary, and can be accessed like a dictionary.
In [7]:
t[b'my-key'] = b'some-value'
In [8]:
t[b'my-key']
Out[8]:
In [9]:
b'another-key' in t
Out[9]:
In [10]:
t[b'another-key'] = b'another-value'
In [11]:
b'another-key' in t
Out[11]:
In [12]:
t.root_hash
Out[12]:
But it also has a simple API with methods you can call in the tree
In [14]:
[m for m in dir(t) if not m.startswith('_')]
Out[14]:
In [12]:
t.set(b'my-key2',b'second value')
In [13]:
t.exists(b'my-key2')
Out[13]:
In [14]:
t.get(b'my-key2')
Out[14]:
In [15]:
t.
In [16]:
t.db
Out[16]:
In [ ]: