mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +00:00
core/vm: 8024 tests should enforce explicit errors (#33787)
This PR makes `TestEIP8024_Execution` verify explicit error types (e.g., `ErrStackUnderflow` vs `ErrInvalidOpCode`) rather than accepting any error. It also fails fast on unexpected opcodes in the mini-interpreter to avoid false positives from missing opcode handling.
This commit is contained in:
parent
7faa676b03
commit
bbb1ab8d16
1 changed files with 75 additions and 34 deletions
|
|
@ -19,6 +19,7 @@ package vm
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
|
|
@ -1015,7 +1016,8 @@ func TestEIP8024_Execution(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
codeHex string
|
||||
wantErr bool
|
||||
wantErr error
|
||||
wantOpcode OpCode
|
||||
wantVals []uint64
|
||||
}{
|
||||
{
|
||||
|
|
@ -1071,53 +1073,68 @@ func TestEIP8024_Execution(t *testing.T) {
|
|||
{
|
||||
name: "INVALID_SWAPN_LOW",
|
||||
codeHex: "e75b",
|
||||
wantErr: true,
|
||||
wantErr: &ErrInvalidOpCode{},
|
||||
wantOpcode: SWAPN,
|
||||
},
|
||||
{
|
||||
name: "JUMP over INVALID_DUPN",
|
||||
codeHex: "600456e65b",
|
||||
wantErr: false,
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "UNDERFLOW_DUPN_1",
|
||||
codeHex: "6000808080808080808080808080808080e600",
|
||||
wantErr: &ErrStackUnderflow{},
|
||||
wantOpcode: DUPN,
|
||||
},
|
||||
// Additional test cases
|
||||
{
|
||||
name: "INVALID_DUPN_LOW",
|
||||
codeHex: "e65b",
|
||||
wantErr: true,
|
||||
wantErr: &ErrInvalidOpCode{},
|
||||
wantOpcode: DUPN,
|
||||
},
|
||||
{
|
||||
name: "INVALID_EXCHANGE_LOW",
|
||||
codeHex: "e850",
|
||||
wantErr: true,
|
||||
wantErr: &ErrInvalidOpCode{},
|
||||
wantOpcode: EXCHANGE,
|
||||
},
|
||||
{
|
||||
name: "INVALID_DUPN_HIGH",
|
||||
codeHex: "e67f",
|
||||
wantErr: true,
|
||||
wantErr: &ErrInvalidOpCode{},
|
||||
wantOpcode: DUPN,
|
||||
},
|
||||
{
|
||||
name: "INVALID_SWAPN_HIGH",
|
||||
codeHex: "e77f",
|
||||
wantErr: true,
|
||||
wantErr: &ErrInvalidOpCode{},
|
||||
wantOpcode: SWAPN,
|
||||
},
|
||||
{
|
||||
name: "INVALID_EXCHANGE_HIGH",
|
||||
codeHex: "e87f",
|
||||
wantErr: true,
|
||||
wantErr: &ErrInvalidOpCode{},
|
||||
wantOpcode: EXCHANGE,
|
||||
},
|
||||
{
|
||||
name: "UNDERFLOW_DUPN",
|
||||
name: "UNDERFLOW_DUPN_2",
|
||||
codeHex: "5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5fe600", // (n=17, need 17 items, have 16)
|
||||
wantErr: true,
|
||||
wantErr: &ErrStackUnderflow{},
|
||||
wantOpcode: DUPN,
|
||||
},
|
||||
{
|
||||
name: "UNDERFLOW_SWAPN",
|
||||
codeHex: "5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5fe700", // (n=17, need 18 items, have 17)
|
||||
wantErr: true,
|
||||
wantErr: &ErrStackUnderflow{},
|
||||
wantOpcode: SWAPN,
|
||||
},
|
||||
{
|
||||
name: "UNDERFLOW_EXCHANGE",
|
||||
codeHex: "60016002e801", // (n,m)=(1,2), need 3 items, have 2
|
||||
wantErr: true,
|
||||
wantErr: &ErrStackUnderflow{},
|
||||
wantOpcode: EXCHANGE,
|
||||
},
|
||||
{
|
||||
name: "PC_INCREMENT",
|
||||
|
|
@ -1133,6 +1150,7 @@ func TestEIP8024_Execution(t *testing.T) {
|
|||
pc := uint64(0)
|
||||
scope := &ScopeContext{Stack: stack, Contract: &Contract{Code: code}}
|
||||
var err error
|
||||
var errOp OpCode
|
||||
for pc < uint64(len(code)) && err == nil {
|
||||
op := code[pc]
|
||||
switch OpCode(op) {
|
||||
|
|
@ -1158,14 +1176,37 @@ func TestEIP8024_Execution(t *testing.T) {
|
|||
case EXCHANGE:
|
||||
_, err = opExchange(&pc, evm, scope)
|
||||
default:
|
||||
err = &ErrInvalidOpCode{opcode: OpCode(op)}
|
||||
t.Fatalf("unexpected opcode %s at pc=%d", OpCode(op), pc)
|
||||
}
|
||||
if err != nil {
|
||||
errOp = OpCode(op)
|
||||
}
|
||||
pc++
|
||||
}
|
||||
if tc.wantErr {
|
||||
if tc.wantErr != nil {
|
||||
// Fail because we wanted an error, but didn't get one.
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, got nil")
|
||||
}
|
||||
// Fail if the wrong opcode threw an error.
|
||||
if errOp != tc.wantOpcode {
|
||||
t.Fatalf("expected error from opcode %s, got %s", tc.wantOpcode, errOp)
|
||||
}
|
||||
// Fail if we don't get the error we expect.
|
||||
switch tc.wantErr.(type) {
|
||||
case *ErrInvalidOpCode:
|
||||
var want *ErrInvalidOpCode
|
||||
if !errors.As(err, &want) {
|
||||
t.Fatalf("expected ErrInvalidOpCode, got %v", err)
|
||||
}
|
||||
case *ErrStackUnderflow:
|
||||
var want *ErrStackUnderflow
|
||||
if !errors.As(err, &want) {
|
||||
t.Fatalf("expected ErrStackUnderflow, got %v", err)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unsupported wantErr type %T", tc.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue