Skip to content

Commit

Permalink
Update C-chain wallet context (#3118)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Jun 14, 2024
1 parent 347a3f8 commit f99a64a
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 98 deletions.
6 changes: 4 additions & 2 deletions tests/e2e/c/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ var _ = e2e.DescribeCChain("[Interchain Workflow]", func() {
ginkgo.By("defining common configuration")
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
cBuilder := cWallet.Builder()
cContext := cBuilder.Context()
avaxAssetID := xContext.AVAXAssetID
// Use the same owner for import funds to X-Chain and P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Expand Down Expand Up @@ -119,7 +121,7 @@ var _ = e2e.DescribeCChain("[Interchain Workflow]", func() {

ginkgo.By("importing AVAX from the C-Chain to the X-Chain", func() {
_, err := xWallet.IssueImportTx(
cWallet.BlockchainID(),
cContext.BlockchainID,
&recipientOwner,
e2e.WithDefaultContext(),
)
Expand All @@ -146,7 +148,7 @@ var _ = e2e.DescribeCChain("[Interchain Workflow]", func() {

ginkgo.By("importing AVAX from the C-Chain to the P-Chain", func() {
_, err = pWallet.IssueImportTx(
cWallet.BlockchainID(),
cContext.BlockchainID,
&recipientOwner,
e2e.WithDefaultContext(),
)
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/p/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
xContext := xBuilder.Context()
pBuilder := pWallet.Builder()
pContext := pBuilder.Context()
cBuilder := cWallet.Builder()
cContext := cBuilder.Context()

ginkgo.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
Expand Down Expand Up @@ -186,7 +188,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL

ginkgo.By("exporting AVAX from the P-Chain to the C-Chain", func() {
_, err := pWallet.IssueExportTx(
cWallet.BlockchainID(),
cContext.BlockchainID,
exportOutputs,
e2e.WithDefaultContext(),
)
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/x/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var _ = e2e.DescribeXChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
recipientEthAddress := evm.GetEthAddress(recipientKey)
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
cBuilder := cWallet.Builder()
cContext := cBuilder.Context()
avaxAssetID := xContext.AVAXAssetID
// Use the same owner for sending to X-Chain and importing funds to P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Expand Down Expand Up @@ -96,7 +98,7 @@ var _ = e2e.DescribeXChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL

ginkgo.By("exporting AVAX from the X-Chain to the C-Chain", func() {
_, err := xWallet.IssueExportTx(
cWallet.BlockchainID(),
cContext.BlockchainID,
exportOutputs,
e2e.WithDefaultContext(),
)
Expand Down
13 changes: 5 additions & 8 deletions wallet/chain/c/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package c

import (
"context"
"errors"
"fmt"
"math/big"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

stdcontext "context"
ethcommon "github.com/ethereum/go-ethereum/common"
)

Expand All @@ -32,11 +32,10 @@ type Backend interface {
BuilderBackend
SignerBackend

AcceptAtomicTx(ctx stdcontext.Context, tx *evm.Tx) error
AcceptAtomicTx(ctx context.Context, tx *evm.Tx) error
}

type backend struct {
Context
common.ChainUTXOs

accountsLock sync.RWMutex
Expand All @@ -49,18 +48,16 @@ type Account struct {
}

func NewBackend(
ctx Context,
utxos common.ChainUTXOs,
accounts map[ethcommon.Address]*Account,
) Backend {
return &backend{
Context: ctx,
ChainUTXOs: utxos,
accounts: accounts,
}
}

func (b *backend) AcceptAtomicTx(ctx stdcontext.Context, tx *evm.Tx) error {
func (b *backend) AcceptAtomicTx(ctx context.Context, tx *evm.Tx) error {
switch tx := tx.UnsignedAtomicTx.(type) {
case *evm.UnsignedImportTx:
for _, input := range tx.ImportedInputs {
Expand Down Expand Up @@ -131,7 +128,7 @@ func (b *backend) AcceptAtomicTx(ctx stdcontext.Context, tx *evm.Tx) error {
return nil
}

func (b *backend) Balance(_ stdcontext.Context, addr ethcommon.Address) (*big.Int, error) {
func (b *backend) Balance(_ context.Context, addr ethcommon.Address) (*big.Int, error) {
b.accountsLock.RLock()
defer b.accountsLock.RUnlock()

Expand All @@ -142,7 +139,7 @@ func (b *backend) Balance(_ stdcontext.Context, addr ethcommon.Address) (*big.In
return account.Balance, nil
}

func (b *backend) Nonce(_ stdcontext.Context, addr ethcommon.Address) (uint64, error) {
func (b *backend) Nonce(_ context.Context, addr ethcommon.Address) (uint64, error) {
b.accountsLock.RLock()
defer b.accountsLock.RUnlock()

Expand Down
37 changes: 23 additions & 14 deletions wallet/chain/c/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package c

import (
"context"
"errors"
"math/big"

Expand All @@ -17,7 +18,6 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

stdcontext "context"
ethcommon "github.com/ethereum/go-ethereum/common"
)

Expand All @@ -41,6 +41,10 @@ var (
// Builder provides a convenient interface for building unsigned C-chain
// transactions.
type Builder interface {
// Context returns the configuration of the chain that this builder uses to
// create transactions.
Context() *Context

// GetBalance calculates the amount of AVAX that this builder has control
// over.
GetBalance(
Expand Down Expand Up @@ -86,16 +90,15 @@ type Builder interface {
// BuilderBackend specifies the required information needed to build unsigned
// C-chain transactions.
type BuilderBackend interface {
Context

UTXOs(ctx stdcontext.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
Balance(ctx stdcontext.Context, addr ethcommon.Address) (*big.Int, error)
Nonce(ctx stdcontext.Context, addr ethcommon.Address) (uint64, error)
UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
Balance(ctx context.Context, addr ethcommon.Address) (*big.Int, error)
Nonce(ctx context.Context, addr ethcommon.Address) (uint64, error)
}

type builder struct {
avaxAddrs set.Set[ids.ShortID]
ethAddrs set.Set[ethcommon.Address]
context *Context
backend BuilderBackend
}

Expand All @@ -110,15 +113,21 @@ type builder struct {
func NewBuilder(
avaxAddrs set.Set[ids.ShortID],
ethAddrs set.Set[ethcommon.Address],
context *Context,
backend BuilderBackend,
) Builder {
return &builder{
avaxAddrs: avaxAddrs,
ethAddrs: ethAddrs,
context: context,
backend: backend,
}
}

func (b *builder) Context() *Context {
return b.context
}

func (b *builder) GetBalance(
options ...common.Option,
) (*big.Int, error) {
Expand Down Expand Up @@ -152,7 +161,7 @@ func (b *builder) GetImportableBalance(
var (
addrs = ops.Addresses(b.avaxAddrs)
minIssuanceTime = ops.MinIssuanceTime()
avaxAssetID = b.backend.AVAXAssetID()
avaxAssetID = b.context.AVAXAssetID
balance uint64
)
for _, utxo := range utxos {
Expand Down Expand Up @@ -186,7 +195,7 @@ func (b *builder) NewImportTx(
var (
addrs = ops.Addresses(b.avaxAddrs)
minIssuanceTime = ops.MinIssuanceTime()
avaxAssetID = b.backend.AVAXAssetID()
avaxAssetID = b.context.AVAXAssetID

importedInputs = make([]*avax.TransferableInput, 0, len(utxos))
importedAmount uint64
Expand Down Expand Up @@ -218,8 +227,8 @@ func (b *builder) NewImportTx(

utils.Sort(importedInputs)
tx := &evm.UnsignedImportTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: b.backend.BlockchainID(),
NetworkID: b.context.NetworkID,
BlockchainID: b.context.BlockchainID,
SourceChain: chainID,
ImportedInputs: importedInputs,
}
Expand Down Expand Up @@ -260,7 +269,7 @@ func (b *builder) NewExportTx(
options ...common.Option,
) (*evm.UnsignedExportTx, error) {
var (
avaxAssetID = b.backend.AVAXAssetID()
avaxAssetID = b.context.AVAXAssetID
exportedOutputs = make([]*avax.TransferableOutput, len(outputs))
exportedAmount uint64
)
Expand All @@ -280,8 +289,8 @@ func (b *builder) NewExportTx(

avax.SortTransferableOutputs(exportedOutputs, evm.Codec)
tx := &evm.UnsignedExportTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: b.backend.BlockchainID(),
NetworkID: b.context.NetworkID,
BlockchainID: b.context.BlockchainID,
DestinationChain: chainID,
ExportedOutputs: exportedOutputs,
}
Expand Down Expand Up @@ -378,7 +387,7 @@ func (b *builder) NewExportTx(
utils.Sort(inputs)
tx.Ins = inputs

snowCtx, err := newSnowContext(b.backend)
snowCtx, err := newSnowContext(b.context)
if err != nil {
return nil, err
}
Expand Down
77 changes: 22 additions & 55 deletions wallet/chain/c/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,66 @@
package c

import (
"context"

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/avm"

stdcontext "context"
)

const Alias = "C"

var _ Context = (*context)(nil)

type Context interface {
NetworkID() uint32
BlockchainID() ids.ID
AVAXAssetID() ids.ID
type Context struct {
NetworkID uint32
BlockchainID ids.ID
AVAXAssetID ids.ID
}

type context struct {
networkID uint32
blockchainID ids.ID
avaxAssetID ids.ID
}

func NewContextFromURI(ctx stdcontext.Context, uri string) (Context, error) {
func NewContextFromURI(ctx context.Context, uri string) (*Context, error) {
infoClient := info.NewClient(uri)
xChainClient := avm.NewClient(uri, "X")
return NewContextFromClients(ctx, infoClient, xChainClient)
}

func NewContextFromClients(
ctx stdcontext.Context,
ctx context.Context,
infoClient info.Client,
xChainClient avm.Client,
) (Context, error) {
) (*Context, error) {
networkID, err := infoClient.GetNetworkID(ctx)
if err != nil {
return nil, err
}

chainID, err := infoClient.GetBlockchainID(ctx, Alias)
blockchainID, err := infoClient.GetBlockchainID(ctx, Alias)
if err != nil {
return nil, err
}

asset, err := xChainClient.GetAssetDescription(ctx, "AVAX")
avaxAsset, err := xChainClient.GetAssetDescription(ctx, "AVAX")
if err != nil {
return nil, err
}

return NewContext(
networkID,
chainID,
asset.AssetID,
), nil
}

func NewContext(
networkID uint32,
blockchainID ids.ID,
avaxAssetID ids.ID,
) Context {
return &context{
networkID: networkID,
blockchainID: blockchainID,
avaxAssetID: avaxAssetID,
}
}

func (c *context) NetworkID() uint32 {
return c.networkID
}

func (c *context) BlockchainID() ids.ID {
return c.blockchainID
}

func (c *context) AVAXAssetID() ids.ID {
return c.avaxAssetID
return &Context{
NetworkID: networkID,
BlockchainID: blockchainID,
AVAXAssetID: avaxAsset.AssetID,
}, nil
}

func newSnowContext(c Context) (*snow.Context, error) {
chainID := c.BlockchainID()
func newSnowContext(c *Context) (*snow.Context, error) {
lookup := ids.NewAliaser()
return &snow.Context{
NetworkID: c.NetworkID(),
NetworkID: c.NetworkID,
SubnetID: constants.PrimaryNetworkID,
ChainID: chainID,
CChainID: chainID,
AVAXAssetID: c.AVAXAssetID(),
ChainID: c.BlockchainID,
CChainID: c.BlockchainID,
AVAXAssetID: c.AVAXAssetID,
Log: logging.NoLog{},
BCLookup: lookup,
}, lookup.Alias(chainID, Alias)
}, lookup.Alias(c.BlockchainID, Alias)
}
Loading

0 comments on commit f99a64a

Please sign in to comment.