2

As an exercise I want to retrieve all transactions from a block and rebuild the merkle patricia trie, then compare the root hash with the transactionRoot stored in the block. My current logic is receive block with complete transactions and then store each one in the trie using the necessary rlp encoding and serialization but I do not recreate the hash stored in the block.

for reference the block i am using can be found here the block hash is 0x441bcda29969fe4c94790b8538b70e30b66c62b69fe6516bea99634d290d16c7

the four block transactions in raw format (from etherscan) are: tx0, tx1, tx2, tx3

const { bufferToHex } = require("@ethereumjs/util");
const {Trie} = require("@ethereumjs/trie");
const {
    AccessListEIP2930Transaction: EIP2930Transaction,
    FeeMarketEIP1559Transaction: EIP1559Transaction,
    Transaction: LegacyTransaction,
} = require("@ethereumjs/tx");

const RLP = require("rlp");

const ethers = require("ethers");

const trie = new Trie();

/* 
    block that contains only 4 transactions 
*/
const blockHash = '0x441bcda29969fe4c94790b8538b70e30b66c62b69fe6516bea99634d290d16c7';

/*
    all 4 raw transactions copied from etherscan 
*/

const rawTxs = {
    tx0:'0xf8ab81a5852e90edd00083012bc294a3bed4e1c75d00fa6f4e5e6922db7261b5e9acd280b844a9059cbb0000000000000000000000008bda8b9823b8490e8cf220dc7b91d97da1c54e250000000000000000000000000000000000000000000000056bc75e2d6310000026a06c89b57113cf7da8aed7911310e03d49be5e40de0bd73af4c9c54726c478691ba056223f039fab98d47c71f84190cf285ce8fc7d9181d6769387e5efd0a970e2e9'
    ,tx1:'0xf8ab81a6852e90edd00083012bc294a3bed4e1c75d00fa6f4e5e6922db7261b5e9acd280b844a9059cbb0000000000000000000000008bda8b9823b8490e8cf220dc7b91d97da1c54e250000000000000000000000000000000000000000000000056bc75e2d6310000026a0d77c66153a661ecc986611dffda129e14528435ed3fd244c3afb0d434e9fd1c1a05ab202908bf6cbc9f57c595e6ef3229bce80a15cdf67487873e57cc7f5ad7c8a'
    ,tx2:'0xf86d8229f185199c82cc008252089488e9a2d38e66057e18545ce03b3ae9ce4fc360538702ce7de1537c008025a096e7a1d9683b205f697b4073a3e2f0d0ad42e708f03e899c61ed6a894a7f916aa05da238fbb96d41a4b5ec0338c86cfcb627d0aa8e556f21528e62f31c32f7e672'
    ,tx3:'0xf86f826b2585199c82cc0083015f9094e955ede0a3dbf651e2891356ecd0509c1edb8d9c8801051fdc4efdc0008025a02190f26e70a82d7f66354a13cda79b6af1aa808db768a787aeb348d425d7d0b3a06a82bd0518bc9b69dc551e20d772a1b06222edfc5d39b6973e4f4dc46ed8b196'

}

(async()=>{

    const provider = new ethers.providers.JsonRpcProvider();

    const block = await provider.send("eth_getBlockByHash",[ blockHash, true ]);
    const results = await Promise.all(block.transactions.map((tx, i) => 
            (async (tx, i) => {

                /*
                    in block returned a transaction has 2 properties 'input' and 'gas'
                    that are expected as 'data' and 'gasLimit' 
                    modifications for ethereumjs 
                */

                tx.data = tx.input;
                delete tx.input;
                tx.gasLimit = tx.gas;
                delete tx.gas;

                let TransactionClass;

                switch (tx.type){
                    case '0x2' :
                        TransactionClass=EIP1559Transaction;
                        break;
                    case '0x1' :
                        TransactionClass=EIP2930Transaction;
                        break;
                    case '0x0' :
                        TransactionClass=LegacyTransaction;
                        break; 
                }

                const serializedTx = TransactionClass.fromTxData(tx).serialize();

                /*
                    check that serialized transaction matches etherscan raw transaction
                */

                console.log(rawTxs[`tx${i}`] === bufferToHex(serializedTx));

                return await trie.put(
                    RLP.encode(i),
                    serializedTx
                );

            })(tx, i) 
        ));

        console.log(`block.transactionsRoot: ${block.transactionsRoot}`)
        console.log(`trie.root: ${bufferToHex(trie.root)}`)        
})();

3
  • 1
    In order to rebuild the merkle patricia trie, you need to have the merkle patricia trie from the previous block. You can't just recreate the world state with the current block. You need to take the entire history into account.
    – Donut
    Jul 11, 2022 at 3:56
  • @Donut thank you for your response. I'm getting closer I think but I'm still not there. When you say you need the MPT of the previous block how do you mean exactly? I amended the above code to compare the transaction root with ancestor blocks but clearly that is naive. Do I have to include the transactions of another block when building the trie? Jul 11, 2022 at 6:28
  • In order to get the current world state, you are required to start from block 1 and the transactions all the way to the current block. Imagine that block 1 has a transaction where 3 Eth are transferred from address 0x01 to 0x02. And that's it, these addresses are never touched again. The accounts are still part of the world state, but you will only get them in there if you replay the transaction from block 1. Does that make sense?
    – Donut
    Jul 11, 2022 at 7:27

1 Answer 1

2

My error was using @ethereumjs/trie's SecureTrie class instead of Trie the above code works now

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.