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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -1013,10 +1014,11 @@ func TestEIP8024_Execution(t *testing.T) {
|
||||||
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
|
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
codeHex string
|
codeHex string
|
||||||
wantErr bool
|
wantErr error
|
||||||
wantVals []uint64
|
wantOpcode OpCode
|
||||||
|
wantVals []uint64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "DUPN",
|
name: "DUPN",
|
||||||
|
|
@ -1069,55 +1071,70 @@ func TestEIP8024_Execution(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "INVALID_SWAPN_LOW",
|
name: "INVALID_SWAPN_LOW",
|
||||||
codeHex: "e75b",
|
codeHex: "e75b",
|
||||||
wantErr: true,
|
wantErr: &ErrInvalidOpCode{},
|
||||||
|
wantOpcode: SWAPN,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "JUMP over INVALID_DUPN",
|
name: "JUMP over INVALID_DUPN",
|
||||||
codeHex: "600456e65b",
|
codeHex: "600456e65b",
|
||||||
wantErr: false,
|
wantErr: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "UNDERFLOW_DUPN_1",
|
||||||
|
codeHex: "6000808080808080808080808080808080e600",
|
||||||
|
wantErr: &ErrStackUnderflow{},
|
||||||
|
wantOpcode: DUPN,
|
||||||
},
|
},
|
||||||
// Additional test cases
|
// Additional test cases
|
||||||
{
|
{
|
||||||
name: "INVALID_DUPN_LOW",
|
name: "INVALID_DUPN_LOW",
|
||||||
codeHex: "e65b",
|
codeHex: "e65b",
|
||||||
wantErr: true,
|
wantErr: &ErrInvalidOpCode{},
|
||||||
|
wantOpcode: DUPN,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "INVALID_EXCHANGE_LOW",
|
name: "INVALID_EXCHANGE_LOW",
|
||||||
codeHex: "e850",
|
codeHex: "e850",
|
||||||
wantErr: true,
|
wantErr: &ErrInvalidOpCode{},
|
||||||
|
wantOpcode: EXCHANGE,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "INVALID_DUPN_HIGH",
|
name: "INVALID_DUPN_HIGH",
|
||||||
codeHex: "e67f",
|
codeHex: "e67f",
|
||||||
wantErr: true,
|
wantErr: &ErrInvalidOpCode{},
|
||||||
|
wantOpcode: DUPN,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "INVALID_SWAPN_HIGH",
|
name: "INVALID_SWAPN_HIGH",
|
||||||
codeHex: "e77f",
|
codeHex: "e77f",
|
||||||
wantErr: true,
|
wantErr: &ErrInvalidOpCode{},
|
||||||
|
wantOpcode: SWAPN,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "INVALID_EXCHANGE_HIGH",
|
name: "INVALID_EXCHANGE_HIGH",
|
||||||
codeHex: "e87f",
|
codeHex: "e87f",
|
||||||
wantErr: true,
|
wantErr: &ErrInvalidOpCode{},
|
||||||
|
wantOpcode: EXCHANGE,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "UNDERFLOW_DUPN",
|
name: "UNDERFLOW_DUPN_2",
|
||||||
codeHex: "5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5fe600", // (n=17, need 17 items, have 16)
|
codeHex: "5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5fe600", // (n=17, need 17 items, have 16)
|
||||||
wantErr: true,
|
wantErr: &ErrStackUnderflow{},
|
||||||
|
wantOpcode: DUPN,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "UNDERFLOW_SWAPN",
|
name: "UNDERFLOW_SWAPN",
|
||||||
codeHex: "5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5fe700", // (n=17, need 18 items, have 17)
|
codeHex: "5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5fe700", // (n=17, need 18 items, have 17)
|
||||||
wantErr: true,
|
wantErr: &ErrStackUnderflow{},
|
||||||
|
wantOpcode: SWAPN,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "UNDERFLOW_EXCHANGE",
|
name: "UNDERFLOW_EXCHANGE",
|
||||||
codeHex: "60016002e801", // (n,m)=(1,2), need 3 items, have 2
|
codeHex: "60016002e801", // (n,m)=(1,2), need 3 items, have 2
|
||||||
wantErr: true,
|
wantErr: &ErrStackUnderflow{},
|
||||||
|
wantOpcode: EXCHANGE,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "PC_INCREMENT",
|
name: "PC_INCREMENT",
|
||||||
|
|
@ -1133,6 +1150,7 @@ func TestEIP8024_Execution(t *testing.T) {
|
||||||
pc := uint64(0)
|
pc := uint64(0)
|
||||||
scope := &ScopeContext{Stack: stack, Contract: &Contract{Code: code}}
|
scope := &ScopeContext{Stack: stack, Contract: &Contract{Code: code}}
|
||||||
var err error
|
var err error
|
||||||
|
var errOp OpCode
|
||||||
for pc < uint64(len(code)) && err == nil {
|
for pc < uint64(len(code)) && err == nil {
|
||||||
op := code[pc]
|
op := code[pc]
|
||||||
switch OpCode(op) {
|
switch OpCode(op) {
|
||||||
|
|
@ -1158,14 +1176,37 @@ func TestEIP8024_Execution(t *testing.T) {
|
||||||
case EXCHANGE:
|
case EXCHANGE:
|
||||||
_, err = opExchange(&pc, evm, scope)
|
_, err = opExchange(&pc, evm, scope)
|
||||||
default:
|
default:
|
||||||
err = &ErrInvalidOpCode{opcode: OpCode(op)}
|
t.Fatalf("unexpected opcode %s at pc=%d", OpCode(op), pc)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
errOp = OpCode(op)
|
||||||
}
|
}
|
||||||
pc++
|
pc++
|
||||||
}
|
}
|
||||||
if tc.wantErr {
|
if tc.wantErr != nil {
|
||||||
|
// Fail because we wanted an error, but didn't get one.
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error, got 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
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue