Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8d288b7
fix(libevm/legacy): disallow remaining gas higher than input gas in `…
qdm12 Feb 4, 2025
6a8fb2e
fix(libevm/legacy): allow to use all the gas in PrecompiledStatefulCo…
qdm12 Feb 4, 2025
40ea889
Add context to assertions
qdm12 Feb 4, 2025
fabf9d9
Remove bad copied comment from stubs_test.go
qdm12 Feb 4, 2025
7948b4a
Simplify check for used gas
qdm12 Feb 4, 2025
58f2cf5
Embed `vm.PrecompileEnvironment` in `stubPrecompileEnvironment`
qdm12 Feb 4, 2025
5c2be65
Move `stubPrecompileEnvironment` to `legacy_test.go`
qdm12 Feb 4, 2025
1cf6551
Rename `testCase(s)` -> `test(s)`
qdm12 Feb 4, 2025
1ba366b
Remove `input` field from tests and set it to a constant
qdm12 Feb 4, 2025
da2f151
Rename test field `cRet` -> `precompileRet`
qdm12 Feb 4, 2025
c34844f
Rename test field `cRemainingGas` -> `remainingGas`
qdm12 Feb 4, 2025
ef76203
Rename test field `cErr` -> `precompileErr`
qdm12 Feb 4, 2025
a5bcae2
Move env declaration in subtest closer to where it's used
qdm12 Feb 4, 2025
5ca19fb
Use unexported sentinel error and require.ErrorIs
qdm12 Feb 4, 2025
45ee298
Check env.UseGas return value
qdm12 Feb 4, 2025
c0126a8
Always return `ret`
qdm12 Feb 5, 2025
6de69da
Reduce nesting
qdm12 Feb 5, 2025
3f1a4b5
Add remainingGas: 0 in test case `zero_remaining_gas`
qdm12 Feb 5, 2025
4910f93
Remove `wantRet` test case field
qdm12 Feb 5, 2025
58902bc
Simplify if check using env.UseGas
qdm12 Feb 6, 2025
b32c8c3
Rework gas logic since it was horrendous
qdm12 Feb 6, 2025
2ddb863
Rename `gas` -> `suppliedGas`
qdm12 Feb 6, 2025
0d1892b
Fix the horrendous order of fields in zero_remaining_gas test case
qdm12 Feb 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions libevm/legacy/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ import (
// stubPrecompileEnvironment implements [vm.PrecompileEnvironment] for testing.
type stubPrecompileEnvironment struct {
vm.PrecompileEnvironment
gasToReturn uint64
gasUsed uint64
gas uint64
}

// Gas returns the gas supplied to the precompile.
func (s *stubPrecompileEnvironment) Gas() uint64 {
return s.gasToReturn
return s.gas
}

// UseGas records the gas used by the precompile.
func (s *stubPrecompileEnvironment) UseGas(gas uint64) bool {
s.gasUsed += gas
func (s *stubPrecompileEnvironment) UseGas(gas uint64) (hasEnoughGas bool) {
if s.gas < gas {
return false
}
s.gas -= gas
return true
}

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

tests := map[string]struct {
envGas uint64
gas uint64
precompileRet []byte
remainingGas uint64
precompileErr error
wantErr error
wantGasUsed uint64
wantGas uint64
}{
"call_error": {
envGas: 10,
gas: 10,
precompileRet: []byte{2},
remainingGas: 6,
precompileErr: errTest,
wantErr: errTest,
wantGasUsed: 4,
wantGas: 6,
},
"remaining_gas_exceeds_supplied_gas": {
envGas: 10,
gas: 10,
precompileRet: []byte{2},
remainingGas: 11,
wantErr: errRemainingGasExceedsSuppliedGas,
wantGas: 10,
},
"zero_remaining_gas": {
remainingGas: 0,
envGas: 10,
gas: 10,
precompileRet: []byte{2},
wantGasUsed: 10,
wantGas: 0,
},
"used_one_gas": {
envGas: 10,
gas: 10,
precompileRet: []byte{2},
remainingGas: 9,
wantGasUsed: 1,
wantGas: 9,
},
}

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

env := &stubPrecompileEnvironment{
gasToReturn: testCase.envGas,
gas: testCase.gas,
}
input := []byte("unused")

ret, err := upgraded(env, input)
require.ErrorIs(t, err, testCase.wantErr)
assert.Equal(t, testCase.precompileRet, ret, "bytes returned by upgraded contract")
assert.Equalf(t, testCase.wantGasUsed, env.gasUsed, "sum of %T.UseGas() calls", env)
assert.Equalf(t, testCase.wantGas, env.gas, "remaining gas in %T", env)
})
}
}