¿Cómo Merkle Tree obtiene el hash del nodo raíz (ejemplo de 'Comprender el ethereum trie')?

Leí Comprender el ethereum trie y trato de hacer ejemplos de ese artículo. Ahora estoy en el ex1.pyprimer ejemplo. Mira el código de ese ejemplo:

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')

después de trabajar alrededor de la línea:

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

Vi cómo se crea el código state.root_hash, en el ejemplo es:

15da97c42b7ed2e1c0c8dab6a6d7e3d9dc0a75580bbc4f1f29c33996d1415dcc

Obtengo otro, pero no entiendo cómo conseguirlo. ¿Alguien puede explicarme?

Entonces tenemos:

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

¿Cómo de eso llegamos state.root_hash?

Respuestas (1)

Leí mal el código: cuando llegamos

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

lo que equivale a:

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

este valor almacenado como [key, value]par en root_node. Entonces obtenemos el RLP común de root_node(por ejemplo, rlp(['\x01\x01\x02', \xc6\x85hello'])). y obtenga la clave para la nueva línea de entrada en LevelDB, bajo clave = sha3 de ese nuevo valor:

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

lo que equivale a:

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

Estos valores los obtuvimos en la trie.pylínea 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

entonces tenemosstate.root_hash