forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(libevm/legacy): allow to use all the gas in PrecompiledStatefulCo…
…ntract
- Loading branch information
Showing
3 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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,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) | ||
}) | ||
} | ||
} |
This file contains 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,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 } |