github.com/ava-labs/avalanchego@v1.11.11/vms/components/avax/atomic_utxos.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package avax
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/ava-labs/avalanchego/chains/atomic"
    10  	"github.com/ava-labs/avalanchego/codec"
    11  	"github.com/ava-labs/avalanchego/ids"
    12  	"github.com/ava-labs/avalanchego/utils/set"
    13  )
    14  
    15  // GetAtomicUTXOs returns exported UTXOs such that at least one of the
    16  // addresses in [addrs] is referenced.
    17  //
    18  // Returns at most [limit] UTXOs.
    19  //
    20  // Returns:
    21  // * The fetched UTXOs
    22  // * The address associated with the last UTXO fetched
    23  // * The ID of the last UTXO fetched
    24  // * Any error that may have occurred upstream.
    25  func GetAtomicUTXOs(
    26  	sharedMemory atomic.SharedMemory,
    27  	codec codec.Manager,
    28  	chainID ids.ID,
    29  	addrs set.Set[ids.ShortID],
    30  	startAddr ids.ShortID,
    31  	startUTXOID ids.ID,
    32  	limit int,
    33  ) ([]*UTXO, ids.ShortID, ids.ID, error) {
    34  	addrsList := make([][]byte, addrs.Len())
    35  	i := 0
    36  	for addr := range addrs {
    37  		copied := addr
    38  		addrsList[i] = copied[:]
    39  		i++
    40  	}
    41  
    42  	allUTXOBytes, lastAddr, lastUTXO, err := sharedMemory.Indexed(
    43  		chainID,
    44  		addrsList,
    45  		startAddr.Bytes(),
    46  		startUTXOID[:],
    47  		limit,
    48  	)
    49  	if err != nil {
    50  		return nil, ids.ShortID{}, ids.Empty, fmt.Errorf("error fetching atomic UTXOs: %w", err)
    51  	}
    52  
    53  	lastAddrID, err := ids.ToShortID(lastAddr)
    54  	if err != nil {
    55  		lastAddrID = ids.ShortEmpty
    56  	}
    57  	lastUTXOID, err := ids.ToID(lastUTXO)
    58  	if err != nil {
    59  		lastUTXOID = ids.Empty
    60  	}
    61  
    62  	utxos := make([]*UTXO, len(allUTXOBytes))
    63  	for i, utxoBytes := range allUTXOBytes {
    64  		utxo := &UTXO{}
    65  		if _, err := codec.Unmarshal(utxoBytes, utxo); err != nil {
    66  			return nil, ids.ShortID{}, ids.Empty, fmt.Errorf("error parsing UTXO: %w", err)
    67  		}
    68  		utxos[i] = utxo
    69  	}
    70  	return utxos, lastAddrID, lastUTXOID, nil
    71  }