Skip to content

Commit 3e993ff

Browse files
holimankaralabe
andcommitted
Eip 1884 v3 (#19743)
* core/vm, tests: implement EIP 1884, add support for feature-tests * core/vm: 1884-changes to extcodehash, move selfbalance opcode * tests: fix statetests * core/vm: move constants, address review concerns * core/vm: word formatting Co-Authored-By: Péter Szilágyi <[email protected]>
1 parent f3478f2 commit 3e993ff

File tree

6 files changed

+141
-32
lines changed

6 files changed

+141
-32
lines changed

core/vm/eips.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package vm
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/ethereum/go-ethereum/params"
23+
)
24+
25+
// EnableEIP enables the given EIP on the config.
26+
// This operation writes in-place, and callers need to ensure that the globally
27+
// defined jump tables are not polluted.
28+
func EnableEIP(eipNum int, jt *JumpTable) error {
29+
switch eipNum {
30+
case 1884:
31+
enable1884(jt)
32+
default:
33+
return fmt.Errorf("undefined eip %d", eipNum)
34+
}
35+
return nil
36+
}
37+
38+
// enable1884 applies EIP-1884 to the given jump table:
39+
// - Increase cost of BALANCE to 700
40+
// - Increase cost of EXTCODEHASH to 700
41+
// - Increase cost of SLOAD to 800
42+
// - Define SELFBALANCE, with cost GasFastStep (5)
43+
func enable1884(jt *JumpTable) {
44+
// Gas cost changes
45+
jt[BALANCE].constantGas = params.BalanceGasEIP1884
46+
jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
47+
jt[SLOAD].constantGas = params.SloadGasEIP1884
48+
49+
// New opcode
50+
jt[SELFBALANCE] = operation{
51+
execute: opSelfBalance,
52+
constantGas: GasFastStep,
53+
minStack: minStack(0, 1),
54+
maxStack: maxStack(0, 1),
55+
valid: true,
56+
}
57+
}
58+
59+
func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
60+
balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(contract.Address()))
61+
stack.push(balance)
62+
return nil, nil
63+
}

core/vm/interpreter.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/common/math"
26+
"github.com/ethereum/go-ethereum/log"
2627
)
2728

2829
// Config are the configuration options for the Interpreter
@@ -36,6 +37,8 @@ type Config struct {
3637

3738
EWASMInterpreter string // External EWASM interpreter options
3839
EVMInterpreter string // External EVM interpreter options
40+
41+
ExtraEips []int // Additional EIPS that are to be enabled
3942
}
4043

