以太坊源碼深入分析(9)-- 以太坊通過EVM執(zhí)行交易過程分析

上一節(jié)分析了同步一個新的區(qū)塊準(zhǔn)備插入本地BlockChain之前需要重放并執(zhí)行新區(qū)塊的所有交易,并產(chǎn)生交易收據(jù)和日志。以太坊是如何執(zhí)行這些交易呢?這就要請出大名鼎鼎的以太坊虛擬機(jī)。
以太坊虛擬機(jī)在執(zhí)行交易分為兩個部分,第一部分是創(chuàng)建EVM,計算交易金額,設(shè)置交易對象,計算交易gas花銷;第二部分是EVM 的虛擬機(jī)解析器通過合約指令,執(zhí)行智能合約代碼,具體來看看源碼。

一,創(chuàng)建EVM,通過EVM執(zhí)行交易流程
上一節(jié)分析BlockChain調(diào)用processor.Process()遍歷block的所有交易,然后調(diào)用:

receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg)。

執(zhí)行交易并返回收據(jù)數(shù)據(jù)

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,首先調(diào)用tx.Message()方法產(chǎn)生交易Message。這個方法通過txdata數(shù)據(jù)來拼接Message對象,并通過簽名方法signer.Sender(tx),對txdata 的V、R 、S三個數(shù)進(jìn)行解密得到這個交易的簽名公鑰(也是就是發(fā)送方的地址)。發(fā)送方的地址在交易數(shù)據(jù)中是沒有的,這主要是為了防止交易數(shù)據(jù)被篡改,任何交易數(shù)據(jù)的變化后通過signer.Sender方法都不能得到正確的地址。
2,調(diào)用 NewEVMContext(msg, header, bc, author)創(chuàng)建EVM的上下文環(huán)境,調(diào)用vm.NewEVM(context, statedb, config, cfg)創(chuàng)建EVM對象,并在內(nèi)部創(chuàng)建一個evm.interpreter(虛擬機(jī)解析器)。
3,調(diào)用ApplyMessage(vmenv, msg, gp)方法通過EVM對象來執(zhí)行Message。
重點看看ApplyMessage()方法的實現(xiàn):

func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) ([]byte, uint64, bool, error) {
    return NewStateTransition(evm, msg, gp).TransitionDb()
}

創(chuàng)建stateTransition對象,執(zhí)行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(st.data, contractCreation, homestead)
    if err != nil {
        return nil, 0, false, err
    }
    if err = st.useGas(gas); err != nil {
        return nil, 0, false, err
    }

    var (
        evm = st.evm
        // vm errors do not effect consensus and are therefor
        // not assigned to err, except for insufficient balance
        // error.
        vmerr error
    )
    if contractCreation {
        ret, _, st.gas, vmerr = evm.Create(sender, st.data, st.gas, st.value)
    } else {
        // Increment the nonce for the next transaction
        st.state.SetNonce(sender.Address(), st.state.GetNonce(sender.Address())+1)
        ret, st.gas, vmerr = evm.Call(sender, st.to().Address(), st.data, st.gas, st.value)
    }
    if vmerr != nil {
        log.Debug("VM returned with error", "err", vmerr)
        // The only possible consensus-error would be if there wasn't
        // sufficient balance to make the transfer happen. The first
        // balance transfer may never fail.
        if vmerr == vm.ErrInsufficientBalance {
            return nil, 0, false, vmerr
        }
    }
    st.refundGas()
    st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice))

    return ret, st.gasUsed(), vmerr != nil, err
}

3.1,調(diào)用IntrinsicGas()方法,通過計算消息的大小以及是否是合約創(chuàng)建交易,來計算此次交易需消耗的gas。
3.2,如果是合約創(chuàng)建交易,調(diào)用evm.Create(sender, st.data, st.gas, st.value)來執(zhí)行message

