《兄弟连区块链入门教程eth源码分析core》由会员分享,可在线阅读,更多相关《兄弟连区块链入门教程eth源码分析core(7页珍藏版)》请在金锄头文库上搜索。
1、兄弟连区块链入门教程eth源码分析core-vm源码分析(二)兄弟连区块链入门教程eth源码分析core-vm源码分析(二),合约创建Create会创 建一个新的合约。/ Create creates a new contract using code as deployment code.func (evm *EVM) Create(caller ContractRef, code byte, gas uint64, value *big.Int) (ret b contractAddr common.Address, leftOverGas uint64, err error) / Dep
2、th check execution. Fail if were trying to execute above the/ limit.if evm.depth int(params.CallCreateDepth) return nil, common.Address, gas, ErrDepthif !evm.CanTransfer(evm.StateDB, caller.Address(), value) return nil, common.Address, gas, ErrInsufficientBalance/ Ensure theres no existing contract
3、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
4、| (contractHash != (common.Hash) &con trac tHash != emp tyCodeHash)如果已经存在return nil, common.Address, 0, ErrContractAddressCollision/ Create a new account on the state snapshot := evm.S tat eDB.Sna 创建一个/Sta teDB 的快照,以便回滚 evm.S tat eDB.Crea teAccoun t(con tr a 创建d账户/ if evm.ChainConfig().IsEIP158(evm.
5、BlockNumber) evm.StateDB.SetNonce(contrac设Addnonc) /evm.Transfer(evm.S tat eDB, caller.Address(), contr a转账ddr, 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, Acco
6、untRef(contractAddr), value, gas) contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)if evm.vmConfig.NoRecursion & evm.depth 0 return nil, contractAddr, gas, nilret, err = run(evm, snapsho t, cO执行0合约 的初始优代码 / check whether the max code size has been exceeded 检/查/初始化生成的代码的长度不超过限制max
7、CodeSizeExceeded := 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 handle
8、d / by the error checking condition below.如果合同创建成功并且没有错误返回,则计算存储代码所需的GAS。如果由于没有足 够的GAS而导致代码不能被存储设置错误,并通过下面的错误检查条件来处理。if err = nil & !maxCodeSizeExceeded createDataGas := uint64(len(ret) * params.CreateDataGas if contract.UseGas(createDataGas) evm.StateDB.SetCode(contractAddr, ret) else err = ErrCode
9、StoreOutOfGas/ 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 were in homestead this also counts for code storage gas errors. 当/错/误返回我们回滚修改,if maxCodeSizeExceeded | (err != nil & (evm.ChainCon
10、fig().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 = errMaxCodeSiz
11、eExceededreturn ret, contractAddr, contract.Gas, errCal方法,无论我们转账或者是执行合约代码都会调用到这里,同时合约里面的cal指令也 会执行到这里。/ Call executes the contract associated with the addr with the given input as/ parameters. It also handles any necessary value transfer required and takes/ the necessary steps to create accounts and
12、 reverses the state in case of an / execution error or failed value transfer./ C执行与给定的input作为参数与addr相关联的合约。 /它/还处理所需的任何必要的转账操作,并采取必要的步骤来创建帐户 /并/在任意错误的情况下回滚所做的操作。func (evm *EVM) Call(caller ContractRef, addr common.Address, input byte, gas uint64, valu *big.Int) (ret byte, leftOverGas uint64, err err
13、or) if evm.vmConfig.NoRecursion & evm.depth 0 return nil, gas, nil/ Fail if were trying to execute above the call depth limit 调/用/深度最多1024if evm.depth int(params.CallCreateDepth) return nil, gas, ErrDepth/ Fail if were trying to transfer more than the available balance 查/看/我们的账户是否有足够的金钱。if !evm.Cont
14、ext.CanTransfer(evm.StateDB, caller.Address(), value) return nil, gas, ErrInsufficientBalancevar (to = AccountRef(addr) snapshot = evm.StateDB.Snapshot()f !evm.StateDB.Exist查a看指定地址是否存在如果地址不存在,杳看是否是nat ive g的合约,nat ive g的合约在/ cont文a件里面0 precompiles := PrecompiledContractsHomestead if evm.ChainConfig(
15、).IsByzantium(evm.BlockNumber) precompiles = PrecompiledContractsByzantiumif precompilesaddr = nil & evm.ChainConfig().IsEIP158(evm.BlockNumber) & value.Sign() = 0 如果不是指定的合约地址,并且value的值为0那么返回正常,而且这次调用没有消耗Gasreturn nil, gas, nil负责在本地状态创建addrevm.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/ E The contract is a scoped environment for this execution context/ only.contr