Skip to content

Atomic VM Refactor #885

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

Draft
wants to merge 166 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 143 commits
Commits
Show all changes
166 commits
Select commit Hold shift + click to select a range
bc26c83
new atomic pkg
ceyonur Dec 11, 2024
fe45c37
Merge branch 'master' into move-atomic-txs
ceyonur Dec 11, 2024
9c3e284
bump avago
ceyonur Dec 11, 2024
ddbbab7
bump versions
ceyonur Dec 11, 2024
0ecd789
move attomic gossip
ceyonur Dec 13, 2024
019bb04
lint
ceyonur Dec 14, 2024
aa50ce6
change newimport clk to time
ceyonur Dec 15, 2024
52081a9
move utils
ceyonur Dec 15, 2024
d93257b
remove extra heaps files
ceyonur Dec 15, 2024
0b10cc4
move database to own pkg
ceyonur Dec 15, 2024
8eb3056
move atomic trie/state/backend to separaet pkg
ceyonur Dec 18, 2024
aad2f5f
remove multicoin/deprecated apis
ceyonur Dec 19, 2024
397c535
Merge branch 'seperate-atomic-pkg-base' into move-atomic-txs
ceyonur Dec 23, 2024
1815741
bump avago
ceyonur Dec 23, 2024
2b13c8e
bump avago
ceyonur Dec 23, 2024
2798ad6
go mod tidy
ceyonur Dec 23, 2024
6bf2ff2
Merge branch 'move-atomic-txs' into move-atomic-gossip
ceyonur Dec 23, 2024
278e055
update releases md
ceyonur Dec 23, 2024
e5f2c27
use address methods from avago
ceyonur Dec 24, 2024
71babe6
bump avago
ceyonur Dec 24, 2024
aab47b6
bump e2e avago version
ceyonur Dec 24, 2024
b9b67bc
Merge branch 'move-atomic-txs' into move-atomic-gossip
ceyonur Dec 24, 2024
6f1adc8
Update plugin/evm/atomic/gossip_test.go
ceyonur Dec 24, 2024
876716f
Update plugin/evm/atomic/mempool.go
ceyonur Dec 24, 2024
682e4bc
Update plugin/evm/config/config.go
ceyonur Dec 24, 2024
38ad868
Update plugin/evm/atomic/tx_heap.go
ceyonur Dec 24, 2024
de328e9
Update plugin/evm/config/config.go
ceyonur Dec 24, 2024
55c0be9
fix reviews
ceyonur Dec 24, 2024
3ac1a1a
Merge branch 'move-atomic-gossip' of github.com:ava-labs/coreth into …
ceyonur Dec 24, 2024
f17d97c
decouple atomic logic from statesyncers
ceyonur Dec 28, 2024
b5777f4
remove unused provider
ceyonur Dec 28, 2024
9c148ff
Merge branch 'move-atomic-gossip' into move-atomic-state
ceyonur Dec 29, 2024
803e50a
fix linter
ceyonur Dec 29, 2024
d45cd2e
Merge branch 'move-atomic-state' into move-atomic-sync
ceyonur Dec 29, 2024
0b40185
nits
ceyonur Dec 29, 2024
4ef96e8
Update plugin/evm/vm.go
ceyonur Dec 30, 2024
caa305d
Update plugin/evm/vm.go
ceyonur Dec 30, 2024
104bab7
Merge branch 'move-atomic-state' into move-atomic-sync
ceyonur Dec 30, 2024
62479e3
Merge branch 'move-atomic-sync' of github.com:ava-labs/coreth into mo…
ceyonur Dec 30, 2024
2f1dbc5
Update plugin/evm/vm_test.go
ceyonur Jan 3, 2025
936bb59
Update plugin/evm/syncervm_test.go
ceyonur Jan 3, 2025
4019a34
review fix
ceyonur Jan 3, 2025
266a55f
Merge branch 'move-atomic-state' of github.com:ava-labs/coreth into m…
ceyonur Jan 3, 2025
f651bad
Reviews
ceyonur Jan 6, 2025
39da24e
Merge branch 'move-atomic-state' into move-atomic-sync
ceyonur Jan 6, 2025
7598938
Merge branch 'master' into seperate-atomic-pkg-base
ceyonur Jan 6, 2025
1958ee8
Merge branch 'seperate-atomic-pkg-base' into move-atomic-state
ceyonur Jan 6, 2025
6d54408
Merge branch 'move-atomic-state' into move-atomic-sync
ceyonur Jan 6, 2025
705da44
Merge branch 'move-atomic-state' into remove-deprecated-apis
ceyonur Jan 7, 2025
460264c
fix tests
ceyonur Jan 7, 2025
9d1af78
update releases md
ceyonur Jan 7, 2025
8186291
Merge branch 'move-atomic-state' into remove-deprecated-apis
ceyonur Jan 7, 2025
b12462a
Merge pull request #709 from ava-labs/move-atomic-state
ceyonur Jan 7, 2025
ab526d4
Update RELEASES.md
ceyonur Jan 7, 2025
06e5064
Update plugin/evm/vm_test.go
ceyonur Jan 7, 2025
7aad0ec
replace deprecated Add64
ceyonur Jan 7, 2025
40f9827
fix ineff assign
ceyonur Jan 7, 2025
f328a6c
Merge branch 'seperate-atomic-pkg-base' into remove-deprecated-apis
ceyonur Jan 7, 2025
11bc6e8
merge master to atomic base branch (#744)
ceyonur Jan 14, 2025
22591f3
Merge branch 'seperate-atomic-pkg-base' into move-atomic-sync
ceyonur Jan 14, 2025
29cc131
Merge branch 'seperate-atomic-pkg-base' into remove-deprecated-apis
ceyonur Jan 15, 2025
072dcf1
Revert removed test
ceyonur Jan 15, 2025
7a3b1b7
Reviews
ceyonur Jan 15, 2025
f56376a
move node types and handlings to separate packages (#729)
ceyonur Jan 16, 2025
dfd2e9b
create new codec with correct type
ceyonur Jan 16, 2025
1ab7a7a
unexport codec
ceyonur Jan 16, 2025
152317d
reviews
ceyonur Jan 17, 2025
bd65190
Merge pull request #752 from ava-labs/sync-type-codec
ceyonur Jan 17, 2025
492b3dd
use concrete returns / callsite interfaces where possible (#756)
Jan 17, 2025
c985df7
Merge pull request #723 from ava-labs/move-atomic-sync
ceyonur Jan 20, 2025
ba22dcc
Merge branch 'master' into merge-master-atomic-base
ceyonur Jan 20, 2025
6bf036c
remove unused var
ceyonur Jan 20, 2025
457a67c
Merge pull request #757 from ava-labs/merge-master-atomic-base
ceyonur Jan 20, 2025
b62ee48
Merge branch 'seperate-atomic-pkg-base' into remove-deprecated-apis
ceyonur Jan 20, 2025
551bd96
remove newImportTx
ceyonur Jan 20, 2025
ae80881
wrap atomic vm
ceyonur Jan 20, 2025
f75f052
set network codec
ceyonur Jan 20, 2025
96eb3ed
unexport inner evm
ceyonur Jan 20, 2025
84ddc34
Update plugin/evm/atomic/vm/vm.go
ceyonur Jan 21, 2025
7ba0e38
Update plugin/evm/atomic/vm/vm.go
ceyonur Jan 21, 2025
1b5c84e
Update plugin/evm/vm.go
ceyonur Jan 21, 2025
90e248c
Update plugin/evm/vm.go
ceyonur Jan 21, 2025
28702f3
nits
ceyonur Jan 21, 2025
709d51c
Merge pull request #711 from ava-labs/remove-deprecated-apis
ceyonur Jan 21, 2025
d3c9753
Merge branch 'atomic-vm-wrapper' of github.com:ava-labs/coreth into a…
ceyonur Jan 21, 2025
75f6b0e
revert extra change
ceyonur Jan 21, 2025
d2a2418
add extension config
ceyonur Jan 22, 2025
1dcfa22
move with interfaces
ceyonur Jan 28, 2025
52d78cd
use visitors
ceyonur Feb 4, 2025
c61e190
fix tests
ceyonur Feb 5, 2025
6776c19
fix linter
ceyonur Feb 5, 2025
f9f54ef
move to factory pkg
ceyonur Feb 5, 2025
b754ffa
change main function
ceyonur Feb 5, 2025
58a2bb8
bump avago
ceyonur Feb 5, 2025
d926bb8
Merge branch 'master' into seperate-atomic-pkg-base
ceyonur Feb 6, 2025
ed2b36f
Merge branch 'seperate-atomic-pkg-base' of github.com:ava-labs/coreth…
ceyonur Feb 6, 2025
92f1abe
Merge branch 'seperate-atomic-pkg-base' into atomic-vm-refactor
ceyonur Feb 6, 2025
f16b85b
refactor: unexport commit from atomic trie
Feb 7, 2025
54e8801
few nits
ceyonur Feb 7, 2025
ce08bac
fix ext config validate
ceyonur Feb 7, 2025
264ddbb
update releases.md
ceyonur Feb 7, 2025
626b319
Merge branch 'atomic-vm-refactor' into atomic-vm-refactor-unexport-co…
Feb 8, 2025
4f69bfc
refactor: unexport commit from atomic trie (#782)
ceyonur Feb 10, 2025
c388601
rename vmerrs to coreerors/move vmerrors to plugin
ceyonur Feb 10, 2025
7f9b894
revert format changes
ceyonur Feb 10, 2025
d54a539
reviews
ceyonur Feb 10, 2025
da82817
nits and reviews
ceyonur Feb 11, 2025
90893c2
fix unsaved changes
ceyonur Feb 11, 2025
f9f4824
set block eth upgrades
ceyonur Feb 11, 2025
6145546
enable dao in test chain configs
ceyonur Feb 11, 2025
c372ec8
simplify: remove blockManager type (#786)
Feb 11, 2025
f4b2a10
remove block manager and move to block
ceyonur Feb 11, 2025
9d7bfc7
bump avalanchego dep
ceyonur Feb 11, 2025
3eb68c6
bump e2e version
ceyonur Feb 11, 2025
9a5e7d3
Merge branch 'atomic-vm-refactor' into remove-block-manager
ceyonur Feb 11, 2025
7dcfc6a
remove block manager and move to block (#787)
ceyonur Feb 11, 2025
a0767d3
Update plugin/evm/vm.go
ceyonur Feb 12, 2025
4634917
Update plugin/evm/vm.go
ceyonur Feb 12, 2025
a30fae0
rename onerror
ceyonur Feb 12, 2025
7a79b9e
move set extension fn above
ceyonur Feb 12, 2025
d6f4ca1
Merge branch 'atomic-vm-refactor' of github.com:ava-labs/coreth into …
ceyonur Feb 12, 2025
d83860b
reorder/fix semantic verify
ceyonur Feb 13, 2025
b6bc1bb
nits
ceyonur Feb 14, 2025
314f447
remove unused err
ceyonur Feb 14, 2025
9a47375
Merge commit '2e4e6bd' into seperate-atomic-pkg-base
ceyonur Feb 20, 2025
5c0c97f
Merge branch 'master' into seperate-atomic-pkg-base
ceyonur Feb 21, 2025
cfdd80b
fix ap3 import
ceyonur Feb 21, 2025
e82419f
Merge branch 'seperate-atomic-pkg-base' into atomic-vm-refactor
ceyonur Feb 24, 2025
950e83a
reintroduce ap3 check in tip
ceyonur Feb 24, 2025
2a06d97
Merge branch 'atomic-vm-refactor' into libevm-atomic-refactor-2
ceyonur Mar 24, 2025
1c927eb
fix test and extra config
ceyonur Mar 25, 2025
db6e2a3
remove todo
ceyonur Mar 25, 2025
e79f334
bump avalanchego dep to atomic-refactor compatible version
ceyonur Mar 25, 2025
efb2ae6
reviews from 768
ceyonur Mar 27, 2025
69a09d4
Merge branch 'libevm' into libevm-atomic-refactor-2
ceyonur Mar 27, 2025
1c43fe5
bump avago
ceyonur Mar 27, 2025
46ea1e4
go mod tidy
ceyonur Mar 27, 2025
0907904
Merge branch 'libevm' into libevm-atomic-refactor-2
ceyonur Apr 8, 2025
5f7ad88
Merge branch 'libevm-atomic-refactor-2' of github.com:ava-labs/coreth…
ceyonur Apr 8, 2025
2f02ab7
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur Apr 19, 2025
d476769
update avalanchego
ceyonur Apr 20, 2025
f3871f6
update action
ceyonur Apr 20, 2025
902d827
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur Apr 22, 2025
c7cd72f
Update plugin/evm/atomic/state/atomic_trie.go
ceyonur May 1, 2025
bb583c6
use block extension for wrapped blocks
ceyonur May 2, 2025
47c03b7
call innerVM functions implicitly
ceyonur May 2, 2025
2446b6d
Merge branch 'libevm-atomic-refactor-2' of github.com:ava-labs/coreth…
ceyonur May 2, 2025
c7e0876
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur May 2, 2025
b8979ba
remove todos
ceyonur May 2, 2025
1ca1e49
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur May 5, 2025
f5031aa
update ci version
ceyonur May 5, 2025
d799f6a
use jq to get sha
ceyonur May 5, 2025
f84ad70
remove echo
ceyonur May 5, 2025
a6e7c84
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur May 14, 2025
168c9e1
go mod tidy
ceyonur May 14, 2025
d35e17b
fix linter
ceyonur May 14, 2025
15a6a80
update Ci monitor
ceyonur May 14, 2025
9b69e79
move leaf handler
ceyonur May 14, 2025
6522bdf
Merge branch 'libevm-atomic-refactor-2' of github.com:ava-labs/coreth…
ceyonur May 14, 2025
f3f3cc9
move log (#957)
ceyonur May 14, 2025
f87e260
add ethclient hooks and examples (#951)
ceyonur May 14, 2025
b99df9c
Update CODEOWNERS (#965)
ceyonur May 15, 2025
032b013
Add license linter (#969)
ceyonur May 19, 2025
38e0319
Remove dead code (#967)
StephenButtolph May 19, 2025
cba182b
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur May 23, 2025
0195fc1
Merge branch 'master' into libevm-atomic-refactor-2
ceyonur May 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:
with:
go-version-file: "go.mod"
- name: Run e2e tests
uses: ava-labs/avalanchego/.github/actions/run-monitored-tmpnet-cmd@d8bfb84e85957e16c6e5bab105f61e29485f369c
uses: ava-labs/avalanchego/.github/actions/run-monitored-tmpnet-cmd@c7351c1ea9ad15f6a65614f158c06ddbb0f722a7
with:
run: ./scripts/tests.e2e.sh
prometheus_username: ${{ secrets.PROMETHEUS_ID || '' }}
Expand Down
3 changes: 2 additions & 1 deletion consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type (
block *types.Block,
parent *types.Header,
statedb *state.StateDB,
config *params.ChainConfig,
) (
blockFeeContribution *big.Int,
extDataGasUsed *big.Int,
Expand Down Expand Up @@ -371,7 +372,7 @@ func (eng *DummyEngine) Finalize(chain consensus.ChainHeaderReader, block *types
err error
)
if eng.cb.OnExtraStateChange != nil {
contribution, extDataGasUsed, err = eng.cb.OnExtraStateChange(block, parent, state)
contribution, extDataGasUsed, err = eng.cb.OnExtraStateChange(block, parent, state, chain.Config())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/test_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

var TestCallbacks = dummy.ConsensusCallbacks{
OnExtraStateChange: func(block *types.Block, _ *types.Header, sdb *state.StateDB) (*big.Int, *big.Int, error) {
OnExtraStateChange: func(block *types.Block, _ *types.Header, sdb *state.StateDB, _ *params.ChainConfig) (*big.Int, *big.Int, error) {
sdb.AddBalanceMultiCoin(common.HexToAddress("0xdeadbeef"), common.HexToHash("0xdeadbeef"), big.NewInt(block.Number().Int64()))
return nil, nil, nil
},
Expand Down
2 changes: 1 addition & 1 deletion eth/gasprice/gasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func newTestBackend(t *testing.T, config *params.ChainConfig, numBlocks int, ext
OnFinalizeAndAssemble: func(*types.Header, *types.Header, *state.StateDB, []*types.Transaction) ([]byte, *big.Int, *big.Int, error) {
return nil, common.Big0, extDataGasUsage, nil
},
OnExtraStateChange: func(*types.Block, *types.Header, *state.StateDB) (*big.Int, *big.Int, error) {
OnExtraStateChange: func(*types.Block, *types.Header, *state.StateDB, *params.ChainConfig) (*big.Int, *big.Int, error) {
return common.Big0, extDataGasUsage, nil
},
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.6

require (
github.com/VictoriaMetrics/fastcache v1.12.1
github.com/ava-labs/avalanchego v1.13.1-rc.0.0.20250421193253-d8bfb84e8595
github.com/ava-labs/avalanchego v1.13.1-rc.0.0.20250419152035-c7351c1ea9ad
github.com/ava-labs/libevm v1.13.14-0.2.0.release
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set/v2 v2.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/avalanchego v1.13.1-rc.0.0.20250421193253-d8bfb84e8595 h1:sazRMIAL0gAJGyhZCkO8zPTVFufwuxPHQoPfmP6qLko=
github.com/ava-labs/avalanchego v1.13.1-rc.0.0.20250421193253-d8bfb84e8595/go.mod h1:6cFD9bIDPn9aDPzTvAjBndeqURKA1C9c4aEeIcvS0Ms=
github.com/ava-labs/avalanchego v1.13.1-rc.0.0.20250419152035-c7351c1ea9ad h1:+Ff14700/W+AGkhew8XfHVb1MVHT5KHj5OyioTRvkDo=
github.com/ava-labs/avalanchego v1.13.1-rc.0.0.20250419152035-c7351c1ea9ad/go.mod h1:9/YOzCqY75s5ku5sbYRwWpI7igbYVdrnkI5XDxZcFtA=
github.com/ava-labs/libevm v1.13.14-0.2.0.release h1:uKGCc5/ceeBbfAPRVtBUxbQt50WzB2pEDb8Uy93ePgQ=
github.com/ava-labs/libevm v1.13.14-0.2.0.release/go.mod h1:+Iol+sVQ1KyoBsHf3veyrBmHCXr3xXRWq6ZXkgVfNLU=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
227 changes: 0 additions & 227 deletions plugin/evm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,12 @@ package evm

import (
"context"
"errors"
"fmt"
"math/big"
"net/http"

"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/coreth/plugin/evm/atomic"
"github.com/ava-labs/coreth/plugin/evm/client"
"github.com/ava-labs/coreth/plugin/evm/upgrade/ap3"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/log"
)

// test constants
const (
GenesisTestAddr = "0x751a0b96e1042bee789452ecb20253fba40dbe85"
GenesisTestKey = "0xabd71b35d559563fea757f0f5edbde286fb8c043105b15abb7cd57189306d7d1"

// Max number of addresses that can be passed in as argument to GetUTXOs
maxGetUTXOsAddrs = 1024
)

var (
errNoAddresses = errors.New("no addresses provided")
errNoSourceChain = errors.New("no source chain provided")
errNilTxID = errors.New("nil transaction ID")

initialBaseFee = big.NewInt(ap3.InitialBaseFee)
)

// SnowmanAPI introduces snowman specific functionality to the evm
type SnowmanAPI struct{ vm *VM }

Expand All @@ -65,202 +37,3 @@ func (api *SnowmanAPI) IssueBlock(ctx context.Context) error {
api.vm.builder.signalTxsReady()
return nil
}

// AvaxAPI offers Avalanche network related API methods
type AvaxAPI struct{ vm *VM }

type VersionReply struct {
Version string `json:"version"`
}

// ClientVersion returns the version of the VM running
func (service *AvaxAPI) Version(r *http.Request, _ *struct{}, reply *VersionReply) error {
reply.Version = Version
return nil
}

// GetUTXOs gets all utxos for passed in addresses
func (service *AvaxAPI) GetUTXOs(r *http.Request, args *api.GetUTXOsArgs, reply *api.GetUTXOsReply) error {
log.Info("EVM: GetUTXOs called", "Addresses", args.Addresses)

if len(args.Addresses) == 0 {
return errNoAddresses
}
if len(args.Addresses) > maxGetUTXOsAddrs {
return fmt.Errorf("number of addresses given, %d, exceeds maximum, %d", len(args.Addresses), maxGetUTXOsAddrs)
}

if args.SourceChain == "" {
return errNoSourceChain
}

chainID, err := service.vm.ctx.BCLookup.Lookup(args.SourceChain)
if err != nil {
return fmt.Errorf("problem parsing source chainID %q: %w", args.SourceChain, err)
}
sourceChain := chainID

addrSet := set.Set[ids.ShortID]{}
for _, addrStr := range args.Addresses {
addr, err := service.vm.ParseServiceAddress(addrStr)
if err != nil {
return fmt.Errorf("couldn't parse address %q: %w", addrStr, err)
}
addrSet.Add(addr)
}

startAddr := ids.ShortEmpty
startUTXO := ids.Empty
if args.StartIndex.Address != "" || args.StartIndex.UTXO != "" {
startAddr, err = service.vm.ParseServiceAddress(args.StartIndex.Address)
if err != nil {
return fmt.Errorf("couldn't parse start index address %q: %w", args.StartIndex.Address, err)
}
startUTXO, err = ids.FromString(args.StartIndex.UTXO)
if err != nil {
return fmt.Errorf("couldn't parse start index utxo: %w", err)
}
}

service.vm.ctx.Lock.Lock()
defer service.vm.ctx.Lock.Unlock()

utxos, endAddr, endUTXOID, err := service.vm.GetAtomicUTXOs(
sourceChain,
addrSet,
startAddr,
startUTXO,
int(args.Limit),
)
if err != nil {
return fmt.Errorf("problem retrieving UTXOs: %w", err)
}

reply.UTXOs = make([]string, len(utxos))
for i, utxo := range utxos {
b, err := atomic.Codec.Marshal(atomic.CodecVersion, utxo)
if err != nil {
return fmt.Errorf("problem marshalling UTXO: %w", err)
}
str, err := formatting.Encode(args.Encoding, b)
if err != nil {
return fmt.Errorf("problem encoding utxo: %w", err)
}
reply.UTXOs[i] = str
}

endAddress, err := service.vm.FormatLocalAddress(endAddr)
if err != nil {
return fmt.Errorf("problem formatting address: %w", err)
}

reply.EndIndex.Address = endAddress
reply.EndIndex.UTXO = endUTXOID.String()
reply.NumFetched = json.Uint64(len(utxos))
reply.Encoding = args.Encoding
return nil
}

func (service *AvaxAPI) IssueTx(r *http.Request, args *api.FormattedTx, response *api.JSONTxID) error {
log.Info("EVM: IssueTx called")

txBytes, err := formatting.Decode(args.Encoding, args.Tx)
if err != nil {
return fmt.Errorf("problem decoding transaction: %w", err)
}

tx := &atomic.Tx{}
if _, err := atomic.Codec.Unmarshal(txBytes, tx); err != nil {
return fmt.Errorf("problem parsing transaction: %w", err)
}
if err := tx.Sign(atomic.Codec, nil); err != nil {
return fmt.Errorf("problem initializing transaction: %w", err)
}

response.TxID = tx.ID()

service.vm.ctx.Lock.Lock()
defer service.vm.ctx.Lock.Unlock()

if err := service.vm.mempool.AddLocalTx(tx); err != nil {
return err
}
service.vm.atomicTxPushGossiper.Add(&atomic.GossipAtomicTx{Tx: tx})
return nil
}

// GetAtomicTxStatus returns the status of the specified transaction
func (service *AvaxAPI) GetAtomicTxStatus(r *http.Request, args *api.JSONTxID, reply *client.GetAtomicTxStatusReply) error {
log.Info("EVM: GetAtomicTxStatus called", "txID", args.TxID)

if args.TxID == ids.Empty {
return errNilTxID
}

service.vm.ctx.Lock.Lock()
defer service.vm.ctx.Lock.Unlock()

_, status, height, _ := service.vm.getAtomicTx(args.TxID)

reply.Status = status
if status == atomic.Accepted {
// Since chain state updates run asynchronously with VM block acceptance,
// avoid returning [Accepted] until the chain state reaches the block
// containing the atomic tx.
lastAccepted := service.vm.blockChain.LastAcceptedBlock()
if height > lastAccepted.NumberU64() {
reply.Status = atomic.Processing
return nil
}

jsonHeight := json.Uint64(height)
reply.BlockHeight = &jsonHeight
}
return nil
}

type FormattedTx struct {
api.FormattedTx
BlockHeight *json.Uint64 `json:"blockHeight,omitempty"`
}

// GetAtomicTx returns the specified transaction
func (service *AvaxAPI) GetAtomicTx(r *http.Request, args *api.GetTxArgs, reply *FormattedTx) error {
log.Info("EVM: GetAtomicTx called", "txID", args.TxID)

if args.TxID == ids.Empty {
return errNilTxID
}

service.vm.ctx.Lock.Lock()
defer service.vm.ctx.Lock.Unlock()

tx, status, height, err := service.vm.getAtomicTx(args.TxID)
if err != nil {
return err
}

if status == atomic.Unknown {
return fmt.Errorf("could not find tx %s", args.TxID)
}

txBytes, err := formatting.Encode(args.Encoding, tx.SignedBytes())
if err != nil {
return err
}
reply.Tx = txBytes
reply.Encoding = args.Encoding
if status == atomic.Accepted {
// Since chain state updates run asynchronously with VM block acceptance,
// avoid returning [Accepted] until the chain state reaches the block
// containing the atomic tx.
lastAccepted := service.vm.blockChain.LastAcceptedBlock()
if height > lastAccepted.NumberU64() {
return nil
}

jsonHeight := json.Uint64(height)
reply.BlockHeight = &jsonHeight
}
return nil
}
79 changes: 79 additions & 0 deletions plugin/evm/atomic/atomictest/shared_memories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package atomictest

import (
"testing"

"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/stretchr/testify/assert"
)

type SharedMemories struct {
ThisChain atomic.SharedMemory
PeerChain atomic.SharedMemory
thisChainID ids.ID
peerChainID ids.ID
}

func (s *SharedMemories) AddItemsToBeRemovedToPeerChain(ops map[ids.ID]*atomic.Requests) error {
for _, reqs := range ops {
puts := make(map[ids.ID]*atomic.Requests)
puts[s.thisChainID] = &atomic.Requests{}
for _, key := range reqs.RemoveRequests {
val := []byte{0x1}
puts[s.thisChainID].PutRequests = append(puts[s.thisChainID].PutRequests, &atomic.Element{Key: key, Value: val})
}
if err := s.PeerChain.Apply(puts); err != nil {
return err
}
}
return nil
}

func (s *SharedMemories) AssertOpsApplied(t *testing.T, ops map[ids.ID]*atomic.Requests) {
t.Helper()
for _, reqs := range ops {
// should be able to get put requests
for _, elem := range reqs.PutRequests {
val, err := s.PeerChain.Get(s.thisChainID, [][]byte{elem.Key})
if err != nil {
t.Fatalf("error finding puts in peerChainMemory: %s", err)
}
assert.Equal(t, elem.Value, val[0])
}

// should not be able to get remove requests
for _, key := range reqs.RemoveRequests {
_, err := s.ThisChain.Get(s.peerChainID, [][]byte{key})
assert.EqualError(t, err, "not found")
}
}
}

func (s *SharedMemories) AssertOpsNotApplied(t *testing.T, ops map[ids.ID]*atomic.Requests) {
t.Helper()
for _, reqs := range ops {
// should not be able to get put requests
for _, elem := range reqs.PutRequests {
_, err := s.PeerChain.Get(s.thisChainID, [][]byte{elem.Key})
assert.EqualError(t, err, "not found")
}

// should be able to get remove requests (these were previously added as puts on peerChain)
for _, key := range reqs.RemoveRequests {
val, err := s.ThisChain.Get(s.peerChainID, [][]byte{key})
assert.NoError(t, err)
assert.Equal(t, []byte{0x1}, val[0])
}
}
}

// TODO: once tests are moved to atomic package, unexport this function
func NewSharedMemories(atomicMemory *atomic.Memory, thisChainID, peerChainID ids.ID) *SharedMemories {
return &SharedMemories{
ThisChain: atomicMemory.NewSharedMemory(thisChainID),
PeerChain: atomicMemory.NewSharedMemory(peerChainID),
thisChainID: thisChainID,
peerChainID: peerChainID,
}
}
Loading
Loading