Skip to content

Commit 44d40ff

Browse files
holimankaralabe
authored andcommitted
core, vm, common: define constantinople fork + shift (#16045)
* core, vm, common: define constantinople fork, start implementation of shift instructions * vm: more testcases * vm: add tests for intpool erroneous intpool handling * core, vm, common: fix constantinople review concerns * vm: add string<->op definitions for new opcodes
1 parent ae9f972 commit 44d40ff

8 files changed

Lines changed: 302 additions & 41 deletions

File tree

‎cmd/puppeth/module_dashboard.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ func deployDashboard(client *sshClient, network string, conf *config, config *da
631631
"Tangerine": conf.Genesis.Config.EIP150Block,
632632
"Spurious": conf.Genesis.Config.EIP155Block,
633633
"Byzantium": conf.Genesis.Config.ByzantiumBlock,
634+
"Constantinople": conf.Genesis.Config.ConstantinopleBlock,
634635
})
635636
files[filepath.Join(workdir, "index.html")] = indexfile.Bytes()
636637

‎common/big.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ var (
2525
Big3 = big.NewInt(3)
2626
Big0 = big.NewInt(0)
2727
Big32 = big.NewInt(32)
28-
Big256 = big.NewInt(0xff)
28+
Big256 = big.NewInt(256)
2929
Big257 = big.NewInt(257)
3030
)

‎core/vm/instructions.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,66 @@ func opMulmod(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S
302302
return nil, nil
303303
}
304304

