I am currently trying to implement Ethereum's Merkle Patricia Trie using Python 3.6 and I am having some troubles and honestly I am frustrated.
I am using the following sources:
- Ethereum Wiki Patricia Tree
- Merkling in Ethereum
- Understanding the Ethereum Trie - Blog
- Understanding the Ethereum Trie - Github Python Code
- Trie Test
I do get the concept of the Merkle Patricia Trie (MPT) and how it works. Yet, I am having problems implementing it.
First I would like to know of the example trie is given in Ethereum Witi is correct or not. I feel that it is not correct.
A trie which contains the following key/values pairs: ('do', 'verb')
, ('dog', 'puppy')
, ('doge', 'coin')
, ('horse', 'stallion')
Their result:
rootHash: [ <16>, hashA ]
hashA: [ <>, <>, <>, <>, hashB, <>, <>, <>, hashC, <>, <>, <>, <>, <>, <>, <>, <> ]
hashC: [ <20 6f 72 73 65>, 'stallion' ]
hashB: [ <00 6f>, hashD ]
hashD: [ <>, <>, <>, <>, <>, <>, hashE, <>, <>, <>, <>, <>, <>, <>, <>, <>, 'verb' ]
hashE: [ <17>, hashF ]
hashF: [ <>, <>, <>, <>, <>, <>, hashG, <>, <>, <>, <>, <>, <>, <>, <>, <>, 'puppy' ]
hashG: [ <35>, 'coin' ]
My result:
rootHash: [ <16>, hashA ]
hashA: [ <>, <>, <>, <>, hashB, <>, <>, <>, [ <20 6f 72 73 65>, 'stallion' ], <>, <>, <>, <>, <>, <>, <>, <> ]
hashB: [ <00 6f>, hashD ]
hashD: [ <>, <>, <>, <>, <>, <>, hashE, <>, <>, <>, <>, <>, <>, <>, <>, <>, 'verb' ]
hashE: [ <17>, [ <>, <>, <>, <>, <>, <>, [ <35>, 'coin' ], <>, <>, <>, <>, <>, <>, <>, <>, <>, 'puppy' ] ]
Why does it look so different? Right under the example is the following sentence:
When one node is referenced inside another node, what is included is
H(rlp.encode(x)), where H(x) = sha3(x) if len(x) >= 32 else x and
rlp.encode is the RLP encoding function.
Furthermore, I tried to compare the results from my code with the JS MPT implementation and this gives a completely different root hash.
I am wondering, what is correct? Do I misunderstand the example? Are there any other "better" documentation? I appreciate any help.
[]
to a separate hash. For example if you extract[ <20 6f 72 73 65>, 'stallion' ]
ashashC
then hashA will match the expected output.