func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {

    // Depth check execution. Fail if we're trying to execute above the
    // limit.
    if evm.depth > int(params.CallCreateDepth) {
        return nil, common.Address{}, gas, ErrDepth
    }
    if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
        return nil, common.Address{}, gas, ErrInsufficientBalance
    }
    // Ensure there's no existing contract already at the designated address
    nonce := evm.StateDB.GetNonce(caller.Address())
    evm.StateDB.SetNonce(caller.Address(), nonce+1)

    contractAddr = crypto.CreateAddress(caller.Address(), nonce)
    contractHash := evm.StateDB.GetCodeHash(contractAddr)
    if evm.StateDB.GetNonce(contractAddr) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
        return nil, common.Address{}, 0, ErrContractAddressCollision
    }
    // Create a new account on the state
    snapshot := evm.StateDB.Snapshot()
    evm.StateDB.CreateAccount(contractAddr)
    if evm.ChainConfig().IsEIP158(evm.BlockNumber) {
        evm.StateDB.SetNonce(contractAddr, 1)
    }
    evm.Transfer(evm.StateDB, caller.Address(), contractAddr, value)

    // initialise a new contract and set the code that is to be used by the
    // E The contract is a scoped evmironment for this execution context
    // only.
    contract := NewContract(caller, AccountRef(contractAddr), value, gas)
    contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)

    if evm.vmConfig.NoRecursion && evm.depth > 0 {
        return nil, contractAddr, gas, nil
    }

    if evm.vmConfig.Debug && evm.depth == 0 {
        evm.vmConfig.Tracer.CaptureStart(caller.Address(), contractAddr, true, code, gas, value)
    }
    start := time.Now()

    ret, err = run(evm, contract, nil)

    // check whether the max code size has been exceeded
    maxCodeSizeExceeded := evm.ChainConfig().IsEIP158(evm.BlockNumber) && len(ret) > params.MaxCodeSize
    // if the contract creation ran successfully and no errors were returned
    // calculate the gas required to store the code. If the code could not
    // be stored due to not enough gas set an error and let it be handled
    // by the error checking condition below.
    if err == nil && !maxCodeSizeExceeded {
        createDataGas := uint64(len(ret)) * params.CreateDataGas
        if contract.UseGas(createDataGas) {
            evm.StateDB.SetCode(contractAddr, ret)
        } else {
            err = ErrCodeStoreOutOfGas
        }
    }

    // When an error was returned by the EVM or when setting the creation code
    // above we revert to the snapshot and consume any gas remaining. Additionally
    // when we're in homestead this also counts for code storage gas errors.
    if maxCodeSizeExceeded || (err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) {
        evm.StateDB.RevertToSnapshot(snapshot)
        if err != errExecutionReverted {
            contract.UseGas(contract.Gas)
        }
    }
    // Assign err if contract code size exceeds the max while the err is still empty.
    if maxCodeSizeExceeded && err == nil {
        err = errMaxCodeSizeExceeded
    }
    if evm.vmConfig.Debug && evm.depth == 0 {
        evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
    }
    return ret, contractAddr, contract.Gas, err
}

3.2.1,evm執(zhí)行棧深度不能超過1024,發(fā)送方持有的以太坊數(shù)量大于此次合約交易金額。
3.2.2,對該發(fā)送方地址的nonce值+1,通過地址和nonce值生成合約地址,通過合約地址得到合約hash值。
3.2.3,記錄一個狀態(tài)快照,用來后見失敗回滾。
3.2.4,為這個合約地址創(chuàng)建一個合約賬戶,并為這個合約賬戶設(shè)置nonce值為1
3.2.5,產(chǎn)生以太坊資產(chǎn)轉(zhuǎn)移,發(fā)送方地址賬戶金額減value值,合約賬戶的金額加value值。
3.2.6,根據(jù)發(fā)送方地址和合約地址,以及金額value 值和gas,合約代碼和代碼hash值,創(chuàng)建一個合約對象
3.2.7,run方法來執(zhí)行合約,內(nèi)部調(diào)用evm的解析器來執(zhí)行合約指令,如果是預(yù)編譯好的合約,則預(yù)編譯執(zhí)行合約就行。
3.2.8,如果執(zhí)行ok,setcode更新這個合約地址狀態(tài),設(shè)置usegas為創(chuàng)建合約的gas。如果執(zhí)行出錯,則回滾到之前快照狀態(tài),設(shè)置usegas為傳入的合約gas。

