2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ethereum理解②(Wrold State編)

Posted at

はじめに

Ethereumの勉強しているため、理解したことをまとめる。
第二弾は、Wrold State編。

※間違っている情報があれば、ご指摘いただければ幸いです。

World Stateとは

World Stateとは、Ethereumのブロックチェーン上の全てのState(状態)のことである。
例えば、以下は全てWorld Stateの一部である。
・アカウントのETH残高
・アカウントが実行したTXの回数
・NFTの所有者アドレス
・コントラクトのオーナアドレス

World Stateは、Ethereumネットワークに参加しているフルノードのストレージに保存されている。
TXの実行によって、World Stateは常に変化していく。

World Stateの詳細

World Stateは、「Address」と「Account State」のマッピングである。
world-state.drawio.png
Addressは、Ethereumのアドレスのことで、20byteの16進数文字列である。
Account Stateは、Ethereum上のアカウントが持っている以下の4つの状態のことである。
nonce (EOAによって実行されたTXの数 or CAによって作成されたコントラクトの数)
( EOA = Externally Owned Account , CA = Contract Account)
balance(アカウントが所有しているETH残高)
codehash(アカウントに紐づくコントラクトコードのハッシュ値。EOAの場合は、空文字のハッシュ値)
storageRoot(スマートコントラクトに保存されているデータの、マークルルートハッシュ)

データの保存のされ方は、キーと値の単純なマッピングではなく、Merkle Patricia Trie(= MTP)という木構造になっている。
World Stateは、**MTPで表現できる。

World State の Merkle Patricia Trieの作成

以下の4つのAccount Stateを表現するMTPを作成する。
0x517b3a => 10ETH
0xc3a292 => 5ETH
0xc7520d => 16ETH
0xc7528e => 7ETH
(※アドレスは本来20byteだが、分かりやすくするため3byteとしている)
world-state-MPT.drawio.png

Merkle Patricia Trieのハッシュの求め方

MTPの各ノードのハッシュは、ノードの値をRLPエンコードし、Keccak256によって求められる。

ブランチノードのハッシュの求め方

keccak256( rlp( Array(17) ) )
hashBの例: keccak256( rlp( [null, null, null, null, null, hashA, null, null, null, null, null, null, hashB, null, null, null, null] ) )

リーフノードのハッシュの求め方

keccak256( rlp( [prefix + path, value] ) )
hashCの例: keccak256( rlp( [0x20a292, 5ETH] ) )

エクステンションノードのハッシュの求め方

keccak256( rlp( [prefix + path, value] ) )
hashDの例: keccak256( rlp( [0x0052, hashE] ) )

まとめ

Merkle Patricia Trieは難しかったが、Ethereumのデータ構造について理解が深まった。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?