From 53fbd7ce63ad9e4fe94b1ff31a22d2cf4392cb54 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sun, 8 Apr 2018 21:49:31 +0200 Subject: [PATCH] experiment: use block context --- core/chain_makers.go | 17 ++- core/evm.go | 12 --- core/state_processor.go | 28 ++++- core/state_transition.go | 4 +- core/vm/evm.go | 44 +++++--- core/vm/gas_table.go | 6 +- core/vm/instructions.go | 168 ++++++++++++++--------------- core/vm/int_pool_verifier.go | 2 +- core/vm/int_pool_verifier_empty.go | 2 +- core/vm/interpreter.go | 24 ++--- core/vm/intpool.go | 18 ++-- core/vm/stack.go | 2 +- 12 files changed, 176 insertions(+), 151 deletions(-) diff --git a/core/chain_makers.go b/core/chain_makers.go index 5f7822f79c..d362ee0f14 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -53,7 +53,8 @@ type BlockGen struct { config *params.ChainConfig engine consensus.Engine -} + + } // SetCoinbase sets the coinbase of the generated block. // It can be called at most once. @@ -98,9 +99,19 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) { b.SetCoinbase(common.Address{}) } b.statedb.Prepare(tx.Hash(), common.Hash{}, len(b.txs)) - signer := types.MakeSigner(b.config, b.header.Number) - receipt, _, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{}, &signer) + blockContext := &vm.BlockContext{ + BlockNumber: b.header.Number, + Coinbase: b.header.Coinbase, + GasLimit: b.header.GasLimit, + Signer: types.MakeSigner(b.config, b.header.Number), + Precompiles: vm.PrecompiledContractsByzantium, // TODO fix + Intpool: vm.NewIntpool(), + Time: b.header.Time, + Difficulty: b.header.Difficulty, + } + + receipt, _, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{}, blockContext) if err != nil { panic(err) } diff --git a/core/evm.go b/core/evm.go index 596ea95fb7..748bf3e24b 100644 --- a/core/evm.go +++ b/core/evm.go @@ -37,23 +37,11 @@ type ChainContext interface { // NewEVMContext creates a new context for use in the EVM. func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context { - // If we don't have an explicit author (i.e. not mining), extract from the header - var beneficiary common.Address - if author == nil { - beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation - } else { - beneficiary = *author - } return vm.Context{ CanTransfer: CanTransfer, Transfer: Transfer, GetHash: GetHashFn(header, chain), Origin: msg.From(), - Coinbase: beneficiary, - BlockNumber: new(big.Int).Set(header.Number), - Time: new(big.Int).Set(header.Time), - Difficulty: new(big.Int).Set(header.Difficulty), - GasLimit: header.GasLimit, GasPrice: new(big.Int).Set(msg.GasPrice()), } } diff --git a/core/state_processor.go b/core/state_processor.go index f46a0306dc..83ccd94693 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" + "math/big" ) // StateProcessor is a basic Processor, which takes care of transitioning @@ -65,12 +66,29 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 { misc.ApplyDAOHardFork(statedb) } - signer := types.MakeSigner(p.config, header.Number) + + precompiles := vm.PrecompiledContractsHomestead + if p.config.ByzantiumBlock.Cmp(block.Number()) <= 0 { + precompiles = vm.PrecompiledContractsByzantium + } + beneficiary, _ := p.engine.Author(block.Header()) + + blockContext := &vm.BlockContext{ + Intpool: vm.NewIntpool(), + Precompiles: precompiles, + + BlockNumber: new(big.Int).Set(block.Header().Number), + Time: new(big.Int).Set(block.Header().Time), + Difficulty: new(big.Int).Set(block.Header().Difficulty), + GasLimit: block.Header().GasLimit, + Coinbase: beneficiary, + Signer: types.MakeSigner(p.config, block.Header().Number), + } // Iterate over and process the individual transactions receipts = make([]*types.Receipt, len(block.Transactions())) for i, tx := range block.Transactions() { statedb.Prepare(tx.Hash(), block.Hash(), i) - receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg, &signer) + receipt, _, err := ApplyTransaction(p.config, p.bc, nil, gp, statedb, header, tx, usedGas, cfg, blockContext) if err != nil { return nil, nil, 0, err } @@ -87,8 +105,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg // and uses the input parameters for its environment. It returns the receipt // for the transaction, gas used and an error if the transaction failed, // indicating the block was invalid. -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, signer *types.Signer) (*types.Receipt, uint64, error) { - msg, err := tx.AsMessage(*signer) +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, blockContext *vm.BlockContext) (*types.Receipt, uint64, error) { + msg, err := tx.AsMessage(blockContext.Signer) if err != nil { return nil, 0, err } @@ -96,7 +114,7 @@ func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common 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, blockContext) // Apply the transaction to the current state (included in the env) _, gas, failed, err := ApplyMessage(vmenv, msg, gp) if err != nil { diff --git a/core/state_transition.go b/core/state_transition.go index 5654cd01ea..177b5f15cf 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -186,7 +186,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo } msg := st.msg sender := vm.AccountRef(msg.From()) - homestead := st.evm.ChainConfig().IsHomestead(st.evm.BlockNumber) + homestead := st.evm.ChainConfig().IsHomestead(st.evm.BlockContext.BlockNumber) contractCreation := msg.To() == nil // Pay intrinsic gas @@ -222,7 +222,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo } } st.refundGas() - st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)) + st.state.AddBalance(st.evm.BlockContext.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)) return ret, st.gasUsed(), vmerr != nil, err } diff --git a/core/vm/evm.go b/core/vm/evm.go index e94930ae58..ea699b8190 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/core/types" ) // emptyCodeHash is used by create to ensure deployment is disallowed to already @@ -41,7 +42,7 @@ type ( // run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter. func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { if contract.CodeAddr != nil { - if p := evm.interpreter.precompiles[*contract.CodeAddr]; p != nil { + if p := evm.BlockContext.Precompiles[*contract.CodeAddr]; p != nil { return RunPrecompiledContract(p, input, contract) } } @@ -63,6 +64,20 @@ type Context struct { Origin common.Address // Provides information for ORIGIN GasPrice *big.Int // Provides information for GASPRICE + //// Block information + //Coinbase common.Address // Provides information for COINBASE + //GasLimit uint64 // Provides information for GASLIMIT + //BlockNumber *big.Int // Provides information for NUMBER + //Time *big.Int // Provides information for TIME + //Difficulty *big.Int // Provides information for DIFFICULTY +} + +// Blockcontext provides block-constant auxiliary information. Should never be modified after +// creation +type BlockContext struct{ + Precompiles map[common.Address]PrecompiledContract + Signer types.Signer + Intpool *IntPool // Block information Coinbase common.Address // Provides information for COINBASE GasLimit uint64 // Provides information for GASLIMIT @@ -81,6 +96,7 @@ type Context struct { // // The EVM should never be reused and is not thread safe. type EVM struct { + BlockContext *BlockContext // Context provides auxiliary blockchain related information Context // StateDB gives access to the underlying state @@ -109,16 +125,17 @@ type EVM struct { // NewEVM returns a new EVM. The returned EVM is not thread safe and should // only ever be used *once*. -func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *EVM { +func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config,blockContext *BlockContext) *EVM { evm := &EVM{ - Context: ctx, - StateDB: statedb, - vmConfig: vmConfig, - chainConfig: chainConfig, - chainRules: chainConfig.Rules(ctx.BlockNumber), + Context: ctx, + StateDB: statedb, + vmConfig: vmConfig, + chainConfig: chainConfig, + chainRules: chainConfig.Rules(blockContext.BlockNumber), + BlockContext: blockContext, } - evm.interpreter = NewInterpreter(evm, vmConfig) + evm.interpreter = NewInterpreter(evm, vmConfig, blockContext) return evm } @@ -151,8 +168,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas snapshot = evm.StateDB.Snapshot() ) if !evm.StateDB.Exist(addr) { - if evm.interpreter.precompiles[addr] == nil && evm.ChainConfig().IsEIP158(evm.BlockNumber) && value.Sign() == 0 { - + if evm.BlockContext.Precompiles[addr] == nil && evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber) && value.Sign() == 0 { // Calling a non existing account, don't do antything, but ping the tracer if evm.vmConfig.Debug && evm.depth == 0 { evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value) @@ -166,7 +182,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas code := evm.StateDB.GetCode(addr) codeHash := evm.StateDB.GetCodeHash(addr) - _, isPrecompile := evm.interpreter.precompiles[addr] + _, isPrecompile := evm.BlockContext.Precompiles[addr] if !isPrecompile && len(code) == 0 { // Shortcut execution if there is no code, @@ -344,7 +360,7 @@ 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) - if evm.ChainConfig().IsEIP158(evm.BlockNumber) { + if evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber) { evm.StateDB.SetNonce(contractAddr, 1) } evm.Transfer(evm.StateDB, caller.Address(), contractAddr, value) @@ -367,7 +383,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I 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 + maxCodeSizeExceeded := evm.ChainConfig().IsEIP158(evm.BlockContext.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 @@ -384,7 +400,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I // 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)) { + if maxCodeSizeExceeded || (err != nil && (evm.ChainConfig().IsHomestead(evm.BlockContext.BlockNumber) || err != ErrCodeStoreOutOfGas)) { evm.StateDB.RevertToSnapshot(snapshot) if err != errExecutionReverted { contract.UseGas(contract.Gas) diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 83adba428e..bd21984575 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -319,7 +319,7 @@ func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem gas = gt.Calls transfersValue = stack.Back(2).Sign() != 0 address = common.BigToAddress(stack.Back(1)) - eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) + eip158 = evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber) ) if eip158 { if transfersValue && evm.StateDB.Empty(address) { @@ -385,11 +385,11 @@ func gasRevert(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { var gas uint64 // EIP150 homestead gas reprice fork: - if evm.ChainConfig().IsEIP150(evm.BlockNumber) { + if evm.ChainConfig().IsEIP150(evm.BlockContext.BlockNumber) { gas = gt.Suicide var ( address = common.BigToAddress(stack.Back(0)) - eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) + eip158 = evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber) ) if eip158 { diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 3a67e1865f..b421754ddd 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -41,7 +41,7 @@ func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac x, y := stack.pop(), stack.peek() math.U256(y.Add(x, y)) - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -49,7 +49,7 @@ func opSub(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac x, y := stack.pop(), stack.peek() math.U256(y.Sub(x, y)) - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -57,7 +57,7 @@ func opMul(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac x, y := stack.pop(), stack.pop() stack.push(math.U256(x.Mul(x, y))) - evm.interpreter.intPool.put(y) + evm.BlockContext.Intpool.put(y) return nil, nil } @@ -69,13 +69,13 @@ func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac } else { y.SetUint64(0) } - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := math.S256(stack.pop()), math.S256(stack.pop()) - res := evm.interpreter.intPool.getZero() + res := evm.BlockContext.Intpool.getZero() if y.Sign() == 0 || x.Sign() == 0 { stack.push(res) @@ -88,7 +88,7 @@ func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta } stack.push(math.U256(res)) } - evm.interpreter.intPool.put(x, y) + evm.BlockContext.Intpool.put(x, y) return nil, nil } @@ -99,13 +99,13 @@ func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac } else { stack.push(math.U256(x.Mod(x, y))) } - evm.interpreter.intPool.put(y) + evm.BlockContext.Intpool.put(y) return nil, nil } func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := math.S256(stack.pop()), math.S256(stack.pop()) - res := evm.interpreter.intPool.getZero() + res := evm.BlockContext.Intpool.getZero() if y.Sign() == 0 { stack.push(res) @@ -118,7 +118,7 @@ func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta } stack.push(math.U256(res)) } - evm.interpreter.intPool.put(x, y) + evm.BlockContext.Intpool.put(x, y) return nil, nil } @@ -126,7 +126,7 @@ func opExp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac base, exponent := stack.pop(), stack.pop() stack.push(math.Exp(base, exponent)) - evm.interpreter.intPool.put(base, exponent) + evm.BlockContext.Intpool.put(base, exponent) return nil, nil } @@ -147,7 +147,7 @@ func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac stack.push(math.U256(num)) } - evm.interpreter.intPool.put(back) + evm.BlockContext.Intpool.put(back) return nil, nil } @@ -164,7 +164,7 @@ func opLt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack } else { y.SetUint64(0) } - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -175,7 +175,7 @@ func opGt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack } else { y.SetUint64(0) } - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -199,7 +199,7 @@ func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac y.SetUint64(0) } } - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -223,7 +223,7 @@ func opSgt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac y.SetUint64(0) } } - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -234,7 +234,7 @@ func opEq(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack } else { y.SetUint64(0) } - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -252,7 +252,7 @@ func opAnd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac x, y := stack.pop(), stack.pop() stack.push(x.And(x, y)) - evm.interpreter.intPool.put(y) + evm.BlockContext.Intpool.put(y) return nil, nil } @@ -260,7 +260,7 @@ func opOr(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack x, y := stack.pop(), stack.peek() y.Or(x, y) - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -268,7 +268,7 @@ func opXor(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac x, y := stack.pop(), stack.peek() y.Xor(x, y) - evm.interpreter.intPool.put(x) + evm.BlockContext.Intpool.put(x) return nil, nil } @@ -280,7 +280,7 @@ func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta } else { val.SetUint64(0) } - evm.interpreter.intPool.put(th) + evm.BlockContext.Intpool.put(th) return nil, nil } @@ -293,7 +293,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S } else { stack.push(x.SetUint64(0)) } - evm.interpreter.intPool.put(y, z) + evm.BlockContext.Intpool.put(y, z) return nil, nil } @@ -306,7 +306,7 @@ func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S } else { stack.push(x.SetUint64(0)) } - evm.interpreter.intPool.put(y, z) + evm.BlockContext.Intpool.put(y, z) return nil, nil } @@ -316,7 +316,7 @@ func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S func opSHL(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards shift, value := math.U256(stack.pop()), math.U256(stack.peek()) - defer evm.interpreter.intPool.put(shift) // First operand back into the pool + defer evm.BlockContext.Intpool.put(shift) // First operand back into the pool if shift.Cmp(common.Big256) >= 0 { value.SetUint64(0) @@ -334,7 +334,7 @@ func opSHL(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac func opSHR(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards shift, value := math.U256(stack.pop()), math.U256(stack.peek()) - defer evm.interpreter.intPool.put(shift) // First operand back into the pool + defer evm.BlockContext.Intpool.put(shift) // First operand back into the pool if shift.Cmp(common.Big256) >= 0 { value.SetUint64(0) @@ -352,7 +352,7 @@ func opSHR(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac func opSAR(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Note, S256 returns (potentially) a new bigint, so we're popping, not peeking this one shift, value := math.U256(stack.pop()), math.S256(stack.pop()) - defer evm.interpreter.intPool.put(shift) // First operand back into the pool + defer evm.BlockContext.Intpool.put(shift) // First operand back into the pool if shift.Cmp(common.Big256) >= 0 { if value.Sign() > 0 { @@ -378,9 +378,9 @@ func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta if evm.vmConfig.EnablePreimageRecording { evm.StateDB.AddPreimage(common.BytesToHash(hash), data) } - stack.push(evm.interpreter.intPool.get().SetBytes(hash)) + stack.push(evm.BlockContext.Intpool.get().SetBytes(hash)) - evm.interpreter.intPool.put(offset, size) + evm.BlockContext.Intpool.put(offset, size) return nil, nil } @@ -406,17 +406,17 @@ func opCaller(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S } func opCallValue(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().Set(contract.value)) + stack.push(evm.BlockContext.Intpool.get().Set(contract.value)) return nil, nil } func opCallDataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32))) + stack.push(evm.BlockContext.Intpool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32))) return nil, nil } func opCallDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().SetInt64(int64(len(contract.Input)))) + stack.push(evm.BlockContext.Intpool.get().SetInt64(int64(len(contract.Input)))) return nil, nil } @@ -428,12 +428,12 @@ func opCallDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, st ) memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length)) - evm.interpreter.intPool.put(memOffset, dataOffset, length) + evm.BlockContext.Intpool.put(memOffset, dataOffset, length) return nil, nil } func opReturnDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().SetUint64(uint64(len(evm.interpreter.returnData)))) + stack.push(evm.BlockContext.Intpool.get().SetUint64(uint64(len(evm.interpreter.returnData)))) return nil, nil } @@ -443,9 +443,9 @@ func opReturnDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, dataOffset = stack.pop() length = stack.pop() - end = evm.interpreter.intPool.get().Add(dataOffset, length) + end = evm.BlockContext.Intpool.get().Add(dataOffset, length) ) - defer evm.interpreter.intPool.put(memOffset, dataOffset, length, end) + defer evm.BlockContext.Intpool.put(memOffset, dataOffset, length, end) if end.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < end.Uint64() { return nil, errReturnDataOutOfBounds @@ -463,7 +463,7 @@ func opExtCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, sta } func opCodeSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - l := evm.interpreter.intPool.get().SetInt64(int64(len(contract.Code))) + l := evm.BlockContext.Intpool.get().SetInt64(int64(len(contract.Code))) stack.push(l) return nil, nil @@ -478,7 +478,7 @@ func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack codeCopy := getDataBig(contract.Code, codeOffset, length) memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) - evm.interpreter.intPool.put(memOffset, codeOffset, length) + evm.BlockContext.Intpool.put(memOffset, codeOffset, length) return nil, nil } @@ -492,64 +492,64 @@ func opExtCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, sta codeCopy := getDataBig(evm.StateDB.GetCode(addr), codeOffset, length) memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) - evm.interpreter.intPool.put(memOffset, codeOffset, length) + evm.BlockContext.Intpool.put(memOffset, codeOffset, length) return nil, nil } func opGasprice(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().Set(evm.GasPrice)) + stack.push(evm.BlockContext.Intpool.get().Set(evm.GasPrice)) return nil, nil } func opBlockhash(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { num := stack.pop() - n := evm.interpreter.intPool.get().Sub(evm.BlockNumber, common.Big257) - if num.Cmp(n) > 0 && num.Cmp(evm.BlockNumber) < 0 { + n := evm.BlockContext.Intpool.get().Sub(evm.BlockContext.BlockNumber, common.Big257) + if num.Cmp(n) > 0 && num.Cmp(evm.BlockContext.BlockNumber) < 0 { stack.push(evm.GetHash(num.Uint64()).Big()) } else { - stack.push(evm.interpreter.intPool.getZero()) + stack.push(evm.BlockContext.Intpool.getZero()) } - evm.interpreter.intPool.put(num, n) + evm.BlockContext.Intpool.put(num, n) return nil, nil } func opCoinbase(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.Coinbase.Big()) + stack.push(evm.BlockContext.Coinbase.Big()) return nil, nil } func opTimestamp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(math.U256(evm.interpreter.intPool.get().Set(evm.Time))) + stack.push(math.U256(evm.BlockContext.Intpool.get().Set(evm.BlockContext.Time))) return nil, nil } func opNumber(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(math.U256(evm.interpreter.intPool.get().Set(evm.BlockNumber))) + stack.push(math.U256(evm.BlockContext.Intpool.get().Set(evm.BlockContext.BlockNumber))) return nil, nil } func opDifficulty(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(math.U256(evm.interpreter.intPool.get().Set(evm.Difficulty))) + stack.push(math.U256(evm.BlockContext.Intpool.get().Set(evm.BlockContext.Difficulty))) return nil, nil } func opGasLimit(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(math.U256(evm.interpreter.intPool.get().SetUint64(evm.GasLimit))) + stack.push(math.U256(evm.BlockContext.Intpool.get().SetUint64(evm.BlockContext.GasLimit))) return nil, nil } func opPop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - evm.interpreter.intPool.put(stack.pop()) + evm.BlockContext.Intpool.put(stack.pop()) return nil, nil } func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { offset := stack.pop() - val := evm.interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32)) + val := evm.BlockContext.Intpool.get().SetBytes(memory.Get(offset.Int64(), 32)) stack.push(val) - evm.interpreter.intPool.put(offset) + evm.BlockContext.Intpool.put(offset) return nil, nil } @@ -558,7 +558,7 @@ func opMstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S mStart, val := stack.pop(), stack.pop() memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32)) - evm.interpreter.intPool.put(mStart, val) + evm.BlockContext.Intpool.put(mStart, val) return nil, nil } @@ -581,7 +581,7 @@ func opSstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S val := stack.pop() evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val)) - evm.interpreter.intPool.put(val) + evm.BlockContext.Intpool.put(val) return nil, nil } @@ -593,7 +593,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta } *pc = pos.Uint64() - evm.interpreter.intPool.put(pos) + evm.BlockContext.Intpool.put(pos) return nil, nil } @@ -609,7 +609,7 @@ func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St *pc++ } - evm.interpreter.intPool.put(pos, cond) + evm.BlockContext.Intpool.put(pos, cond) return nil, nil } @@ -618,17 +618,17 @@ func opJumpdest(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack } func opPc(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().SetUint64(*pc)) + stack.push(evm.BlockContext.Intpool.get().SetUint64(*pc)) return nil, nil } func opMsize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().SetInt64(int64(memory.Len()))) + stack.push(evm.BlockContext.Intpool.get().SetInt64(int64(memory.Len()))) return nil, nil } func opGas(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.push(evm.interpreter.intPool.get().SetUint64(contract.Gas)) + stack.push(evm.BlockContext.Intpool.get().SetUint64(contract.Gas)) return nil, nil } @@ -639,7 +639,7 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S input = memory.Get(offset.Int64(), size.Int64()) gas = contract.Gas ) - if evm.ChainConfig().IsEIP150(evm.BlockNumber) { + if evm.ChainConfig().IsEIP150(evm.BlockContext.BlockNumber) { gas -= gas / 64 } @@ -649,15 +649,15 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S // homestead we must check for CodeStoreOutOfGasError (homestead only // rule) and treat as an error, if the ruleset is frontier we must // ignore this error and pretend the operation was successful. - if evm.ChainConfig().IsHomestead(evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas { - stack.push(evm.interpreter.intPool.getZero()) + if evm.ChainConfig().IsHomestead(evm.BlockContext.BlockNumber) && suberr == ErrCodeStoreOutOfGas { + stack.push(evm.BlockContext.Intpool.getZero()) } else if suberr != nil && suberr != ErrCodeStoreOutOfGas { - stack.push(evm.interpreter.intPool.getZero()) + stack.push(evm.BlockContext.Intpool.getZero()) } else { stack.push(addr.Big()) } contract.Gas += returnGas - evm.interpreter.intPool.put(value, offset, size) + evm.BlockContext.Intpool.put(value, offset, size) if suberr == errExecutionReverted { return res, nil @@ -667,7 +667,7 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Pop gas. The actual gas in in evm.callGasTemp. - evm.interpreter.intPool.put(stack.pop()) + evm.BlockContext.Intpool.put(stack.pop()) gas := evm.callGasTemp // Pop other call parameters. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() @@ -681,22 +681,22 @@ func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta } ret, returnGas, err := evm.Call(contract, toAddr, args, gas, value) if err != nil { - stack.push(evm.interpreter.intPool.getZero()) + stack.push(evm.BlockContext.Intpool.getZero()) } else { - stack.push(evm.interpreter.intPool.get().SetUint64(1)) + stack.push(evm.BlockContext.Intpool.get().SetUint64(1)) } if err == nil || err == errExecutionReverted { memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } contract.Gas += returnGas - evm.interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize) + evm.BlockContext.Intpool.put(addr, value, inOffset, inSize, retOffset, retSize) return ret, nil } func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Pop gas. The actual gas is in evm.callGasTemp. - evm.interpreter.intPool.put(stack.pop()) + evm.BlockContext.Intpool.put(stack.pop()) gas := evm.callGasTemp // Pop other call parameters. addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() @@ -710,22 +710,22 @@ func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack } ret, returnGas, err := evm.CallCode(contract, toAddr, args, gas, value) if err != nil { - stack.push(evm.interpreter.intPool.getZero()) + stack.push(evm.BlockContext.Intpool.getZero()) } else { - stack.push(evm.interpreter.intPool.get().SetUint64(1)) + stack.push(evm.BlockContext.Intpool.get().SetUint64(1)) } if err == nil || err == errExecutionReverted { memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } contract.Gas += returnGas - evm.interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize) + evm.BlockContext.Intpool.put(addr, value, inOffset, inSize, retOffset, retSize) return ret, nil } func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Pop gas. The actual gas is in evm.callGasTemp. - evm.interpreter.intPool.put(stack.pop()) + evm.BlockContext.Intpool.put(stack.pop()) gas := evm.callGasTemp // Pop other call parameters. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() @@ -735,22 +735,22 @@ func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, st ret, returnGas, err := evm.DelegateCall(contract, toAddr, args, gas) if err != nil { - stack.push(evm.interpreter.intPool.getZero()) + stack.push(evm.BlockContext.Intpool.getZero()) } else { - stack.push(evm.interpreter.intPool.get().SetUint64(1)) + stack.push(evm.BlockContext.Intpool.get().SetUint64(1)) } if err == nil || err == errExecutionReverted { memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } contract.Gas += returnGas - evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) + evm.BlockContext.Intpool.put(addr, inOffset, inSize, retOffset, retSize) return ret, nil } func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { // Pop gas. The actual gas is in evm.callGasTemp. - evm.interpreter.intPool.put(stack.pop()) + evm.BlockContext.Intpool.put(stack.pop()) gas := evm.callGasTemp // Pop other call parameters. addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() @@ -760,16 +760,16 @@ func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac ret, returnGas, err := evm.StaticCall(contract, toAddr, args, gas) if err != nil { - stack.push(evm.interpreter.intPool.getZero()) + stack.push(evm.BlockContext.Intpool.getZero()) } else { - stack.push(evm.interpreter.intPool.get().SetUint64(1)) + stack.push(evm.BlockContext.Intpool.get().SetUint64(1)) } if err == nil || err == errExecutionReverted { memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } contract.Gas += returnGas - evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) + evm.BlockContext.Intpool.put(addr, inOffset, inSize, retOffset, retSize) return ret, nil } @@ -777,7 +777,7 @@ func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S offset, size := stack.pop(), stack.pop() ret := memory.GetPtr(offset.Int64(), size.Int64()) - evm.interpreter.intPool.put(offset, size) + evm.BlockContext.Intpool.put(offset, size) return ret, nil } @@ -785,7 +785,7 @@ func opRevert(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S offset, size := stack.pop(), stack.pop() ret := memory.GetPtr(offset.Int64(), size.Int64()) - evm.interpreter.intPool.put(offset, size) + evm.BlockContext.Intpool.put(offset, size) return ret, nil } @@ -819,10 +819,10 @@ func makeLog(size int) executionFunc { Data: d, // This is a non-consensus field, but assigned here because // core/state doesn't know the current block number. - BlockNumber: evm.BlockNumber.Uint64(), + BlockNumber: evm.BlockContext.BlockNumber.Uint64(), }) - evm.interpreter.intPool.put(mStart, mSize) + evm.BlockContext.Intpool.put(mStart, mSize) return nil, nil } } @@ -842,7 +842,7 @@ func makePush(size uint64, pushByteSize int) executionFunc { endMin = startMin + pushByteSize } - integer := evm.interpreter.intPool.get() + integer := evm.BlockContext.Intpool.get() stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize))) *pc += size @@ -853,7 +853,7 @@ func makePush(size uint64, pushByteSize int) executionFunc { // make dup instruction function func makeDup(size int64) executionFunc { return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { - stack.dup(evm.interpreter.intPool, int(size)) + stack.dup(evm.BlockContext.Intpool, int(size)) return nil, nil } } diff --git a/core/vm/int_pool_verifier.go b/core/vm/int_pool_verifier.go index 82fbfed699..7c6c5ce436 100644 --- a/core/vm/int_pool_verifier.go +++ b/core/vm/int_pool_verifier.go @@ -22,7 +22,7 @@ import "fmt" const verifyPool = true -func verifyIntegerPool(ip *intPool) { +func verifyIntegerPool(ip *IntPool) { for i, item := range ip.pool.data { if item.Cmp(checkVal) != 0 { panic(fmt.Sprintf("%d'th item failed aggressive pool check. Value was modified", i)) diff --git a/core/vm/int_pool_verifier_empty.go b/core/vm/int_pool_verifier_empty.go index a5f1dc02b7..164a227dc4 100644 --- a/core/vm/int_pool_verifier_empty.go +++ b/core/vm/int_pool_verifier_empty.go @@ -20,4 +20,4 @@ package vm const verifyPool = false -func verifyIntegerPool(ip *intPool) {} +func verifyIntegerPool(ip *IntPool) {} diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 6dccf2616f..84e3fc4953 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -20,7 +20,6 @@ import ( "fmt" "sync/atomic" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/params" ) @@ -50,42 +49,35 @@ type Interpreter struct { evm *EVM cfg Config gasTable params.GasTable - intPool *intPool readOnly bool // Whether to throw on stateful modifications returnData []byte // Last CALL's return data for subsequent reuse - precompiles map[common.Address]PrecompiledContract + blockContext *BlockContext } // NewInterpreter returns a new instance of the Interpreter. -func NewInterpreter(evm *EVM, cfg Config) *Interpreter { +func NewInterpreter(evm *EVM, cfg Config,blockContext *BlockContext) *Interpreter { // We use the STOP instruction whether to see // the jump table was initialised. If it was not // we'll set the default jump table. if !cfg.JumpTable[STOP].valid { switch { - case evm.ChainConfig().IsConstantinople(evm.BlockNumber): + case evm.ChainConfig().IsConstantinople(blockContext.BlockNumber): cfg.JumpTable = constantinopleInstructionSet - case evm.ChainConfig().IsByzantium(evm.BlockNumber): + case evm.ChainConfig().IsByzantium(blockContext.BlockNumber): cfg.JumpTable = byzantiumInstructionSet - case evm.ChainConfig().IsHomestead(evm.BlockNumber): + case evm.ChainConfig().IsHomestead(blockContext.BlockNumber): cfg.JumpTable = homesteadInstructionSet default: cfg.JumpTable = frontierInstructionSet } } - precompiles := PrecompiledContractsHomestead - if evm.ChainConfig().IsByzantium(evm.BlockNumber) { - precompiles = PrecompiledContractsByzantium - } - return &Interpreter{ evm: evm, cfg: cfg, - gasTable: evm.ChainConfig().GasTable(evm.BlockNumber), - intPool: newZerosizeIntPool(), - precompiles: precompiles, + gasTable: evm.ChainConfig().GasTable(blockContext.BlockNumber), + blockContext: blockContext, } } @@ -211,7 +203,7 @@ func (in *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err er // 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) + verifyIntegerPool(in.blockContext.Intpool) } // if the operation clears the return data (e.g. it has returning data) // set the last return to the result of the operation. diff --git a/core/vm/intpool.go b/core/vm/intpool.go index 9408a67a74..1b78029537 100644 --- a/core/vm/intpool.go +++ b/core/vm/intpool.go @@ -22,23 +22,23 @@ var checkVal = big.NewInt(-42) const poolLimit = 256 -// intPool is a pool of big integers that +// IntPool is a pool of big integers that // can be reused for all big.Int operations. -type intPool struct { +type IntPool struct { pool *Stack } -func newIntPool() *intPool { - return &intPool{pool: newstack()} +func NewIntpool() *IntPool { + return &IntPool{pool: newstack()} } -func newZerosizeIntPool() *intPool { - return &intPool{pool: newZeroSizeStack()} +func newZerosizeIntPool() *IntPool { + return &IntPool{pool: newZeroSizeStack()} } // get retrieves a big int from the pool, allocating one if the pool is empty. // Note, the returned int's value is arbitrary and will not be zeroed! -func (p *intPool) get() *big.Int { +func (p *IntPool) get() *big.Int { if p.pool.len() > 0 { return p.pool.pop() } @@ -47,7 +47,7 @@ func (p *intPool) get() *big.Int { // getZero retrieves a big int from the pool, setting it to zero or allocating // a new one if the pool is empty. -func (p *intPool) getZero() *big.Int { +func (p *IntPool) getZero() *big.Int { if p.pool.len() > 0 { return p.pool.pop().SetUint64(0) } @@ -56,7 +56,7 @@ func (p *intPool) getZero() *big.Int { // put returns an allocated big int to the pool to be later reused by get calls. // Note, the values as saved as is; neither put nor get zeroes the ints out! -func (p *intPool) put(is ...*big.Int) { +func (p *IntPool) put(is ...*big.Int) { if len(p.pool.data) > poolLimit { return } diff --git a/core/vm/stack.go b/core/vm/stack.go index 139a54ef9f..95e796b05d 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -64,7 +64,7 @@ func (st *Stack) swap(n int) { st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n] } -func (st *Stack) dup(pool *intPool, n int) { +func (st *Stack) dup(pool *IntPool, n int) { st.push(pool.get().Set(st.data[st.len()-n])) }