From 591ac8133d504b3e49d81a3d17f0a9f873d6bede Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Wed, 1 Feb 2017 15:01:34 +0100 Subject: [PATCH] core/vm: implement opcode REVERT - EIP#140 --- core/vm/evm.go | 20 +++++++++------ core/vm/instructions.go | 20 ++++++++++++++- core/vm/jump_table.go | 8 ++++++ core/vm/opcodes.go | 3 +++ params/config.go | 56 ++++++++++++++++++++--------------------- params/util.go | 4 +-- 6 files changed, 73 insertions(+), 38 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index 8c28bef718..87d12d2cb1 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -155,8 +155,9 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas // 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) - + if err != errRevert { + contract.UseGas(contract.Gas) + } evm.StateDB.RevertToSnapshot(snapshot) } return ret, contract.Gas, err @@ -193,7 +194,9 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, ret, err = run(evm, contract, input) if err != nil { - contract.UseGas(contract.Gas) + if err != errRevert { + contract.UseGas(contract.Gas) + } evm.StateDB.RevertToSnapshot(snapshot) } @@ -228,7 +231,9 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by ret, err = run(evm, contract, input) if err != nil { - contract.UseGas(contract.Gas) + if err != errRevert { + contract.UseGas(contract.Gas) + } evm.StateDB.RevertToSnapshot(snapshot) } @@ -290,10 +295,11 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I // when we're in homestead this also counts for code storage gas errors. if maxCodeSizeExceeded || (err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) { - evm.StateDB.RevertToSnapshot(snapshot) + if err != errRevert { + contract.UseGas(contract.Gas) + } - // Nothing should be returned when an error is thrown. - return nil, contractAddr, 0, err + evm.StateDB.RevertToSnapshot(snapshot) } // If the vm returned with an error the return value should be set to nil. // This isn't consensus critical but merely to for behaviour reasons such as diff --git a/core/vm/instructions.go b/core/vm/instructions.go index bfc0a668e4..d8737785f1 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -17,6 +17,7 @@ package vm import ( + "errors" "fmt" "math/big" @@ -27,7 +28,10 @@ import ( "github.com/ethereum/go-ethereum/params" ) -var bigZero = new(big.Int) +var ( + bigZero = new(big.Int) + errRevert = errors.New("standard throw") +) func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { x, y := stack.pop(), stack.pop() @@ -669,6 +673,20 @@ func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S return ret, nil } +func opRevert(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { + // if not metropolis return an error. REVERT is not supported + // during pre-metropolis. + if !evm.ChainConfig().IsMetropolis(evm.BlockNumber) { + return nil, fmt.Errorf("invalid opcode %x", REVERT) + } + + offset, size := stack.pop(), stack.pop() + ret := memory.GetPtr(offset.Int64(), size.Int64()) + + evm.interpreter.intPool.put(offset, size) + return ret, errRevert +} + func opStop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { return nil, nil } diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index ed30100ac1..ccb38e8dae 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -390,6 +390,14 @@ func NewJumpTable() [256]operation { validateStack: makeStackFunc(0, 1), valid: true, }, + // TODO: + // * Determine cost + REVERT: { + execute: opRevert, + gasCost: constGasFunc(GasFastestStep), + validateStack: makeStackFunc(2, 0), + valid: true, + }, JUMPDEST: { execute: opJumpdest, gasCost: constGasFunc(params.JumpdestGas), diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index d4ba7f1563..3c3796d2d2 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -202,6 +202,7 @@ const ( RETURN DELEGATECALL + REVERT = 0xfd SELFDESTRUCT = 0xff ) @@ -353,6 +354,7 @@ var opCodeToString = map[OpCode]string{ CREATE: "CREATE", CALL: "CALL", RETURN: "RETURN", + REVERT: "REVERT", CALLCODE: "CALLCODE", DELEGATECALL: "DELEGATECALL", SELFDESTRUCT: "SELFDESTRUCT", @@ -500,6 +502,7 @@ var stringToOp = map[string]OpCode{ "CREATE": CREATE, "CALL": CALL, "RETURN": RETURN, + "REVERT": REVERT, "CALLCODE": CALLCODE, "SELFDESTRUCT": SELFDESTRUCT, } diff --git a/params/config.go b/params/config.go index 0556142237..b29371960d 100644 --- a/params/config.go +++ b/params/config.go @@ -26,28 +26,28 @@ import ( var ( // MainnetChainConfig is the chain parameters to run a node on the main network. MainnetChainConfig = &ChainConfig{ - ChainId: MainNetChainID, - HomesteadBlock: MainNetHomesteadBlock, - DAOForkBlock: MainNetDAOForkBlock, - DAOForkSupport: true, - EIP150Block: MainNetHomesteadGasRepriceBlock, - EIP150Hash: MainNetHomesteadGasRepriceHash, - EIP155Block: MainNetSpuriousDragon, - EIP158Block: MainNetSpuriousDragon, - EIP198Block: MainNetMetropolis, + ChainId: MainNetChainID, + HomesteadBlock: MainNetHomesteadBlock, + DAOForkBlock: MainNetDAOForkBlock, + DAOForkSupport: true, + EIP150Block: MainNetHomesteadGasRepriceBlock, + EIP150Hash: MainNetHomesteadGasRepriceHash, + EIP155Block: MainNetSpuriousDragon, + EIP158Block: MainNetSpuriousDragon, + MetropolisBlock: MainNetMetropolis, } // TestnetChainConfig is the chain parameters to run a node on the test network. TestnetChainConfig = &ChainConfig{ - ChainId: big.NewInt(3), - HomesteadBlock: big.NewInt(0), - DAOForkBlock: nil, - DAOForkSupport: true, - EIP150Block: big.NewInt(0), - EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), - EIP155Block: big.NewInt(10), - EIP158Block: big.NewInt(10), - EIP198Block: TestNetMetropolis, + ChainId: big.NewInt(3), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: nil, + DAOForkSupport: true, + EIP150Block: big.NewInt(0), + EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"), + EIP155Block: big.NewInt(10), + EIP158Block: big.NewInt(10), + MetropolisBlock: TestNetMetropolis, } // AllProtocolChanges contains every protocol change (EIPs) @@ -82,12 +82,12 @@ type ChainConfig struct { EIP155Block *big.Int `json:"eip155Block"` // EIP155 HF block EIP158Block *big.Int `json:"eip158Block"` // EIP158 HF block - EIP198Block *big.Int `json:"eip198Block"` // EIP198 HF block + MetropolisBlock *big.Int `json:"metropolisBlock"` // Metropolis switch block (nil = no fork, 0 = alraedy on homestead) } // String implements the fmt.Stringer interface. func (c *ChainConfig) String() string { - return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v EIP198: %v}", + return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Metropolis: %v}", c.ChainId, c.HomesteadBlock, c.DAOForkBlock, @@ -95,7 +95,7 @@ func (c *ChainConfig) String() string { c.EIP150Block, c.EIP155Block, c.EIP158Block, - c.EIP198Block, + c.MetropolisBlock, ) } @@ -205,12 +205,11 @@ func configNumEqual(x, y *big.Int) bool { return x.Cmp(y) == 0 } -func (c *ChainConfig) IsEIP198(num *big.Int) bool { - if c.EIP198Block == nil || num == nil { +func (c *ChainConfig) IsMetropolis(num *big.Int) bool { + if c.MetropolisBlock == nil || num == nil { return false } - return num.Cmp(c.EIP198Block) >= 0 - + return num.Cmp(c.MetropolisBlock) >= 0 } // ConfigCompatError is raised if the locally-stored blockchain is initialised with a @@ -250,10 +249,11 @@ func (err *ConfigCompatError) Error() string { // Rules is a one time interface meaning that it shouldn't be used in between transition // phases. type Rules struct { - ChainId *big.Int - IsHomestead, IsEIP150, IsEIP155, IsEIP158, IsEIP198 bool + ChainId *big.Int + IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool + IsMetropolis bool } 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), IsEIP198: c.IsEIP198(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)} } diff --git a/params/util.go b/params/util.go index 149dab4292..e2ee921475 100644 --- a/params/util.go +++ b/params/util.go @@ -38,8 +38,8 @@ var ( TestNetSpuriousDragon = big.NewInt(10) MainNetSpuriousDragon = big.NewInt(2675000) - TestNetMetropolis = big.NewInt(11) - MainNetMetropolis = big.NewInt(10000000) + TestNetMetropolisBlock = big.NewInt(11) + MainNetMetropolisBlock = big.NewInt(5000000) TestNetChainID = big.NewInt(3) // Test net default chain ID MainNetChainID = big.NewInt(1) // main net default chain ID