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 <martin@swende.se>
This commit is contained in:
Sina Mahmoodi 2023-11-14 15:14:38 +03:00 committed by GitHub
parent fa8d39807d
commit e803ef09ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 48 deletions

View File

@ -22,53 +22,37 @@ import (
"encoding/hex" "encoding/hex"
) )
// Tests disassembling the instructions for valid evm code // Tests disassembling instructions
func TestInstructionIteratorValid(t *testing.T) { func TestInstructionIterator(t *testing.T) {
cnt := 0 for i, tc := range []struct {
script, _ := hex.DecodeString("61000000") want int
code string
wantErr string
}{
{2, "61000000", ""}, // valid code
{0, "6100", "incomplete push instruction at 0"}, // invalid code
{2, "5900", ""}, // push0
{0, "", ""}, // empty
it := NewInstructionIterator(script) } {
for it.Next() { var (
cnt++ have int
} code, _ = hex.DecodeString(tc.code)
it = NewInstructionIterator(code)
if err := it.Error(); err != nil { )
t.Errorf("Expected 2, but encountered error %v instead.", err) for it.Next() {
} have++
if cnt != 2 { }
t.Errorf("Expected 2, but got %v instead.", cnt) var haveErr = ""
} if it.Error() != nil {
} haveErr = it.Error().Error()
}
// Tests disassembling the instructions for invalid evm code if haveErr != tc.wantErr {
func TestInstructionIteratorInvalid(t *testing.T) { t.Errorf("test %d: encountered error: %q want %q", i, haveErr, tc.wantErr)
cnt := 0 continue
script, _ := hex.DecodeString("6100") }
if have != tc.want {
it := NewInstructionIterator(script) t.Errorf("wrong instruction count, have %d want %d", have, tc.want)
for it.Next() { }
cnt++
}
if it.Error() == nil {
t.Errorf("Expected an error, but got %v instead.", cnt)
}
}
// Tests disassembling the instructions for empty evm code
func TestInstructionIteratorEmpty(t *testing.T) {
cnt := 0
script, _ := hex.DecodeString("")
it := NewInstructionIterator(script)
for it.Next() {
cnt++
}
if err := it.Error(); err != nil {
t.Errorf("Expected 0, but encountered error %v instead.", err)
}
if cnt != 0 {
t.Errorf("Expected 0, but got %v instead.", cnt)
} }
} }

View File

@ -25,7 +25,7 @@ type OpCode byte
// IsPush specifies if an opcode is a PUSH opcode. // IsPush specifies if an opcode is a PUSH opcode.
func (op OpCode) IsPush() bool { func (op OpCode) IsPush() bool {
return PUSH1 <= op && op <= PUSH32 return PUSH0 <= op && op <= PUSH32
} }
// 0x0 range - arithmetic ops. // 0x0 range - arithmetic ops.