Skip to content

Commit

Permalink
P-Chain - repackaged wallet backends (#2757)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
  • Loading branch information
abi87 and StephenButtolph authored Mar 19, 2024
1 parent e88e565 commit 9cef7d3
Show file tree
Hide file tree
Showing 21 changed files with 398 additions and 417 deletions.
9 changes: 6 additions & 3 deletions tests/antithesis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ func (w *workload) issueXToPTransfer(ctx context.Context) {
avaxAssetID = xWallet.AVAXAssetID()
avaxBalance = balances[avaxAssetID]
xBaseTxFee = xWallet.BaseTxFee()
pBaseTxFee = pWallet.BaseTxFee()
pBuilder = pWallet.Builder()
pContext = pBuilder.Context()
pBaseTxFee = pContext.BaseTxFee
txFees = xBaseTxFee + pBaseTxFee
neededBalance = txFees + units.Avax
)
Expand Down Expand Up @@ -471,9 +473,10 @@ func (w *workload) issuePToXTransfer(ctx context.Context) {
}

var (
avaxAssetID = pWallet.AVAXAssetID()
pContext = pBuilder.Context()
avaxAssetID = pContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
pBaseTxFee = pWallet.BaseTxFee()
pBaseTxFee = pContext.BaseTxFee
xBaseTxFee = xWallet.BaseTxFee()
txFees = pBaseTxFee + xBaseTxFee
neededBalance = txFees + units.Schmeckle
Expand Down
7 changes: 5 additions & 2 deletions tests/e2e/p/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
cWallet := baseWallet.C()
pWallet := baseWallet.P()

pBuilder := pWallet.Builder()
pContext := pBuilder.Context()

ginkgo.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
avaxAssetID := xWallet.AVAXAssetID()
Expand Down Expand Up @@ -114,7 +117,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
Subnet: constants.PrimaryNetworkID,
},
nodePOP,
pWallet.AVAXAssetID(),
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{rewardKey.Address()},
Expand Down Expand Up @@ -143,7 +146,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
},
Subnet: constants.PrimaryNetworkID,
},
pWallet.AVAXAssetID(),
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{rewardKey.Address()},
Expand Down
13 changes: 8 additions & 5 deletions tests/e2e/p/staking_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
baseWallet := e2e.NewWallet(keychain, nodeURI)
pWallet := baseWallet.P()

pBuilder := pWallet.Builder()
pContext := pBuilder.Context()

const (
delegationPercent = 0.10 // 10%
delegationShare = reward.PercentDenominator * delegationPercent
Expand All @@ -130,7 +133,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
Subnet: constants.PrimaryNetworkID,
},
alphaPOP,
pWallet.AVAXAssetID(),
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{alphaValidationRewardKey.Address()},
Expand Down Expand Up @@ -159,7 +162,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
Subnet: constants.PrimaryNetworkID,
},
betaPOP,
pWallet.AVAXAssetID(),
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{betaValidationRewardKey.Address()},
Expand Down Expand Up @@ -191,7 +194,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
},
Subnet: constants.PrimaryNetworkID,
},
pWallet.AVAXAssetID(),
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{gammaDelegationRewardKey.Address()},
Expand All @@ -214,7 +217,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
},
Subnet: constants.PrimaryNetworkID,
},
pWallet.AVAXAssetID(),
pContext.AVAXAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{deltaDelegationRewardKey.Address()},
Expand Down Expand Up @@ -276,7 +279,7 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
pWallet := baseWallet.P()
balances, err := pWallet.Builder().GetBalance()
require.NoError(err)
rewardBalances[rewardKey.Address()] = balances[pWallet.AVAXAssetID()]
rewardBalances[rewardKey.Address()] = balances[pContext.AVAXAssetID]
}
require.Len(rewardBalances, len(rewardKeys))

