mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
core/vm, params: implemented STATIC_CALL - EIP116
This commit is contained in:
parent
f2e94ff94b
commit
a5047c73d5
8 changed files with 140 additions and 21 deletions
|
|
@ -70,6 +70,7 @@ var (
|
||||||
allToolsArchiveFiles = []string{
|
allToolsArchiveFiles = []string{
|
||||||
"COPYING",
|
"COPYING",
|
||||||
executablePath("abigen"),
|
executablePath("abigen"),
|
||||||
|
executablePath("bootnode"),
|
||||||
executablePath("evm"),
|
executablePath("evm"),
|
||||||
executablePath("geth"),
|
executablePath("geth"),
|
||||||
executablePath("swarm"),
|
executablePath("swarm"),
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,51 @@ func (evm *EVM) Cancel() {
|
||||||
atomic.StoreInt32(&evm.abort, 1)
|
atomic.StoreInt32(&evm.abort, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
|
||||||
|
if evm.vmConfig.NoRecursion && evm.depth > 0 {
|
||||||
|
return nil, gas, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Depth check execution. Fail if we're trying to execute above the
|
||||||
|
// limit.
|
||||||
|
if evm.depth > int(params.CallCreateDepth) {
|
||||||
|
return nil, gas, ErrDepth
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure the readonly is only set if we aren't in readonly yet
|
||||||
|
// this makes also sure that the readonly flag isn't removed for
|
||||||
|
// child calls.
|
||||||
|
if !evm.interpreter.readonly {
|
||||||
|
evm.interpreter.readonly = true
|
||||||
|
defer func() { evm.interpreter.readonly = false }()
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
to = AccountRef(addr)
|
||||||
|
snapshot = evm.StateDB.Snapshot()
|
||||||
|
)
|
||||||
|
if !evm.StateDB.Exist(addr) {
|
||||||
|
return nil, 0, errWriteProtection
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialise a new contract and set the code that is to be used by the
|
||||||
|
// E The contract is a scoped evmironment for this execution context
|
||||||
|
// only.
|
||||||
|
contract := NewContract(caller, to, new(big.Int), gas)
|
||||||
|
contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
|
||||||
|
|
||||||
|
ret, err = evm.interpreter.Run(contract, input)
|
||||||
|
// When an error was returned by the EVM or when setting the creation code
|
||||||
|
// above we revert to the snapshot and consume any gas remaining. Additionally
|
||||||
|
// when we're in homestead this also counts for code storage gas errors.
|
||||||
|
if err != nil {
|
||||||
|
contract.UseGas(contract.Gas)
|
||||||
|
|
||||||
|
evm.StateDB.RevertToSnapshot(snapshot)
|
||||||
|
}
|
||||||
|
return ret, contract.Gas, err
|
||||||
|
}
|
||||||
|
|
||||||
// Call executes the contract associated with the addr with the given input as parameters. It also handles any
|
// Call executes the contract associated with the addr with the given input as parameters. It also handles any
|
||||||
// necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
|
// necessary value transfer required and takes the necessary steps to create accounts and reverses the state in
|
||||||
// case of an execution error or failed value transfer.
|
// case of an execution error or failed value transfer.
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
|
@ -27,7 +28,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bigZero = new(big.Int)
|
var (
|
||||||
|
bigZero = new(big.Int)
|
||||||
|
errWriteProtection = errors.New("evm write protection")
|
||||||
|
)
|
||||||
|
|
||||||
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
x, y := stack.pop(), stack.pop()
|
x, y := stack.pop(), stack.pop()
|
||||||
|
|
@ -569,6 +573,40 @@ func opCreate(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func opStaticCall(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||||
|
// EIP116 availability check; return an invalid opcode error on fault
|
||||||
|
if !evm.ChainConfig().IsMetropolis(evm.BlockNumber) {
|
||||||
|
return nil, fmt.Errorf("invalid opcode %x", STATIC_CALL)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pop gas
|
||||||
|
gas := stack.pop().Uint64()
|
||||||
|
// pop address
|
||||||
|
addr := stack.pop()
|
||||||
|
// pop input size and offset
|
||||||
|
inOffset, inSize := stack.pop(), stack.pop()
|
||||||
|
// pop return size and offset
|
||||||
|
retOffset, retSize := stack.pop(), stack.pop()
|
||||||
|
|
||||||
|
address := common.BigToAddress(addr)
|
||||||
|
|
||||||
|
// Get the arguments from the memory
|
||||||
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
||||||
|
|
||||||
|
ret, returnGas, err := evm.StaticCall(contract, address, args, gas)
|
||||||
|
if err != nil {
|
||||||
|
stack.push(new(big.Int))
|
||||||
|
} else {
|
||||||
|
stack.push(big.NewInt(1))
|
||||||
|
|
||||||
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
||||||
|
}
|
||||||
|
contract.Gas += returnGas
|
||||||
|
|
||||||
|
evm.interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
gas := stack.pop().Uint64()
|
gas := stack.pop().Uint64()
|
||||||
// pop gas and value of the stack.
|
// pop gas and value of the stack.
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,8 @@ type Interpreter struct {
|
||||||
cfg Config
|
cfg Config
|
||||||
gasTable params.GasTable
|
gasTable params.GasTable
|
||||||
intPool *intPool
|
intPool *intPool
|
||||||
|
|
||||||
|
readonly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInterpreter returns a new instance of the Interpreter.
|
// NewInterpreter returns a new instance of the Interpreter.
|
||||||
|
|
@ -137,6 +139,15 @@ func (evm *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err e
|
||||||
|
|
||||||
// get the operation from the jump table matching the opcode
|
// get the operation from the jump table matching the opcode
|
||||||
operation := evm.cfg.JumpTable[op]
|
operation := evm.cfg.JumpTable[op]
|
||||||
|
// if the interpreter is operating in readonly mode, make sure no
|
||||||
|
// state-modifying operation is performed.
|
||||||
|
if evm.readonly && evm.env.chainConfig.IsMetropolis(evm.env.BlockNumber) {
|
||||||
|
if operation.writes ||
|
||||||
|
((op == CALL || op == CALLCODE) && stack.Back(3).BitLen() > 0) {
|
||||||
|
return nil, errWriteProtection
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// if the op is invalid abort the process and return an error
|
// if the op is invalid abort the process and return an error
|
||||||
if !operation.valid {
|
if !operation.valid {
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,8 @@ type operation struct {
|
||||||
// jumps indicates whether operation made a jump. This prevents the program
|
// jumps indicates whether operation made a jump. This prevents the program
|
||||||
// counter from further incrementing.
|
// counter from further incrementing.
|
||||||
jumps bool
|
jumps bool
|
||||||
|
// writes determines whether this a state modifying operation
|
||||||
|
writes bool
|
||||||
// valid is used to check whether the retrieved operation is valid and known
|
// valid is used to check whether the retrieved operation is valid and known
|
||||||
valid bool
|
valid bool
|
||||||
}
|
}
|
||||||
|
|
@ -357,6 +359,7 @@ func NewJumpTable() [256]operation {
|
||||||
gasCost: gasSStore,
|
gasCost: gasSStore,
|
||||||
validateStack: makeStackFunc(2, 0),
|
validateStack: makeStackFunc(2, 0),
|
||||||
valid: true,
|
valid: true,
|
||||||
|
writes: true,
|
||||||
},
|
},
|
||||||
JUMP: {
|
JUMP: {
|
||||||
execute: opJump,
|
execute: opJump,
|
||||||
|
|
@ -821,6 +824,7 @@ func NewJumpTable() [256]operation {
|
||||||
validateStack: makeStackFunc(3, 1),
|
validateStack: makeStackFunc(3, 1),
|
||||||
memorySize: memoryCreate,
|
memorySize: memoryCreate,
|
||||||
valid: true,
|
valid: true,
|
||||||
|
writes: true,
|
||||||
},
|
},
|
||||||
CALL: {
|
CALL: {
|
||||||
execute: opCall,
|
execute: opCall,
|
||||||
|
|
@ -857,6 +861,7 @@ func NewJumpTable() [256]operation {
|
||||||
validateStack: makeStackFunc(1, 0),
|
validateStack: makeStackFunc(1, 0),
|
||||||
halts: true,
|
halts: true,
|
||||||
valid: true,
|
valid: true,
|
||||||
|
writes: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -201,6 +201,7 @@ const (
|
||||||
CALLCODE
|
CALLCODE
|
||||||
RETURN
|
RETURN
|
||||||
DELEGATECALL
|
DELEGATECALL
|
||||||
|
STATIC_CALL
|
||||||
|
|
||||||
SELFDESTRUCT = 0xff
|
SELFDESTRUCT = 0xff
|
||||||
)
|
)
|
||||||
|
|
@ -356,6 +357,7 @@ var opCodeToString = map[OpCode]string{
|
||||||
CALLCODE: "CALLCODE",
|
CALLCODE: "CALLCODE",
|
||||||
DELEGATECALL: "DELEGATECALL",
|
DELEGATECALL: "DELEGATECALL",
|
||||||
SELFDESTRUCT: "SELFDESTRUCT",
|
SELFDESTRUCT: "SELFDESTRUCT",
|
||||||
|
STATIC_CALL: "STATIC_CALL",
|
||||||
|
|
||||||
PUSH: "PUSH",
|
PUSH: "PUSH",
|
||||||
DUP: "DUP",
|
DUP: "DUP",
|
||||||
|
|
@ -405,6 +407,7 @@ var stringToOp = map[string]OpCode{
|
||||||
"CALLDATASIZE": CALLDATASIZE,
|
"CALLDATASIZE": CALLDATASIZE,
|
||||||
"CALLDATACOPY": CALLDATACOPY,
|
"CALLDATACOPY": CALLDATACOPY,
|
||||||
"DELEGATECALL": DELEGATECALL,
|
"DELEGATECALL": DELEGATECALL,
|
||||||
|
"STATIC_CALL": STATIC_CALL,
|
||||||
"CODESIZE": CODESIZE,
|
"CODESIZE": CODESIZE,
|
||||||
"CODECOPY": CODECOPY,
|
"CODECOPY": CODECOPY,
|
||||||
"GASPRICE": GASPRICE,
|
"GASPRICE": GASPRICE,
|
||||||
|
|
|
||||||
|
|
@ -25,26 +25,28 @@ import (
|
||||||
|
|
||||||
// MainnetChainConfig is the chain parameters to run a node on the main network.
|
// MainnetChainConfig is the chain parameters to run a node on the main network.
|
||||||
var MainnetChainConfig = &ChainConfig{
|
var MainnetChainConfig = &ChainConfig{
|
||||||
ChainId: MainNetChainID,
|
ChainId: MainNetChainID,
|
||||||
HomesteadBlock: MainNetHomesteadBlock,
|
HomesteadBlock: MainNetHomesteadBlock,
|
||||||
DAOForkBlock: MainNetDAOForkBlock,
|
DAOForkBlock: MainNetDAOForkBlock,
|
||||||
DAOForkSupport: true,
|
DAOForkSupport: true,
|
||||||
EIP150Block: MainNetHomesteadGasRepriceBlock,
|
EIP150Block: MainNetHomesteadGasRepriceBlock,
|
||||||
EIP150Hash: MainNetHomesteadGasRepriceHash,
|
EIP150Hash: MainNetHomesteadGasRepriceHash,
|
||||||
EIP155Block: MainNetSpuriousDragon,
|
EIP155Block: MainNetSpuriousDragon,
|
||||||
EIP158Block: MainNetSpuriousDragon,
|
EIP158Block: MainNetSpuriousDragon,
|
||||||
|
MetropolisBlock: MainNetMetropolisBlock,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestnetChainConfig is the chain parameters to run a node on the test network.
|
// TestnetChainConfig is the chain parameters to run a node on the test network.
|
||||||
var TestnetChainConfig = &ChainConfig{
|
var TestnetChainConfig = &ChainConfig{
|
||||||
ChainId: big.NewInt(3),
|
ChainId: big.NewInt(3),
|
||||||
HomesteadBlock: big.NewInt(0),
|
HomesteadBlock: big.NewInt(0),
|
||||||
DAOForkBlock: nil,
|
DAOForkBlock: nil,
|
||||||
DAOForkSupport: true,
|
DAOForkSupport: true,
|
||||||
EIP150Block: big.NewInt(0),
|
EIP150Block: big.NewInt(0),
|
||||||
EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
|
EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
|
||||||
EIP155Block: big.NewInt(10),
|
EIP155Block: big.NewInt(10),
|
||||||
EIP158Block: big.NewInt(10),
|
EIP158Block: big.NewInt(10),
|
||||||
|
MetropolisBlock: big.NewInt(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
// AllProtocolChanges contains every protocol change (EIPs)
|
// AllProtocolChanges contains every protocol change (EIPs)
|
||||||
|
|
@ -55,7 +57,7 @@ var TestnetChainConfig = &ChainConfig{
|
||||||
// means that all fields must be set at all times. This forces
|
// means that all fields must be set at all times. This forces
|
||||||
// anyone adding flags to the config to also have to set these
|
// anyone adding flags to the config to also have to set these
|
||||||
// fields.
|
// fields.
|
||||||
var AllProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0)}
|
var AllProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0)}
|
||||||
|
|
||||||
// ChainConfig is the core config which determines the blockchain settings.
|
// ChainConfig is the core config which determines the blockchain settings.
|
||||||
//
|
//
|
||||||
|
|
@ -75,11 +77,13 @@ type ChainConfig struct {
|
||||||
|
|
||||||
EIP155Block *big.Int `json:"eip155Block"` // EIP155 HF block
|
EIP155Block *big.Int `json:"eip155Block"` // EIP155 HF block
|
||||||
EIP158Block *big.Int `json:"eip158Block"` // EIP158 HF block
|
EIP158Block *big.Int `json:"eip158Block"` // EIP158 HF block
|
||||||
|
|
||||||
|
MetropolisBlock *big.Int `json:"metropolisBlock"` // Metropolis switch block (nil = no fork, 0 = alraedy on homestead)
|
||||||
}
|
}
|
||||||
|
|
||||||
// String implements the Stringer interface.
|
// String implements the Stringer interface.
|
||||||
func (c *ChainConfig) String() string {
|
func (c *ChainConfig) String() string {
|
||||||
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v}",
|
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Metropolis: %v}",
|
||||||
c.ChainId,
|
c.ChainId,
|
||||||
c.HomesteadBlock,
|
c.HomesteadBlock,
|
||||||
c.DAOForkBlock,
|
c.DAOForkBlock,
|
||||||
|
|
@ -87,11 +91,12 @@ func (c *ChainConfig) String() string {
|
||||||
c.EIP150Block,
|
c.EIP150Block,
|
||||||
c.EIP155Block,
|
c.EIP155Block,
|
||||||
c.EIP158Block,
|
c.EIP158Block,
|
||||||
|
c.MetropolisBlock,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
TestChainConfig = &ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), true, new(big.Int), common.Hash{}, new(big.Int), new(big.Int)}
|
TestChainConfig = &ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), true, new(big.Int), common.Hash{}, new(big.Int), new(big.Int), new(big.Int)}
|
||||||
TestRules = TestChainConfig.Rules(new(big.Int))
|
TestRules = TestChainConfig.Rules(new(big.Int))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -145,6 +150,14 @@ func (c *ChainConfig) IsEIP158(num *big.Int) bool {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ChainConfig) IsMetropolis(num *big.Int) bool {
|
||||||
|
if c.MetropolisBlock == nil || num == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return num.Cmp(c.MetropolisBlock) >= 0
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
|
// Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
|
||||||
// that do not have or require information about the block.
|
// that do not have or require information about the block.
|
||||||
//
|
//
|
||||||
|
|
@ -153,8 +166,9 @@ func (c *ChainConfig) IsEIP158(num *big.Int) bool {
|
||||||
type Rules struct {
|
type Rules struct {
|
||||||
ChainId *big.Int
|
ChainId *big.Int
|
||||||
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
|
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
|
||||||
|
IsMetropolis bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ChainConfig) Rules(num *big.Int) Rules {
|
func (c *ChainConfig) Rules(num *big.Int) Rules {
|
||||||
return Rules{ChainId: new(big.Int).Set(c.ChainId), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num)}
|
return Rules{ChainId: new(big.Int).Set(c.ChainId), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num), IsMetropolis: c.IsMetropolis(num)}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,8 @@ var (
|
||||||
TestNetSpuriousDragon = big.NewInt(10)
|
TestNetSpuriousDragon = big.NewInt(10)
|
||||||
MainNetSpuriousDragon = big.NewInt(2675000)
|
MainNetSpuriousDragon = big.NewInt(2675000)
|
||||||
|
|
||||||
|
MainNetMetropolisBlock = big.NewInt(5000000)
|
||||||
|
|
||||||
TestNetChainID = big.NewInt(3) // Test net default chain ID
|
TestNetChainID = big.NewInt(3) // Test net default chain ID
|
||||||
MainNetChainID = big.NewInt(1) // main net default chain ID
|
MainNetChainID = big.NewInt(1) // main net default chain ID
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue