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

Allow P-chain wallet to be used by the platformvm #3314

Merged
merged 5 commits into from
Aug 20, 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
23 changes: 12 additions & 11 deletions wallet/chain/p/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/vms/types"
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
"github.com/ava-labs/avalanchego/wallet/chain/p/wallet"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common/utxotest"

Expand Down Expand Up @@ -174,7 +175,7 @@ func TestBaseTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, nil)
backend = wallet.NewBackend(e.context, chainUTXOs, nil)
builder = builder.New(set.Of(utxoAddr), e.context, backend)
)

Expand Down Expand Up @@ -214,7 +215,7 @@ func TestAddSubnetValidatorTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, subnetOwners)
backend = wallet.NewBackend(e.context, chainUTXOs, subnetOwners)
builder = builder.New(set.Of(utxoAddr, subnetAuthAddr), e.context, backend)
)

Expand Down Expand Up @@ -246,7 +247,7 @@ func TestRemoveSubnetValidatorTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, subnetOwners)
backend = wallet.NewBackend(e.context, chainUTXOs, subnetOwners)
builder = builder.New(set.Of(utxoAddr, subnetAuthAddr), e.context, backend)
)

Expand Down Expand Up @@ -287,7 +288,7 @@ func TestCreateChainTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, subnetOwners)
backend = wallet.NewBackend(e.context, chainUTXOs, subnetOwners)
builder = builder.New(set.Of(utxoAddr, subnetAuthAddr), e.context, backend)
)

Expand Down Expand Up @@ -327,7 +328,7 @@ func TestCreateSubnetTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, subnetOwners)
backend = wallet.NewBackend(e.context, chainUTXOs, subnetOwners)
builder = builder.New(set.Of(utxoAddr, subnetAuthAddr), e.context, backend)
)

Expand Down Expand Up @@ -359,7 +360,7 @@ func TestTransferSubnetOwnershipTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, subnetOwners)
backend = wallet.NewBackend(e.context, chainUTXOs, subnetOwners)
builder = builder.New(set.Of(utxoAddr, subnetAuthAddr), e.context, backend)
)

Expand Down Expand Up @@ -399,7 +400,7 @@ func TestImportTx(t *testing.T) {
constants.PlatformChainID: utxos,
sourceChainID: importedUTXOs,
})
backend = NewBackend(e.context, chainUTXOs, nil)
backend = wallet.NewBackend(e.context, chainUTXOs, nil)
builder = builder.New(set.Of(utxoAddr), e.context, backend)
)

Expand Down Expand Up @@ -436,7 +437,7 @@ func TestExportTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, nil)
backend = wallet.NewBackend(e.context, chainUTXOs, nil)
builder = builder.New(set.Of(utxoAddr), e.context, backend)
)

Expand Down Expand Up @@ -486,7 +487,7 @@ func TestTransformSubnetTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, subnetOwners)
backend = wallet.NewBackend(e.context, chainUTXOs, subnetOwners)
builder = builder.New(set.Of(utxoAddr, subnetAuthAddr), e.context, backend)
)

Expand Down Expand Up @@ -578,7 +579,7 @@ func TestAddPermissionlessValidatorTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, nil)
backend = wallet.NewBackend(e.context, chainUTXOs, nil)
builder = builder.New(set.Of(utxoAddr, rewardAddr), e.context, backend)
)

Expand Down Expand Up @@ -631,7 +632,7 @@ func TestAddPermissionlessDelegatorTx(t *testing.T) {
chainUTXOs = utxotest.NewDeterministicChainUTXOs(t, map[ids.ID][]*avax.UTXO{
constants.PlatformChainID: utxos,
})
backend = NewBackend(e.context, chainUTXOs, nil)
backend = wallet.NewBackend(e.context, chainUTXOs, nil)
builder = builder.New(set.Of(utxoAddr, rewardAddr), e.context, backend)
)

Expand Down
60 changes: 60 additions & 0 deletions wallet/chain/p/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package p

import (
"errors"

"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/wallet/chain/p/wallet"

Check failure on line 11 in wallet/chain/p/client.go

View workflow job for this annotation

GitHub Actions / Unit (macos-12)

found packages wallet (backend.go) and p (backend_visitor.go) in /Users/runner/work/avalanchego/avalanchego/wallet/chain/p/wallet

Check failure on line 11 in wallet/chain/p/client.go

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-20.04)

