go-ethereum/core/vm
Arran Schlosberg 44f23c8869
refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73)
## Why this should be merged

Aligns precompiled contracts with the pattern used in
`vm.EVMInterpreter` for regular contracts and simplifies gas accounting
by using existing mechanisms. The most notable simplification occurs
when there are multiple error paths that return early and have to
account for consumed gas (any local solution would likely just mirror
this one).

This forms part of a broader, long-term direction of feature parity
between precompiled and bytecode contracts, exposed via
`vm.PrecompileEnvironment`.

The `vm.Contract.Value()` method is also exposed as a natural
accompaniment to this change.

## How this works

The original signature for `vm.PrecompiledStatefulContract` received the
amount of gas available and then returned the amount remaining; this has
been removed in lieu of exposing the existing `vm.Contract.UseGas()`
method via the `vm.PrecompileEnvironment`. A new `legacy` package wraps
the old signature and converts it to a new one so `ava-labs/coreth`
doesn't need to be refactored.

```go
func oldPrecompile(env vm.PrecompileEnvironment, input []byte, gas uint64) ([]byte, uint64, error) {
  // ...
  if err != nil {
    return nil, gas - gasCost, err // pattern susceptible to bugs; should it be `nil, gas, err` ?
  }
  // ...
  return output, gas - gasCost, nil  
}

func newPrecompile(env vm.PrecompileEnvironment, input []byte) ([]byte, error) {
  // The original `gas` argument is still available as `env.Gas()`
  // ...
  if !env.UseGas(gasCost) { // an explicit point at which gas is consumed
    return nil, vm.ErrOutOfGas
  }
  // ...
  if err != nil {
    return nil, err
  }
  // ...
  return output, nil
}
```

## How this was tested

Existing unit test modified to use the `legacy` adaptor.

---------

Signed-off-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
Co-authored-by: Quentin McGaw <quentin.mcgaw@gmail.com>
2024-12-18 13:54:51 +00:00
..
runtime [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
testdata crypto/kzg4844: use the new trusted setup file and format (#28383) 2023-10-22 16:05:04 +02:00
analysis.go core/vm: clarify comment (#27045) 2023-04-04 02:59:40 -04:00
analysis_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
common.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
contract.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
contracts.go refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73) 2024-12-18 13:54:51 +00:00
contracts.libevm.go refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73) 2024-12-18 13:54:51 +00:00
contracts.libevm_test.go refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73) 2024-12-18 13:54:51 +00:00
contracts_fuzz_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
contracts_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
doc.go core/vm: remove JIT VM codes (#16362) 2018-03-26 13:48:04 +03:00
eips.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
environment.libevm.go refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73) 2024-12-18 13:54:51 +00:00
errors.go core/vm: implement EIP-3860: Limit and meter initcode (#23847) 2023-01-11 04:05:47 -05:00
evm.go Merge branch 'auto-rename-module_source-2bd6bd01d2e8561dd7fc21b631f4a34ac16627a1_workflow-c1fc594f020d23958b641a4e5a856b6e52c49d3bece94b95594864db16c1b0fc-main' into arr4n/rename-module 2024-10-17 13:07:28 +11:00
evm.libevm_test.go Merge branch 'auto-rename-module_source-2bd6bd01d2e8561dd7fc21b631f4a34ac16627a1_workflow-c1fc594f020d23958b641a4e5a856b6e52c49d3bece94b95594864db16c1b0fc-main' into arr4n/rename-module 2024-10-17 13:07:28 +11:00
gas.go core/vm: use uint256 in EVM implementation (#20787) 2020-06-08 15:24:40 +03:00
gas_table.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
gas_table_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
hooks.libevm.go Merge branch 'auto-rename-module_source-2bd6bd01d2e8561dd7fc21b631f4a34ac16627a1_workflow-c1fc594f020d23958b641a4e5a856b6e52c49d3bece94b95594864db16c1b0fc-main' into arr4n/rename-module 2024-10-17 13:07:28 +11:00
instructions.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
instructions_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
interface.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
interpreter.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
interpreter_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
jump_table.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
jump_table_export.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
jump_table_test.go all: fix typos in comments (#28881) 2024-02-05 22:16:32 +01:00
libevm_test.go refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73) 2024-12-18 13:54:51 +00:00
logger.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
memory.go core/vm: implement EIP-5656, mcopy instruction (#26181) 2023-07-11 03:55:34 -04:00
memory_table.go core/vm: implement EIP-5656, mcopy instruction (#26181) 2023-07-11 03:55:34 -04:00
memory_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
opcodes.go eth/tracers/js: fix isPush for push0 (#28520) 2023-11-14 13:14:38 +01:00
operations_acl.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
options.libevm.go refactor: abstract options package (#74) 2024-11-21 13:59:51 -08:00
stack.go core/vm: clean up some dead functions (#24851) 2022-05-11 08:03:35 +03:00
stack.libevm.go feat: override EVM.Reset() args (#36) 2024-09-25 15:03:36 +01:00
stack.libevm_test.go Merge branch 'auto-rename-module_source-2bd6bd01d2e8561dd7fc21b631f4a34ac16627a1_workflow-c1fc594f020d23958b641a4e5a856b6e52c49d3bece94b95594864db16c1b0fc-main' into arr4n/rename-module 2024-10-17 13:07:28 +11:00
stack_table.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00