github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/txs/txstest/backend.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package txstest
     5  
     6  import (
     7  	"context"
     8  	"math"
     9  
    10  	"github.com/MetalBlockchain/metalgo/chains/atomic"
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/utils/constants"
    13  	"github.com/MetalBlockchain/metalgo/utils/set"
    14  	"github.com/MetalBlockchain/metalgo/vms/components/avax"
    15  	"github.com/MetalBlockchain/metalgo/vms/platformvm/fx"
    16  	"github.com/MetalBlockchain/metalgo/vms/platformvm/state"
    17  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    18  	"github.com/MetalBlockchain/metalgo/wallet/chain/p/builder"
    19  	"github.com/MetalBlockchain/metalgo/wallet/chain/p/signer"
    20  )
    21  
    22  var (
    23  	_ builder.Backend = (*Backend)(nil)
    24  	_ signer.Backend  = (*Backend)(nil)
    25  )
    26  
    27  func newBackend(
    28  	addrs set.Set[ids.ShortID],
    29  	state state.State,
    30  	sharedMemory atomic.SharedMemory,
    31  ) *Backend {
    32  	return &Backend{
    33  		addrs:        addrs,
    34  		state:        state,
    35  		sharedMemory: sharedMemory,
    36  	}
    37  }
    38  
    39  type Backend struct {
    40  	addrs        set.Set[ids.ShortID]
    41  	state        state.State
    42  	sharedMemory atomic.SharedMemory
    43  }
    44  
    45  func (b *Backend) UTXOs(_ context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error) {
    46  	if sourceChainID == constants.PlatformChainID {
    47  		return avax.GetAllUTXOs(b.state, b.addrs)
    48  	}
    49  
    50  	utxos, _, _, err := avax.GetAtomicUTXOs(
    51  		b.sharedMemory,
    52  		txs.Codec,
    53  		sourceChainID,
    54  		b.addrs,
    55  		ids.ShortEmpty,
    56  		ids.Empty,
    57  		math.MaxInt,
    58  	)
    59  	return utxos, err
    60  }
    61  
    62  func (b *Backend) GetUTXO(_ context.Context, chainID, utxoID ids.ID) (*avax.UTXO, error) {
    63  	if chainID == constants.PlatformChainID {
    64  		return b.state.GetUTXO(utxoID)
    65  	}
    66  
    67  	utxoBytes, err := b.sharedMemory.Get(chainID, [][]byte{utxoID[:]})
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	utxo := avax.UTXO{}
    73  	if _, err := txs.Codec.Unmarshal(utxoBytes[0], &utxo); err != nil {
    74  		return nil, err
    75  	}
    76  	return &utxo, nil
    77  }
    78  
    79  func (b *Backend) GetSubnetOwner(_ context.Context, subnetID ids.ID) (fx.Owner, error) {
    80  	return b.state.GetSubnetOwner(subnetID)
    81  }