Expand Down
5 changes: 4 additions & 1 deletion tests/e2e/p/validator_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ var _ = e2e.DescribePChain("[Validator Sets]", func() {
baseWallet := e2e.NewWallet(keychain, nodeURI)
pWallet := baseWallet.P()

pBuilder := pWallet.Builder()
pContext := pBuilder.Context()

const delegatorCount = 15
ginkgo.By(fmt.Sprintf("adding %d delegators", delegatorCount), func() {
rewardKey, err := secp256k1.NewPrivateKey()
require.NoError(err)
avaxAssetID := pWallet.AVAXAssetID()
avaxAssetID := pContext.AVAXAssetID
startTime := time.Now().Add(tmpnet.DefaultValidatorStartTimeDiff)
endTime := startTime.Add(time.Second * 360)
// This is the default flag value for MinDelegatorStake.
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/p/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ var _ = e2e.DescribePChain("[Workflow]", func() {
baseWallet := e2e.NewWallet(keychain, nodeURI)

pWallet := baseWallet.P()
avaxAssetID := baseWallet.P().AVAXAssetID()
pBuilder := pWallet.Builder()
pContext := pBuilder.Context()
avaxAssetID := pContext.AVAXAssetID
xWallet := baseWallet.X()
pChainClient := platformvm.NewClient(nodeURI.URI)

Expand Down
27 changes: 14 additions & 13 deletions wallet/chain/p/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package p

import (
"context"
"sync"

"github.com/ava-labs/avalanchego/database"
Expand All @@ -13,31 +14,31 @@ import (
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
"github.com/ava-labs/avalanchego/wallet/chain/p/signer"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

stdcontext "context"
)

var _ Backend = (*backend)(nil)

// Backend defines the full interface required to support a P-chain wallet.
type Backend interface {
common.ChainUTXOs
BuilderBackend
SignerBackend
builder.Backend
signer.Backend

AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error
AcceptTx(ctx context.Context, tx *txs.Tx) error
}

type backend struct {
Context
common.ChainUTXOs

context *builder.Context

subnetOwnerLock sync.RWMutex
subnetOwner map[ids.ID]fx.Owner // subnetID -> owner
}

func NewBackend(ctx Context, utxos common.ChainUTXOs, subnetTxs map[ids.ID]*txs.Tx) Backend {
func NewBackend(context *builder.Context, utxos common.ChainUTXOs, subnetTxs map[ids.ID]*txs.Tx) Backend {
subnetOwner := make(map[ids.ID]fx.Owner)
for txID, tx := range subnetTxs { // first get owners from the CreateSubnetTx
createSubnetTx, ok := tx.Unsigned.(*txs.CreateSubnetTx)
Expand All @@ -54,13 +55,13 @@ func NewBackend(ctx Context, utxos common.ChainUTXOs, subnetTxs map[ids.ID]*txs.
subnetOwner[transferSubnetOwnershipTx.Subnet] = transferSubnetOwnershipTx.Owner
}
return &backend{
Context: ctx,
ChainUTXOs: utxos,
context: context,
subnetOwner: subnetOwner,
}
}

func (b *backend) AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error {
func (b *backend) AcceptTx(ctx context.Context, tx *txs.Tx) error {
txID := tx.ID()
err := tx.Unsigned.Visit(&backendVisitor{
b: b,
Expand All @@ -75,7 +76,7 @@ func (b *backend) AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error {
return b.addUTXOs(ctx, constants.PlatformChainID, producedUTXOSlice)
}

func (b *backend) addUTXOs(ctx stdcontext.Context, destinationChainID ids.ID, utxos []*avax.UTXO) error {
func (b *backend) addUTXOs(ctx context.Context, destinationChainID ids.ID, utxos []*avax.UTXO) error {
for _, utxo := range utxos {
if err := b.AddUTXO(ctx, destinationChainID, utxo); err != nil {
return err
Expand All @@ -84,7 +85,7 @@ func (b *backend) addUTXOs(ctx stdcontext.Context, destinationChainID ids.ID, ut
return nil
}

func (b *backend) removeUTXOs(ctx stdcontext.Context, sourceChain ids.ID, utxoIDs set.Set[ids.ID]) error {
func (b *backend) removeUTXOs(ctx context.Context, sourceChain ids.ID, utxoIDs set.Set[ids.ID]) error {
for utxoID := range utxoIDs {
if err := b.RemoveUTXO(ctx, sourceChain, utxoID); err != nil {
return err
Expand All @@ -93,7 +94,7 @@ func (b *backend) removeUTXOs(ctx stdcontext.Context, sourceChain ids.ID, utxoID
return nil
}

func (b *backend) GetSubnetOwner(_ stdcontext.Context, subnetID ids.ID) (fx.Owner, error) {
func (b *backend) GetSubnetOwner(_ context.Context, subnetID ids.ID) (fx.Owner, error) {
b.subnetOwnerLock.RLock()
defer b.subnetOwnerLock.RUnlock()

Expand Down
17 changes: 11 additions & 6 deletions wallet/chain/p/backend_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@
package p

import (
"context"
"errors"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"

stdcontext "context"
)

var _ txs.Visitor = (*backendVisitor)(nil)
var (
_ txs.Visitor = (*backendVisitor)(nil)

ErrUnsupportedTxType = errors.New("unsupported tx type")
)

// backendVisitor handles accepting of transactions for the backend
type backendVisitor struct {
b *backend
ctx stdcontext.Context
ctx context.Context
txID ids.ID
}

func (*backendVisitor) AdvanceTimeTx(*txs.AdvanceTimeTx) error {
return errUnsupportedTxType
return ErrUnsupportedTxType
}

func (*backendVisitor) RewardValidatorTx(*txs.RewardValidatorTx) error {
return errUnsupportedTxType
return ErrUnsupportedTxType
}

func (b *backendVisitor) AddValidatorTx(tx *txs.AddValidatorTx) error {
Expand Down
Loading

0 comments on commit 9cef7d3

Please sign in to comment.