Skip to content

Commit

Permalink
remove vmerrs (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur authored Feb 24, 2025
1 parent 95f9444 commit 6448d1c
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 100 deletions.
5 changes: 2 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/utils"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common"
cmath "github.com/ava-labs/libevm/common/math"
"github.com/ava-labs/libevm/core/vm"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (result *ExecutionResult) Return() []byte {
// Revert returns the concrete revert reason if the execution is aborted by `REVERT`
// opcode. Note the reason can be nil if no data supplied with revert opcode.
func (result *ExecutionResult) Revert() []byte {
if result.Err != vmerrs.ErrExecutionReverted {
if result.Err != vm.ErrExecutionReverted {
return nil
}
return common.CopyBytes(result.ReturnData)
Expand Down Expand Up @@ -475,7 +474,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {

// Check whether the init code size has been exceeded.
if rulesExtra.IsDurango && contractCreation && len(msg.Data) > params.MaxInitCodeSize {
return nil, fmt.Errorf("%w: code size %v limit %v", vmerrs.ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize)
return nil, fmt.Errorf("%w: code size %v limit %v", vm.ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize)
}

// Execute the preparatory steps for state transition which includes:
Expand Down
4 changes: 2 additions & 2 deletions core/txpool/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto/kzg4844"
"github.com/ava-labs/libevm/log"
)
Expand Down Expand Up @@ -85,7 +85,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
}
// Check whether the init code size has been exceeded
if opts.Config.IsShanghai(head.Number, head.Time) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize {
return fmt.Errorf("%w: code size %v, limit %v", vmerrs.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
return fmt.Errorf("%w: code size %v, limit %v", vm.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
}
// Transactions can't be negative. This may never happen using RLP decoded
// transactions but may occur for transactions created using the RPC.
Expand Down
3 changes: 1 addition & 2 deletions eth/gasestimator/gasestimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/log"
Expand Down Expand Up @@ -128,7 +127,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
return 0, nil, err
}
if failed {
if result != nil && !errors.Is(result.Err, vmerrs.ErrOutOfGas) {
if result != nil && !errors.Is(result.Err, vm.ErrOutOfGas) {
return 0, result.Revert(), result.Err
}
return 0, nil, fmt.Errorf("gas required exceeds allowance (%d)", hi)
Expand Down
6 changes: 3 additions & 3 deletions internal/ethapi/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"fmt"

"github.com/ava-labs/coreth/accounts/abi"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common/hexutil"
"github.com/ava-labs/libevm/core/vm"
)

// revertError is an API error that encompasses an EVM revert with JSON error
Expand All @@ -54,11 +54,11 @@ func (e *revertError) ErrorData() interface{} {

// newRevertError creates a revertError instance with the provided revert data.
func newRevertError(revert []byte) *revertError {
err := vmerrs.ErrExecutionReverted
err := vm.ErrExecutionReverted

reason, errUnpack := abi.UnpackRevert(revert)
if errUnpack == nil {
err = fmt.Errorf("%w: %v", vmerrs.ErrExecutionReverted, reason)
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
}
return &revertError{
error: err,
Expand Down
22 changes: 10 additions & 12 deletions nativeasset/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math/big"

"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/log"
Expand Down Expand Up @@ -56,18 +55,18 @@ func UnpackNativeAssetBalanceInput(input []byte) (common.Address, common.Hash, e
func (b *NativeAssetBalance) Run(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
// input: encodePacked(address 20 bytes, assetID 32 bytes)
if suppliedGas < b.GasCost {
return nil, 0, vmerrs.ErrOutOfGas
return nil, 0, vm.ErrOutOfGas
}
remainingGas = suppliedGas - b.GasCost

address, assetID, err := UnpackNativeAssetBalanceInput(input)
if err != nil {
return nil, remainingGas, vmerrs.ErrExecutionReverted
return nil, remainingGas, vm.ErrExecutionReverted
}

res, overflow := uint256.FromBig(accessibleState.GetStateDB().GetBalanceMultiCoin(address, assetID))
if overflow {
return nil, remainingGas, vmerrs.ErrExecutionReverted
return nil, remainingGas, vm.ErrExecutionReverted
}
return common.LeftPadBytes(res.Bytes(), 32), remainingGas, nil
}
Expand Down Expand Up @@ -107,7 +106,7 @@ func UnpackNativeAssetCallInput(input []byte) (common.Address, common.Hash, *big
func (c *NativeAssetCall) Run(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
env := accessibleState.GetPrecompileEnv()
if !env.UseGas(c.GasCost) {
return nil, 0, vmerrs.ErrOutOfGas
return nil, 0, vm.ErrOutOfGas
}
ret, err = c.run(env, accessibleState.GetStateDB(), caller, addr, input, readOnly)
// This precompile will be wrapped in a libevm `legacy` wrapper, which
Expand All @@ -123,26 +122,26 @@ func (c *NativeAssetCall) Run(accessibleState contract.AccessibleState, caller c
// avoids mixing gas-accounting patterns when using env.Call().
func (c *NativeAssetCall) run(env vm.PrecompileEnvironment, stateDB contract.StateDB, caller common.Address, addr common.Address, input []byte, readOnly bool) (ret []byte, err error) {
if readOnly {
return nil, vmerrs.ErrExecutionReverted
return nil, vm.ErrExecutionReverted
}

to, assetID, assetAmount, callData, err := UnpackNativeAssetCallInput(input)
if err != nil {
log.Debug("unpacking native asset call input failed", "err", err)
return nil, vmerrs.ErrExecutionReverted
return nil, vm.ErrExecutionReverted
}

// Note: it is not possible for a negative assetAmount to be passed in here due to the fact that decoding a
// byte slice into a *big.Int type will always return a positive value, as documented on [big.Int.SetBytes].
if assetAmount.Sign() != 0 && stateDB.GetBalanceMultiCoin(caller, assetID).Cmp(assetAmount) < 0 {
return nil, vmerrs.ErrInsufficientBalance
return nil, vm.ErrInsufficientBalance
}

snapshot := stateDB.Snapshot()

if !stateDB.Exist(to) {
if !env.UseGas(c.CallNewAccountGas) {
return nil, vmerrs.ErrOutOfGas
return nil, vm.ErrOutOfGas
}
stateDB.CreateAccount(to)
}
Expand All @@ -152,13 +151,12 @@ func (c *NativeAssetCall) run(env vm.PrecompileEnvironment, stateDB contract.Sta
stateDB.AddBalanceMultiCoin(to, assetID, assetAmount)

ret, err = env.Call(to, callData, env.Gas(), new(uint256.Int), vm.WithUNSAFECallerAddressProxying())

// When an error was returned by the EVM or when setting the creation code
// above we revert to the snapshot and consume any gas remaining. Additionally
// when we're in homestead this also counts for code storage gas errors.
if err != nil {
stateDB.RevertToSnapshot(snapshot)
if err != vmerrs.ErrExecutionReverted {
if err != vm.ErrExecutionReverted {
env.UseGas(env.Gas())
}
// TODO: consider clearing up unused snapshots:
Expand All @@ -171,5 +169,5 @@ func (c *NativeAssetCall) run(env vm.PrecompileEnvironment, stateDB contract.Sta
type DeprecatedContract struct{}

func (*DeprecatedContract) Run(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
return nil, suppliedGas, vmerrs.ErrExecutionReverted
return nil, suppliedGas, vm.ErrExecutionReverted
}
19 changes: 9 additions & 10 deletions nativeasset/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ava-labs/coreth/core/state"
. "github.com/ava-labs/coreth/nativeasset"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common"
ethtypes "github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
Expand Down Expand Up @@ -184,7 +183,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: big0,
gasInput: params.AssetBalanceApricot,
expectedGasRemaining: 0,
expectedErr: vmerrs.ErrExecutionReverted,
expectedErr: vm.ErrExecutionReverted,
expectedResult: nil,
name: "native asset balance: invalid input data reverts",
},
Expand All @@ -202,7 +201,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: big0,
gasInput: params.AssetBalanceApricot - 1,
expectedGasRemaining: 0,
expectedErr: vmerrs.ErrOutOfGas,
expectedErr: vm.ErrOutOfGas,
expectedResult: nil,
name: "native asset balance: insufficient gas errors",
},
Expand All @@ -220,7 +219,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: u256Hundred,
gasInput: params.AssetBalanceApricot,
expectedGasRemaining: params.AssetBalanceApricot,
expectedErr: vmerrs.ErrInsufficientBalance,
expectedErr: vm.ErrInsufficientBalance,
expectedResult: nil,
name: "native asset balance: non-zero value with insufficient funds reverts before running pre-compile",
},
Expand Down Expand Up @@ -309,7 +308,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: uint256.NewInt(50),
gasInput: params.AssetCallApricot,
expectedGasRemaining: 0,
expectedErr: vmerrs.ErrInsufficientBalance,
expectedErr: vm.ErrInsufficientBalance,
expectedResult: nil,
name: "native asset call: insufficient multicoin funds",
stateDBCheck: func(t *testing.T, stateDB stateDB) {
Expand Down Expand Up @@ -341,7 +340,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: uint256.NewInt(51),
gasInput: params.AssetCallApricot,
expectedGasRemaining: params.AssetCallApricot,
expectedErr: vmerrs.ErrInsufficientBalance,
expectedErr: vm.ErrInsufficientBalance,
expectedResult: nil,
name: "native asset call: insufficient funds",
stateDBCheck: func(t *testing.T, stateDB stateDB) {
Expand Down Expand Up @@ -373,7 +372,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: uint256.NewInt(50),
gasInput: params.AssetCallApricot - 1,
expectedGasRemaining: 0,
expectedErr: vmerrs.ErrOutOfGas,
expectedErr: vm.ErrOutOfGas,
expectedResult: nil,
name: "native asset call: insufficient gas for native asset call",
},
Expand All @@ -394,7 +393,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: uint256.NewInt(50),
gasInput: params.AssetCallApricot + params.CallNewAccountGas - 1,
expectedGasRemaining: 0,
expectedErr: vmerrs.ErrOutOfGas,
expectedErr: vm.ErrOutOfGas,
expectedResult: nil,
name: "native asset call: insufficient gas to create new account",
stateDBCheck: func(t *testing.T, stateDB stateDB) {
Expand Down Expand Up @@ -426,7 +425,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: uint256.NewInt(50),
gasInput: params.AssetCallApricot + params.CallNewAccountGas,
expectedGasRemaining: params.CallNewAccountGas,
expectedErr: vmerrs.ErrExecutionReverted,
expectedErr: vm.ErrExecutionReverted,
expectedResult: nil,
name: "native asset call: invalid input",
},
Expand All @@ -447,7 +446,7 @@ func TestStatefulPrecompile(t *testing.T) {
value: big0,
gasInput: params.AssetCallApricot + params.CallNewAccountGas,
expectedGasRemaining: params.AssetCallApricot + params.CallNewAccountGas,
expectedErr: vmerrs.ErrExecutionReverted,
expectedErr: vm.ErrExecutionReverted,
expectedResult: nil,
name: "deprecated contract",
},
Expand Down
4 changes: 2 additions & 2 deletions precompile/contract/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/ava-labs/coreth/accounts/abi"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto"
)

Expand Down Expand Up @@ -43,7 +43,7 @@ func CalculateFunctionSelector(functionSignature string) []byte {
// DeductGas checks if [suppliedGas] is sufficient against [requiredGas] and deducts [requiredGas] from [suppliedGas].
func DeductGas(suppliedGas uint64, requiredGas uint64) (uint64, error) {
if suppliedGas < requiredGas {
return 0, vmerrs.ErrOutOfGas
return 0, vm.ErrOutOfGas
}
return suppliedGas - requiredGas, nil
}
Expand Down
6 changes: 3 additions & 3 deletions precompile/contracts/warp/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/warp/payload"
"github.com/ava-labs/coreth/accounts/abi"
"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/vmerrs"

_ "embed"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/common/math"
"github.com/ava-labs/libevm/core/vm"
)

const (
Expand Down Expand Up @@ -236,13 +236,13 @@ func sendWarpMessage(accessibleState contract.AccessibleState, caller common.Add
// This ensures that we charge gas before we unpack the variable sized input.
payloadGas, overflow := math.SafeMul(SendWarpMessageGasCostPerByte, uint64(len(input)))
if overflow {
return nil, 0, vmerrs.ErrOutOfGas
return nil, 0, vm.ErrOutOfGas
}
if remainingGas, err = contract.DeductGas(remainingGas, payloadGas); err != nil {
return nil, 0, err
}
if readOnly {
return nil, remainingGas, vmerrs.ErrWriteProtection
return nil, remainingGas, vm.ErrWriteProtection
}
// unpack the arguments
payloadData, err := UnpackSendWarpMessageInput(input)
Expand Down
18 changes: 9 additions & 9 deletions precompile/contracts/warp/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/ava-labs/coreth/precompile/testutils"
"github.com/ava-labs/coreth/predicate"
"github.com/ava-labs/coreth/utils"
"github.com/ava-labs/coreth/vmerrs"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -75,7 +75,7 @@ func TestGetBlockchainID(t *testing.T) {
},
SuppliedGas: GetBlockchainIDGasCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
}

Expand Down Expand Up @@ -109,21 +109,21 @@ func TestSendWarpMessage(t *testing.T) {
InputFn: func(t testing.TB) []byte { return sendWarpMessageInput },
SuppliedGas: SendWarpMessageGasCost + uint64(len(sendWarpMessageInput[4:])*int(SendWarpMessageGasCostPerByte)),
ReadOnly: true,
ExpectedErr: vmerrs.ErrWriteProtection.Error(),
ExpectedErr: vm.ErrWriteProtection.Error(),
},
"send warp message insufficient gas for first step": {
Caller: callerAddr,
InputFn: func(t testing.TB) []byte { return sendWarpMessageInput },
SuppliedGas: SendWarpMessageGasCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
"send warp message insufficient gas for payload bytes": {
Caller: callerAddr,
InputFn: func(t testing.TB) []byte { return sendWarpMessageInput },
SuppliedGas: SendWarpMessageGasCost + uint64(len(sendWarpMessageInput[4:])*int(SendWarpMessageGasCostPerByte)) - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
"send warp message invalid input": {
Caller: callerAddr,
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
},
SuppliedGas: GetVerifiedWarpMessageBaseCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
"get message out of gas": {
Caller: callerAddr,
Expand All @@ -375,7 +375,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
},
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)) - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
"get message invalid predicate packing": {
Caller: callerAddr,
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) {
},
SuppliedGas: GetVerifiedWarpMessageBaseCost - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
"get message out of gas": {
Caller: callerAddr,
Expand All @@ -653,7 +653,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) {
},
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)) - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
ExpectedErr: vm.ErrOutOfGas.Error(),
},
"get message invalid predicate packing": {
Caller: callerAddr,
Expand Down
Loading

0 comments on commit 6448d1c

Please sign in to comment.