305+
// opSHL implements Shift Left
306+
// The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
307+
// and pushes on the stack arg2 shifted to the left by arg1 number of bits.
308+
func opSHL(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
309+
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
310+
shift, value := math.U256(stack.pop()), math.U256(stack.peek())
311+
defer evm.interpreter.intPool.put(shift) // First operand back into the pool
312+
313+
if shift.Cmp(common.Big256) >= 0 {
314+
value.SetUint64(0)
315+
return nil, nil
316+
}
317+
n := uint(shift.Uint64())
318+
math.U256(value.Lsh(value, n))
319+
320+
return nil, nil
321+
}
322+
323+
// opSHR implements Logical Shift Right
324+
// The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
325+
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
326+
func opSHR(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
327+
// Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
328+
shift, value := math.U256(stack.pop()), math.U256(stack.peek())
329+
defer evm.interpreter.intPool.put(shift) // First operand back into the pool
330+
331+
if shift.Cmp(common.Big256) >= 0 {
332+
value.SetUint64(0)
333+
return nil, nil
334+
}
335+
n := uint(shift.Uint64())
336+
math.U256(value.Rsh(value, n))
337+
338+
return nil, nil
339+
}
340+
341+
// opSAR implements Arithmetic Shift Right
342+
// The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
343+
// and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
344+
func opSAR(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
345+
// Note, S256 returns (potentially) a new bigint, so we're popping, not peeking this one
346+
shift, value := math.U256(stack.pop()), math.S256(stack.pop())
347+
defer evm.interpreter.intPool.put(shift) // First operand back into the pool
348+
349+
if shift.Cmp(common.Big256) >= 0 {
350+
if value.Sign() > 0 {
351+
value.SetUint64(0)
352+
} else {
353+
value.SetInt64(-1)
354+
}
355+
stack.push(math.U256(value))
356+
return nil, nil
357+
}
358+
n := uint(shift.Uint64())
359+
value.Rsh(value, n)
360+
stack.push(math.U256(value))
361+
362+
return nil, nil
363+
}
364+
305365
func opSha3(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
306366
offset, size := stack.pop(), stack.pop()
307367
data := memory.Get(offset.Int64(), size.Int64())

‎core/vm/instructions_test.go‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,48 @@ import (
2424
"github.com/ethereum/go-ethereum/params"
2525
)
2626

27+
type twoOperandTest struct {
28+
x string
29+
y string
30+
expected string
31+
}
32+
33+
func testTwoOperandOp(t *testing.T, tests []twoOperandTest, opFn func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error)) {
34+
var (
35+
env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
36+
stack = newstack()
37+
pc = uint64(0)
38+
)
39+
for i, test := range tests {
40+
x := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
41+
shift := new(big.Int).SetBytes(common.Hex2Bytes(test.y))
42+
expected := new(big.Int).SetBytes(common.Hex2Bytes(test.expected))
43+
stack.push(x)
44+
stack.push(shift)
45+
opFn(&pc, env, nil, nil, stack)
46+
actual := stack.pop()
47+
if actual.Cmp(expected) != 0 {
48+
t.Errorf("Testcase %d, expected %v, got %v", i, expected, actual)
49+
}
50+
// Check pool usage
51+
// 1.pool is not allowed to contain anything on the stack
52+
// 2.pool is not allowed to contain the same pointers twice
53+
if env.interpreter.intPool.pool.len() > 0 {
54+
55+
poolvals := make(map[*big.Int]struct{})
56+
poolvals[actual] = struct{}{}
57+
58+
for env.interpreter.intPool.pool.len() > 0 {
59+
key := env.interpreter.intPool.get()
60+
if _, exist := poolvals[key]; exist {
61+
t.Errorf("Testcase %d, pool contains double-entry", i)
62+
}
63+
poolvals[key] = struct{}{}
64+
}
65+
}
66+
}
67+
}
68+
2769
func TestByteOp(t *testing.T) {
2870
var (
2971
env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
@@ -57,6 +99,98 @@ func TestByteOp(t *testing.T) {
5799
}
58100
}
59101

102+
func TestSHL(t *testing.T) {
103+
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
104+
tests := []twoOperandTest{
105+
{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
106+
{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"},
107+
{"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
108+
{"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
109+
{"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
110+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
111+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
112+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"},
113+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
114+
{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
115+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"},
116+
}
117+
testTwoOperandOp(t, tests, opSHL)
118+
}
119+
120+
func TestSHR(t *testing.T) {
121+
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right
122+
tests := []twoOperandTest{
123+
{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
124+
{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
125+
{"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"},
126+
{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
127+
{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
128+
{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"},
129+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
130+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
131+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"},
132+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
133+
{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
134+
}
135+
testTwoOperandOp(t, tests, opSHR)
136+
}
137+
138+
func TestSAR(t *testing.T) {
139+
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right
140+
tests := []twoOperandTest{
141+
{"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"},
142+
{"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
143+
{"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"},
144+
{"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
145+
{"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
146+
{"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
147+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
148+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
149+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
150+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
151+
{"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"},
152+
{"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
153+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"},
154+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"},
155+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"},
156+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"},
157+
}
158+
159+
testTwoOperandOp(t, tests, opSAR)
160+
}
161+
162+
func TestSGT(t *testing.T) {
163+
tests := []twoOperandTest{
164+
{"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
165+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
166+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
167+
{"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"},
168+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
169+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"},
170+
{"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
171+
{"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
172+
{"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"},
173+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
174+
}
175+
testTwoOperandOp(t, tests, opSgt)
176+
}
177+
178+
func TestSLT(t *testing.T) {
179+
tests := []twoOperandTest{
180+
{"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
181+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
182+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
183+
{"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
184+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"},
185+
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
186+
{"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"},
187+
{"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"},
188+
{"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"},
189+
{"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"},
190+
}
191+
testTwoOperandOp(t, tests, opSlt)
192+
}
193+
60194
func opBenchmark(bench *testing.B, op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) {
61195
var (
62196
env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
@@ -259,3 +393,22 @@ func BenchmarkOpMulmod(b *testing.B) {
259393

260394
opBenchmark(b, opMulmod, x, y, z)
261395
}
396+
397+
func BenchmarkOpSHL(b *testing.B) {
398+
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
399+
y := "ff"
400+
401+
opBenchmark(b, opSHL, x, y)
402+
}
403+
func BenchmarkOpSHR(b *testing.B) {
404+
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
405+
y := "ff"
406+
407+
opBenchmark(b, opSHR, x, y)
408+
}
409+
func BenchmarkOpSAR(b *testing.B) {
410+
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
411+
y := "ff"
412+
413+
opBenchmark(b, opSAR, x, y)
414+
}

‎core/vm/interpreter.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func NewInterpreter(evm *EVM, cfg Config) *Interpreter {
6868
// we'll set the default jump table.
6969
if !cfg.JumpTable[STOP].valid {
7070
switch {
71+
case evm.ChainConfig().IsConstantinople(evm.BlockNumber):
72+
cfg.JumpTable = constantinopleInstructionSet
7173
case evm.ChainConfig().IsByzantium(evm.BlockNumber):
7274
cfg.JumpTable = byzantiumInstructionSet
7375
case evm.ChainConfig().IsHomestead(evm.BlockNumber):

‎core/vm/jump_table.go‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,38 @@ type operation struct {
5151
}
5252

5353
var (
54-
frontierInstructionSet = NewFrontierInstructionSet()
55-
homesteadInstructionSet = NewHomesteadInstructionSet()
56-
byzantiumInstructionSet = NewByzantiumInstructionSet()
54+
frontierInstructionSet = NewFrontierInstructionSet()
55+
homesteadInstructionSet = NewHomesteadInstructionSet()
56+
byzantiumInstructionSet = NewByzantiumInstructionSet()
57+
constantinopleInstructionSet = NewConstantinopleInstructionSet()
5758
)
5859

60+
// NewConstantinopleInstructionSet returns the frontier, homestead
61+
// byzantium and contantinople instructions.
62+
func NewConstantinopleInstructionSet() [256]operation {
63+
// instructions that can be executed during the byzantium phase.
64+
instructionSet := NewByzantiumInstructionSet()
65+
instructionSet[SHL] = operation{
66+
execute: opSHL,
67+
gasCost: constGasFunc(GasFastestStep),
68+
validateStack: makeStackFunc(2, 1),
69+
valid: true,
70+
}
71+
instructionSet[SHR] = operation{
72+
execute: opSHR,
73+
gasCost: constGasFunc(GasFastestStep),
74+
validateStack: makeStackFunc(2, 1),
75+
valid: true,
76+
}
77+
instructionSet[SAR] = operation{
78+
execute: opSAR,
79+
gasCost: constGasFunc(GasFastestStep),
80+
validateStack: makeStackFunc(2, 1),
81+
valid: true,
82+
}
83+
return instructionSet
84+
}
85+
5986
// NewByzantiumInstructionSet returns the frontier, homestead and
6087
// byzantium instructions.
6188
func NewByzantiumInstructionSet() [256]operation {

‎core/vm/opcodes.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const (
6363
XOR
6464
NOT
6565
BYTE
66+
SHL
67+
SHR
68+
SAR
6669

6770
SHA3 = 0x20
6871
)
@@ -234,6 +237,9 @@ var opCodeToString = map[OpCode]string{
234237
OR: "OR",
235238
XOR: "XOR",
236239
BYTE: "BYTE",
240+
SHL: "SHL",
241+
SHR: "SHR",
242+
SAR: "SAR",
237243
ADDMOD: "ADDMOD",
238244
MULMOD: "MULMOD",
239245

@@ -400,6 +406,9 @@ var stringToOp = map[string]OpCode{
400406
"OR": OR,
401407
"XOR": XOR,
402408
"BYTE": BYTE,
409+
"SHL": SHL,
410+
"SHR": SHR,
411+
"SAR": SAR,
403412
"ADDMOD": ADDMOD,
404413
"MULMOD": MULMOD,
405414
"SHA3": SHA3,

0 commit comments

Comments
 (0)