Skip to content

Commit e803ef0

Browse files
s1naholiman
andauthored
eth/tracers/js: fix isPush for push0 (#28520)
Fixes so that `push0` opcode is correctly reported as `true` by the `IsPush` function --------- Co-authored-by: Martin Holst Swende <[email protected]>
1 parent fa8d398 commit e803ef0

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

core/asm/asm_test.go

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,37 @@ import (
2222
"encoding/hex"
2323
)
2424

25-
// Tests disassembling the instructions for valid evm code
26-
func TestInstructionIteratorValid(t *testing.T) {
27-
cnt := 0
28-
script, _ := hex.DecodeString("61000000")
29-
30-
it := NewInstructionIterator(script)
31-
for it.Next() {
32-
cnt++
33-
}
34-
35-
if err := it.Error(); err != nil {
36-
t.Errorf("Expected 2, but encountered error %v instead.", err)
37-
}
38-
if cnt != 2 {
39-
t.Errorf("Expected 2, but got %v instead.", cnt)
40-
}
41-
}
42-
43-
// Tests disassembling the instructions for invalid evm code
44-
func TestInstructionIteratorInvalid(t *testing.T) {
45-
cnt := 0
46-
script, _ := hex.DecodeString("6100")
47-
48-
it := NewInstructionIterator(script)
49-
for it.Next() {
50-
cnt++
51-
}
52-
53-
if it.Error() == nil {
54-
t.Errorf("Expected an error, but got %v instead.", cnt)
55-
}
56-
}
57-
58-
// Tests disassembling the instructions for empty evm code
59-
func TestInstructionIteratorEmpty(t *testing.T) {
60-
cnt := 0
61-
script, _ := hex.DecodeString("")
62-
63-
it := NewInstructionIterator(script)
64-
for it.Next() {
65-
cnt++
66-
}
67-
68-
if err := it.Error(); err != nil {
69-
t.Errorf("Expected 0, but encountered error %v instead.", err)
70-
}
71-
if cnt != 0 {
72-
t.Errorf("Expected 0, but got %v instead.", cnt)
25+
// Tests disassembling instructions
26+
func TestInstructionIterator(t *testing.T) {
27+
for i, tc := range []struct {
28+
want int
29+
code string
30+
wantErr string
31+
}{
32+
{2, "61000000", ""}, // valid code
33+
{0, "6100", "incomplete push instruction at 0"}, // invalid code
34+
{2, "5900", ""}, // push0
35+
{0, "", ""}, // empty
36+
37+
} {
38+
var (
39+
have int
40+
code, _ = hex.DecodeString(tc.code)
41+
it = NewInstructionIterator(code)
42+
)
43+
for it.Next() {
44+
have++
45+
}
46+
var haveErr = ""
47+
if it.Error() != nil {
48+
haveErr = it.Error().Error()
49+
}
50+
if haveErr != tc.wantErr {
51+
t.Errorf("test %d: encountered error: %q want %q", i, haveErr, tc.wantErr)
52+
continue
53+
}
54+
if have != tc.want {
55+
t.Errorf("wrong instruction count, have %d want %d", have, tc.want)
56+
}
7357
}
7458
}

core/vm/opcodes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type OpCode byte
2525

2626
// IsPush specifies if an opcode is a PUSH opcode.
2727
func (op OpCode) IsPush() bool {
28-
return PUSH1 <= op && op <= PUSH32
28+
return PUSH0 <= op && op <= PUSH32
2929
}
3030

3131
// 0x0 range - arithmetic ops.

0 commit comments

Comments
 (0)