Files
zeekayandHanzo Dev 938b06d485 Stop requiring the node this module is imported by
proto required luxfi/node and luxfi/node required proto, with no replace
directive on either side. The cycle was not load-bearing: most of it was proto
reaching into the node for packages proto already owns — p/warp, p/txs,
p/signer, p/stakeable, p/security, x/txs — which now name themselves.

Two were real. The UTXO components moved here, where the chains that use them
already live, and the node is the only other caller, so that dependency now
runs one way. And p/config held the node's chain manager to queue a chain the
P-Chain had accepted a CreateChainTx for; it calls one method, so it declares
one method. A P-Chain that imports the node to describe QueueChainCreation is
what made two modules require each other.

Dropping the requirement dropped a registration with it: lux.ParseUTXO is a
registry filled by an init, which the node used to link in on proto's behalf.
The packages that call it now say so themselves.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-06 12:49:55 -07:00

102 lines
2.5 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package lux
import (
"bytes"
"cmp"
"errors"
"fmt"
"strconv"
"strings"
"github.com/luxfi/ids"
"github.com/luxfi/vm/components/verify"
"github.com/luxfi/util"
)
var (
errNilUTXOID = errors.New("nil utxo ID is not valid")
errMalformedUTXOIDString = errors.New("unexpected number of tokens in string")
errFailedDecodingUTXOIDTxID = errors.New("failed decoding UTXOID TxID")
errFailedDecodingUTXOIDIndex = errors.New("failed decoding UTXOID index")
_ verify.Verifiable = (*UTXOID)(nil)
_ utils.Sortable[*UTXOID] = (*UTXOID)(nil)
)
type UTXOID struct {
// Serialized:
TxID ids.ID `serialize:"true" json:"txID"`
OutputIndex uint32 `serialize:"true" json:"outputIndex"`
// Symbol is false if the UTXO should be part of the DB
Symbol bool `json:"-"`
// id is the unique ID of a UTXO, it is calculated from TxID and OutputIndex
id ids.ID
}
// InputSource returns the source of the UTXO that this input is spending
func (utxo *UTXOID) InputSource() (ids.ID, uint32) {
return utxo.TxID, utxo.OutputIndex
}
// InputID returns a unique ID of the UTXO that this input is spending
func (utxo *UTXOID) InputID() ids.ID {
if utxo.id == ids.Empty {
utxo.id = utxo.TxID.Prefix(uint64(utxo.OutputIndex))
}
return utxo.id
}
// Symbolic returns if this is the ID of a UTXO in the DB, or if it is a
// symbolic input
func (utxo *UTXOID) Symbolic() bool {
return utxo.Symbol
}
func (utxo *UTXOID) String() string {
return fmt.Sprintf("%s:%d", utxo.TxID, utxo.OutputIndex)
}
// UTXOIDFromString attempts to parse a string into a UTXOID
func UTXOIDFromString(s string) (*UTXOID, error) {
ss := strings.Split(s, ":")
if len(ss) != 2 {
return nil, errMalformedUTXOIDString
}
txID, err := ids.FromString(ss[0])
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedDecodingUTXOIDTxID, err)
}
idx, err := strconv.ParseUint(ss[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedDecodingUTXOIDIndex, err)
}
return &UTXOID{
TxID: txID,
OutputIndex: uint32(idx),
}, nil
}
func (utxo *UTXOID) Verify() error {
if utxo == nil {
return errNilUTXOID
}
return nil
}
func (utxo *UTXOID) Compare(other *UTXOID) int {
utxoID, utxoIndex := utxo.InputSource()
otherID, otherIndex := other.InputSource()
if txIDComp := bytes.Compare(utxoID[:], otherID[:]); txIDComp != 0 {
return txIDComp
}
return cmp.Compare(utxoIndex, otherIndex)
}