LoginSignup
1
0

More than 1 year has passed since last update.

TransactionsRoot(もしくはTransactionsTrieなど)の手動での計算方法

Posted at

Londonハードフォーク以降の話

誰も見ないシリーズ(シリーズになるかどうかは不明)

EthereumのBlockのBlockHeaderに格納されているTransactionsRoot(もしくはTransactionsTrieなど)
はBlockに格納されるTransaction一式から生成されるMerkle Patricia Trieのroot hashである。

Rootの計算方法は、ひたすらTrieにKey-Valueを登録すればよい。
Merkle Patricia Trie自体の説明は他に譲り、かつ、そこらへんの処理はライブラリに任せて、ここでは何をKeyとValueに入れなければいけないのかのみ説明する。

Key : TransactionIndexをRLP encodeした値
Value : Transactionの中身全部をserializeした値*

*余談だが、この値をKeccak256でハッシュ化するとTranasactionHashが計算可能である

Keyに関しては単純でRLPEncodeなので(RLPEncode自体の説明は調べれば出るのでGO!)
indexが1なら0x01みたいな感じである(もちろん文字列ではない、念のため)

Valueに関しては、とりあえずTransactionHashが正しく計算できるならばその一段階前と考えれば問題ない。
serializeの中身は、格納されたデータをRLPEncodeして、Transactionタイプを付記している
https://github.com/ethereumjs/ethereumjs-monorepo/blob/f15f28b580923d040c6bdf9de68ff38cf1f5fab4/packages/tx/src/eip1559Transaction.ts#L297
(上記のeip1559Transactionの場合、Transactionタイプである2をencode後、追記していることがわかる)

厄介なのは、LegacyなTransaction以外が出現したことにより、Transactionタイプによって分岐をかけないといけない点である

https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/tx/src
Ethereumjsを参考として提示するが、
2021/10現在
eip1559Transaction
eip2930Transaction
legacyTransaction
の三つのTransactionが存在しているので、それぞれのTransactionで正しい値を格納して処理しよう

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0