Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 10 additions & 3 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,11 @@
// equivalents.
package legacy

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

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

// PrecompiledStatefulContract is the legacy signature of
// [vm.PrecompiledStatefulContract], which explicitly accepts and returns gas
Expand All @@ -31,7 +35,10 @@ 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 {
if remainingGas > gas {
return nil, fmt.Errorf("remaining gas %d exceeds supplied gas %d", remainingGas, gas)
}
if used := gas - remainingGas; used <= gas {
env.UseGas(used)
}
return ret, err
Expand Down
100 changes: 100 additions & 0 deletions libevm/legacy/legacy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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"
)

func TestPrecompiledStatefulContract_Upgrade(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
envGas uint64
input []byte
cRet []byte
cRemainingGas uint64
cErr error
wantRet []byte
wantErr string
wantGasUsed uint64
}{
"call_error": {
envGas: 10,
input: []byte{1},
cRet: []byte{2},
cRemainingGas: 6,
cErr: errors.New("test error"),
wantRet: []byte{2},
wantErr: "test error",
wantGasUsed: 4,
},
"remaining_gas_exceeds_supplied_gas": {
envGas: 10,
input: []byte{1},
cRet: []byte{2},
cRemainingGas: 11,
wantErr: "remaining gas 11 exceeds supplied gas 10",
},
"zero_remaining_gas": {
envGas: 10,
input: []byte{1},
cRet: []byte{2},
wantRet: []byte{2},
wantGasUsed: 10,
},
"used_one_gas": {
envGas: 10,
input: []byte{1},
cRet: []byte{2},
cRemainingGas: 9,
wantRet: []byte{2},
wantGasUsed: 1,
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

env := &stubPrecompileEnvironment{
gasToReturn: testCase.envGas,
}
c := PrecompiledStatefulContract(func(env vm.PrecompileEnvironment, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
return testCase.cRet, testCase.cRemainingGas, testCase.cErr
})

upgraded := c.Upgrade()

ret, err := upgraded(env, testCase.input)
if testCase.wantErr == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, testCase.wantErr)
}
assert.Equal(t, testCase.wantRet, ret)
assert.Equal(t, testCase.wantGasUsed, env.gasUsed)
})
}
}
67 changes: 67 additions & 0 deletions libevm/legacy/stubs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 provides converters between legacy types and their refactored
// equivalents.

package legacy

import (
"math/big"

"github.com/holiman/uint256"

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

var _ vm.PrecompileEnvironment = (*stubPrecompileEnvironment)(nil)

// stubPrecompileEnvironment implements [vm.PrecompileEnvironment] for testing.
type stubPrecompileEnvironment struct {
gasToReturn uint64
gasUsed uint64
}

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

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

func (s *stubPrecompileEnvironment) Call(addr common.Address, input []byte, gas uint64, value *uint256.Int, _ ...vm.CallOption) (ret []byte, _ error) {
return nil, nil
}

func (s *stubPrecompileEnvironment) ChainConfig() *params.ChainConfig { return nil }
func (s *stubPrecompileEnvironment) Rules() params.Rules { return params.Rules{} }
func (s *stubPrecompileEnvironment) StateDB() vm.StateDB { return nil }
func (s *stubPrecompileEnvironment) ReadOnlyState() libevm.StateReader { return nil }
func (s *stubPrecompileEnvironment) IncomingCallType() vm.CallType { return vm.Call }
func (s *stubPrecompileEnvironment) Addresses() *libevm.AddressContext { return nil }
func (s *stubPrecompileEnvironment) ReadOnly() bool { return false }
func (s *stubPrecompileEnvironment) Value() *uint256.Int { return nil }
func (s *stubPrecompileEnvironment) BlockHeader() (h types.Header, err error) { return h, nil }
func (s *stubPrecompileEnvironment) BlockNumber() *big.Int { return nil }
func (s *stubPrecompileEnvironment) BlockTime() uint64 { return 0 }