3.3,如果不是新創(chuàng)建的合約,則調(diào)用evm.Call(sender, st.to().Address(), st.data, st.gas, st.value)方法,同時更新發(fā)送方地址nonce值+1.

func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
    if evm.vmConfig.NoRecursion && evm.depth > 0 {
        return nil, gas, nil
    }

    // Fail if we're trying to execute above the call depth limit
    if evm.depth > int(params.CallCreateDepth) {
        return nil, gas, ErrDepth
    }
    // Fail if we're trying to transfer more than the available balance
    if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
        return nil, gas, ErrInsufficientBalance
    }

    var (
        to       = AccountRef(addr)
        snapshot = evm.StateDB.Snapshot()
    )
    if !evm.StateDB.Exist(addr) {
        precompiles := PrecompiledContractsHomestead
        if evm.ChainConfig().IsByzantium(evm.BlockNumber) {
            precompiles = PrecompiledContractsByzantium
        }
        if precompiles[addr] == nil && evm.ChainConfig().IsEIP158(evm.BlockNumber) && value.Sign() == 0 {
            return nil, gas, nil
        }
        evm.StateDB.CreateAccount(addr)
    }
    evm.Transfer(evm.StateDB, caller.Address(), to.Address(), value)

    // Initialise a new contract and set the code that is to be used by the EVM.
    // The contract is a scoped environment for this execution context only.
    contract := NewContract(caller, to, value, gas)
    contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))

    start := time.Now()

    // Capture the tracer start/end events in debug mode
    if evm.vmConfig.Debug && evm.depth == 0 {
        evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)

        defer func() { // Lazy evaluation of the parameters
            evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
        }()
    }
    ret, err = run(evm, contract, input)

    // When an error was returned by the EVM or when setting the creation code
    // above we revert to the snapshot and consume any gas remaining. Additionally
    // when we're in homestead this also counts for code storage gas errors.
    if err != nil {
        evm.StateDB.RevertToSnapshot(snapshot)
        if err != errExecutionReverted {
            contract.UseGas(contract.Gas)
        }
    }
    return ret, contract.Gas, err
}

evm.call方法和evm.create方法大致相同,我們來說說不一樣的地方。
3.3.1,call方法調(diào)用的是一個存在的合約地址的合約,所以不用創(chuàng)建合約賬戶。如果call方法發(fā)現(xiàn)本地沒有合約接收方的賬戶,則需要創(chuàng)建一個接收方的賬戶,并更新本地狀態(tài)數(shù)據(jù)庫。
3.3.2,create方法的資金transfer轉(zhuǎn)移是在創(chuàng)建合約用戶賬戶和這個合約賬戶之間發(fā)生,而call方法的資金轉(zhuǎn)移是在合約的發(fā)送方和合約的接收方之間產(chǎn)生。

3.4,TransitionDb()方法執(zhí)行完合約,調(diào)用st.refundGas()方法計算合約退稅,調(diào)用evm SSTORE指令 或者evm SUICIDE指令銷毀合約十都會產(chǎn)生退稅。
3.5,計算合約產(chǎn)生的gas總數(shù),加入到礦工賬戶,作為礦工收入。

4,回到最開始的ApplyTransaction()方法,根據(jù)EVM的執(zhí)行結(jié)果,拼接交易receipt數(shù)據(jù),其中receipt.Logs日志數(shù)據(jù)是EVM執(zhí)行指令代碼的時候產(chǎn)生的,receipt.Bloom根據(jù)日志數(shù)據(jù)建立bloom過濾器。