found packages wallet (backend.go) and p (backend_visitor.go) in /home/runner/work/avalanchego/avalanchego/wallet/chain/p/wallet

Check failure on line 11 in wallet/chain/p/client.go

View workflow job for this annotation

GitHub Actions / Unit (ubuntu-22.04)

found packages wallet (backend.go) and p (backend_visitor.go) in /home/runner/work/avalanchego/avalanchego/wallet/chain/p/wallet

Check failure on line 11 in wallet/chain/p/client.go

View workflow job for this annotation

GitHub Actions / Unit (custom-arm64-focal)

found packages wallet (backend.go) and p (backend_visitor.go) in /home/runner/work/avalanchego/avalanchego/wallet/chain/p/wallet

Check failure on line 11 in wallet/chain/p/client.go

View workflow job for this annotation

GitHub Actions / Unit (custom-arm64-jammy)

found packages wallet (backend.go) and p (backend_visitor.go) in /home/runner/work/avalanchego/avalanchego/wallet/chain/p/wallet
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

var (
ErrNotCommitted = errors.New("not committed")

_ wallet.Client = (*client)(nil)
)

func NewClient(
c platformvm.Client,
b wallet.Backend,
) wallet.Client {
return &client{
client: c,
backend: b,
}
}

type client struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if it is not simpler to just export these fields? The constructor doesn't do anything special so i think it might make sense to skip it altogether and just initialize the instance with its fields (exported) directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapping function is nice to enforce that they are provided imo (as the struct is not useful without populating them).

If this signature looks weird I'd be down to unexport the Client type entirely to avoid invalid use.

client platformvm.Client
backend wallet.Backend
}

func (c *client) IssueTx(
tx *txs.Tx,
options ...common.Option,
) error {
ops := common.NewOptions(options)
ctx := ops.Context()
txID, err := c.client.IssueTx(ctx, tx.Bytes())
if err != nil {
return err
}

if f := ops.PostIssuanceFunc(); f != nil {
f(txID)
}

if ops.AssumeDecided() {
return c.backend.AcceptTx(ctx, tx)
}

if err := platformvm.AwaitTxAccepted(c.client, ctx, txID, ops.PollFrequency()); err != nil {
return err
}

return c.backend.AcceptTx(ctx, tx)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package p
package wallet

import (
"context"
Expand Down
56 changes: 15 additions & 41 deletions wallet/chain/p/wallet.go → wallet/chain/p/wallet/wallet.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package p
package wallet

import (
"errors"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
Expand All @@ -25,7 +24,17 @@ var (
_ Wallet = (*wallet)(nil)
)

type Client interface {
// IssueTx issues the signed tx.
IssueTx(
tx *txs.Tx,
options ...common.Option,
) error
}

type Wallet interface {
Client

// Builder returns the builder that will be used to create the transactions.
Builder() builder.Builder

Expand Down Expand Up @@ -246,33 +255,24 @@ type Wallet interface {
utx txs.UnsignedTx,
options ...common.Option,
) (*txs.Tx, error)

// IssueTx issues the signed tx.
IssueTx(
tx *txs.Tx,
options ...common.Option,
) error
}

func NewWallet(
func New(
client Client,
builder builder.Builder,
signer walletsigner.Signer,
client platformvm.Client,
backend Backend,
) Wallet {
return &wallet{
Backend: backend,
Client: client,
builder: builder,
signer: signer,
client: client,
}
}

type wallet struct {
Backend
Client
builder builder.Builder
signer walletsigner.Signer
client platformvm.Client
}

func (w *wallet) Builder() builder.Builder {
Expand Down Expand Up @@ -499,29 +499,3 @@ func (w *wallet) IssueUnsignedTx(

return tx, w.IssueTx(tx, options...)
}

func (w *wallet) IssueTx(
tx *txs.Tx,
options ...common.Option,
) error {
ops := common.NewOptions(options)
ctx := ops.Context()
txID, err := w.client.IssueTx(ctx, tx.Bytes())
if err != nil {
return err
}

if f := ops.PostIssuanceFunc(); f != nil {
f(txID)
}

if ops.AssumeDecided() {
return w.Backend.AcceptTx(ctx, tx)
}

if err := platformvm.AwaitTxAccepted(w.client, ctx, txID, ops.PollFrequency()); err != nil {
return err
}

return w.Backend.AcceptTx(ctx, tx)
}
Loading
Loading