forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 3
fix(libevm/legacy): PrecompiledStatefulContract
gas and remaining gas handling
#114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
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 6a8fb2e
fix(libevm/legacy): allow to use all the gas in PrecompiledStatefulCo…
qdm12 40ea889
Add context to assertions
qdm12 fabf9d9
Remove bad copied comment from stubs_test.go
qdm12 7948b4a
Simplify check for used gas
qdm12 58f2cf5
Embed `vm.PrecompileEnvironment` in `stubPrecompileEnvironment`
qdm12 5c2be65
Move `stubPrecompileEnvironment` to `legacy_test.go`
qdm12 1cf6551
Rename `testCase(s)` -> `test(s)`
qdm12 1ba366b
Remove `input` field from tests and set it to a constant
qdm12 da2f151
Rename test field `cRet` -> `precompileRet`
qdm12 c34844f
Rename test field `cRemainingGas` -> `remainingGas`
qdm12 ef76203
Rename test field `cErr` -> `precompileErr`
qdm12 a5bcae2
Move env declaration in subtest closer to where it's used
qdm12 5ca19fb
Use unexported sentinel error and require.ErrorIs
qdm12 45ee298
Check env.UseGas return value
qdm12 c0126a8
Always return `ret`
qdm12 6de69da
Reduce nesting
qdm12 3f1a4b5
Add remainingGas: 0 in test case `zero_remaining_gas`
qdm12 4910f93
Remove `wantRet` test case field
qdm12 58902bc
Simplify if check using env.UseGas
qdm12 b32c8c3
Rework gas logic since it was horrendous
qdm12 2ddb863
Rename `gas` -> `suppliedGas`
qdm12 0d1892b
Fix the horrendous order of fields in zero_remaining_gas test case
qdm12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2025 the libevm authors. | ||
// | ||
// The libevm additions to go-ethereum are free software: you can redistribute | ||
// them and/or modify them under the terms of the GNU Lesser General Public License | ||
// as published by the Free Software Foundation, either version 3 of the License, | ||
// or (at your option) any later version. | ||
// | ||
// The libevm additions are distributed in the hope that they will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see | ||
// <http://www.gnu.org/licenses/>. | ||
|
||
package legacy | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/libevm/core/vm" | ||
) | ||
|
||
// stubPrecompileEnvironment implements [vm.PrecompileEnvironment] for testing. | ||
type stubPrecompileEnvironment struct { | ||
vm.PrecompileEnvironment | ||
gas uint64 | ||
} | ||
|
||
func (s *stubPrecompileEnvironment) Gas() uint64 { | ||
return s.gas | ||
} | ||
|
||
func (s *stubPrecompileEnvironment) UseGas(gas uint64) (hasEnoughGas bool) { | ||
if s.gas < gas { | ||
return false | ||
} | ||
s.gas -= gas | ||
return true | ||
} | ||
|
||
func TestPrecompiledStatefulContract_Upgrade(t *testing.T) { | ||
t.Parallel() | ||
|
||
errTest := errors.New("test error") | ||
|
||
tests := map[string]struct { | ||
gas uint64 | ||
precompileRet []byte | ||
remainingGas uint64 | ||
precompileErr error | ||
wantErr error | ||
wantGas uint64 | ||
}{ | ||
"call_error": { | ||
gas: 10, | ||
precompileRet: []byte{2}, | ||
remainingGas: 6, | ||
precompileErr: errTest, | ||
wantErr: errTest, | ||
wantGas: 6, | ||
}, | ||
"remaining_gas_exceeds_supplied_gas": { | ||
gas: 10, | ||
precompileRet: []byte{2}, | ||
remainingGas: 11, | ||
wantErr: errRemainingGasExceedsSuppliedGas, | ||
wantGas: 10, | ||
}, | ||
"zero_remaining_gas": { | ||
remainingGas: 0, | ||
gas: 10, | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
precompileRet: []byte{2}, | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
wantGas: 0, | ||
}, | ||
"used_one_gas": { | ||
gas: 10, | ||
precompileRet: []byte{2}, | ||
remainingGas: 9, | ||
wantGas: 9, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
testCase := test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
c := PrecompiledStatefulContract(func(env vm.PrecompileEnvironment, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { | ||
return testCase.precompileRet, testCase.remainingGas, testCase.precompileErr | ||
}) | ||
|
||
upgraded := c.Upgrade() | ||
|
||
env := &stubPrecompileEnvironment{ | ||
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.wantGas, env.gas, "remaining gas in %T", env) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.