Skip to content

Commit

Permalink
X-Chain - repackaged wallet backends (#2762)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
abi87 and StephenButtolph authored Mar 19, 2024
1 parent 9cef7d3 commit 0118611
Show file tree
Hide file tree
Showing 25 changed files with 331 additions and 309 deletions.
4 changes: 2 additions & 2 deletions indexer/examples/x-chain-blocks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/ava-labs/avalanchego/indexer"
"github.com/ava-labs/avalanchego/vms/proposervm/block"
"github.com/ava-labs/avalanchego/wallet/chain/x"
"github.com/ava-labs/avalanchego/wallet/chain/x/builder"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

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

avmBlockBytes := proposerVMBlock.Block()
avmBlock, err := x.Parser.ParseBlock(avmBlockBytes)
avmBlock, err := builder.Parser.ParseBlock(avmBlockBytes)
if err != nil {
log.Fatalf("failed to parse avm block: %s\n", err)
}
Expand Down
45 changes: 27 additions & 18 deletions tests/antithesis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/propertyfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"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"

xtxs "github.com/ava-labs/avalanchego/vms/avm/txs"
ptxs "github.com/ava-labs/avalanchego/vms/platformvm/txs"
xbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder"
)

const NumKeys = 5
Expand Down Expand Up @@ -69,8 +69,10 @@ func main() {
workloads[0] = genesisWorkload

var (
genesisXWallet = wallet.X()
avaxAssetID = genesisXWallet.AVAXAssetID()
genesisXWallet = wallet.X()
genesisXBuilder = genesisXWallet.Builder()
genesisXContext = genesisXBuilder.Context()
avaxAssetID = genesisXContext.AVAXAssetID
)
for i := 1; i < NumKeys; i++ {
key, err := secp256k1.NewPrivateKey()
Expand Down Expand Up @@ -192,7 +194,8 @@ func (w *workload) run(ctx context.Context) {
log.Fatalf("failed to fetch P-chain balances: %s", err)
}
var (
avaxAssetID = xWallet.AVAXAssetID()
xContext = xBuilder.Context()
avaxAssetID = xContext.AVAXAssetID
xAVAX = xBalances[avaxAssetID]
pAVAX = pBalances[avaxAssetID]
)
Expand Down Expand Up @@ -245,9 +248,10 @@ func (w *workload) issueXChainBaseTx(ctx context.Context) {
}

var (
avaxAssetID = xWallet.AVAXAssetID()
xContext = xBuilder.Context()
avaxAssetID = xContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
baseTxFee = xWallet.BaseTxFee()
baseTxFee = xContext.BaseTxFee
neededBalance = baseTxFee + units.Schmeckle
)
if avaxBalance < neededBalance {
Expand Down Expand Up @@ -294,9 +298,10 @@ func (w *workload) issueXChainCreateAssetTx(ctx context.Context) {
}

var (
avaxAssetID = xWallet.AVAXAssetID()
xContext = xBuilder.Context()
avaxAssetID = xContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
neededBalance = xWallet.CreateAssetTxFee()
neededBalance = xContext.CreateAssetTxFee
)
if avaxBalance < neededBalance {
log.Printf("skipping X-chain tx issuance due to insufficient balance: %d < %d", avaxBalance, neededBalance)
Expand Down Expand Up @@ -342,10 +347,11 @@ func (w *workload) issueXChainOperationTx(ctx context.Context) {
}

var (
avaxAssetID = xWallet.AVAXAssetID()
xContext = xBuilder.Context()
avaxAssetID = xContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
createAssetTxFee = xWallet.CreateAssetTxFee()
baseTxFee = xWallet.BaseTxFee()
createAssetTxFee = xContext.CreateAssetTxFee
baseTxFee = xContext.BaseTxFee
neededBalance = createAssetTxFee + baseTxFee
)
if avaxBalance < neededBalance {
Expand Down Expand Up @@ -405,9 +411,10 @@ func (w *workload) issueXToPTransfer(ctx context.Context) {
}

var (
avaxAssetID = xWallet.AVAXAssetID()
xContext = xBuilder.Context()
avaxAssetID = xContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
xBaseTxFee = xWallet.BaseTxFee()
xBaseTxFee = xContext.BaseTxFee
pBuilder = pWallet.Builder()
pContext = pBuilder.Context()
pBaseTxFee = pContext.BaseTxFee
Expand Down Expand Up @@ -441,7 +448,7 @@ func (w *workload) issueXToPTransfer(ctx context.Context) {
log.Printf("created X-chain export transaction %s in %s", exportTx.ID(), time.Since(exportStartTime))

var (
xChainID = xWallet.BlockchainID()
xChainID = xContext.BlockchainID
importStartTime = time.Now()
)
importTx, err := pWallet.IssueImportTx(
Expand All @@ -464,6 +471,7 @@ func (w *workload) issuePToXTransfer(ctx context.Context) {
var (
xWallet = w.wallet.X()
pWallet = w.wallet.P()
xBuilder = xWallet.Builder()
pBuilder = pWallet.Builder()
)
balances, err := pBuilder.GetBalance()
Expand All @@ -473,11 +481,12 @@ func (w *workload) issuePToXTransfer(ctx context.Context) {
}

var (
xContext = xBuilder.Context()
pContext = pBuilder.Context()
avaxAssetID = pContext.AVAXAssetID
avaxBalance = balances[avaxAssetID]
pBaseTxFee = pContext.BaseTxFee
xBaseTxFee = xWallet.BaseTxFee()
xBaseTxFee = xContext.BaseTxFee
txFees = pBaseTxFee + xBaseTxFee
neededBalance = txFees + units.Schmeckle
)
Expand All @@ -487,7 +496,7 @@ func (w *workload) issuePToXTransfer(ctx context.Context) {
}

var (
xChainID = xWallet.BlockchainID()
xChainID = xContext.BlockchainID
owner = w.makeOwner()
exportStartTime = time.Now()
)
Expand Down Expand Up @@ -573,7 +582,7 @@ func (w *workload) confirmPChainTx(ctx context.Context, tx *ptxs.Tx) {

func (w *workload) verifyXChainTxConsumedUTXOs(ctx context.Context, tx *xtxs.Tx) {
txID := tx.ID()
chainID := w.wallet.X().BlockchainID()
chainID := w.wallet.X().Builder().Context().BlockchainID
for _, uri := range w.uris {
client := avm.NewClient(uri, "X")

Expand All @@ -582,7 +591,7 @@ func (w *workload) verifyXChainTxConsumedUTXOs(ctx context.Context, tx *xtxs.Tx)
ctx,
utxos,
client,
x.Parser.Codec(),
xbuilder.Parser.Codec(),
chainID,
chainID,
w.addrs.List(),
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/banff/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ var _ = ginkgo.Describe("[Banff]", func() {
// Get the P-chain and the X-chain wallets
pWallet := wallet.P()
xWallet := wallet.X()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()

// Pull out useful constants to use when issuing transactions.
xChainID := xWallet.BlockchainID()
xChainID := xContext.BlockchainID
owner := &secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/c/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ var _ = e2e.DescribeCChain("[Interchain Workflow]", func() {
pWallet := baseWallet.P()

ginkgo.By("defining common configuration")
avaxAssetID := xWallet.AVAXAssetID()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
avaxAssetID := xContext.AVAXAssetID
// Use the same owner for import funds to X-Chain and P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Threshold: 1,
Expand All @@ -107,7 +109,7 @@ var _ = e2e.DescribeCChain("[Interchain Workflow]", func() {

ginkgo.By("exporting AVAX from the C-Chain to the X-Chain", func() {
_, err := cWallet.IssueExportTx(
xWallet.BlockchainID(),
xContext.BlockchainID,
exportOutputs,
e2e.WithDefaultContext(),
e2e.WithSuggestedGasPrice(ethClient),
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/p/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
cWallet := baseWallet.C()
pWallet := baseWallet.P()

xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
pBuilder := pWallet.Builder()
pContext := pBuilder.Context()

ginkgo.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
avaxAssetID := xWallet.AVAXAssetID()
avaxAssetID := xContext.AVAXAssetID
// Use the same owner for sending to X-Chain and importing funds to P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Threshold: 1,
Expand Down Expand Up @@ -158,7 +160,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL

ginkgo.By("exporting AVAX from the P-Chain to the X-Chain", func() {
_, err := pWallet.IssueExportTx(
xWallet.BlockchainID(),
xContext.BlockchainID,
exportOutputs,
e2e.WithDefaultContext(),
)
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/p/permissionless_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ var _ = e2e.DescribePChain("[Permissionless Subnets]", func() {

pWallet := baseWallet.P()
xWallet := baseWallet.X()
xChainID := xWallet.BlockchainID()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
xChainID := xContext.BlockchainID

var validatorID ids.NodeID
ginkgo.By("retrieving the node ID of a primary network validator", func() {
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 @@ -45,6 +45,8 @@ var _ = e2e.DescribePChain("[Workflow]", func() {
pContext := pBuilder.Context()
avaxAssetID := pContext.AVAXAssetID
xWallet := baseWallet.X()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
pChainClient := platformvm.NewClient(nodeURI.URI)

tests.Outf("{{blue}} fetching minimal stake amounts {{/}}\n")
Expand Down Expand Up @@ -143,7 +145,7 @@ var _ = e2e.DescribePChain("[Workflow]", func() {

ginkgo.By("export avax from P to X chain", func() {
_, err := pWallet.IssueExportTx(
xWallet.BlockchainID(),
xContext.BlockchainID,
[]*avax.TransferableOutput{
{
Asset: avax.Asset{
Expand Down
8 changes: 5 additions & 3 deletions tests/e2e/x/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ var _ = e2e.DescribeXChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL

ginkgo.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
avaxAssetID := xWallet.AVAXAssetID()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
avaxAssetID := xContext.AVAXAssetID
// Use the same owner for sending to X-Chain and importing funds to P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Threshold: 1,
Expand Down Expand Up @@ -106,7 +108,7 @@ var _ = e2e.DescribeXChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL

ginkgo.By("importing AVAX from the X-Chain to the C-Chain", func() {
_, err := cWallet.IssueImportTx(
xWallet.BlockchainID(),
xContext.BlockchainID,
recipientEthAddress,
e2e.WithDefaultContext(),
e2e.WithSuggestedGasPrice(ethClient),
Expand All @@ -132,7 +134,7 @@ var _ = e2e.DescribeXChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL

ginkgo.By("importing AVAX from the X-Chain to the P-Chain", func() {
_, err := pWallet.IssueImportTx(
xWallet.BlockchainID(),
xContext.BlockchainID,
&recipientOwner,
e2e.WithDefaultContext(),
)
Expand Down
7 changes: 5 additions & 2 deletions tests/e2e/x/transfer/virtuous.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ var _ = e2e.DescribeXChainSerial("[Virtuous Transfer Tx AVAX]", func() {

keychain := secp256k1fx.NewKeychain(testKeys...)
baseWallet := e2e.NewWallet(keychain, e2e.Env.GetRandomNodeURI())
avaxAssetID := baseWallet.X().AVAXAssetID()
xWallet := baseWallet.X()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
avaxAssetID := xContext.AVAXAssetID

wallets := make([]primary.Wallet, len(testKeys))
shortAddrs := make([]ids.ShortID, len(testKeys))
Expand Down Expand Up @@ -146,7 +149,7 @@ var _ = e2e.DescribeXChainSerial("[Virtuous Transfer Tx AVAX]", func() {

amountToTransfer := senderOrigBal / 10

senderNewBal := senderOrigBal - amountToTransfer - baseWallet.X().BaseTxFee()
senderNewBal := senderOrigBal - amountToTransfer - xContext.BaseTxFee
receiverNewBal := receiverOrigBal + amountToTransfer

ginkgo.By("X-Chain transfer with wrong amount must fail", func() {
Expand Down
23 changes: 13 additions & 10 deletions wallet/chain/x/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@
package x

import (
"context"

"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/wallet/chain/x/builder"
"github.com/ava-labs/avalanchego/wallet/chain/x/signer"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

stdcontext "context"
)

var _ Backend = (*backend)(nil)

// Backend defines the full interface required to support an X-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
}

func NewBackend(ctx Context, utxos common.ChainUTXOs) Backend {
func NewBackend(context *builder.Context, utxos common.ChainUTXOs) Backend {
return &backend{
Context: ctx,
ChainUTXOs: utxos,
context: context,
}
}

func (b *backend) AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error {
func (b *backend) AcceptTx(ctx context.Context, tx *txs.Tx) error {
err := tx.Unsigned.Visit(&backendVisitor{
b: b,
ctx: ctx,
Expand All @@ -43,7 +46,7 @@ func (b *backend) AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error {
return err
}

chainID := b.Context.BlockchainID()
chainID := b.context.BlockchainID
inputUTXOs := tx.Unsigned.InputUTXOs()
for _, utxoID := range inputUTXOs {
if utxoID.Symbol {
Expand Down
6 changes: 3 additions & 3 deletions wallet/chain/x/backend_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
package x

import (
"context"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/components/avax"

stdcontext "context"
)

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

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

Expand Down
Loading

0 comments on commit 0118611

Please sign in to comment.