core/vm: implement opcode REVERT - eip#140

This commit is contained in:
Jeffrey Wilcke 2017-02-01 15:01:34 +01:00
parent 725c2646a0
commit 12be105f2d
No known key found for this signature in database
GPG key ID: 63FF149CD6945F9E
6 changed files with 79 additions and 28 deletions

View file

@ -139,8 +139,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 // 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 err != nil { if err != nil {
contract.UseGas(contract.Gas) if err != errRevert {
contract.UseGas(contract.Gas)
}
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
} }
return ret, contract.Gas, err return ret, contract.Gas, err
@ -177,7 +178,9 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
ret, err = evm.interpreter.Run(contract, input) ret, err = evm.interpreter.Run(contract, input)
if err != nil { if err != nil {
contract.UseGas(contract.Gas) if err != errRevert {
contract.UseGas(contract.Gas)
}
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
} }
@ -212,7 +215,9 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
ret, err = evm.interpreter.Run(contract, input) ret, err = evm.interpreter.Run(contract, input)
if err != nil { if err != nil {
contract.UseGas(contract.Gas) if err != errRevert {
contract.UseGas(contract.Gas)
}
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
} }
@ -275,10 +280,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. // when we're in homestead this also counts for code storage gas errors.
if maxCodeSizeExceeded || if maxCodeSizeExceeded ||
(err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) { (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. evm.StateDB.RevertToSnapshot(snapshot)
return nil, contractAddr, 0, err
} }
// If the vm returned with an error the return value should be set to nil. // 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 // This isn't consensus critical but merely to for behaviour reasons such as

View file

@ -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)
errRevert = errors.New("standard throw")
)
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()
@ -669,6 +673,20 @@ func opReturn(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
return ret, nil 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) { func opStop(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
return nil, nil return nil, nil
} }

View file

@ -390,6 +390,14 @@ func NewJumpTable() [256]operation {
validateStack: makeStackFunc(0, 1), validateStack: makeStackFunc(0, 1),
valid: true, valid: true,
}, },
// TODO:
// * Determine cost
REVERT: {
execute: opRevert,
gasCost: constGasFunc(GasFastestStep),
validateStack: makeStackFunc(2, 0),
valid: true,
},
JUMPDEST: { JUMPDEST: {
execute: opJumpdest, execute: opJumpdest,
gasCost: constGasFunc(params.JumpdestGas), gasCost: constGasFunc(params.JumpdestGas),

View file

@ -202,6 +202,7 @@ const (
RETURN RETURN
DELEGATECALL DELEGATECALL
REVERT = 0xfd
SELFDESTRUCT = 0xff SELFDESTRUCT = 0xff
) )
@ -353,6 +354,7 @@ var opCodeToString = map[OpCode]string{
CREATE: "CREATE", CREATE: "CREATE",
CALL: "CALL", CALL: "CALL",
RETURN: "RETURN", RETURN: "RETURN",
REVERT: "REVERT",
CALLCODE: "CALLCODE", CALLCODE: "CALLCODE",
DELEGATECALL: "DELEGATECALL", DELEGATECALL: "DELEGATECALL",
SELFDESTRUCT: "SELFDESTRUCT", SELFDESTRUCT: "SELFDESTRUCT",
@ -500,6 +502,7 @@ var stringToOp = map[string]OpCode{
"CREATE": CREATE, "CREATE": CREATE,
"CALL": CALL, "CALL": CALL,
"RETURN": RETURN, "RETURN": RETURN,
"REVERT": REVERT,
"CALLCODE": CALLCODE, "CALLCODE": CALLCODE,
"SELFDESTRUCT": SELFDESTRUCT, "SELFDESTRUCT": SELFDESTRUCT,
} }

View file

@ -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)}
} }

View file

@ -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
) )