-
Notifications
You must be signed in to change notification settings - Fork 814
feat: emulate package for temporary libevm registration
#4430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ARR4N
wants to merge
13
commits into
master
Choose a base branch
from
arr4n/emulate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−32
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f144f6d
feat: `emulate` package for temporary `libevm` registration
ARR4N 247b95c
chore: copyright headers
ARR4N 815d804
chore: I hate `golangci-lint`; tell me how to fix the problem!!!
ARR4N f47d43b
Merge branch 'master' into arr4n/emulate
ARR4N 9a7dbde
chore: fix typo
ARR4N c16f48e
refactor: split function args over new lines
ARR4N 676e08c
refactor: only using concrete type constraints
ARR4N 9324ff5
Merge branch 'master' into arr4n/emulate
ARR4N 77d36fe
Merge branch 'master' into arr4n/emulate
ARR4N 49e1156
chore: bump `subnet-evm`
ARR4N 3b008c7
chore: bump `subnet-evm`
ARR4N 38acae4
Merge branch 'master' into arr4n/emulate
ARR4N c13e4a4
chore: `gofmt`
ARR4N File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| // Package emulate provides temporary emulation of coreth (C-Chain) and | ||
| // subnet-evm (EVM L1) behaviours. All functions are safe for concurrent use | ||
| // with each other, but all hold the same mutex so their execution SHOULD be | ||
| // short-lived. | ||
| package emulate | ||
|
|
||
| import ( | ||
| cchain "github.com/ava-labs/coreth/plugin/evm" | ||
| subnet "github.com/ava-labs/subnet-evm/plugin/evm" | ||
| ) | ||
|
|
||
| // CChain executes `fn` as if running in a `coreth` node. | ||
| func CChain(fn func() error) error { | ||
| return cchain.WithTempRegisteredLibEVMExtras(fn) | ||
| } | ||
|
|
||
| // SubnetEVM executes `fn` as if running in a `subnet-evm` node. | ||
| func SubnetEVM(fn func() error) error { | ||
| return subnet.WithTempRegisteredLibEVMExtras(fn) | ||
| } | ||
|
|
||
| // CChainVal executes `fn` as if running in a `coreth` node. | ||
| func CChainVal[T any](fn func() (T, error)) (T, error) { | ||
| return val(CChain, fn) | ||
| } | ||
|
|
||
| // SubnetEVMVal executes `fn` as if running in a `subnet-evm` node. | ||
| func SubnetEVMVal[T any](fn func() (T, error)) (T, error) { | ||
| return val(SubnetEVM, fn) | ||
| } | ||
|
|
||
| func val[T any]( | ||
| wrap func(func() error) error, | ||
| fn func() (T, error), | ||
| ) (T, error) { | ||
| var v T | ||
| err := wrap(func() error { | ||
| var err error | ||
| v, err = fn() | ||
| return err | ||
| }) | ||
| return v, err | ||
| } |
This file contains hidden or 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,88 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package emulate | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/ava-labs/libevm/core/types" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| cchain "github.com/ava-labs/coreth/plugin/evm/customtypes" | ||
| subnet "github.com/ava-labs/subnet-evm/plugin/evm/customtypes" | ||
| ) | ||
|
|
||
| // setAndGetMillis is an arbitrary function that can be run if and only if | ||
| // emulating either `coreth` or `subnet-evm`. If the respective emulation isn't | ||
| // active then it will cause `libevm` to panic. In addition to the panicking | ||
| // behaviour, it asserts that it is the only active emulation. | ||
| func setAndGetMillis[T *cchain.HeaderExtra | *subnet.HeaderExtra]( | ||
| t *testing.T, | ||
| active *atomic.Int64, | ||
| withExtra func(*types.Header, T) *types.Header, | ||
| extra T, | ||
| blockMillis func(*types.Block) *uint64, | ||
| retErr error, | ||
| ) func() (*uint64, error) { | ||
| return func() (*uint64, error) { | ||
| require.True(t, active.CompareAndSwap(0, 1)) | ||
| defer func() { | ||
| require.True(t, active.CompareAndSwap(1, 0)) | ||
| }() | ||
|
|
||
| b := types.NewBlockWithHeader(withExtra( | ||
| &types.Header{}, | ||
| extra, | ||
| )) | ||
| return blockMillis(b), retErr | ||
| } | ||
| } | ||
|
|
||
| func TestEmulation(t *testing.T) { | ||
| const milli = uint64(1234) | ||
| newUint64 := func(u uint64) *uint64 { return &u } | ||
| sentinel := errors.New("uh oh") | ||
|
|
||
| var active atomic.Int64 | ||
| onCChain := setAndGetMillis( | ||
| t, &active, | ||
| cchain.WithHeaderExtra, | ||
| &cchain.HeaderExtra{TimeMilliseconds: newUint64(milli)}, | ||
| cchain.BlockTimeMilliseconds, | ||
| sentinel, | ||
| ) | ||
| onSubnetEVM := setAndGetMillis( | ||
| t, &active, | ||
| subnet.WithHeaderExtra, | ||
| &subnet.HeaderExtra{TimeMilliseconds: newUint64(milli)}, | ||
| subnet.BlockTimeMilliseconds, | ||
| sentinel, | ||
| ) | ||
|
|
||
| start := make(chan struct{}) | ||
|
|
||
| t.Run("coreth", func(t *testing.T) { | ||
| t.Parallel() | ||
| <-start | ||
| for range 1000 { | ||
| got, err := CChainVal(onCChain) | ||
| require.ErrorIs(t, err, sentinel) | ||
| require.Equal(t, milli, *got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("subnet-evm", func(t *testing.T) { | ||
| t.Parallel() | ||
| <-start | ||
| for range 1000 { | ||
| got, err := SubnetEVMVal(onSubnetEVM) | ||
| require.ErrorIs(t, err, sentinel) | ||
| require.Equal(t, milli, *got) | ||
| } | ||
| }) | ||
|
|
||
| close(start) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.