experiment: use block context

This commit is contained in:
Martin Holst Swende 2018-04-08 21:49:31 +02:00
parent 52f01ed388
commit 53fbd7ce63
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
12 changed files with 176 additions and 151 deletions

View file

@ -53,7 +53,8 @@ type BlockGen struct {
config *params.ChainConfig config *params.ChainConfig
engine consensus.Engine engine consensus.Engine
}
}
// SetCoinbase sets the coinbase of the generated block. // SetCoinbase sets the coinbase of the generated block.
// It can be called at most once. // 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.SetCoinbase(common.Address{})
} }
b.statedb.Prepare(tx.Hash(), common.Hash{}, len(b.txs)) 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 { if err != nil {
panic(err) panic(err)
} }

View file

@ -37,23 +37,11 @@ type ChainContext interface {
// NewEVMContext creates a new context for use in the EVM. // NewEVMContext creates a new context for use in the EVM.
func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context { 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{ return vm.Context{
CanTransfer: CanTransfer, CanTransfer: CanTransfer,
Transfer: Transfer, Transfer: Transfer,
GetHash: GetHashFn(header, chain), GetHash: GetHashFn(header, chain),
Origin: msg.From(), 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()), GasPrice: new(big.Int).Set(msg.GasPrice()),
} }
} }

View file

@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"math/big"
) )
// StateProcessor is a basic Processor, which takes care of transitioning // 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 { if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(statedb) 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 // Iterate over and process the individual transactions
receipts = make([]*types.Receipt, len(block.Transactions())) receipts = make([]*types.Receipt, len(block.Transactions()))
for i, tx := range block.Transactions() { for i, tx := range block.Transactions() {
statedb.Prepare(tx.Hash(), block.Hash(), i) 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 { if err != nil {
return nil, nil, 0, err 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 // and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed, // for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid. // 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) { 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(*signer) msg, err := tx.AsMessage(blockContext.Signer)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -96,7 +114,7 @@ func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common
context := NewEVMContext(msg, header, bc, author) context := NewEVMContext(msg, header, bc, author)
// Create a new environment which holds all relevant information // Create a new environment which holds all relevant information
// about the transaction and calling mechanisms. // 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) // 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)
if err != nil { if err != nil {

View file

@ -186,7 +186,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
} }
msg := st.msg msg := st.msg
sender := vm.AccountRef(msg.From()) 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 contractCreation := msg.To() == nil
// Pay intrinsic gas // Pay intrinsic gas
@ -222,7 +222,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
} }
} }
st.refundGas() 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 return ret, st.gasUsed(), vmerr != nil, err
} }

View file

