Skip to content

Commit

Permalink
Allow P-chain wallet to be used by the platformvm (#3314)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Aug 20, 2024
1 parent 76f1918 commit bfe9fbb
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 90 deletions.
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
54 changes: 54 additions & 0 deletions wallet/chain/p/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package p

import (
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/wallet/chain/p/wallet"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)

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

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

type Client struct {
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
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
61 changes: 15 additions & 46 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,13 @@
// 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 @@ -19,13 +17,19 @@ import (
walletsigner "github.com/ava-labs/avalanchego/wallet/chain/p/signer"
)

var (
ErrNotCommitted = errors.New("not committed")
var _ Wallet = (*wallet)(nil)

_ 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 +250,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 +494,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

0 comments on commit bfe9fbb

Please sign in to comment.