Skip to content
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

fix(libevm/legacy): PrecompiledStatefulContract gas and remaining gas handling #114

Merged
merged 23 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 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
20 changes: 16 additions & 4 deletions libevm/legacy/legacy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 the libevm authors.
// Copyright 2024-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
Expand All @@ -18,7 +18,16 @@
// equivalents.
package legacy

import "github.com/ava-labs/libevm/core/vm"
import (
"errors"
"fmt"

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

var (
errRemainingGasExceedsSuppliedGas = errors.New("remaining gas exceeds supplied gas")
)

// PrecompiledStatefulContract is the legacy signature of
// [vm.PrecompiledStatefulContract], which explicitly accepts and returns gas
Expand All @@ -31,8 +40,11 @@ func (c PrecompiledStatefulContract) Upgrade() vm.PrecompiledStatefulContract {
return func(env vm.PrecompileEnvironment, input []byte) ([]byte, error) {
gas := env.Gas()
ret, remainingGas, err := c(env, input, gas)
if used := gas - remainingGas; used < gas {
env.UseGas(used)
if remainingGas > gas {
return ret, fmt.Errorf("%w: %d > %d", errRemainingGasExceedsSuppliedGas, remainingGas, gas)
}
if !env.UseGas(gas - remainingGas) {
return ret, vm.ErrOutOfGas
}
return ret, err
}
Expand Down
111 changes: 111 additions & 0 deletions libevm/legacy/legacy_test.go
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 {
suppliedGas uint64
precompileRet []byte
remainingGas uint64
precompileErr error
wantErr error
wantGas uint64
}{
"call_error": {
suppliedGas: 10,
precompileRet: []byte{2},
remainingGas: 6,
precompileErr: errTest,
wantErr: errTest,
wantGas: 6,
},
"remaining_gas_exceeds_supplied_gas": {
suppliedGas: 10,
precompileRet: []byte{2},
remainingGas: 11,
wantErr: errRemainingGasExceedsSuppliedGas,
wantGas: 10,
},
"zero_remaining_gas": {
suppliedGas: 10,
precompileRet: []byte{2},
remainingGas: 0,
wantGas: 0,
},
"used_one_gas": {
suppliedGas: 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.suppliedGas,
}
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)
})
}
}