4

How do I get the Merkle Patricia tries of a block from a client? Do clients implement a standard call to retrieve it?

1

As far as I know, no client provide such an API.

I've created a repository on Github with an example showing how to read all Patricia trie entries in a block https://github.com/medvedev1088/ethereum-merkle-patricia-trie-example

var Trie = require('merkle-patricia-tree');
var rlp = require('rlp');
var levelup = require('levelup');
var leveldown = require('leveldown');
var db = levelup(leveldown('/your_home_dir/Library/Ethereum/rinkeby/geth/chaindata'));

// the block state root, rinkeby, block number 1775804
// the block state root can be obtained by invoking web3.eth.getBlock(<blockNumber>) in `stateRoot` field
var root = '0xe4a6ff741ec2e0d0cd274a745756028df27312161bdb4557b6da434349f716a9';
var trie = new Trie(db, root);

trie.checkRoot(root, function (err, val) {
  console.log('Root exists:', val);
});

var stream = trie.createReadStream();

stream.on('data', function (data) {
  console.log('key:' + data.key.toString('hex'));

  //accouts are rlp encoded
  var decodedVal = rlp.decode(data.value);
  console.log(decodedVal);
});

stream.on('end', function (val) {
  console.log('done reading!');
});

Example output:

Root exists: true
key:000104f8b6aa6b1035516117a98bf1c06873dc9016950e4befd79a7c533cd074
[ <Buffer 01>,
  <Buffer >,
  <Buffer 56 e8 1f 17 1b cc 55 a6 ff 83 45 e6 92 c0 f8 6e 5b 48 e0 1b 99 6c ad c0 01 62 2f b5 e3 63 b4 21>,
  <Buffer d1 d2 9e e7 4a 6d 03 24 41 89 dd b3 92 39 ad c2 a5 f7 7b a9 1a 8d f4 59 f1 7a 17 2d bd 96 21 3d> ]
key:00016a7e1a013fb93d632850f6af2dab61fe8db06a47f11a4e99170fa8bb5996
[ <Buffer 01>,
  <Buffer >,
  <Buffer 56 e8 1f 17 1b cc 55 a6 ff 83 45 e6 92 c0 f8 6e 5b 48 e0 1b 99 6c ad c0 01 62 2f b5 e3 63 b4 21>,
  <Buffer d1 d2 9e e7 4a 6d 03 24 41 89 dd b3 92 39 ad c2 a5 f7 7b a9 1a 8d f4 59 f1 7a 17 2d bd 96 21 3d> ]

Each keys is the keccak hash of an address, each value is an array of nonce, balance, storageRoot, codeHash.

What's interesting is that the keys are hashes of addresses, so you can't list all of the addresses by reading the Patricia trie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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