上一节分析了同步一个新的区块准备插入本地BlockChain之前需要重放并执行新区块的所有交易,并产生交易收据和日志。以太坊是如何执行这些交易呢?这就要请出大名鼎鼎的以太坊虚拟机。
以太坊虚拟机在执行交易分为两个部分,第一部分是创建EVM,计算交易金额,设置交易对象,计算交易gas花销;第二部分是EVM 的虚拟机解析器通过合约指令,执行智能合约代码,具体来看看源码。
2,调用 NewEVMContext(msg, header, bc, author)创建EVM的上下文环境,调用vm.NewEVM(context, statedb, config, cfg)创建EVM对象,并在内部创建一个evm.interpreter(虚拟机解析器)。
3,调用ApplyMessage(vmenv, msg, gp)方法通过EVM对象来执行Message。
以太坊虚拟机在执行交易分为两个部分,第一部分是创建EVM,计算交易金额,设置交易对象,计算交易gas花销;第二部分是EVM 的虚拟机解析器通过合约指令,执行智能合约代码,具体来看看源码。
一,创建EVM,通过EVM执行交易流程
上一节分析BlockChain调用processor.Process()遍历block的所有交易,然后调用:
receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg)。
执行交易并返回收据数据
func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error) {
msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
if err != nil {
return nil, 0, err
}
// Create a new context to be used in the EVM environment
context := NewEVMContext(msg, header, bc, author)
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(context, statedb, config, cfg)
// Apply the transaction to the current state (included in the env)
_, gas, failed, err := ApplyMessage(vmenv, msg, gp)
if err != nil {
return nil, 0, err
}
// Update the state with pending changes
var root []byte
if config.IsByzantium(header.Number) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
}
*usedGas += gas
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
// based on the eip phase, we're passing wether the root touch-delete accounts.
receipt := types.NewReceipt(root, failed, *usedGas)
receipt.TxHash = tx.Hash()
receipt.GasUsed = gas
// if the transaction created a contract, store the creation address in the receipt.
if msg.To() == nil {
receipt.ContractAddress = crypto.CreateAddress(vmenv.Context.Origin, tx.Nonce())
}
// Set the receipt logs and create a bloom for filtering
receipt.Logs = statedb.GetLogs(tx.Hash())
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
return receipt, gas, err
}
1,首先调用tx.Message()方法产生交易Message。这个方法通过txdata数据来拼接Message对象,并通过签名方法signer.Sender(tx),对txdata 的V、R 、S三个数进行解密得到这个交易的签名公钥(也是就是发送方的地址)。发送方的地址在交易数据中是没有的,这主要是为了防止交易数据被篡改,任何交易数据的变化后通过signer.Sender方法都不能得到正确的地址。
2,调用 NewEVMContext(msg, header, bc, author)创建EVM的上下文环境,调用vm.NewEVM(context, statedb, config, cfg)创建EVM对象,并在内部创建一个evm.interpreter(虚拟机解析器)。
3,调用ApplyMessage(vmenv, msg, gp)方法通过EVM对象来执行Message。
重点看看ApplyMessage()方法的实现:
func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) ([]byte, uint64, bool, error) {
return NewStateTransition(evm, msg, gp).TransitionDb()
}
创建stateTransition对象,执行TransitionDb()方法:
func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bool, err error) {
if err = st.preCheck(); err != nil {
return
}
msg := st.msg
sender := st.from() // err checked in preCheck
homestead := st.evm.ChainConfig().IsHomestead(st.evm.BlockNumber)
contractCreation := msg.To() == nil
// Pay intrinsic gas
gas, err := IntrinsicGas(