@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "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 // 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. // 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) { func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) {
if contract.CodeAddr != nil { 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) return RunPrecompiledContract(p, input, contract)
} }
} }
@ -63,6 +64,20 @@ type Context struct {
Origin common.Address // Provides information for ORIGIN Origin common.Address // Provides information for ORIGIN
GasPrice *big.Int // Provides information for GASPRICE 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 // Block information
Coinbase common.Address // Provides information for COINBASE Coinbase common.Address // Provides information for COINBASE
GasLimit uint64 // Provides information for GASLIMIT GasLimit uint64 // Provides information for GASLIMIT
@ -81,6 +96,7 @@ type Context struct {
// //
// The EVM should never be reused and is not thread safe. // The EVM should never be reused and is not thread safe.
type EVM struct { type EVM struct {
BlockContext *BlockContext
// Context provides auxiliary blockchain related information // Context provides auxiliary blockchain related information
Context Context
// StateDB gives access to the underlying state // 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 // NewEVM returns a new EVM. The returned EVM is not thread safe and should
// only ever be used *once*. // 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{ evm := &EVM{
Context: ctx, Context: ctx,
StateDB: statedb, StateDB: statedb,
vmConfig: vmConfig, vmConfig: vmConfig,
chainConfig: chainConfig, chainConfig: chainConfig,
chainRules: chainConfig.Rules(ctx.BlockNumber), chainRules: chainConfig.Rules(blockContext.BlockNumber),
BlockContext: blockContext,
} }
evm.interpreter = NewInterpreter(evm, vmConfig) evm.interpreter = NewInterpreter(evm, vmConfig, blockContext)
return evm return evm
} }
@ -151,8 +168,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
snapshot = evm.StateDB.Snapshot() snapshot = evm.StateDB.Snapshot()
) )
if !evm.StateDB.Exist(addr) { 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 // Calling a non existing account, don't do antything, but ping the tracer
if evm.vmConfig.Debug && evm.depth == 0 { if evm.vmConfig.Debug && evm.depth == 0 {
evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value) 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) code := evm.StateDB.GetCode(addr)
codeHash := evm.StateDB.GetCodeHash(addr) codeHash := evm.StateDB.GetCodeHash(addr)
_, isPrecompile := evm.interpreter.precompiles[addr] _, isPrecompile := evm.BlockContext.Precompiles[addr]
if !isPrecompile && len(code) == 0 { if !isPrecompile && len(code) == 0 {
// Shortcut execution if there is no code, // 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 // Create a new account on the state
snapshot := evm.StateDB.Snapshot() snapshot := evm.StateDB.Snapshot()
evm.StateDB.CreateAccount(contractAddr) evm.StateDB.CreateAccount(contractAddr)
if evm.ChainConfig().IsEIP158(evm.BlockNumber) { if evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber) {
evm.StateDB.SetNonce(contractAddr, 1) evm.StateDB.SetNonce(contractAddr, 1)
} }
evm.Transfer(evm.StateDB, caller.Address(), contractAddr, value) 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) ret, err = run(evm, contract, nil)
// check whether the max code size has been exceeded // 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 // if the contract creation ran successfully and no errors were returned
// calculate the gas required to store the code. If the code could not // 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 // 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 // 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 // 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. // 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) evm.StateDB.RevertToSnapshot(snapshot)
if err != errExecutionReverted { if err != errExecutionReverted {
contract.UseGas(contract.Gas) contract.UseGas(contract.Gas)

View file

@ -319,7 +319,7 @@ func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
gas = gt.Calls gas = gt.Calls
transfersValue = stack.Back(2).Sign() != 0 transfersValue = stack.Back(2).Sign() != 0
address = common.BigToAddress(stack.Back(1)) address = common.BigToAddress(stack.Back(1))
eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) eip158 = evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber)
) )
if eip158 { if eip158 {
if transfersValue && evm.StateDB.Empty(address) { 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) { func gasSuicide(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var gas uint64 var gas uint64
// EIP150 homestead gas reprice fork: // EIP150 homestead gas reprice fork:
if evm.ChainConfig().IsEIP150(evm.BlockNumber) { if evm.ChainConfig().IsEIP150(evm.BlockContext.BlockNumber) {
gas = gt.Suicide gas = gt.Suicide
var ( var (
address = common.BigToAddress(stack.Back(0)) address = common.BigToAddress(stack.Back(0))
eip158 = evm.ChainConfig().IsEIP158(evm.BlockNumber) eip158 = evm.ChainConfig().IsEIP158(evm.BlockContext.BlockNumber)
) )
if eip158 { if eip158 {

View file

@ -41,7 +41,7 @@ func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
x, y := stack.pop(), stack.peek() x, y := stack.pop(), stack.peek()
math.U256(y.Add(x, y)) math.U256(y.Add(x, y))
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil 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() x, y := stack.pop(), stack.peek()
math.U256(y.Sub(x, y)) math.U256(y.Sub(x, y))
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil 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() x, y := stack.pop(), stack.pop()
stack.push(math.U256(x.Mul(x, y))) stack.push(math.U256(x.Mul(x, y)))
evm.interpreter.intPool.put(y) evm.BlockContext.Intpool.put(y)
return nil, nil return nil, nil
} }
@ -69,13 +69,13 @@ func opDiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
} else { } else {
y.SetUint64(0) y.SetUint64(0)
} }
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil return nil, nil
} }
func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x, y := math.S256(stack.pop()), math.S256(stack.pop()) 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 { if y.Sign() == 0 || x.Sign() == 0 {
stack.push(res) stack.push(res)
@ -88,7 +88,7 @@ func opSdiv(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
} }
stack.push(math.U256(res)) stack.push(math.U256(res))
} }
evm.interpreter.intPool.put(x, y) evm.BlockContext.Intpool.put(x, y)
return nil, nil return nil, nil
} }
@ -99,13 +99,13 @@ func opMod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
} else { } else {
stack.push(math.U256(x.Mod(x, y))) stack.push(math.U256(x.Mod(x, y)))
} }
evm.interpreter.intPool.put(y) evm.BlockContext.Intpool.put(y)
return nil, nil return nil, nil
} }
func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
x, y := math.S256(stack.pop()), math.S256(stack.pop()) x, y := math.S256(stack.pop()), math.S256(stack.pop())
res := evm.interpreter.intPool.getZero() res := evm.BlockContext.Intpool.getZero()
if y.Sign() == 0 { if y.Sign() == 0 {
stack.push(res) stack.push(res)
@ -118,7 +118,7 @@ func opSmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
} }
stack.push(math.U256(res)) stack.push(math.U256(res))
} }
evm.interpreter.intPool.put(x, y) evm.BlockContext.Intpool.put(x, y)
return nil, nil 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() base, exponent := stack.pop(), stack.pop()
stack.push(math.Exp(base, exponent)) stack.push(math.Exp(base, exponent))
evm.interpreter.intPool.put(base, exponent) evm.BlockContext.Intpool.put(base, exponent)
return nil, nil return nil, nil
} }
@ -147,7 +147,7 @@ func opSignExtend(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stac
stack.push(math.U256(num)) stack.push(math.U256(num))
} }
evm.interpreter.intPool.put(back) evm.BlockContext.Intpool.put(back)
return nil, nil return nil, nil
} }
@ -164,7 +164,7 @@ func opLt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack
} else { } else {
y.SetUint64(0) y.SetUint64(0)
} }
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil return nil, nil
} }
@ -175,7 +175,7 @@ func opGt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack
} else { } else {
y.SetUint64(0) y.SetUint64(0)
} }
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil return nil, nil
} }
@ -199,7 +199,7 @@ func opSlt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
y.SetUint64(0) y.SetUint64(0)
} }
} }
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil return nil, nil
} }
@ -223,7 +223,7 @@ func opSgt(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stac
y.SetUint64(0) y.SetUint64(0)
} }
} }
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil return nil, nil
} }
@ -234,7 +234,7 @@ func opEq(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack
} else { } else {
y.SetUint64(0) y.SetUint64(0)
} }
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil 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() x, y := stack.pop(), stack.pop()
stack.push(x.And(x, y)) stack.push(x.And(x, y))
evm.interpreter.intPool.put(y) evm.BlockContext.Intpool.put(y)
return nil, nil 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() x, y := stack.pop(), stack.peek()
y.Or(x, y) y.Or(x, y)
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil 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() x, y := stack.pop(), stack.peek()
y.Xor(x, y) y.Xor(x, y)
evm.interpreter.intPool.put(x) evm.BlockContext.Intpool.put(x)
return nil, nil return nil, nil
} }
@ -280,7 +280,7 @@ func opByte(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
} else { } else {
val.SetUint64(0) val.SetUint64(0)
} }
evm.interpreter.intPool.put(th) evm.BlockContext.Intpool.put(th)
return nil, nil return nil, nil
} }
@ -293,7 +293,7 @@ func opAddmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
} else { } else {
stack.push(x.SetUint64(0)) stack.push(x.SetUint64(0))
} }
evm.interpreter.intPool.put(y, z) evm.BlockContext.Intpool.put(y, z)
return nil, nil return nil, nil
} }
@ -306,7 +306,7 @@ func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
} else { } else {
stack.push(x.SetUint64(0)) stack.push(x.SetUint64(0))
} }
evm.interpreter.intPool.put(y, z) evm.BlockContext.Intpool.put(y, z)
return nil, nil 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) { 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 // 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()) 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 { if shift.Cmp(common.Big256) >= 0 {
value.SetUint64(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) { 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 // 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()) 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 { if shift.Cmp(common.Big256) >= 0 {
value.SetUint64(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) { 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 // 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()) 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 shift.Cmp(common.Big256) >= 0 {
if value.Sign() > 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 { if evm.vmConfig.EnablePreimageRecording {
evm.StateDB.AddPreimage(common.BytesToHash(hash), data) 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 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) { 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 return nil, nil
} }
func opCallDataLoad(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opCallDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 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)) 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 return nil, nil
} }
func opReturnDataSize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
@ -443,9 +443,9 @@ func opReturnDataCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory,
dataOffset = stack.pop() dataOffset = stack.pop()
length = 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() { if end.BitLen() > 64 || uint64(len(evm.interpreter.returnData)) < end.Uint64() {
return nil, errReturnDataOutOfBounds 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) { 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) stack.push(l)
return nil, nil return nil, nil
@ -478,7 +478,7 @@ func opCodeCopy(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
codeCopy := getDataBig(contract.Code, codeOffset, length) codeCopy := getDataBig(contract.Code, codeOffset, length)
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
evm.interpreter.intPool.put(memOffset, codeOffset, length) evm.BlockContext.Intpool.put(memOffset, codeOffset, length)
return nil, nil 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) codeCopy := getDataBig(evm.StateDB.GetCode(addr), codeOffset, length)
memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
evm.interpreter.intPool.put(memOffset, codeOffset, length) evm.BlockContext.Intpool.put(memOffset, codeOffset, length)
return nil, nil return nil, nil
} }
func opGasprice(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opBlockhash(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opBlockhash(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
num := stack.pop() num := stack.pop()
n := evm.interpreter.intPool.get().Sub(evm.BlockNumber, common.Big257) n := evm.BlockContext.Intpool.get().Sub(evm.BlockContext.BlockNumber, common.Big257)
if num.Cmp(n) > 0 && num.Cmp(evm.BlockNumber) < 0 { if num.Cmp(n) > 0 && num.Cmp(evm.BlockContext.BlockNumber) < 0 {
stack.push(evm.GetHash(num.Uint64()).Big()) stack.push(evm.GetHash(num.Uint64()).Big())
} else { } 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 return nil, nil
} }
func opCoinbase(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opTimestamp(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opNumber(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opDifficulty(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opGasLimit(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opPop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opMload(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
offset := stack.pop() 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) stack.push(val)
evm.interpreter.intPool.put(offset) evm.BlockContext.Intpool.put(offset)
return nil, nil 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() mStart, val := stack.pop(), stack.pop()
memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32)) memory.Set(mStart.Uint64(), 32, math.PaddedBigBytes(val, 32))
evm.interpreter.intPool.put(mStart, val) evm.BlockContext.Intpool.put(mStart, val)
return nil, nil return nil, nil
} }
@ -581,7 +581,7 @@ func opSstore(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
val := stack.pop() val := stack.pop()
evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val)) evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val))
evm.interpreter.intPool.put(val) evm.BlockContext.Intpool.put(val)
return nil, nil return nil, nil
} }
@ -593,7 +593,7 @@ func opJump(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Sta
} }
*pc = pos.Uint64() *pc = pos.Uint64()
evm.interpreter.intPool.put(pos) evm.BlockContext.Intpool.put(pos)
return nil, nil return nil, nil
} }
@ -609,7 +609,7 @@ func opJumpi(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *St
*pc++ *pc++
} }
evm.interpreter.intPool.put(pos, cond) evm.BlockContext.Intpool.put(pos, cond)
return nil, nil 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) { 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 return nil, nil
} }
func opMsize(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
func opGas(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 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()) input = memory.Get(offset.Int64(), size.Int64())
gas = contract.Gas gas = contract.Gas
) )
if evm.ChainConfig().IsEIP150(evm.BlockNumber) { if evm.ChainConfig().IsEIP150(evm.BlockContext.BlockNumber) {
gas -= gas / 64 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 // homestead we must check for CodeStoreOutOfGasError (homestead only
// rule) and treat as an error, if the ruleset is frontier we must // rule) and treat as an error, if the ruleset is frontier we must
// ignore this error and pretend the operation was successful. // ignore this error and pretend the operation was successful.
if evm.ChainConfig().IsHomestead(evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas { if evm.ChainConfig().IsHomestead(evm.BlockContext.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
stack.push(evm.interpreter.intPool.getZero()) stack.push(evm.BlockContext.Intpool.getZero())
} else if suberr != nil && suberr != ErrCodeStoreOutOfGas { } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
stack.push(evm.interpreter.intPool.getZero()) stack.push(evm.BlockContext.Intpool.getZero())
} else { } else {
stack.push(addr.Big()) stack.push(addr.Big())
} }
contract.Gas += returnGas contract.Gas += returnGas
evm.interpreter.intPool.put(value, offset, size) evm.BlockContext.Intpool.put(value, offset, size)
if suberr == errExecutionReverted { if suberr == errExecutionReverted {
return res, nil 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) { func opCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Pop gas. The actual gas in in evm.callGasTemp. // Pop gas. The actual gas in in evm.callGasTemp.
evm.interpreter.intPool.put(stack.pop()) evm.BlockContext.Intpool.put(stack.pop())
gas := evm.callGasTemp gas := evm.callGasTemp
// Pop other call parameters. // Pop other call parameters.
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 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) ret, returnGas, err := evm.Call(contract, toAddr, args, gas, value)
if err != nil { if err != nil {
stack.push(evm.interpreter.intPool.getZero()) stack.push(evm.BlockContext.Intpool.getZero())
} else { } else {
stack.push(evm.interpreter.intPool.get().SetUint64(1)) stack.push(evm.BlockContext.Intpool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == errExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas 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 return ret, nil
} }
func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opCallCode(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Pop gas. The actual gas is in evm.callGasTemp. // Pop gas. The actual gas is in evm.callGasTemp.
evm.interpreter.intPool.put(stack.pop()) evm.BlockContext.Intpool.put(stack.pop())
gas := evm.callGasTemp gas := evm.callGasTemp
// Pop other call parameters. // Pop other call parameters.
addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 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) ret, returnGas, err := evm.CallCode(contract, toAddr, args, gas, value)
if err != nil { if err != nil {
stack.push(evm.interpreter.intPool.getZero()) stack.push(evm.BlockContext.Intpool.getZero())
} else { } else {
stack.push(evm.interpreter.intPool.get().SetUint64(1)) stack.push(evm.BlockContext.Intpool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == errExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas 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 return ret, nil
} }
func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opDelegateCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Pop gas. The actual gas is in evm.callGasTemp. // Pop gas. The actual gas is in evm.callGasTemp.
evm.interpreter.intPool.put(stack.pop()) evm.BlockContext.Intpool.put(stack.pop())
gas := evm.callGasTemp gas := evm.callGasTemp
// Pop other call parameters. // Pop other call parameters.
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 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) ret, returnGas, err := evm.DelegateCall(contract, toAddr, args, gas)
if err != nil { if err != nil {
stack.push(evm.interpreter.intPool.getZero()) stack.push(evm.BlockContext.Intpool.getZero())
} else { } else {
stack.push(evm.interpreter.intPool.get().SetUint64(1)) stack.push(evm.BlockContext.Intpool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == errExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas contract.Gas += returnGas
evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) evm.BlockContext.Intpool.put(addr, inOffset, inSize, retOffset, retSize)
return ret, nil return ret, nil
} }
func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
// Pop gas. The actual gas is in evm.callGasTemp. // Pop gas. The actual gas is in evm.callGasTemp.
evm.interpreter.intPool.put(stack.pop()) evm.BlockContext.Intpool.put(stack.pop())
gas := evm.callGasTemp gas := evm.callGasTemp
// Pop other call parameters. // Pop other call parameters.
addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 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) ret, returnGas, err := evm.StaticCall(contract, toAddr, args, gas)
if err != nil { if err != nil {
stack.push(evm.interpreter.intPool.getZero()) stack.push(evm.BlockContext.Intpool.getZero())
} else { } else {
stack.push(evm.interpreter.intPool.get().SetUint64(1)) stack.push(evm.BlockContext.Intpool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == errExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas contract.Gas += returnGas
evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) evm.BlockContext.Intpool.put(addr, inOffset, inSize, retOffset, retSize)
return ret, nil 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() offset, size := stack.pop(), stack.pop()
ret := memory.GetPtr(offset.Int64(), size.Int64()) ret := memory.GetPtr(offset.Int64(), size.Int64())
evm.interpreter.intPool.put(offset, size) evm.BlockContext.Intpool.put(offset, size)
return ret, nil 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() offset, size := stack.pop(), stack.pop()
ret := memory.GetPtr(offset.Int64(), size.Int64()) ret := memory.GetPtr(offset.Int64(), size.Int64())
evm.interpreter.intPool.put(offset, size) evm.BlockContext.Intpool.put(offset, size)
return ret, nil return ret, nil
} }
@ -819,10 +819,10 @@ func makeLog(size int) executionFunc {
Data: d, Data: d,
// This is a non-consensus field, but assigned here because // This is a non-consensus field, but assigned here because
// core/state doesn't know the current block number. // 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 return nil, nil
} }
} }
@ -842,7 +842,7 @@ func makePush(size uint64, pushByteSize int) executionFunc {
endMin = startMin + pushByteSize endMin = startMin + pushByteSize
} }
integer := evm.interpreter.intPool.get() integer := evm.BlockContext.Intpool.get()
stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize))) stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
*pc += size *pc += size
@ -853,7 +853,7 @@ func makePush(size uint64, pushByteSize int) executionFunc {
// make dup instruction function // make dup instruction function
func makeDup(size int64) executionFunc { func makeDup(size int64) executionFunc {
return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 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 return nil, nil
} }
} }

