go-ethereum/core
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
..
asm [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
bloombits [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
forkid [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
rawdb [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
state feat: types.HeaderHooks for RLP overrides (#89) 2024-12-17 13:54:58 +00:00
txpool [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
types feat: internalise command (#90) 2024-12-17 17:20:02 +01:00
vm refactor: PrecompileEnvironment.{,Use,Refund}Gas() in lieu of args (#73) 2024-12-18 13:54:51 +00:00
.gitignore Renamed chain => core 2014-12-04 10:28:02 +01:00
bench_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
block_validator.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
block_validator_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain_insert.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain_reader.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain_repair_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain_sethead_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain_snapshot_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blockchain_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
blocks.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
bloom_indexer.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
chain_indexer.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
chain_indexer_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
chain_makers.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
chain_makers_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
dao_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
error.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
events.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00: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
forkchoice.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
gaspool.go core, miner: revert block gas counter in case of invalid transaction (#26799) 2023-03-07 05:23:52 -05:00
gen_genesis.go refactor: generate internal Header.encodeRLP() for override (#86) 2024-12-11 10:20:58 +00:00
genesis.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
genesis_alloc.go cmd, core, params: add support for the Holesky testnet (#28007) 2023-08-25 18:11:40 +03:00
genesis_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
headerchain.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
headerchain_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
mkalloc.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
rlp_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
sender_cacher.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
state_prefetcher.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
state_processor.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
state_processor_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
state_transition.go Merge branch 'auto-rename-module_source-2bd6bd01d2e8561dd7fc21b631f4a34ac16627a1_workflow-c1fc594f020d23958b641a4e5a856b6e52c49d3bece94b95594864db16c1b0fc-main' into arr4n/rename-module 2024-10-17 13:07:28 +11:00
state_transition.libevm.go chore: post-rename cleanup + libevm intro (#68) 2024-10-30 09:51:41 -04:00
state_transition.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
txindexer.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
txindexer_test.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00
types.go [AUTO] rename Go module + update internal import paths 2024-10-17 01:49:47 +00:00