4144
// Interpreter is used to run Ethereum based contracts and will utilise the
@@ -88,20 +91,29 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {
8891
// the jump table was initialised. If it was not
8992
// we'll set the default jump table.
9093
if !cfg.JumpTable[STOP].valid {
94+
var jt JumpTable
9195
switch {
9296
case evm.chainRules.IsConstantinople:
93-
cfg.JumpTable = constantinopleInstructionSet
97+
jt = constantinopleInstructionSet
9498
case evm.chainRules.IsByzantium:
95-
cfg.JumpTable = byzantiumInstructionSet
99+
jt = byzantiumInstructionSet
96100
case evm.chainRules.IsEIP158:
97-
cfg.JumpTable = spuriousDragonInstructionSet
101+
jt = spuriousDragonInstructionSet
98102
case evm.chainRules.IsEIP150:
99-
cfg.JumpTable = tangerineWhistleInstructionSet
103+
jt = tangerineWhistleInstructionSet
100104
case evm.chainRules.IsHomestead:
101-
cfg.JumpTable = homesteadInstructionSet
105+
jt = homesteadInstructionSet
102106
default:
103-
cfg.JumpTable = frontierInstructionSet
107+
jt = frontierInstructionSet
108+
}
109+
for i, eip := range cfg.ExtraEips {
110+
if err := EnableEIP(eip, &jt); err != nil {
111+
// Disable it, so caller can check if it's activated or not
112+
cfg.ExtraEips = append(cfg.ExtraEips[:i], cfg.ExtraEips[i+1:]...)
113+
log.Error("EIP activation failed", "eip", eip, "error", err)
114+
}
104115
}
116+
cfg.JumpTable = jt
105117
}
106118

107119
return &EVMInterpreter{

core/vm/jump_table.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ var (
6262
constantinopleInstructionSet = newConstantinopleInstructionSet()
6363
)
6464

65+
// JumpTable contains the EVM opcodes supported at a given fork.
66+
type JumpTable [256]operation
67+
6568
// NewConstantinopleInstructionSet returns the frontier, homestead
6669
// byzantium and contantinople instructions.
67-
func newConstantinopleInstructionSet() [256]operation {
70+
func newConstantinopleInstructionSet() JumpTable {
6871
// instructions that can be executed during the byzantium phase.
6972
instructionSet := newByzantiumInstructionSet()
7073
instructionSet[SHL] = operation{
@@ -90,7 +93,7 @@ func newConstantinopleInstructionSet() [256]operation {
9093
}
9194
instructionSet[EXTCODEHASH] = operation{
9295
execute: opExtCodeHash,
93-
constantGas: params.ExtcodeHashGas,
96+
constantGas: params.ExtcodeHashGasConstantinople,
9497
minStack: minStack(1, 1),
9598
maxStack: maxStack(1, 1),
9699
valid: true,
@@ -111,7 +114,7 @@ func newConstantinopleInstructionSet() [256]operation {
111114

112115
// NewByzantiumInstructionSet returns the frontier, homestead and
113116
// byzantium instructions.
114-
func newByzantiumInstructionSet() [256]operation {
117+
func newByzantiumInstructionSet() JumpTable {
115118
// instructions that can be executed during the homestead phase.
116119
instructionSet := newSpuriousDragonInstructionSet()
117120
instructionSet[STATICCALL] = operation{
@@ -154,15 +157,15 @@ func newByzantiumInstructionSet() [256]operation {
154157
}
155158

156159
// EIP 158 a.k.a Spurious Dragon
157-
func newSpuriousDragonInstructionSet() [256]operation {
160+
func newSpuriousDragonInstructionSet() JumpTable {
158161
instructionSet := newTangerineWhistleInstructionSet()
159162
instructionSet[EXP].dynamicGas = gasExpEIP158
160163
return instructionSet
161164

162165
}
163166

164167
// EIP 150 a.k.a Tangerine Whistle
165-
func newTangerineWhistleInstructionSet() [256]operation {
168+
func newTangerineWhistleInstructionSet() JumpTable {
166169
instructionSet := newHomesteadInstructionSet()
167170
instructionSet[BALANCE].constantGas = params.BalanceGasEIP150
168171
instructionSet[EXTCODESIZE].constantGas = params.ExtcodeSizeGasEIP150
@@ -176,7 +179,7 @@ func newTangerineWhistleInstructionSet() [256]operation {
176179

177180
// NewHomesteadInstructionSet returns the frontier and homestead
178181
// instructions that can be executed during the homestead phase.
179-
func newHomesteadInstructionSet() [256]operation {
182+
func newHomesteadInstructionSet() JumpTable {
180183
instructionSet := newFrontierInstructionSet()
181184
instructionSet[DELEGATECALL] = operation{
182185
execute: opDelegateCall,
@@ -193,8 +196,8 @@ func newHomesteadInstructionSet() [256]operation {
193196

194197
// NewFrontierInstructionSet returns the frontier instructions
195198
// that can be executed during the frontier phase.
196-
func newFrontierInstructionSet() [256]operation {
197-
return [256]operation{
199+
func newFrontierInstructionSet() JumpTable {
200+
return JumpTable{
198201
STOP: {
199202
execute: opStop,
200203
constantGas: 0,

core/vm/opcodes.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const (
101101
NUMBER
102102
DIFFICULTY
103103
GASLIMIT
104+
SELFBALANCE = 0x47
104105
)
105106

106107
// 0x50 range - 'storage' and execution.
@@ -271,12 +272,13 @@ var opCodeToString = map[OpCode]string{
271272
EXTCODEHASH: "EXTCODEHASH",
272273

273274
// 0x40 range - block operations.
274-
BLOCKHASH: "BLOCKHASH",
275-
COINBASE: "COINBASE",
276-
TIMESTAMP: "TIMESTAMP",
277-
NUMBER: "NUMBER",
278-
DIFFICULTY: "DIFFICULTY",
279-
GASLIMIT: "GASLIMIT",
275+
BLOCKHASH: "BLOCKHASH",
276+
COINBASE: "COINBASE",
277+
TIMESTAMP: "TIMESTAMP",
278+
NUMBER: "NUMBER",
279+
DIFFICULTY: "DIFFICULTY",
280+
GASLIMIT: "GASLIMIT",
281+
SELFBALANCE: "SELFBALANCE",
280282

281283
// 0x50 range - 'storage' and execution.
282284
POP: "POP",
@@ -444,6 +446,7 @@ var stringToOp = map[string]OpCode{
444446
"NUMBER": NUMBER,
445447
"DIFFICULTY": DIFFICULTY,
446448
"GASLIMIT": GASLIMIT,
449+
"SELFBALANCE": SELFBALANCE,
447450
"POP": POP,
448451
"MLOAD": MLOAD,
449452
"MSTORE": MSTORE,

params/protocol_params.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,19 @@ const (
7070
TxDataNonZeroGas uint64 = 68 // Per byte of data attached to a transaction that is not equal to zero. NOTE: Not payable on data of calls between transactions.
7171

7272
// These have been changed during the course of the chain
73-
CallGasFrontier uint64 = 40 // Once per CALL operation & message call transaction.
74-
CallGasEIP150 uint64 = 700 // Static portion of gas for CALL-derivates after EIP 150 (Tangerine)
75-
BalanceGasFrontier uint64 = 20 // The cost of a BALANCE operation
76-
BalanceGasEIP150 uint64 = 400 // The cost of a BALANCE operation after Tangerine
77-
ExtcodeSizeGasFrontier uint64 = 20 // Cost of EXTCODESIZE before EIP 150 (Tangerine)
78-
ExtcodeSizeGasEIP150 uint64 = 700 // Cost of EXTCODESIZE after EIP 150 (Tangerine)
79-
SloadGasFrontier uint64 = 50
80-
SloadGasEIP150 uint64 = 200
81-
ExtcodeHashGas uint64 = 400 // Cost of EXTCODEHASH (introduced in Constantinople)
82-
SelfdestructGasEIP150 uint64 = 5000 // Cost of SELFDESTRUCT post EIP 150 (Tangerine)
73+
CallGasFrontier uint64 = 40 // Once per CALL operation & message call transaction.
74+
CallGasEIP150 uint64 = 700 // Static portion of gas for CALL-derivates after EIP 150 (Tangerine)
75+
BalanceGasFrontier uint64 = 20 // The cost of a BALANCE operation
76+
BalanceGasEIP150 uint64 = 400 // The cost of a BALANCE operation after Tangerine
77+
BalanceGasEIP1884 uint64 = 700 // The cost of a BALANCE operation after EIP 1884 (part of Istanbul)
78+
ExtcodeSizeGasFrontier uint64 = 20 // Cost of EXTCODESIZE before EIP 150 (Tangerine)
79+
ExtcodeSizeGasEIP150 uint64 = 700 // Cost of EXTCODESIZE after EIP 150 (Tangerine)
80+
SloadGasFrontier uint64 = 50
81+
SloadGasEIP150 uint64 = 200
82+
SloadGasEIP1884 uint64 = 800 // Cost of SLOAD after EIP 1884 (part of Istanbul)
83+
ExtcodeHashGasConstantinople uint64 = 400 // Cost of EXTCODEHASH (introduced in Constantinople)
84+
ExtcodeHashGasEIP1884 uint64 = 700 // Cost of EXTCODEHASH after EIP 1884 (part in Istanbul)
85+
SelfdestructGasEIP150 uint64 = 5000 // Cost of SELFDESTRUCT post EIP 150 (Tangerine)
8386

8487
// EXP has a dynamic portion depending on the size of the exponent
8588
ExpByteFrontier uint64 = 10 // was set to 10 in Frontier

tests/state_test_util.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"math/big"
24+
"strconv"
2425
"strings"
2526

2627
"github.com/ethereum/go-ethereum/common"
@@ -109,6 +110,29 @@ type stTransactionMarshaling struct {
109110
PrivateKey hexutil.Bytes
110111
}
111112

113+
// getVMConfig takes a fork definition and returns a chain config.
114+
// The fork definition can be
115+
// - a plain forkname, e.g. `Byzantium`,
116+
// - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`.
117+
func getVMConfig(forkString string) (baseConfig *params.ChainConfig, eips []int, err error) {
118+
var (
119+
splitForks = strings.Split(forkString, "+")
120+
ok bool
121+
baseName, eipsStrings = splitForks[0], splitForks[1:]
122+
)
123+
if baseConfig, ok = Forks[baseName]; !ok {
124+
return nil, nil, UnsupportedForkError{baseName}
125+
}
126+
for _, eip := range eipsStrings {
127+
if eipNum, err := strconv.Atoi(eip); err != nil {
128+
return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum)
129+
} else {
130+
eips = append(eips, eipNum)
131+
}
132+
}
133+
return baseConfig, eips, nil
134+
}
135+
112136
// Subtests returns all valid subtests of the test.
113137
func (t *StateTest) Subtests() []StateSubtest {
114138
var sub []StateSubtest
@@ -122,10 +146,11 @@ func (t *StateTest) Subtests() []StateSubtest {
122146

123147
// Run executes a specific subtest.
124148
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) (*state.StateDB, error) {
125-
config, ok := Forks[subtest.Fork]
126-
if !ok {
149+
config, eips, err := getVMConfig(subtest.Fork)
150+
if err != nil {
127151
return nil, UnsupportedForkError{subtest.Fork}
128152
}
153+
vmconfig.ExtraEips = eips
129154
block := t.genesis(config).ToBlock(nil)
130155
statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre)
131156

0 commit comments

Comments
 (0)