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

implement txs.Visitor in X chain wallet #1299

Merged
merged 8 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 7 additions & 33 deletions wallet/chain/x/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package x

import (
"fmt"

stdcontext "context"
danlaine marked this conversation as resolved.
Show resolved Hide resolved

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -48,38 +46,14 @@ func NewBackend(ctx Context, chainID ids.ID, utxos ChainUTXOs) Backend {
}
}

// TODO: implement txs.Visitor here
func (b *backend) AcceptTx(ctx stdcontext.Context, tx *txs.Tx) error {
switch utx := tx.Unsigned.(type) {
case *txs.BaseTx, *txs.CreateAssetTx, *txs.OperationTx:
case *txs.ImportTx:
for _, input := range utx.ImportedIns {
utxoID := input.UTXOID.InputID()
if err := b.RemoveUTXO(ctx, utx.SourceChain, utxoID); err != nil {
return err
}
}
case *txs.ExportTx:
txID := tx.ID()
for i, out := range utx.ExportedOuts {
err := b.AddUTXO(
ctx,
utx.DestinationChain,
&avax.UTXO{
UTXOID: avax.UTXOID{
TxID: txID,
OutputIndex: uint32(len(utx.Outs) + i),
},
Asset: avax.Asset{ID: out.AssetID()},
Out: out.Out,
},
)
if err != nil {
return err
}
}
default:
return fmt.Errorf("%w: %T", errUnknownTxType, tx.Unsigned)
err := tx.Unsigned.Visit(&backendVisitor{
b: b,
ctx: ctx,
txID: tx.ID(),
})
if err != nil {
return err
}

inputUTXOs := tx.Unsigned.InputUTXOs()
Expand Down
64 changes: 64 additions & 0 deletions wallet/chain/x/backend_visitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package x

import (
stdcontext "context"

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

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

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

func (*backendVisitor) BaseTx(*txs.BaseTx) error {
return nil
}

func (*backendVisitor) CreateAssetTx(*txs.CreateAssetTx) error {
return nil
}

func (*backendVisitor) OperationTx(*txs.OperationTx) error {
return nil
}

func (b *backendVisitor) ImportTx(tx *txs.ImportTx) error {
for _, in := range tx.ImportedIns {
utxoID := in.UTXOID.InputID()
if err := b.b.RemoveUTXO(b.ctx, tx.SourceChain, utxoID); err != nil {
return err
}
}
return nil
}

func (b *backendVisitor) ExportTx(tx *txs.ExportTx) error {
for i, out := range tx.ExportedOuts {
err := b.b.AddUTXO(
b.ctx,
tx.DestinationChain,
&avax.UTXO{
UTXOID: avax.UTXOID{
TxID: b.txID,
OutputIndex: uint32(len(tx.Outs) + i),
},
Asset: avax.Asset{ID: out.AssetID()},
Out: out.Out,
},
)
if err != nil {
return err
}
}
return nil
}