Skip to content

Commit b32c8c3

Browse files
committed
Rework gas logic since it was horrendous
1 parent 58902bc commit b32c8c3

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

libevm/legacy/legacy_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ import (
2929
// stubPrecompileEnvironment implements [vm.PrecompileEnvironment] for testing.
3030
type stubPrecompileEnvironment struct {
3131
vm.PrecompileEnvironment
32-
gasToReturn uint64
33-
gasUsed uint64
32+
gas uint64
3433
}
3534

36-
// Gas returns the gas supplied to the precompile.
3735
func (s *stubPrecompileEnvironment) Gas() uint64 {
38-
return s.gasToReturn
36+
return s.gas
3937
}
4038

41-
// UseGas records the gas used by the precompile.
42-
func (s *stubPrecompileEnvironment) UseGas(gas uint64) bool {
43-
s.gasUsed += gas
39+
func (s *stubPrecompileEnvironment) UseGas(gas uint64) (hasEnoughGas bool) {
40+
if s.gas < gas {
41+
return false
42+
}
43+
s.gas -= gas
4444
return true
4545
}
4646

@@ -50,38 +50,39 @@ func TestPrecompiledStatefulContract_Upgrade(t *testing.T) {
5050
errTest := errors.New("test error")
5151

5252
tests := map[string]struct {
53-
envGas uint64
53+
gas uint64
5454
precompileRet []byte
5555
remainingGas uint64
5656
precompileErr error
5757
wantErr error
58-
wantGasUsed uint64
58+
wantGas uint64
5959
}{
6060
"call_error": {
61-
envGas: 10,
61+
gas: 10,
6262
precompileRet: []byte{2},
6363
remainingGas: 6,
6464
precompileErr: errTest,
6565
wantErr: errTest,
66-
wantGasUsed: 4,
66+
wantGas: 6,
6767
},
6868
"remaining_gas_exceeds_supplied_gas": {
69-
envGas: 10,
69+
gas: 10,
7070
precompileRet: []byte{2},
7171
remainingGas: 11,
7272
wantErr: errRemainingGasExceedsSuppliedGas,
73+
wantGas: 10,
7374
},
7475
"zero_remaining_gas": {
7576
remainingGas: 0,
76-
envGas: 10,
77+
gas: 10,
7778
precompileRet: []byte{2},
78-
wantGasUsed: 10,
79+
wantGas: 0,
7980
},
8081
"used_one_gas": {
81-
envGas: 10,
82+
gas: 10,
8283
precompileRet: []byte{2},
8384
remainingGas: 9,
84-
wantGasUsed: 1,
85+
wantGas: 9,
8586
},
8687
}
8788

@@ -97,14 +98,14 @@ func TestPrecompiledStatefulContract_Upgrade(t *testing.T) {
9798
upgraded := c.Upgrade()
9899

99100
env := &stubPrecompileEnvironment{
100-
gasToReturn: testCase.envGas,
101+
gas: testCase.gas,
101102
}
102103
input := []byte("unused")
103104

104105
ret, err := upgraded(env, input)
105106
require.ErrorIs(t, err, testCase.wantErr)
106107
assert.Equal(t, testCase.precompileRet, ret, "bytes returned by upgraded contract")
107-
assert.Equalf(t, testCase.wantGasUsed, env.gasUsed, "sum of %T.UseGas() calls", env)
108+
assert.Equalf(t, testCase.wantGas, env.gas, "remaining gas in %T", env)
108109
})
109110
}
110111
}

0 commit comments

Comments
 (0)