View file

@ -22,7 +22,7 @@ import "fmt"
const verifyPool = true const verifyPool = true
func verifyIntegerPool(ip *intPool) { func verifyIntegerPool(ip *IntPool) {
for i, item := range ip.pool.data { for i, item := range ip.pool.data {
if item.Cmp(checkVal) != 0 { if item.Cmp(checkVal) != 0 {
panic(fmt.Sprintf("%d'th item failed aggressive pool check. Value was modified", i)) panic(fmt.Sprintf("%d'th item failed aggressive pool check. Value was modified", i))

View file

@ -20,4 +20,4 @@ package vm
const verifyPool = false const verifyPool = false
func verifyIntegerPool(ip *intPool) {} func verifyIntegerPool(ip *IntPool) {}

View file

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"sync/atomic" "sync/atomic"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
@ -50,42 +49,35 @@ type Interpreter struct {
evm *EVM evm *EVM
cfg Config cfg Config
gasTable params.GasTable gasTable params.GasTable
intPool *intPool
readOnly bool // Whether to throw on stateful modifications readOnly bool // Whether to throw on stateful modifications
returnData []byte // Last CALL's return data for subsequent reuse 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. // 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 // We use the STOP instruction whether to see
// the jump table was initialised. If it was not // the jump table was initialised. If it was not
// we'll set the default jump table. // we'll set the default jump table.
if !cfg.JumpTable[STOP].valid { if !cfg.JumpTable[STOP].valid {
switch { switch {
case evm.ChainConfig().IsConstantinople(evm.BlockNumber): case evm.ChainConfig().IsConstantinople(blockContext.BlockNumber):
cfg.JumpTable = constantinopleInstructionSet cfg.JumpTable = constantinopleInstructionSet
case evm.ChainConfig().IsByzantium(evm.BlockNumber): case evm.ChainConfig().IsByzantium(blockContext.BlockNumber):
cfg.JumpTable = byzantiumInstructionSet cfg.JumpTable = byzantiumInstructionSet
case evm.ChainConfig().IsHomestead(evm.BlockNumber): case evm.ChainConfig().IsHomestead(blockContext.BlockNumber):
cfg.JumpTable = homesteadInstructionSet cfg.JumpTable = homesteadInstructionSet
default: default:
cfg.JumpTable = frontierInstructionSet cfg.JumpTable = frontierInstructionSet
} }
} }
precompiles := PrecompiledContractsHomestead
if evm.ChainConfig().IsByzantium(evm.BlockNumber) {
precompiles = PrecompiledContractsByzantium
}
return &Interpreter{ return &Interpreter{
evm: evm, evm: evm,
cfg: cfg, cfg: cfg,
gasTable: evm.ChainConfig().GasTable(evm.BlockNumber), gasTable: evm.ChainConfig().GasTable(blockContext.BlockNumber),
intPool: newZerosizeIntPool(), blockContext: blockContext,
precompiles: precompiles,
} }
} }
@ -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 // verifyPool is a build flag. Pool verification makes sure the integrity
// of the integer pool by comparing values to a default value. // of the integer pool by comparing values to a default value.
if verifyPool { if verifyPool {
verifyIntegerPool(in.intPool) verifyIntegerPool(in.blockContext.Intpool)
} }
// if the operation clears the return data (e.g. it has returning data) // if the operation clears the return data (e.g. it has returning data)
// set the last return to the result of the operation. // set the last return to the result of the operation.

View file

@ -22,23 +22,23 @@ var checkVal = big.NewInt(-42)
const poolLimit = 256 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. // can be reused for all big.Int operations.
type intPool struct { type IntPool struct {
pool *Stack pool *Stack
} }
func newIntPool() *intPool { func NewIntpool() *IntPool {
return &intPool{pool: newstack()} return &IntPool{pool: newstack()}
} }
func newZerosizeIntPool() *intPool { func newZerosizeIntPool() *IntPool {
return &intPool{pool: newZeroSizeStack()} return &IntPool{pool: newZeroSizeStack()}
} }
// get retrieves a big int from the pool, allocating one if the pool is empty. // 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! // 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 { if p.pool.len() > 0 {
return p.pool.pop() 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 // getZero retrieves a big int from the pool, setting it to zero or allocating
// a new one if the pool is empty. // 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 { if p.pool.len() > 0 {
return p.pool.pop().SetUint64(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. // 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! // 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 { if len(p.pool.data) > poolLimit {
return return
} }

View file

@ -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] 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])) st.push(pool.get().Set(st.data[st.len()-n]))
} }