core/vm: implement opcode REVERT - EIP#140

This commit is contained in:
Jeffrey Wilcke 2017-02-01 15:01:34 +01:00
parent 0906d61746
commit 591ac8133d
6 changed files with 73 additions and 38 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -34,7 +34,7 @@ var (
EIP150Hash: MainNetHomesteadGasRepriceHash,
EIP155Block: MainNetSpuriousDragon,
EIP158Block: MainNetSpuriousDragon,
EIP198Block: MainNetMetropolis,
MetropolisBlock: MainNetMetropolis,
}
// TestnetChainConfig is the chain parameters to run a node on the test network.
@ -47,7 +47,7 @@ var (
EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
EIP155Block: big.NewInt(10),
EIP158Block: big.NewInt(10),
EIP198Block: TestNetMetropolis,
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
@ -251,9 +250,10 @@ func (err *ConfigCompatError) Error() string {
// phases.
type Rules struct {
ChainId *big.Int
IsHomestead, IsEIP150, IsEIP155, IsEIP158, IsEIP198 bool
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)}
}

View file

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