教你如何了解虛擬機 EVM 的源始碼

Milo Chen
8 min readAug 17, 2019

--

今天剛好在翻譯 Mastering Ethereum 的中譯本,覺得應該來搭配Trace Source Code 的方式來深刻理解一下這玩意,就寫下這篇

用一張圖解釋 EVM 的話,其實就只有小小的這張圖

不懂也沒關係,哥帶你來了解一下這系統的架構

EVM source code hack

Ethereum Virtual Machine 以太坊虛擬機, 處理智能合約 Smart Contract

當 Transaction 送到 node 後,node 內的 EVM 會被 create 出來,完成 transaction 的任務。簡單來說 EVM 是一個用高階程式在模擬一個小電腦的運算. How to check this concept in the source code ?

最近在翻譯 Mastering Ethereum EVM 的內容,
https://github.com/cypherpunks-core/ethereumbook_zh/blob/master/%E7%AC%AC%E5%8D%81%E5%9B%9B%E7%AB%A0.asciidoc
邊翻的過程還可兼 understanding code

transaction 會有各種 failed 的狀況,這邊可參考,順便可以體驗一下小電腦的概念
https://takenobu-hs.github.io/downloads/ethereum_evm_illustrated.pdf

EVM Source Code in go-ethereum (Geth)
https://github.com/ethereum/go-ethereum/blob/master/core/vm/evm.go

The code show how to use the runtime …
https://github.com/ethereum/go-ethereum/blob/master/core/vm/runtime/runtime_example_test.go

由此可知,Execute 就是啟動那個 transaction 該作的事

在Execute 下去,可以看它作了什麼事

https://github.com/ethereum/go-ethereum/blob/dbb03fe9893dd19f6b1de1ee3b768317f22fd135/core/vm/runtime/env.go#L25

The code NewEnv → vm.NewEVM()

vm.NewEVM() is what we want …

Then you can check here … evm.go
type Context structure 對到的是 transaction 內容

evm := &EVM{ 
Context: ctx,
StateDB: statedb,
vmConfig: vmConfig,
chainConfig: chainConfig,
chainRules: chainConfig.Rules(ctx.BlockNumber),
interpreters: make([]Interpreter, 0, 1),
}

今天#COSCUP 有講到 EWASM 在 evm.go https://github.com/ethereum/go-ethereum/blob/master/core/vm/evm.go

之中有這段 chainConfig.IsEWASM(ctx.BlockNumber)

evm.go 中還有關鍵的

type EVM struct {
Context
StateDB StateDB
depth int
chainConfig *params.ChainConfig
chainRules params.Rules
vmConfig Config
interpreters []Interpreter
interpreter Interpreter
abort int32
callGasTemp uint64
}
type Context struct {
CanTransfer CanTransferFunc
Transfer TransferFunc
GetHash GetHashFunc
Origin common.Address
GasPrice *big.Int
Coinbase common.Address
GasLimit uint64
BlockNumber *big.Int
Time *big.Int
Difficulty *big.Int
}

如果你去看 Mastering Ethereum 書籍中 Transaction 相關細節的介紹,會很清楚明白這樣子的 structure 是什麼意思。

另外提一個,那麼像裡面有 instructions.go , 截圖一段給您看看

內容會運用 stack 的方式作運算, 有修過組合語言的話,好像會感覺有熟悉感 ? https://github.com/ethereum/go-ethereum/blob/master/core/vm/instructions.go

講到這邊,你必須要好奇到 EVM 的運作,最重要的經典之處,就是在於它到底是如何改變 account status 的資料呢 ? 關於這部份你可以追 SSTORE 的程式(如附圖所示) . 在instruction.go檔 https://github.com/ethereum/go-ethereum/blob/master/core/vm/instructions.go 之中,

opSstore 就是那個美妙的關鍵解答了,SSTORE 的 S 是 Storage Store 的意思

以太坊每個 private key 所對應的是一個 account , 對每個合約帳戶 Contract Account ,它會有自己的 Storage. 而 SSTORE 是EVM 的組語,它代表的是寫資料到 Storage上。這個 Storage 是所有人可以查閱的到,如果你有在寫 Smart Contract 的話,那些 smart contract 所儲存的值 ,都是儲存在這 Storage 的喔 :D

你可以發現,StateDB 就是重頭戲啦 … 因此了解 EVM 如何與其它node 的連動性,就是 Trace StateDB 的程式啦。換句話說,理解 Account Storage 就是理解 StateDB 就對了

StateDB 的話,有另外的解析
StateDB 的程式碼解析請參考下面 :D
https://blog.csdn.net/lj900911/article/details/85047957

這份檔的原始碼解析,我想已經介紹很完整了,我就不用再為大家多作介紹了。

簡單總結 :

(1) Execute 對 transaction 的 byte-string 執行
(2) 執行時就會 Create EVM 出來,針對 transaction 的內容作處理
(3) EVM 組語有許多指令,在 instruction.go 中會分別實現
(4) 對於合約帳戶 Storage 儲存的部份則是用到 StateDB

文章最後打個小廣告

Mastering Ethereum 跟本是這世界上最有價值的區塊鏈參考書籍之一。紮實讀便可全部腦通。

想來一起幫忙翻譯神書 Mastering Ethereum 的朋友,會學習的,請來關注
https://github.com/cypherpunks-core/ethereumbook_zh

希望能為區塊鏈產業作到應用價值落地的,可以來這互助一同學習努力一下
https://www.facebook.com/groups/dapp.101

--

--

Milo Chen
Milo Chen

Written by Milo Chen

Study in Law/CS/EE & Dev in blockchain, AI, IoT, mobile app. Good in almost programming language with github https://github.com/milochen0418. 永遠十八歲/對世界好奇/INFP型

No responses yet