Skip to content

Commit 6448d1c

Browse files
authored
remove vmerrs (#829)
1 parent 95f9444 commit 6448d1c

File tree

11 files changed

+43
-100
lines changed

11 files changed

+43
-100
lines changed

core/state_transition.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/ava-labs/coreth/core/types"
3535
"github.com/ava-labs/coreth/params"
3636
"github.com/ava-labs/coreth/utils"
37-
"github.com/ava-labs/coreth/vmerrs"
3837
"github.com/ava-labs/libevm/common"
3938
cmath "github.com/ava-labs/libevm/common/math"
4039
"github.com/ava-labs/libevm/core/vm"
@@ -72,7 +71,7 @@ func (result *ExecutionResult) Return() []byte {
7271
// Revert returns the concrete revert reason if the execution is aborted by `REVERT`
7372
// opcode. Note the reason can be nil if no data supplied with revert opcode.
7473
func (result *ExecutionResult) Revert() []byte {
75-
if result.Err != vmerrs.ErrExecutionReverted {
74+
if result.Err != vm.ErrExecutionReverted {
7675
return nil
7776
}
7877
return common.CopyBytes(result.ReturnData)
@@ -475,7 +474,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
475474

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

481480
// Execute the preparatory steps for state transition which includes:

core/txpool/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import (
3535
"github.com/ava-labs/coreth/core/state"
3636
"github.com/ava-labs/coreth/core/types"
3737
"github.com/ava-labs/coreth/params"
38-
"github.com/ava-labs/coreth/vmerrs"
3938
"github.com/ava-labs/libevm/common"
39+
"github.com/ava-labs/libevm/core/vm"
4040
"github.com/ava-labs/libevm/crypto/kzg4844"
4141
"github.com/ava-labs/libevm/log"
4242
)
@@ -85,7 +85,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
8585
}
8686
// Check whether the init code size has been exceeded
8787
if opts.Config.IsShanghai(head.Number, head.Time) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize {
88-
return fmt.Errorf("%w: code size %v, limit %v", vmerrs.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
88+
return fmt.Errorf("%w: code size %v, limit %v", vm.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
8989
}
9090
// Transactions can't be negative. This may never happen using RLP decoded
9191
// transactions but may occur for transactions created using the RPC.

eth/gasestimator/gasestimator.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/ava-labs/coreth/core/state"
3838
"github.com/ava-labs/coreth/core/types"
3939
"github.com/ava-labs/coreth/params"
40-
"github.com/ava-labs/coreth/vmerrs"
4140
"github.com/ava-labs/libevm/common"
4241
"github.com/ava-labs/libevm/core/vm"
4342
"github.com/ava-labs/libevm/log"
@@ -128,7 +127,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
128127
return 0, nil, err
129128
}
130129
if failed {
131-
if result != nil && !errors.Is(result.Err, vmerrs.ErrOutOfGas) {
130+
if result != nil && !errors.Is(result.Err, vm.ErrOutOfGas) {
132131
return 0, result.Revert(), result.Err
133132
}
134133
return 0, nil, fmt.Errorf("gas required exceeds allowance (%d)", hi)

internal/ethapi/errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"fmt"
3131

3232
"github.com/ava-labs/coreth/accounts/abi"
33-
"github.com/ava-labs/coreth/vmerrs"
3433
"github.com/ava-labs/libevm/common/hexutil"
34+
"github.com/ava-labs/libevm/core/vm"
3535
)
3636

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

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

5959
reason, errUnpack := abi.UnpackRevert(revert)
6060
if errUnpack == nil {
61-
err = fmt.Errorf("%w: %v", vmerrs.ErrExecutionReverted, reason)
61+
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
6262
}
6363
return &revertError{
6464
error: err,

nativeasset/contract.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math/big"
99

1010
"github.com/ava-labs/coreth/precompile/contract"
11-
"github.com/ava-labs/coreth/vmerrs"
1211
"github.com/ava-labs/libevm/common"
1312
"github.com/ava-labs/libevm/core/vm"
1413
"github.com/ava-labs/libevm/log"
@@ -56,18 +55,18 @@ func UnpackNativeAssetBalanceInput(input []byte) (common.Address, common.Hash, e
5655
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) {
5756
// input: encodePacked(address 20 bytes, assetID 32 bytes)
5857
if suppliedGas < b.GasCost {
59-
return nil, 0, vmerrs.ErrOutOfGas
58+
return nil, 0, vm.ErrOutOfGas
6059
}
6160
remainingGas = suppliedGas - b.GasCost
6261

6362
address, assetID, err := UnpackNativeAssetBalanceInput(input)
6463
if err != nil {
65-
return nil, remainingGas, vmerrs.ErrExecutionReverted
64+
return nil, remainingGas, vm.ErrExecutionReverted
6665
}
6766

6867
res, overflow := uint256.FromBig(accessibleState.GetStateDB().GetBalanceMultiCoin(address, assetID))
6968
if overflow {
70-
return nil, remainingGas, vmerrs.ErrExecutionReverted
69+
return nil, remainingGas, vm.ErrExecutionReverted
7170
}
7271
return common.LeftPadBytes(res.Bytes(), 32), remainingGas, nil
7372
}
@@ -107,7 +106,7 @@ func UnpackNativeAssetCallInput(input []byte) (common.Address, common.Hash, *big
107106
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) {
108107
env := accessibleState.GetPrecompileEnv()
109108
if !env.UseGas(c.GasCost) {
110-
return nil, 0, vmerrs.ErrOutOfGas
109+
return nil, 0, vm.ErrOutOfGas
111110
}
112111
ret, err = c.run(env, accessibleState.GetStateDB(), caller, addr, input, readOnly)
113112
// This precompile will be wrapped in a libevm `legacy` wrapper, which
@@ -123,26 +122,26 @@ func (c *NativeAssetCall) Run(accessibleState contract.AccessibleState, caller c
123122
// avoids mixing gas-accounting patterns when using env.Call().
124123
func (c *NativeAssetCall) run(env vm.PrecompileEnvironment, stateDB contract.StateDB, caller common.Address, addr common.Address, input []byte, readOnly bool) (ret []byte, err error) {
125124
if readOnly {
126-
return nil, vmerrs.ErrExecutionReverted
125+
return nil, vm.ErrExecutionReverted
127126
}
128127

129128
to, assetID, assetAmount, callData, err := UnpackNativeAssetCallInput(input)
130129
if err != nil {
131130
log.Debug("unpacking native asset call input failed", "err", err)
132-
return nil, vmerrs.ErrExecutionReverted
131+
return nil, vm.ErrExecutionReverted
133132
}
134133

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

141140
snapshot := stateDB.Snapshot()
142141

143142
if !stateDB.Exist(to) {
144143
if !env.UseGas(c.CallNewAccountGas) {
145-
return nil, vmerrs.ErrOutOfGas
144+
return nil, vm.ErrOutOfGas
146145
}
147146
stateDB.CreateAccount(to)
148147
}
@@ -152,13 +151,12 @@ func (c *NativeAssetCall) run(env vm.PrecompileEnvironment, stateDB contract.Sta
152151
stateDB.AddBalanceMultiCoin(to, assetID, assetAmount)
153152

154153
ret, err = env.Call(to, callData, env.Gas(), new(uint256.Int), vm.WithUNSAFECallerAddressProxying())
155-
156154
// When an error was returned by the EVM or when setting the creation code
157155
// above we revert to the snapshot and consume any gas remaining. Additionally
158156
// when we're in homestead this also counts for code storage gas errors.
159157
if err != nil {
160158
stateDB.RevertToSnapshot(snapshot)
161-
if err != vmerrs.ErrExecutionReverted {
159+
if err != vm.ErrExecutionReverted {
162160
env.UseGas(env.Gas())
163161
}
164162
// TODO: consider clearing up unused snapshots:
@@ -171,5 +169,5 @@ func (c *NativeAssetCall) run(env vm.PrecompileEnvironment, stateDB contract.Sta
171169
type DeprecatedContract struct{}
172170

173171
func (*DeprecatedContract) Run(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
174-
return nil, suppliedGas, vmerrs.ErrExecutionReverted
172+
return nil, suppliedGas, vm.ErrExecutionReverted
175173
}

nativeasset/contract_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/ava-labs/coreth/core/state"
1212
. "github.com/ava-labs/coreth/nativeasset"
1313
"github.com/ava-labs/coreth/params"
14-
"github.com/ava-labs/coreth/vmerrs"
1514
"github.com/ava-labs/libevm/common"
1615
ethtypes "github.com/ava-labs/libevm/core/types"
1716
"github.com/ava-labs/libevm/core/vm"
@@ -184,7 +183,7 @@ func TestStatefulPrecompile(t *testing.T) {
184183
value: big0,
185184
gasInput: params.AssetBalanceApricot,
186185
expectedGasRemaining: 0,
187-
expectedErr: vmerrs.ErrExecutionReverted,
186+
expectedErr: vm.ErrExecutionReverted,
188187
expectedResult: nil,
189188
name: "native asset balance: invalid input data reverts",
190189
},
@@ -202,7 +201,7 @@ func TestStatefulPrecompile(t *testing.T) {
202201
value: big0,
203202
gasInput: params.AssetBalanceApricot - 1,
204203
expectedGasRemaining: 0,
205-
expectedErr: vmerrs.ErrOutOfGas,
204+
expectedErr: vm.ErrOutOfGas,
206205
expectedResult: nil,
207206
name: "native asset balance: insufficient gas errors",
208207
},
@@ -220,7 +219,7 @@ func TestStatefulPrecompile(t *testing.T) {
220219
value: u256Hundred,
221220
gasInput: params.AssetBalanceApricot,
222221
expectedGasRemaining: params.AssetBalanceApricot,
223-
expectedErr: vmerrs.ErrInsufficientBalance,
222+
expectedErr: vm.ErrInsufficientBalance,
224223
expectedResult: nil,
225224
name: "native asset balance: non-zero value with insufficient funds reverts before running pre-compile",
226225
},
@@ -309,7 +308,7 @@ func TestStatefulPrecompile(t *testing.T) {
309308
value: uint256.NewInt(50),
310309
gasInput: params.AssetCallApricot,
311310
expectedGasRemaining: 0,
312-
expectedErr: vmerrs.ErrInsufficientBalance,
311+
expectedErr: vm.ErrInsufficientBalance,
313312
expectedResult: nil,
314313
name: "native asset call: insufficient multicoin funds",
315314
stateDBCheck: func(t *testing.T, stateDB stateDB) {
@@ -341,7 +340,7 @@ func TestStatefulPrecompile(t *testing.T) {
341340
value: uint256.NewInt(51),
342341
gasInput: params.AssetCallApricot,
343342
expectedGasRemaining: params.AssetCallApricot,
344-
expectedErr: vmerrs.ErrInsufficientBalance,
343+
expectedErr: vm.ErrInsufficientBalance,
345344
expectedResult: nil,
346345
name: "native asset call: insufficient funds",
347346
stateDBCheck: func(t *testing.T, stateDB stateDB) {
@@ -373,7 +372,7 @@ func TestStatefulPrecompile(t *testing.T) {
373372
value: uint256.NewInt(50),
374373
gasInput: params.AssetCallApricot - 1,
375374
expectedGasRemaining: 0,
376-
expectedErr: vmerrs.ErrOutOfGas,
375+
expectedErr: vm.ErrOutOfGas,
377376
expectedResult: nil,
378377
name: "native asset call: insufficient gas for native asset call",
379378
},
@@ -394,7 +393,7 @@ func TestStatefulPrecompile(t *testing.T) {
394393
value: uint256.NewInt(50),
395394
gasInput: params.AssetCallApricot + params.CallNewAccountGas - 1,
396395
expectedGasRemaining: 0,
397-
expectedErr: vmerrs.ErrOutOfGas,
396+
expectedErr: vm.ErrOutOfGas,
398397
expectedResult: nil,
399398
name: "native asset call: insufficient gas to create new account",
400399
stateDBCheck: func(t *testing.T, stateDB stateDB) {
@@ -426,7 +425,7 @@ func TestStatefulPrecompile(t *testing.T) {
426425
value: uint256.NewInt(50),
427426
gasInput: params.AssetCallApricot + params.CallNewAccountGas,
428427
expectedGasRemaining: params.CallNewAccountGas,
429-
expectedErr: vmerrs.ErrExecutionReverted,
428+
expectedErr: vm.ErrExecutionReverted,
430429
expectedResult: nil,
431430
name: "native asset call: invalid input",
432431
},
@@ -447,7 +446,7 @@ func TestStatefulPrecompile(t *testing.T) {
447446
value: big0,
448447
gasInput: params.AssetCallApricot + params.CallNewAccountGas,
449448
expectedGasRemaining: params.AssetCallApricot + params.CallNewAccountGas,
450-
expectedErr: vmerrs.ErrExecutionReverted,
449+
expectedErr: vm.ErrExecutionReverted,
451450
expectedResult: nil,
452451
name: "deprecated contract",
453452
},

precompile/contract/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/ava-labs/coreth/accounts/abi"
12-
"github.com/ava-labs/coreth/vmerrs"
12+
"github.com/ava-labs/libevm/core/vm"
1313
"github.com/ava-labs/libevm/crypto"
1414
)
1515

@@ -43,7 +43,7 @@ func CalculateFunctionSelector(functionSignature string) []byte {
4343
// DeductGas checks if [suppliedGas] is sufficient against [requiredGas] and deducts [requiredGas] from [suppliedGas].
4444
func DeductGas(suppliedGas uint64, requiredGas uint64) (uint64, error) {
4545
if suppliedGas < requiredGas {
46-
return 0, vmerrs.ErrOutOfGas
46+
return 0, vm.ErrOutOfGas
4747
}
4848
return suppliedGas - requiredGas, nil
4949
}

precompile/contracts/warp/contract.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/ava-labs/avalanchego/vms/platformvm/warp/payload"
1212
"github.com/ava-labs/coreth/accounts/abi"
1313
"github.com/ava-labs/coreth/precompile/contract"
14-
"github.com/ava-labs/coreth/vmerrs"
1514

1615
_ "embed"
1716

1817
"github.com/ava-labs/libevm/common"
1918
"github.com/ava-labs/libevm/common/math"
19+
"github.com/ava-labs/libevm/core/vm"
2020
)
2121

2222
const (
@@ -236,13 +236,13 @@ func sendWarpMessage(accessibleState contract.AccessibleState, caller common.Add
236236
// This ensures that we charge gas before we unpack the variable sized input.
237237
payloadGas, overflow := math.SafeMul(SendWarpMessageGasCostPerByte, uint64(len(input)))
238238
if overflow {
239-
return nil, 0, vmerrs.ErrOutOfGas
239+
return nil, 0, vm.ErrOutOfGas
240240
}
241241
if remainingGas, err = contract.DeductGas(remainingGas, payloadGas); err != nil {
242242
return nil, 0, err
243243
}
244244
if readOnly {
245-
return nil, remainingGas, vmerrs.ErrWriteProtection
245+
return nil, remainingGas, vm.ErrWriteProtection
246246
}
247247
// unpack the arguments
248248
payloadData, err := UnpackSendWarpMessageInput(input)

precompile/contracts/warp/contract_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/ava-labs/coreth/precompile/testutils"
2020
"github.com/ava-labs/coreth/predicate"
2121
"github.com/ava-labs/coreth/utils"
22-
"github.com/ava-labs/coreth/vmerrs"
2322
"github.com/ava-labs/libevm/common"
23+
"github.com/ava-labs/libevm/core/vm"
2424
"github.com/stretchr/testify/require"
2525
)
2626

@@ -75,7 +75,7 @@ func TestGetBlockchainID(t *testing.T) {
7575
},
7676
SuppliedGas: GetBlockchainIDGasCost - 1,
7777
ReadOnly: false,
78-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
78+
ExpectedErr: vm.ErrOutOfGas.Error(),
7979
},
8080
}
8181

@@ -109,21 +109,21 @@ func TestSendWarpMessage(t *testing.T) {
109109
InputFn: func(t testing.TB) []byte { return sendWarpMessageInput },
110110
SuppliedGas: SendWarpMessageGasCost + uint64(len(sendWarpMessageInput[4:])*int(SendWarpMessageGasCostPerByte)),
111111
ReadOnly: true,
112-
ExpectedErr: vmerrs.ErrWriteProtection.Error(),
112+
ExpectedErr: vm.ErrWriteProtection.Error(),
113113
},
114114
"send warp message insufficient gas for first step": {
115115
Caller: callerAddr,
116116
InputFn: func(t testing.TB) []byte { return sendWarpMessageInput },
117117
SuppliedGas: SendWarpMessageGasCost - 1,
118118
ReadOnly: false,
119-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
119+
ExpectedErr: vm.ErrOutOfGas.Error(),
120120
},
121121
"send warp message insufficient gas for payload bytes": {
122122
Caller: callerAddr,
123123
InputFn: func(t testing.TB) []byte { return sendWarpMessageInput },
124124
SuppliedGas: SendWarpMessageGasCost + uint64(len(sendWarpMessageInput[4:])*int(SendWarpMessageGasCostPerByte)) - 1,
125125
ReadOnly: false,
126-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
126+
ExpectedErr: vm.ErrOutOfGas.Error(),
127127
},
128128
"send warp message invalid input": {
129129
Caller: callerAddr,
@@ -362,7 +362,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
362362
},
363363
SuppliedGas: GetVerifiedWarpMessageBaseCost - 1,
364364
ReadOnly: false,
365-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
365+
ExpectedErr: vm.ErrOutOfGas.Error(),
366366
},
367367
"get message out of gas": {
368368
Caller: callerAddr,
@@ -375,7 +375,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
375375
},
376376
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)) - 1,
377377
ReadOnly: false,
378-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
378+
ExpectedErr: vm.ErrOutOfGas.Error(),
379379
},
380380
"get message invalid predicate packing": {
381381
Caller: callerAddr,
@@ -640,7 +640,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) {
640640
},
641641
SuppliedGas: GetVerifiedWarpMessageBaseCost - 1,
642642
ReadOnly: false,
643-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
643+
ExpectedErr: vm.ErrOutOfGas.Error(),
644644
},
645645
"get message out of gas": {
646646
Caller: callerAddr,
@@ -653,7 +653,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) {
653653
},
654654
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)) - 1,
655655
ReadOnly: false,
656-
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
656+
ExpectedErr: vm.ErrOutOfGas.Error(),
657657
},
658658
"get message invalid predicate packing": {
659659
Caller: callerAddr,

0 commit comments

Comments
 (0)