2

I read Understanding the ethereum trie and try to do examples from that article. Now i am at ex1.py - first exaple. Look at code from that example:

import sys
sys.path.append('src')
import trie, utils, rlp

#initialize trie
state = trie.Trie('triedb', trie.BLANK_ROOT)
state.update('\x01\x01\x02', rlp.encode(['hello']))
print 'root hash', state.root_hash.encode('hex')
k, v = state.root_node
print 'root node:', [k, v]
print 'hp encoded key, in hex:', k.encode('hex')

after work around line:

state.update('\x01\x01\x02', rlp.encode(['hello']))

I saw how code create state.root_hash, in example it is:

15da97c42b7ed2e1c0c8dab6a6d7e3d9dc0a75580bbc4f1f29c33996d1415dcc

I obtain another, but not understood how get it. Can anyone explain me?

So we has:

key = pack_nibbles(with_terminator('\x01\x01\x02')
value = rlp.encode(['hello'])

How from that we got state.root_hash?

1 Answer 1

2

I read code bad: when we got

key = pack_nibbles(with_terminator('\x01\x01\x02')
value = rlp.encode(['hello'])

which is tantamount to:

key = '\x01\x01\x02'
value = '\xc6\x85hello'

this value stored like [key, value] pair in root_node. Then we get common RLP of root_node (e.g. rlp(['\x01\x01\x02', \xc6\x85hello'])). and obtain key for new entry line in LevelDB, under key = sha3 of that new value:

// one line in DB with
key:   '̈́ �ƅhello
value: 'J[�Q�H+�� ���^�$q�b�����*{'

which is tantamount to:

key:   [' \x01\x01\x02', '\xc6\x85hello']
value: '4a5b19d151e796482b08a1e020f1f7ef5ea7240c0171fd629598fee612892a7b'

This values we got in trie.py, line 151, func get_root_hash:

def get_root_hash(self):
    #...
    val = rlp.encode(self.root_node)
    key = utils.sha3(val)
    self.db.put(key, val)
    return key

so we got state.root_hash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.