二,EVM 的虛擬機(jī)解析器通過運行合約指令,執(zhí)行智能合約代碼
我們從 3.2.7 執(zhí)行合約的run()方法入手,它調(diào)用了evm.interpreter.Run(contract, input)方法

func (in *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err error) {
    // Increment the call depth which is restricted to 1024
    in.evm.depth++
    defer func() { in.evm.depth-- }()

    // Reset the previous call's return data. It's unimportant to preserve the old buffer
    // as every returning call will return new data anyway.
    in.returnData = nil

    // Don't bother with the execution if there's no code.
    if len(contract.Code) == 0 {
        return nil, nil
    }

    var (
        op    OpCode        // current opcode
        mem   = NewMemory() // bound memory
        stack = newstack()  // local stack
        // For optimisation reason we're using uint64 as the program counter.
        // It's theoretically possible to go above 2^64. The YP defines the PC
        // to be uint256. Practically much less so feasible.
        pc   = uint64(0) // program counter
        cost uint64
        // copies used by tracer
        pcCopy  uint64 // needed for the deferred Tracer
        gasCopy uint64 // for Tracer to log gas remaining before execution
        logged  bool   // deferred Tracer should ignore already logged steps
    )
    contract.Input = input

    if in.cfg.Debug {
        defer func() {
            if err != nil {
                if !logged {
                    in.cfg.Tracer.CaptureState(in.evm, pcCopy, op, gasCopy, cost, mem, stack, contract, in.evm.depth, err)
                } else {
                    in.cfg.Tracer.CaptureFault(in.evm, pcCopy, op, gasCopy, cost, mem, stack, contract, in.evm.depth, err)
                }
            }
        }()
    }
    // The Interpreter main run loop (contextual). This loop runs until either an
    // explicit STOP, RETURN or SELFDESTRUCT is executed, an error occurred during
    // the execution of one of the operations or until the done flag is set by the
    // parent context.
    for atomic.LoadInt32(&in.evm.abort) == 0 {
        if in.cfg.Debug {
            // Capture pre-execution values for tracing.
            logged, pcCopy, gasCopy = false, pc, contract.Gas
        }

        // Get the operation from the jump table and validate the stack to ensure there are
        // enough stack items available to perform the operation.
        op = contract.GetOp(pc)
        operation := in.cfg.JumpTable[op]
        if !operation.valid {
            return nil, fmt.Errorf("invalid opcode 0x%x", int(op))
        }
        if err := operation.validateStack(stack); err != nil {
            return nil, err
        }
        // If the operation is valid, enforce and write restrictions
        if err := in.enforceRestrictions(op, operation, stack); err != nil {
            return nil, err
        }

        var memorySize uint64
        // calculate the new memory size and expand the memory to fit
        // the operation
        if operation.memorySize != nil {
            memSize, overflow := bigUint64(operation.memorySize(stack))
            if overflow {
                return nil, errGasUintOverflow
            }
            // memory is expanded in words of 32 bytes. Gas
            // is also calculated in words.
            if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow {
                return nil, errGasUintOverflow
            }
        }

        if !in.cfg.DisableGasMetering {
            // consume the gas and return an error if not enough gas is available.
            // cost is explicitly set so that the capture state defer method cas get the proper cost
            cost, err = operation.gasCost(in.gasTable, in.evm, contract, stack, mem, memorySize)
            if err != nil || !contract.UseGas(cost) {
                return nil, ErrOutOfGas
            }
        }
        if memorySize > 0 {
            mem.Resize(memorySize)
        }

        if in.cfg.Debug {
            in.cfg.Tracer.CaptureState(in.evm, pc, op, gasCopy, cost, mem, stack, contract, in.evm.depth, err)
            logged = true
        }

        // execute the operation
        res, err := operation.execute(&pc, in.evm, contract, mem, stack)
        // verifyPool is a build flag. Pool verification makes sure the integrity
        // of the integer pool by comparing values to a default value.
        if verifyPool {
            verifyIntegerPool(in.intPool)
        }
        // if the operation clears the return data (e.g. it has returning data)
        // set the last return to the result of the operation.
        if operation.returns {
            in.returnData = res
        }

        switch {
        case err != nil:
            return nil, err
        case operation.reverts:
            return res, errExecutionReverted
        case operation.halts:
            return res, nil
        case !operation.jumps:
            pc++
        }
    }
    return nil, nil
}

