Skip to content

Commit 5ca19fb

Browse files
committed
Use unexported sentinel error and require.ErrorIs
1 parent a5bcae2 commit 5ca19fb

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

libevm/legacy/legacy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919
package legacy
2020

2121
import (
22+
"errors"
2223
"fmt"
2324

2425
"github.com/ava-labs/libevm/core/vm"
2526
)
2627

28+
var (
29+
errRemainingGasExceedsSuppliedGas = errors.New("remaining gas exceeds supplied gas")
30+
)
31+
2732
// PrecompiledStatefulContract is the legacy signature of
2833
// [vm.PrecompiledStatefulContract], which explicitly accepts and returns gas
2934
// values. Instances SHOULD NOT use the [vm.PrecompileEnvironment]
@@ -36,7 +41,7 @@ func (c PrecompiledStatefulContract) Upgrade() vm.PrecompiledStatefulContract {
3641
gas := env.Gas()
3742
ret, remainingGas, err := c(env, input, gas)
3843
if remainingGas > gas {
39-
return nil, fmt.Errorf("remaining gas %d exceeds supplied gas %d", remainingGas, gas)
44+
return nil, fmt.Errorf("%w: %d > %d", errRemainingGasExceedsSuppliedGas, remainingGas, gas)
4045
}
4146
if used := gas - remainingGas; used > 0 {
4247
env.UseGas(used)

libevm/legacy/legacy_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,31 @@ func (s *stubPrecompileEnvironment) UseGas(gas uint64) bool {
4747
func TestPrecompiledStatefulContract_Upgrade(t *testing.T) {
4848
t.Parallel()
4949

50+
errTest := errors.New("test error")
51+
5052
tests := map[string]struct {
5153
envGas uint64
5254
precompileRet []byte
5355
remainingGas uint64
5456
precompileErr error
5557
wantRet []byte
56-
wantErr string
58+
wantErr error
5759
wantGasUsed uint64
5860
}{
5961
"call_error": {
6062
envGas: 10,
6163
precompileRet: []byte{2},
6264
remainingGas: 6,
63-
precompileErr: errors.New("test error"),
65+
precompileErr: errTest,
6466
wantRet: []byte{2},
65-
wantErr: "test error",
67+
wantErr: errTest,
6668
wantGasUsed: 4,
6769
},
6870
"remaining_gas_exceeds_supplied_gas": {
6971
envGas: 10,
7072
precompileRet: []byte{2},
7173
remainingGas: 11,
72-
wantErr: "remaining gas 11 exceeds supplied gas 10",
74+
wantErr: errRemainingGasExceedsSuppliedGas,
7375
},
7476
"zero_remaining_gas": {
7577
envGas: 10,
@@ -103,11 +105,7 @@ func TestPrecompiledStatefulContract_Upgrade(t *testing.T) {
103105
input := []byte("unused")
104106

105107
ret, err := upgraded(env, input)
106-
if testCase.wantErr == "" {
107-
require.NoError(t, err)
108-
} else {
109-
require.EqualError(t, err, testCase.wantErr)
110-
}
108+
require.ErrorIs(t, err, testCase.wantErr)
111109
assert.Equal(t, testCase.wantRet, ret, "bytes returned by upgraded contract")
112110
assert.Equalf(t, testCase.wantGasUsed, env.gasUsed, "sum of %T.UseGas() calls", env)
113111
})

0 commit comments

Comments
 (0)