From 3fea872db3dc3bfc5cb501da6a1d81034611ed6d Mon Sep 17 00:00:00 2001 From: Alberto Benegiamo Date: Thu, 9 May 2024 09:30:17 +0200 Subject: [PATCH] cleanup --- vms/avm/environment_test.go | 23 +++++++++-- vms/avm/service.go | 36 +++++------------ vms/avm/service_backend.go | 17 -------- vms/avm/txs/builder/context.go | 25 ++++++++++++ vms/avm/txs/builder/tx_builders.go | 62 +++++++++++++++++------------- vms/avm/vm.go | 21 ++++++++-- vms/avm/wallet_service.go | 4 +- vms/avm/wallet_service_backend.go | 16 -------- 8 files changed, 111 insertions(+), 93 deletions(-) create mode 100644 vms/avm/txs/builder/context.go diff --git a/vms/avm/environment_test.go b/vms/avm/environment_test.go index 6045c09bd2fc..59d27b40636b 100644 --- a/vms/avm/environment_test.go +++ b/vms/avm/environment_test.go @@ -31,6 +31,7 @@ import ( "github.com/ava-labs/avalanchego/vms/avm/config" "github.com/ava-labs/avalanchego/vms/avm/fxs" "github.com/ava-labs/avalanchego/vms/avm/txs" + "github.com/ava-labs/avalanchego/vms/avm/txs/builder" "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/nftfx" "github.com/ava-labs/avalanchego/vms/secp256k1fx" @@ -203,6 +204,11 @@ func setup(tb testing.TB, c *envConfig) *environment { stopVertexID := ids.GenerateTestID() issuer := make(chan common.Message, 1) + var ( + serviceBackend = newServiceBackend(vm.ctx, &vm.Config, vm.state, vm.ctx.SharedMemory, vm.parser.Codec()) + walletBackend = NewWalletServiceBackend(vm) + ) + env := &environment{ genesisBytes: genesisBytes, genesisTx: getCreateTxFromGenesisTest(tb, genesisBytes, assetName), @@ -210,11 +216,22 @@ func setup(tb testing.TB, c *envConfig) *environment { issuer: issuer, vm: vm, service: &Service{ - vm: vm, - txBuilderBackend: newServiceBackend(vm.feeAssetID, vm.ctx, &vm.Config, vm.state, vm.ctx.SharedMemory, vm.parser.Codec()), + vm: vm, + b: builder.NewBuilder( + vm.ctx, + &vm.Config, + vm.feeAssetID, + serviceBackend, + ), }, walletService: &WalletService{ - walletServiceBackend: NewWalletServiceBackend(vm), + walletServiceBackend: walletBackend, + b: builder.NewBuilder( + vm.ctx, + &vm.Config, + vm.feeAssetID, + walletBackend, + ), }, } diff --git a/vms/avm/service.go b/vms/avm/service.go index 903db39f379a..214c12cdf3c1 100644 --- a/vms/avm/service.go +++ b/vms/avm/service.go @@ -62,8 +62,8 @@ type FormattedAssetID struct { // Service defines the base service for the asset vm type Service struct { - vm *VM - txBuilderBackend *serviceBackend + vm *VM + b *builder.Builder } // GetBlock returns the requested block. @@ -793,9 +793,7 @@ func (s *Service) buildCreateAssetTx(args *CreateAssetArgs) (*txs.Tx, ids.ShortI initialStateOuts = append(initialStateOuts, minter) } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - return builder.BuildCreateAssetTx( - s.txBuilderBackend, + return s.b.BuildCreateAssetTx( args.Name, args.Symbol, args.Denomination, @@ -914,9 +912,7 @@ func (s *Service) buildCreateNFTAsset(args *CreateNFTAssetArgs) (*txs.Tx, ids.Sh initialStateOuts = append(initialStateOuts, minter) } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - return builder.BuildCreateAssetTx( - s.txBuilderBackend, + return s.b.BuildCreateAssetTx( args.Name, args.Symbol, 0, // NFTs are non-fungible @@ -1232,8 +1228,7 @@ func (s *Service) buildSendMultiple(args *SendMultipleArgs) (*txs.Tx, ids.ShortI }) } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - return builder.BuildBaseTx(s.txBuilderBackend, outs, memoBytes, kc, changeAddr) + return s.b.BuildBaseTx(outs, memoBytes, kc, changeAddr) } // MintArgs are arguments for passing into Mint requests @@ -1312,12 +1307,6 @@ func (s *Service) buildMint(args *MintArgs) (*txs.Tx, ids.ShortID, error) { return nil, ids.ShortEmpty, err } - // Get all UTXOs/keys for the user - _, kc, err := s.vm.LoadUser(args.Username, args.Password, nil) - if err != nil { - return nil, ids.ShortEmpty, err - } - outputs := map[ids.ID]*secp256k1fx.TransferOutput{ assetID: { Amt: uint64(args.Amount), @@ -1328,8 +1317,7 @@ func (s *Service) buildMint(args *MintArgs) (*txs.Tx, ids.ShortID, error) { }, } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - tx, err := builder.MintFTs(s.txBuilderBackend, outputs, feeKc, changeAddr) + tx, err := s.b.MintFTs(outputs, feeKc, changeAddr) if err != nil { return nil, ids.ShortEmpty, err } @@ -1415,8 +1403,7 @@ func (s *Service) buildSendNFT(args *SendNFTArgs) (*txs.Tx, ids.ShortID, error) return nil, ids.ShortEmpty, err } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - tx, err := builder.BuildOperation(s.txBuilderBackend, ops, kc, changeAddr) + tx, err := s.b.BuildOperation(ops, kc, changeAddr) if err != nil { return nil, ids.ShortEmpty, err } @@ -1502,9 +1489,7 @@ func (s *Service) buildMintNFT(args *MintNFTArgs) (*txs.Tx, ids.ShortID, error) return nil, ids.ShortEmpty, err } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - tx, err := builder.MintNFT( - s.txBuilderBackend, + tx, err := s.b.MintNFT( assetID, payloadBytes, []*secp256k1fx.OutputOwners{{ @@ -1575,7 +1560,7 @@ func (s *Service) buildImport(args *ImportArgs) (*txs.Tx, error) { return nil, err } - return builder.BuildImportTx(s.txBuilderBackend, chainID, to, kc) + return s.b.BuildImportTx(chainID, to, kc) } // ExportArgs are arguments for passing into ExportAVA requests @@ -1668,6 +1653,5 @@ func (s *Service) buildExport(args *ExportArgs) (*txs.Tx, ids.ShortID, error) { return nil, ids.ShortEmpty, err } - s.txBuilderBackend.ResetAddresses(kc.Addresses()) - return builder.BuildExportTx(s.txBuilderBackend, chainID, to, assetID, uint64(args.Amount), kc, changeAddr) + return s.b.BuildExportTx(chainID, to, assetID, uint64(args.Amount), kc, changeAddr) } diff --git a/vms/avm/service_backend.go b/vms/avm/service_backend.go index ee1a350d5f84..da892857fedc 100644 --- a/vms/avm/service_backend.go +++ b/vms/avm/service_backend.go @@ -17,30 +17,18 @@ import ( "github.com/ava-labs/avalanchego/vms/avm/state" "github.com/ava-labs/avalanchego/vms/avm/txs/builder" "github.com/ava-labs/avalanchego/vms/components/avax" - - walletbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder" ) var _ builder.TxBuilderBackend = (*serviceBackend)(nil) func newServiceBackend( - feeAssetID ids.ID, ctx *snow.Context, cfg *config.Config, state state.State, sharedMemory atomic.SharedMemory, codec codec.Manager, ) *serviceBackend { - backendCtx := &walletbuilder.Context{ - NetworkID: ctx.NetworkID, - BlockchainID: ctx.XChainID, - AVAXAssetID: feeAssetID, - BaseTxFee: cfg.TxFee, - CreateAssetTxFee: cfg.CreateAssetTxFee, - } - return &serviceBackend{ - ctx: backendCtx, xchainID: ctx.XChainID, cfg: cfg, state: state, @@ -50,7 +38,6 @@ func newServiceBackend( } type serviceBackend struct { - ctx *walletbuilder.Context xchainID ids.ID cfg *config.Config addrs set.Set[ids.ShortID] @@ -59,10 +46,6 @@ type serviceBackend struct { codec codec.Manager } -func (b *serviceBackend) Context() *walletbuilder.Context { - return b.ctx -} - func (b *serviceBackend) ResetAddresses(addrs set.Set[ids.ShortID]) { b.addrs = addrs } diff --git a/vms/avm/txs/builder/context.go b/vms/avm/txs/builder/context.go new file mode 100644 index 000000000000..9408fd04efa9 --- /dev/null +++ b/vms/avm/txs/builder/context.go @@ -0,0 +1,25 @@ +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package builder + +import ( + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow" + "github.com/ava-labs/avalanchego/vms/avm/config" + "github.com/ava-labs/avalanchego/wallet/chain/x/builder" +) + +func newContext( + ctx *snow.Context, + cfg *config.Config, + feeAssetID ids.ID, +) *builder.Context { + return &builder.Context{ + NetworkID: ctx.NetworkID, + BlockchainID: ctx.XChainID, + AVAXAssetID: feeAssetID, + BaseTxFee: cfg.TxFee, + CreateAssetTxFee: cfg.CreateAssetTxFee, + } +} diff --git a/vms/avm/txs/builder/tx_builders.go b/vms/avm/txs/builder/tx_builders.go index e432f61d2aea..b1c994ed5bf6 100644 --- a/vms/avm/txs/builder/tx_builders.go +++ b/vms/avm/txs/builder/tx_builders.go @@ -8,7 +8,9 @@ import ( "fmt" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/utils/set" + "github.com/ava-labs/avalanchego/vms/avm/config" "github.com/ava-labs/avalanchego/vms/avm/txs" "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/components/verify" @@ -22,19 +24,34 @@ type TxBuilderBackend interface { builder.Backend signer.Backend - Context() *builder.Context ResetAddresses(addrs set.Set[ids.ShortID]) } -func BuildCreateAssetTx( +type Builder struct { + backend TxBuilderBackend + ctx *builder.Context +} + +func NewBuilder( + ctx *snow.Context, + cfg *config.Config, + feeAssetID ids.ID, backend TxBuilderBackend, +) *Builder { + return &Builder{ + backend: backend, + ctx: newContext(ctx, cfg, feeAssetID), + } +} + +func (b *Builder) BuildCreateAssetTx( name, symbol string, denomination byte, initialStates map[uint32][]verify.State, kc *secp256k1fx.Keychain, changeAddr ids.ShortID, ) (*txs.Tx, ids.ShortID, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) utx, err := xBuilder.NewCreateAssetTx( name, @@ -55,14 +72,13 @@ func BuildCreateAssetTx( return tx, changeAddr, nil } -func BuildBaseTx( - backend TxBuilderBackend, +func (b *Builder) BuildBaseTx( outs []*avax.TransferableOutput, memo []byte, kc *secp256k1fx.Keychain, changeAddr ids.ShortID, ) (*txs.Tx, ids.ShortID, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) utx, err := xBuilder.NewBaseTx( outs, @@ -80,15 +96,14 @@ func BuildBaseTx( return tx, changeAddr, nil } -func MintNFT( - backend TxBuilderBackend, +func (b *Builder) MintNFT( assetID ids.ID, payload []byte, owners []*secp256k1fx.OutputOwners, kc *secp256k1fx.Keychain, changeAddr ids.ShortID, ) (*txs.Tx, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) utx, err := xBuilder.NewOperationTxMintNFT( assetID, @@ -103,13 +118,12 @@ func MintNFT( return signer.SignUnsigned(context.Background(), xSigner, utx) } -func MintFTs( - backend TxBuilderBackend, +func (b *Builder) MintFTs( outputs map[ids.ID]*secp256k1fx.TransferOutput, kc *secp256k1fx.Keychain, changeAddr ids.ShortID, ) (*txs.Tx, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) utx, err := xBuilder.NewOperationTxMintFT( outputs, @@ -122,13 +136,12 @@ func MintFTs( return signer.SignUnsigned(context.Background(), xSigner, utx) } -func BuildOperation( - backend TxBuilderBackend, +func (b *Builder) BuildOperation( ops []*txs.Operation, kc *secp256k1fx.Keychain, changeAddr ids.ShortID, ) (*txs.Tx, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) utx, err := xBuilder.NewOperationTx( ops, @@ -141,13 +154,12 @@ func BuildOperation( return signer.SignUnsigned(context.Background(), xSigner, utx) } -func BuildImportTx( - backend TxBuilderBackend, +func (b *Builder) BuildImportTx( sourceChain ids.ID, to ids.ShortID, kc *secp256k1fx.Keychain, ) (*txs.Tx, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) outOwner := &secp256k1fx.OutputOwners{ Locktime: 0, @@ -166,8 +178,7 @@ func BuildImportTx( return signer.SignUnsigned(context.Background(), xSigner, utx) } -func BuildExportTx( - backend TxBuilderBackend, +func (b *Builder) BuildExportTx( destinationChain ids.ID, to ids.ShortID, exportedAssetID ids.ID, @@ -175,7 +186,7 @@ func BuildExportTx( kc *secp256k1fx.Keychain, changeAddr ids.ShortID, ) (*txs.Tx, ids.ShortID, error) { - xBuilder, xSigner := builders(backend, kc) + xBuilder, xSigner := b.builders(kc) outputs := []*avax.TransferableOutput{{ Asset: avax.Asset{ID: exportedAssetID}, @@ -205,14 +216,13 @@ func BuildExportTx( return tx, changeAddr, nil } -func builders(backend TxBuilderBackend, kc *secp256k1fx.Keychain) (builder.Builder, signer.Signer) { +func (b *Builder) builders(kc *secp256k1fx.Keychain) (builder.Builder, signer.Signer) { var ( addrs = kc.Addresses() - builder = builder.New(addrs, backend.Context(), backend) - signer = signer.New(kc, backend) + builder = builder.New(addrs, b.ctx, b.backend) + signer = signer.New(kc, b.backend) ) - - backend.ResetAddresses(addrs) + b.backend.ResetAddresses(addrs) return builder, signer } diff --git a/vms/avm/vm.go b/vms/avm/vm.go index 5b74efe88dcf..1c58cc367401 100644 --- a/vms/avm/vm.go +++ b/vms/avm/vm.go @@ -36,6 +36,7 @@ import ( "github.com/ava-labs/avalanchego/vms/avm/network" "github.com/ava-labs/avalanchego/vms/avm/state" "github.com/ava-labs/avalanchego/vms/avm/txs" + "github.com/ava-labs/avalanchego/vms/avm/txs/builder" "github.com/ava-labs/avalanchego/vms/avm/txs/mempool" "github.com/ava-labs/avalanchego/vms/avm/utxo" "github.com/ava-labs/avalanchego/vms/components/avax" @@ -243,7 +244,16 @@ func (vm *VM) Initialize( return err } - vm.walletService.walletServiceBackend = NewWalletServiceBackend(vm) + walletServiceBackend := NewWalletServiceBackend(vm) + vm.walletService = WalletService{ + walletServiceBackend: walletServiceBackend, + b: builder.NewBuilder( + vm.ctx, + &vm.Config, + vm.feeAssetID, + walletServiceBackend, + ), + } // use no op impl when disabled in config if avmConfig.IndexTransactions { @@ -337,8 +347,13 @@ func (vm *VM) CreateHandlers(context.Context) (map[string]http.Handler, error) { rpcServer.RegisterAfterFunc(vm.metrics.AfterRequest) // name this service "avm" if err := rpcServer.RegisterService(&Service{ - vm: vm, - txBuilderBackend: newServiceBackend(vm.feeAssetID, vm.ctx, &vm.Config, vm.state, vm.ctx.SharedMemory, vm.parser.Codec()), + vm: vm, + b: builder.NewBuilder( + vm.ctx, + &vm.Config, + vm.feeAssetID, + newServiceBackend(vm.ctx, &vm.Config, vm.state, vm.ctx.SharedMemory, vm.parser.Codec()), + ), }, "avm"); err != nil { return nil, err } diff --git a/vms/avm/wallet_service.go b/vms/avm/wallet_service.go index 8bcf9cde3d5a..fdea5debed70 100644 --- a/vms/avm/wallet_service.go +++ b/vms/avm/wallet_service.go @@ -26,6 +26,7 @@ var errMissingUTXO = errors.New("missing utxo") type WalletService struct { *walletServiceBackend + b *builder.Builder } func (w *WalletService) decided(txID ids.ID) { @@ -223,8 +224,7 @@ func (w *WalletService) SendMultiple(_ *http.Request, args *SendMultipleArgs, re }) } - w.walletServiceBackend.ResetAddresses(kc.Addresses()) - tx, _, err := builder.BuildBaseTx(w.walletServiceBackend, outs, memoBytes, kc, changeAddr) + tx, _, err := w.b.BuildBaseTx(outs, memoBytes, kc, changeAddr) if err != nil { return err } diff --git a/vms/avm/wallet_service_backend.go b/vms/avm/wallet_service_backend.go index 110458cf6e22..5a0088cdc132 100644 --- a/vms/avm/wallet_service_backend.go +++ b/vms/avm/wallet_service_backend.go @@ -15,23 +15,12 @@ import ( "github.com/ava-labs/avalanchego/vms/avm/txs" "github.com/ava-labs/avalanchego/vms/avm/txs/builder" "github.com/ava-labs/avalanchego/vms/components/avax" - - walletbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder" ) var _ builder.TxBuilderBackend = (*walletServiceBackend)(nil) func NewWalletServiceBackend(vm *VM) *walletServiceBackend { - backendCtx := &walletbuilder.Context{ - NetworkID: vm.ctx.NetworkID, - BlockchainID: vm.ctx.XChainID, - AVAXAssetID: vm.feeAssetID, - BaseTxFee: vm.Config.TxFee, - CreateAssetTxFee: vm.Config.CreateAssetTxFee, - } - return &walletServiceBackend{ - ctx: backendCtx, vm: vm, pendingTxs: linked.NewHashmap[ids.ID, *txs.Tx](), utxos: make([]*avax.UTXO, 0), @@ -39,7 +28,6 @@ func NewWalletServiceBackend(vm *VM) *walletServiceBackend { } type walletServiceBackend struct { - ctx *walletbuilder.Context vm *VM pendingTxs *linked.Hashmap[ids.ID, *txs.Tx] utxos []*avax.UTXO @@ -47,10 +35,6 @@ type walletServiceBackend struct { addrs set.Set[ids.ShortID] } -func (b *walletServiceBackend) Context() *walletbuilder.Context { - return b.ctx -} - func (b *walletServiceBackend) ResetAddresses(addrs set.Set[ids.ShortID]) { b.addrs = addrs }