我們直接看解析器處理的主循環(huán),之前的代碼都是在初始化一些臨時變量。
1,首先調(diào)用contract.GetOp(pc)從和約二進(jìn)制數(shù)據(jù)里取得第pc個opcode,opcode是以太坊虛擬機(jī)指令,一共不超過256個,正好一個byte大小能裝下。
2,從解析器的JumpTable表中查到op對應(yīng)的operation。比如opcode是SHA3(0x20),取到的operation就是

    SHA3: {
            execute:       opSha3,
            gasCost:       gasSha3,
            validateStack: makeStackFunc(2, 1),
            memorySize:    memorySha3,
            valid:         true,
        }

execute表示指令對應(yīng)的執(zhí)行方法
gasCost表示執(zhí)行這個指令需要消耗的gas
validateStack計算是不是解析器棧溢出
memorySize用于計算operation的占用內(nèi)存大小

3,如果operation可用,解析器棧不超過1024,且讀寫不沖突
4,計算operation的memorysize,不能大于64位。
5,根據(jù)不同的指令,指令的memorysize等,調(diào)用operation.gasCost()方法計算執(zhí)行operation指令需要消耗的gas。
6,調(diào)用operation.execute(&pc, in.evm, contract, mem, stack)執(zhí)行指令對應(yīng)的方法。
7,operation.reverts值是true或者operation.halts值是true的指令,會跳出主循環(huán),否則繼續(xù)遍歷下個op。
8,operation指令集里面有4個特殊的指令LOG0,LOG1,LOG2,LOG3,它們的指令執(zhí)行方法makeLog()會產(chǎn)生日志數(shù)據(jù),日志內(nèi)容包括EVM解析棧內(nèi)容,指令內(nèi)存數(shù)據(jù),區(qū)塊信息,合約信息等。這些日志數(shù)據(jù)會寫入到tx的Receipt的logs里面,并存入本地ldb數(shù)據(jù)庫。

總結(jié)
EVM是以太坊的核心功能,得益于EVM,以太坊把區(qū)塊鏈帶入了2.0時代,這是一個非常偉大的進(jìn)步。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 簡介 不管你們知不知道以太坊(Ethereum blockchain)是什么,但是你們大概都聽說過以太坊。最近在新...
    Lilymoana閱讀 3,998評論 1 22
  • 1區(qū)塊 所有的交易都被分組為“區(qū)塊”。區(qū)塊鏈包含一系列鏈接在一 起的這樣的塊。 在以太坊,一個區(qū)塊包括: 區(qū)塊頭 ...
    布尼區(qū)塊鏈閱讀 2,512評論 0 2
  • 以太坊(Ethereum ):下一代智能合約和去中心化應(yīng)用平臺 翻譯:巨蟹 、少平 譯者注:中文讀者可以到以太坊愛...
    車圣閱讀 3,924評論 1 7
  • 今天上班感覺身體不是太舒服但也是把每一步都仔細(xì)的做好了雖然中間出了些小毛病但都解決了 晚上出去醫(yī)院打了點滴到現(xiàn)在才...
    京心達(dá)侯天祥閱讀 82評論 0 0
  • 那年冬天,我告別了江南小城的親友,第一次乘坐飛機(jī)來到了特區(qū)――深圳。深圳離我生活的小城太遠(yuǎn)太遠(yuǎn),以至于在我心中的印...
    花清香hp閱讀 759評論 4 7

友情鏈接更多精彩內(nèi)容