diff --git a/core/state_processor.go b/core/state_processor.go index 4dc58b9dea..0c0c22d65f 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -59,7 +59,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg usedGas = new(uint64) header = block.Header() allLogs []*types.Log - gp = new(GasPool).AddGas(block.GasLimit()) + gp = new(GasPool).AddGas(block.GasLimit()) //gaspool初始值均为0,通过block_gaslimit完成初始化 ) // Mutate the the block and state according to any hard-fork specs if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { @@ -90,13 +90,14 @@ func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common 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) + vmenv := vm.NewEVM(context, statedb, config, cfg) // core/evm.go 包括interpretor的建立 // Apply the transaction to the current state (included in the env) - _, gas, failed, err := ApplyMessage(vmenv, msg, gp) + _, gas, failed, err := ApplyMessage(vmenv, msg, gp) //TransitionDb() 执行交易 transition state if err != nil { return nil, 0, err } @@ -107,7 +108,7 @@ func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common } else { root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() } - *usedGas += gas + *usedGas += gas //将单笔交易的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. diff --git a/core/vm/evm.go b/core/vm/evm.go index 46e7baff4c..4e936f60e6 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -320,7 +320,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I if evm.depth > int(params.CallCreateDepth) { return nil, common.Address{}, gas, ErrDepth } - if !evm.CanTransfer(evm.StateDB, caller.Address(), value) { + 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 @@ -334,12 +334,14 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I } // Create a new account on the state snapshot := evm.StateDB.Snapshot() - evm.StateDB.CreateAccount(contractAddr) + evm.StateDB.CreateAccount(contractAddr) //合约账户 if evm.ChainConfig().IsEIP158(evm.BlockNumber) { evm.StateDB.SetNonce(contractAddr, 1) } evm.Transfer(evm.StateDB, caller.Address(), contractAddr, value) - + //向合约账户转账,但transfer()具体实现未找到 + // 还有一个core/evm.go 包含静态方法 + //value即为amount // 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. @@ -351,11 +353,11 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I } if evm.vmConfig.Debug && evm.depth == 0 { - evm.vmConfig.Tracer.CaptureStart(caller.Address(), contractAddr, true, code, gas, value) + evm.vmConfig.Tracer.CaptureStart(caller.Address(), contractAddr, true, code, gas, value) // contract.code为caller.Address? } start := time.Now() - ret, err = run(evm, contract, nil) + ret, err = run(evm, contract, nil) //开始执行code // check whether the max code size has been exceeded maxCodeSizeExceeded := evm.ChainConfig().IsEIP158(evm.BlockNumber) && len(ret) > params.MaxCodeSize diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 6525aa212c..4ea5f2a7d9 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1087,7 +1087,7 @@ func (s *PublicTransactionPoolAPI) sign(addr common.Address, tx *types.Transacti // Look up the wallet containing the requested signer account := accounts.Account{Address: addr} - wallet, err := s.b.AccountManager().Find(account) + wallet, err := s.b.AccountManager().Find(account) //manage.go Find attempts to locate the wallet corresponding to a specific account if err != nil { return nil, err } @@ -1193,7 +1193,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Sen // Look up the wallet containing the requested signer account := accounts.Account{Address: args.From} - wallet, err := s.b.AccountManager().Find(account) + wallet, err := s.b.AccountManager().Find(account) // accounts/manage.go Find attempts to locate the wallet corresponding to a specific account if err != nil { return common.Hash{}, err } @@ -1201,7 +1201,7 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Sen if args.Nonce == nil { // Hold the addresse's mutex around signing to prevent concurrent assignment of // the same nonce to multiple accounts. - s.nonceLock.LockAddr(args.From) + s.nonceLock.LockAddr(args.From) defer s.nonceLock.UnlockAddr(args.From) } @@ -1213,11 +1213,10 @@ func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args Sen tx := args.toTransaction() var chainID *big.Int - if config := s.b.ChainConfig(); config.IsEIP155(s.b.CurrentBlock().Number()) { - chainID = config.ChainId + if config := s.b.ChainConfig(); config.IsEIP155(s.b.CurrentBlock().Number()) { // param/config.go type/Block.go + chainID = config.ChainId //Choose fork } - signed, err := wallet.SignTx(account, tx, chainID) - if err != nil { + signed, err := wallet.SignTx(account, tx, chainID) // accounts/acount.go wallet.go return common.Hash{}, err } return submitTransaction(ctx, s.b, signed) @@ -1267,6 +1266,8 @@ type SignTransactionResult struct { // SignTransaction will sign the given transaction with the from account. // The node needs to have the private key of the account corresponding with // the given from address and it needs to be unlocked. + +//对发送来的交易进行签名 func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args SendTxArgs) (*SignTransactionResult, error) { if args.Gas == nil { return nil, fmt.Errorf("gas not specified")