Skip to content
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

Move UTXOs definition from primary to primary/common #2741

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
16 changes: 9 additions & 7 deletions wallet/subnet/primary/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ava-labs/coreth/ethclient"
"github.com/ava-labs/coreth/plugin/evm"
"github.com/ethereum/go-ethereum/common"

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/codec"
Expand All @@ -24,6 +23,9 @@ import (
"github.com/ava-labs/avalanchego/wallet/chain/c"
"github.com/ava-labs/avalanchego/wallet/chain/p"
"github.com/ava-labs/avalanchego/wallet/chain/x"

walletcommon "github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
ethcommon "github.com/ethereum/go-ethereum/common"
)

const (
Expand Down Expand Up @@ -60,7 +62,7 @@ type AVAXState struct {
XCTX x.Context
CClient evm.Client
CCTX c.Context
UTXOs UTXOs
UTXOs walletcommon.UTXOs
}

func FetchState(
Expand Down Expand Up @@ -91,7 +93,7 @@ func FetchState(
return nil, err
}

utxos := NewUTXOs()
utxos := walletcommon.NewUTXOs()
addrList := addrs.List()
chains := []struct {
id ids.ID
Expand Down Expand Up @@ -143,13 +145,13 @@ func FetchState(

type EthState struct {
Client ethclient.Client
Accounts map[common.Address]*c.Account
Accounts map[ethcommon.Address]*c.Account
}

func FetchEthState(
ctx context.Context,
uri string,
addrs set.Set[common.Address],
addrs set.Set[ethcommon.Address],
) (*EthState, error) {
path := fmt.Sprintf(
"%s/ext/%s/C/rpc",
Expand All @@ -161,7 +163,7 @@ func FetchEthState(
return nil, err
}

accounts := make(map[common.Address]*c.Account, addrs.Len())
accounts := make(map[ethcommon.Address]*c.Account, addrs.Len())
for addr := range addrs {
balance, err := client.BalanceAt(ctx, addr, nil)
if err != nil {
Expand All @@ -188,7 +190,7 @@ func FetchEthState(
// expires, then the returned error will be immediately reported.
func AddAllUTXOs(
ctx context.Context,
utxos UTXOs,
utxos walletcommon.UTXOs,
client UTXOClient,
codec codec.Manager,
sourceChainID ids.ID,
Expand Down
124 changes: 124 additions & 0 deletions wallet/subnet/primary/common/utxos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,139 @@ package common

import (
"context"
"sync"

"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/avax"
)

var (
_ UTXOs = (*utxos)(nil)
_ ChainUTXOs = (*chainUTXOs)(nil)
)

type ChainUTXOs interface {
AddUTXO(ctx context.Context, destinationChainID ids.ID, utxo *avax.UTXO) error
RemoveUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) error

UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
GetUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) (*avax.UTXO, error)
}

type UTXOs interface {
AddUTXO(ctx context.Context, sourceChainID, destinationChainID ids.ID, utxo *avax.UTXO) error
RemoveUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error

UTXOs(ctx context.Context, sourceChainID, destinationChainID ids.ID) ([]*avax.UTXO, error)
GetUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*avax.UTXO, error)
}

func NewUTXOs() UTXOs {
return &utxos{
sourceToDestToUTXOIDToUTXO: make(map[ids.ID]map[ids.ID]map[ids.ID]*avax.UTXO),
}
}

func NewChainUTXOs(chainID ids.ID, utxos UTXOs) ChainUTXOs {
return &chainUTXOs{
utxos: utxos,
chainID: chainID,
}
}

type utxos struct {
lock sync.RWMutex
// sourceChainID -> destinationChainID -> utxoID -> utxo
sourceToDestToUTXOIDToUTXO map[ids.ID]map[ids.ID]map[ids.ID]*avax.UTXO
}

func (u *utxos) AddUTXO(_ context.Context, sourceChainID, destinationChainID ids.ID, utxo *avax.UTXO) error {
u.lock.Lock()
defer u.lock.Unlock()

destToUTXOIDToUTXO, ok := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
if !ok {
destToUTXOIDToUTXO = make(map[ids.ID]map[ids.ID]*avax.UTXO)
u.sourceToDestToUTXOIDToUTXO[sourceChainID] = destToUTXOIDToUTXO
}

utxoIDToUTXO, ok := destToUTXOIDToUTXO[destinationChainID]
if !ok {
utxoIDToUTXO = make(map[ids.ID]*avax.UTXO)
destToUTXOIDToUTXO[destinationChainID] = utxoIDToUTXO
}

utxoIDToUTXO[utxo.InputID()] = utxo
return nil
}

func (u *utxos) RemoveUTXO(_ context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error {
u.lock.Lock()
defer u.lock.Unlock()

destToUTXOIDToUTXO := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
utxoIDToUTXO := destToUTXOIDToUTXO[destinationChainID]
_, ok := utxoIDToUTXO[utxoID]
if !ok {
return nil
}

delete(utxoIDToUTXO, utxoID)
if len(utxoIDToUTXO) != 0 {
return nil
}

delete(destToUTXOIDToUTXO, destinationChainID)
if len(destToUTXOIDToUTXO) != 0 {
return nil
}

delete(u.sourceToDestToUTXOIDToUTXO, sourceChainID)
return nil
}

func (u *utxos) UTXOs(_ context.Context, sourceChainID, destinationChainID ids.ID) ([]*avax.UTXO, error) {
u.lock.RLock()
defer u.lock.RUnlock()

destToUTXOIDToUTXO := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
utxoIDToUTXO := destToUTXOIDToUTXO[destinationChainID]
return maps.Values(utxoIDToUTXO), nil
}

func (u *utxos) GetUTXO(_ context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*avax.UTXO, error) {
u.lock.RLock()
defer u.lock.RUnlock()

destToUTXOIDToUTXO := u.sourceToDestToUTXOIDToUTXO[sourceChainID]
utxoIDToUTXO := destToUTXOIDToUTXO[destinationChainID]
utxo, ok := utxoIDToUTXO[utxoID]
if !ok {
return nil, database.ErrNotFound
}
return utxo, nil
}

type chainUTXOs struct {
utxos UTXOs
chainID ids.ID
}

func (c *chainUTXOs) AddUTXO(ctx context.Context, destinationChainID ids.ID, utxo *avax.UTXO) error {
return c.utxos.AddUTXO(ctx, c.chainID, destinationChainID, utxo)
}

func (c *chainUTXOs) RemoveUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) error {
return c.utxos.RemoveUTXO(ctx, sourceChainID, c.chainID, utxoID)
}

func (c *chainUTXOs) UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error) {
return c.utxos.UTXOs(ctx, sourceChainID, c.chainID)
}

func (c *chainUTXOs) GetUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) (*avax.UTXO, error) {
return c.utxos.GetUTXO(ctx, sourceChainID, c.chainID, utxoID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/wallet/chain/p"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

func main() {
Expand All @@ -35,7 +36,7 @@ func main() {
}
log.Printf("fetched state of %s in %s\n", addrStr, time.Since(fetchStartTime))

pUTXOs := primary.NewChainUTXOs(constants.PlatformChainID, state.UTXOs)
pUTXOs := common.NewChainUTXOs(constants.PlatformChainID, state.UTXOs)
pBackend := p.NewBackend(state.PCTX, pUTXOs, nil)
pBuilder := p.NewBuilder(addresses, pBackend)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/wallet/chain/x"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

func main() {
Expand All @@ -36,7 +37,7 @@ func main() {

xChainID := state.XCTX.BlockchainID()

xUTXOs := primary.NewChainUTXOs(xChainID, state.UTXOs)
xUTXOs := common.NewChainUTXOs(xChainID, state.UTXOs)
xBackend := x.NewBackend(state.XCTX, xUTXOs)
xBuilder := x.NewBuilder(addresses, xBackend)

Expand Down
136 changes: 0 additions & 136 deletions wallet/subnet/primary/utxos.go

This file was deleted.

Loading
Loading