Commit graph

15044 commits

Author SHA1 Message Date
Michael Kaplan
bb1e421e2b
rpc: avoid unnecessary RST_STREAM, PING frames sent by client (#33122)
Context from Cloudflare blog:
https://blog.cloudflare.com/go-and-enhance-your-calm/#reading-bodies-in-go-can-be-unintuitive

We were able to reproduce the same issue discussed by Cloudflare in
their recent blog post above using the `ethclient`.
2025-11-11 12:41:11 -05:00
Arran Schlosberg
1bccf4f2dd
refactor!: temporary extras require proof of global lock (#238)
## Why this should be merged

The `temporary.WithTempRegisteredExtras()` global lock introduced in
#234 wasn't fit for purpose when used in `coreth` as it required central
coordination of registration, types, and usage of the payload accessor.

## How this works

Instead of a central registration point, the new
`libevm.WithTemporaryExtrasLock()` function takes out a global lock and
provides the caller with a handle that proves the lock is held. All of
the override functions, e.g. `params.WithTempRegisteredExtras()` now
require a current lock, which will be propagated by the respective
`coreth` functions.

See https://github.com/ava-labs/coreth/pull/1328 for intended usage in
`coreth` and `subnet-evm`. A consumer of both of these can then safely
do the following:

```go
import (
    "github.com/ava-labs/libevm/libevm"

    coreth "github.com/ava-labs/coreth/plugin/evm"
    subnet "github.com/ava-labs/subnet-evm/plugin/evm"
)

// asCChain calls `fn` while emulating `coreth`. It is safe for concurrent usage with [asSubnetEVM].
func asCChain(fn func() error) error {
    return libevm.WithTemporaryExtrasLock(func(l libevm.ExtrasLock) error {
        return coreth.WithTempRegisteredLibEVMExtras(l, fn)
    })
}

// asSubnetEVM calls `fn` while emulating `subnet-evm`. It is safe for concurrent usage with [asCChain].
func asSubnetEVM(fn func() error) error {
    return libevm.WithTemporaryExtrasLock(func(l libevm.ExtrasLock) error {
        return subnet.WithTempRegisteredLibEVMExtras(l, fn)
    })
}
```

## How this was tested

Unit test of the new function plus existing integration tests of all
modified code.
2025-10-16 14:27:15 +00:00
Arran Schlosberg
51afedec91
chore: Go 1.24 (#232)
## Why this should be merged

Version increase across all `ava-labs` EVM-related code.

## How this works

Bump to 1.24.8 in both `go.mod` files + requisite version increase for
`golangci-lint`.

## How this was tested

Existing tests.

---------

Co-authored-by: Austin Larson <austin.larson@avalabs.org>
2025-10-13 14:07:33 -04:00
Ceyhun Onur
9ed36b613a
feat: Add support for named types with underlying basic types (#236)
## Why this should be merged

The current rlpgen partially supports named types with basic underlying,
and it generates the rlp without correct conversion i.e:
`w.WriteUint64(obj.Uint64NewT)`

This PR adds the full support it

## How this works

Adds check for `isNamedWithBasicUnderlying` and then returns a `op` with
`makeNamedBasicOp` which is basically a `basicOp` but with the `typ`
refers to the named type so that it can correctly encode decode with the
named type.

## How this was tested

Added UT tests

---------

Signed-off-by: Ceyhun Onur <ceyhunonur54@gmail.com>
2025-10-08 08:07:57 +01:00
Arran Schlosberg
910e897c54
feat: reentrancy.Guard for stateful precompiles (#212)
## Why this should be merged

The `vm.PrecompileEnvironment.Call()` method requires careful usage
because of reentrancy vulnerabilities (this is common to all outgoing
`CALL`s in the EVM, not just stateful precompiles). This package
provides a common method of protection, a reentrancy guard.

## How this works

Provides a function that returns `vm.ErrExecutionReverted` if called
twice, by the same contract, in the same transaction, with the same
identifier.

## How this was tested

Unit and integration tests.
2025-10-03 19:56:29 +00:00
Arran Schlosberg
35926db4d6
feat: temporary extras (#234)
## Why this should be merged

Registration of extras requires runtime enforcement of payload types. In
production, only a single set of types can be registered and any
attempts at re-registration will panic (by design). Although this makes
production usage safe, it doesn't allow downstream consumers (e.g. data
indexers) to use extras from different chains (e.g. `coreth` _and_
`subnet-evm`) at the same time.

## How this works

1. The `libevm/register.AtMostOnce` type can now be overridden
temporarily.
2. `params`, `core/types`, `core/vm`, and `state` packages introduce
`WithTempRegisteredExtras()` functions.
3. `libevm/temporary.WithRegisteredExtras()` provides "atomic" override
of all extras.

In all cases, the scope of the override is limited to the life of a
single function call.

## How this was tested

Relative to numbered list above:

1. Unit test of new and existing functionality.
2. Integration tests of both packages, demonstrating both payload and
behavioural override.
2025-10-02 17:42:26 +01:00
Arran Schlosberg
414b1f5dff
test: PrecompileEnvironment.Value() under DELEGATECALL (#216)
## Why this should be merged

Completeness.

## How this works

n/a

## How this was tested

Originally the `DELEGATECALL` test asserted that the `CALLVALUE` was
zero, but this didn't demonstrate that it was inherited (e.g.
`STATICCALL` always receives zero). The value sent to the caller of the
precompile is now non-zero, to precisely demonstrate correct behaviour
of all call types.
2025-09-25 15:19:03 +01:00
Jonathan Oppenheimer
7a59fddf77
build: update supranational/blst dependancy to 0.3.14 (#233)
## Why this should be merged
Allow `blst` module to compile with Go 1.24.
2025-09-25 08:40:44 +01:00
Arran Schlosberg
ba9ab538b9
feat: options.ApplyTo for default values (#225)
## Why this should be merged

Adds default-value support to the `options` package, which could
previously only create configured types from zero values.

## How this works

Exposes the option-application loop as the `ApplyTo()` function and
refactors `As()` to share this code path.

## How this was tested

Testable example acting as a unit test.
2025-09-09 15:11:33 +01:00
Michael Kaplan
4bc19e9f8f
Update feature.yml (#223)
## Why this should be merged
Non-unique IDs in feature-request template body.

## How this works
Makes IDs unique. See
[here](https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/common-validation-errors-when-creating-issue-forms#body-must-have-unique-ids).

## How this was tested

[Inspection of GitHub preview](https://github.com/ava-labs/libevm/blob/michaelkaplan13/fix-feature-issue-template/.github/ISSUE_TEMPLATE/feature.yml)

Signed-off-by: Michael Kaplan <55204436+michaelkaplan13@users.noreply.github.com>
2025-09-05 09:53:27 +01:00
Arran Schlosberg
72e9ad7962
feat: export P256Verify precompile (#222)
## Why this should be merged

Allows direct access to the P-256 verification precompile. Currently the
only alternative is to rely on the planned but not guaranteed address
that Ethereum will use.

## How this works

Embed the non-exported `p256verify` type in an exported struct to allow
its methods to be exposed.

## How this was tested

Run the test suite for `p256verify` on `P256Verify`.
2025-09-04 18:01:42 +00:00
Arran Schlosberg
e4220c7ed4
refactor: rawdb tests (#221)
## Why this should be merged

The need for examples to be in `package rawdb_test` made it difficult to
test other code that relied on non-exported `rawdb` identifiers.

## How this works

1. Move all testable examples from `database.libevm_test.go` to
`examples.libevm_test.go` _without any change_.
2. Change `database.libevm_test.go` to be in `package rawdb` and extend
the `WithSkipFreezers()` test.

## How this was tested

No change at all to testable examples. The `WithSkipFreezers()` test now
touches both the happy and (specific) error paths to demonstrate exact
behaviour (under the old approach it could have just been that there was
no error, even without the option).
2025-09-03 16:34:48 +01:00
rodrigo
f19cd58957
feat(rawdb): skip freezers (#220)
## Why this should be merged

As described in https://github.com/ava-labs/coreth/issues/1137, database
inspection of a Coreth chain database fails because `InspectDatabase()`
expects for freezers to be used. This PR fixes this bug by adding an
option to skip freezer inspection.

This PR should be followed with a downstream PR in Coreth to pass in an
option to skip freezer inspection during database inspection.

## How this works

Extends `inspectDatabaseConfig` with a `skipFreezers` field and adds the
associated option function for it.

## How this was tested

CI + ran `InspectDatabase()` against Coreth locally
2025-09-03 14:50:44 +01:00
Felix Lange
9e4c147bf2
core/vm: implement EIP-7951 - precompile for secp256r1 (#31991)
https://github.com/ethereum/EIPs/pull/9833

Based on #27540, #30043

---------

Co-authored-by: Ulaş Erdoğan <uerdogan2001@hotmail.com>
2025-08-13 16:39:36 -04:00
Jonathan Oppenheimer
ba21474eeb
Delete custom secp256 implementation (#215)
## Why this should be merged
This PR removes the custom secp256 implementation in a single, seperate
commit, as requested in https://github.com/ava-labs/libevm/pull/214
2025-08-12 10:39:07 -04:00
Arran Schlosberg
62dc2ea087
refactor: set EVM.readOnly and depth before running stateful precompile (#210)
## Why this should be merged

Improved clarity.

## How this works

Semantically equivalent refactor. These values aren't accessible /
needed during execution of a stateful precompile, but are important when
making outgoing calls so were originally only set in the
`PrecompileEnvironment.Call()` implementation. This was confusing and
led to a false-positive bug report.

## How this was tested

Existing tests.
2025-08-12 01:35:13 +01:00
Arran Schlosberg
e7035f19ee
feat!: disambiguate EVM-semantic and raw caller/self addresses for precompiles (#211)
## Why this should be merged

Provides precompiles with unambiguous access to contextual addresses,
without the consumer needing to understand how they change under
different call types.

## How this works

The `libevm.AddressContext` type, which used to carry 3 addresses, now
provides different versions of `Caller` and `Self`. The EVM-semantic
versions are as defined by the rules of the EVM (and available before
this change). The raw versions are the unmodified caller and self.

## How this was tested

Extension of existing UTs to include raw addresses in addition to
existing, EVM-semantic ones.
2025-08-07 23:48:32 +01:00
Austin Larson
e35febe777
feat: enable invalidating txs (#208)
## Why this should be merged

In case a tx's execution (specifically a registered precompile) should
be invalidated, this allows the EVM to find this error.

## How this works

Adds a setter and getter that can be called from a precompile.

## How this was tested

UT
2025-08-01 13:43:47 -04:00
Arran Schlosberg
464de82910
feat: state-key transformation w/ override (#205)
## Why this should be merged

`ava-labs/coreth` has a partitioned state-address space, achieved by
setting or clearing a specific bit in the hash used to key the space.
This change allows such behaviour to be achieved with pure `libevm`
instead of the `StateDB` wrapping that `coreth` currently uses.

## How this works

Introduction of `state.StateDBHooks` interface, including a
`TransformStateKey()` method that allows for arbitrary change of state
key. If registered, this hook will be honoured by
`StateDB.{Get,GetCommitted,State}Key()` methods unless they receive a
`stateconf.SkipStateKeyTransformation` option.

## How this was tested

Unit test of `SetState() -> GetState() + GetCommittedState()` round trip
with and without options to skip.
2025-07-22 13:08:53 -04:00
Stephen Buttolph
99f0d0b1de
Add *StateDB.TxHash for usage with Warp (#204)
## Why this should be merged

Coreth and Subnet-EVM require access to the current tx hash in the Warp
precompile (for selecting the correct predicate results).

This can not be exposed as a wrapper through the `OverrideEVMResetArgs`
because `SetTxContext` is called _prior_ to resetting the EVM instance.
Therefore, the `SetTxContext` function on any DB wrapper would never be
called.

## How this works

Exposes the existing `txHash` through a `TxHash()` function.

## How this was tested

Added a trivial unit test.
2025-07-21 22:20:20 -04:00
Martin HS
08e2b6da8a
crypto/kz4844: pass blobs by ref (#29050)
This change makes use of the following underlying changes to the kzg-libraries in order to avoid passing large things on the stack:

- c-kzg: https://github.com/ethereum/c-kzg-4844/pull/393 and
- go-kzg: https://github.com/crate-crypto/go-kzg-4844/pull/63
2025-06-25 11:14:37 +01:00
Marius van der Wijden
8ae463196c
crypto/secp256k1: update libsecp256k1 (#31242)
Updates the libsecp256k1 dependency to commit:
c0d9480fbbf8eccbd4be23ed27f6f2af6f3b211e

PR:
```
BenchmarkSign-24    	   57756	     21214 ns/op	     164 B/op	       3 allocs/op
BenchmarkRecover-24    	   37156	     33044 ns/op	      80 B/op	       1 allocs/op
BenchmarkEcrecoverSignature-24    	   36889	     32935 ns/op	      80 B/op	       1 allocs/op
BenchmarkVerifySignature-24    	   41163	     29207 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecompressPubkey-24    	  318624	      4062 ns/op	     304 B/op	       6 allocs/op
```

Master:
```
BenchmarkSign-24    	   34509	     35330 ns/op	     164 B/op	       3 allocs/op
BenchmarkRecover-24    	   25418	     47725 ns/op	      80 B/op	       1 allocs/op
BenchmarkEcrecoverSignature-24    	   25735	     47591 ns/op	      80 B/op	       1 allocs/op
BenchmarkVerifySignature-24    	   29108	     41097 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecompressPubkey-24    	  294747	      4143 ns/op	     304 B/op	       6 allocs/op
```

Performance seems to be improved significantly:
```
Sign-24      34.86µ ± 3%   21.66µ ± 2%  -37.86% (p=0.000 n=10)
Recover-24   46.14µ ± 3%   33.24µ ± 2%  -27.95% (p=0.000 n=10)
```
2025-06-23 16:52:59 +01:00
Arran Schlosberg
ff3977602d
chore: yamllint ignore all of upstream crypto/secp256k1 2025-06-23 16:52:58 +01:00
Arran Schlosberg
d4c22ca34b
chore: stochastic gradient descent to fix goheader (#196)
## Why this should be merged

Avoids false positives from `goheader`.

## How this works

Checks out entire history to give the linter a reference point for new
issues only.

## How this was tested

With great frustration by waiting for CI.
2025-06-23 15:52:20 +00:00
Arran Schlosberg
f850ca6bfe
chore: placate goheader (#195)
## Why this should be merged

`goheader` linter apparently hasn't been running but now decided to and
is blocking another PR. It's not worth the time to investigatie why
(probably a version update).

## How this works

Update header comments as required.

## How this was tested

N/A
2025-06-23 15:25:18 +00:00
Arran Schlosberg
263186c08f
chore: bump libevm minor version to 0.3.0 (#192)
## Why this should be merged

Addition of recent, functionality constitutes a bump to the minor
version. Doing this on `main` first for consistency and will cut a
release candidate after.

## How this works

Magic

## How this was tested

Super magic
2025-06-20 09:31:36 -04:00
Austin Larson
9b97d60230
feat: triedb.Database.Update options via statedb.Commit (#190)
## Why this should be merged

To allow more thorough handling of duplicate state roots (or any other
info other users would like), additional information can be provided to
a call of `statedb.Commit`. This change allows arbitrary types to be
sent to `triedb` as well as the `SnapshotTree`. However, this is a
breaking change for those using the functionality already, since the
snapshot commit option is wrapped with another call.

## How this works

See the edited libevm test for usage.

## How this was tested

Edited test case to include `TrieDBUpdateOption` and ensures the payload
is sent.
2025-06-18 17:54:37 +00:00
Stephen Buttolph
ab5ad25dc3
crypto/bn256/cloudflare: pull in upstream fix for R27 and R29 usage (#32057)
Pulls in https://github.com/cloudflare/bn256/pull/48 to remove usage of
R27 and R29 [which are reserved](https://go.dev/doc/asm#arm64).
2025-06-18 10:48:15 -04:00
Arran Schlosberg
0bfe4a0380
chore: fix linter issues that CI missed (#188)
## Why this should be merged

#185 was allowed to merge despite CI requiring the `lint` job.

## How this works

Fix linter issues.

## How this was tested

The first commit is an empty commit, expected to fail CI, to confirm
that the job is in fact required.
2025-06-11 21:06:26 +01:00
Arran Schlosberg
bedfd12e2d
feat: RulesHooks.MinimumGasConsumption (#185)
## Why this should be merged

Required for
[ACP-194](https://github.com/avalanche-foundation/ACPs/tree/main/ACPs/194-streaming-asynchronous-execution#gas-charged)
$\lambda$ bound on gas consumption.

## How this works

Hook into `core.StateTransition.TransitionDb()` as this is the bottom of
all execution paths (e.g. `core.ApplyTransaction()` as used in SAE,
`core.StateProcessor.Process(*Block,...)`, etc.). Once consumed gas is
no longer changing (i.e. after all spends and refunds), the transaction
limit is passed to the hook to determine the minimum consumption, which
is applied.

## How this was tested

Unit test via `core.ApplyTransaction()` as this is our entry point in
SAE.

---------

Signed-off-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
2025-06-11 18:22:33 +00:00
Arran Schlosberg
2672fbd7cd
feat: RIP-7212 precompile for P256 ECDSA verification (#184)
## Why this should be merged

Implementation for
[ACP-204](https://github.com/avalanche-foundation/ACPs/pull/204).

## How this works

Implements
[RIP-7212](https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md)
and an additional `Pack(hash,r,s,key)` function with
precompile-compatible output.

## How this was tested

Unit tests including fuzzing, test case from the proposed but unmerged
geth implementation, and vectors from [Project
Wycheproof](https://github.com/C2SP/wycheproof).
2025-06-10 10:28:02 -04:00
Arran Schlosberg
fd03f3ac29
chore: deprecate cherry-picking on release (#186)
## Why this should be merged

Our old strategy of only cherry-picking on release branches made the
`main` branch incompatible with dependent repos.

## How this works

The goal of the old strategy was to avoid cherry-picking on the same
branch to which we would later merge the duplicated, upstream commit.
This can instead be achieved by tracking the cherry-picked commits and
reverting them as part of the geth sync. Since we already do this for
our own changes and a single cherry-pick (see #128), there's no need to
use the two approaches simultaneously.

This PR deletes the cherry-picking mechanism and removes the
release-branch test that enforced its proper usage. It will be followed
up by a series of PRs, one per cherry-pick that would have otherwise
been placed on release branches.

## How this was tested

n/a
2025-06-10 15:18:42 +01:00
Sina M
7b6ff3e0ff
internal/ethapi: fix panic in debug methods (#31157)
Fixes an error when the block is not found in debug methods.
2025-06-10 15:08:41 +01:00
Felix Lange
955616d7c2
crypto: add IsOnCurve check (#31100) 2025-06-10 15:08:40 +01:00
maskpp
7688f988c2
core/state/snapshot: add a missing lock (#30001)
* upgrade lock usage

* revert unnecessary change
2025-06-10 15:08:40 +01:00
Nathan
7c5a8086a7
eth/gasprice: add query limit for FeeHistory to defend DDOS attack (#29644)
* eth/gasprice: add query limit for FeeHistory to defend DDOS attack

* fix return values after cherry-pick

---------

Co-authored-by: Eric <45141191+zlacfzy@users.noreply.github.com>
2025-06-10 15:08:39 +01:00
Nathan
b570d5ee0c
params: print time value instead of pointer in ConfigCompatError (#29514) 2025-06-10 15:08:38 +01:00
Aaron Chen
9f0e1f8da1
core/state: fix bug in statedb.Copy and remove unnecessary preallocation (#29563)
This change removes an unnecessary preallocation and fixes a flaw with no-op copies of some parts of the statedb
2025-06-10 15:08:38 +01:00
Shiming Zhang
8580388508
internal/build, rpc: add missing HTTP response body Close() calls (#29223)
Co-authored-by: Felix Lange <fjl@twurst.com>
2025-06-10 15:08:34 +01:00
Austin Larson
7342b55267
feat(triedb): expose backend for custom use (#183)
## Why this should be merged

To override the standard trie behavior for custom databases in
`core/state/database.go`, custom methods may need to be defined on the
backend database.

## How this works

Exposes `backend` interface for the API

## How this was tested

No tests necessary - isn't used in `libevm`.
2025-05-30 14:29:56 -04:00
Arran Schlosberg
2d94327f8d
feat: partially expose internal/ethapi package (#182)
## Why this should be merged

Exposes API implementations for use by SAE.

## How this works

Type aliases and constructor propagation.

## How this was tested

N/A
2025-05-27 10:29:35 -04:00
Arran Schlosberg
c2e6df1b99
refactor(core/vm): expose clearing of registered hooks in tests (#181)
## Why this should be merged

Importing some `avalanchego` test fixtures automatically imports
`coreth` packages that register extras, which affects testing of other
VMs that depend on `libevm`. All other registered extras can be cleared
with the pattern in this PR.

## How this works

Uses the `register.AtMostOnce` type already used in `params` and
`types`. I forgot to update the `vm` package when implementing the type.

## How this was tested

Existing unit tests.
2025-04-16 14:05:45 +01:00
Stephen Buttolph
86a2fa8836
Verify key length in range proofs (#176)
## Why this should be merged

This enforces an assumption that is currently made by
`VerifyRangeProof`. If a key is a prefix of another key, the current
code can panic.

## How this works

Enforces all keys have the same length.

## How this was tested

Added a unit test.
2025-04-10 14:14:42 -04:00
Quentin McGaw
d7bb4f6050
feat(core/types): export rlpHash() (#174)
#163 was removed by mistake in #162 so this re-adds it

Co-authored-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
2025-03-27 16:32:41 +01:00
Quentin McGaw
979064cfdb
fix(ci): go_tooling CI job git checkout tip of PR (#173)
## Why this should be merged

The release-branch CI job tests branch properties of the `git` DAG but
GitHub's `actions/checkout` by default creates a speculative merge
commit against which PR CI is run. Running against this hypothetical
situation makes the tests fail.

## How this works

[`actions/checkout` uses PR
tip](https://github.com/actions/checkout?tab=readme-ov-file#checkout-pull-request-head-commit-instead-of-merge-commit)

## How this was tested

Future run against a release branch.

---------

Signed-off-by: Quentin McGaw <quentin.mcgaw@gmail.com>
2025-03-26 11:29:45 +01:00
Quentin McGaw
7b930f19b7
fix(libevm/tooling/release): small cherrypick.sh fixes (#171)
- Use grep `-E` flag instead of `-P` flag so it works on all platforms
- Add `-S` flag to `git cherry-pick` since it doesn't always sign all commits after resolving conflicts
2025-03-26 11:26:10 +01:00
Arran Schlosberg
1d75a11657
chore: release-branch test logs HEAD (#172)
## Why this should be merged

Debugging release-branch test failures in CI but not locally.

## How this works

Logs `HEAD` before testing branch properties.

## How this was tested

NA
2025-03-26 09:59:35 +00:00
Quentin McGaw
319a92458d
feat(params): export newTimestampCompatError with alias NewTimestampCompatError (#168)
Signed-off-by: Quentin McGaw <quentin.mcgaw@gmail.com>
Co-authored-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
2025-03-25 20:40:27 +01:00
Quentin McGaw
87a2d57b96
chore!(triedb): remove config argument for DBConstructor (#167) 2025-03-25 11:22:59 +01:00
Arran Schlosberg
7be6bee7ab
chore: enable metrics by default (#165)
## Why this should be merged

The `metrics.Enabled` global is misleadingly named and disables
_constructors_, not individual metrics when they're used. This resulted
in all `ava-labs/coreth` metrics being no-ops because it was only set to
`true` after they were all constructed. As the majority of metrics are
globals, they're created during package initialisation so simply setting
`metrics.Enabled = true` somewhere in `coreth` isn't a guarantee of
ordering. The old `coreth/metrics` package simply set the global to
`true` by default.

## How this works

`metrics.Enabled = true` at the point of declaration.

## How this was tested

Local inspection of metrics on a running binary.
2025-03-20 16:24:22 +01:00