mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-21 22:36:42 +00:00

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"github.com/kaspanet/kaspad/rpcclient"
|
|
"github.com/kaspanet/kaspad/rpcmodel"
|
|
"github.com/kaspanet/kaspad/util"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
resultsCount = 1000
|
|
minConfirmations = 10
|
|
)
|
|
|
|
func findUnspentTXO(cfg *ConfigFlags, client *rpcclient.Client, addrPubKeyHash *util.AddressPubKeyHash) (*wire.Outpoint, *wire.MsgTx, error) {
|
|
txs, err := collectTransactions(client, addrPubKeyHash)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
utxos := buildUTXOs(txs)
|
|
for outpoint, tx := range utxos {
|
|
// Skip TXOs that can't pay for registration
|
|
if tx.TxOut[outpoint.Index].Value < cfg.RegistryTxFee {
|
|
continue
|
|
}
|
|
|
|
return &outpoint, tx, nil
|
|
}
|
|
|
|
return nil, nil, nil
|
|
}
|
|
|
|
func collectTransactions(client *rpcclient.Client, addrPubKeyHash *util.AddressPubKeyHash) ([]*wire.MsgTx, error) {
|
|
txs := make([]*wire.MsgTx, 0)
|
|
skip := 0
|
|
for {
|
|
results, err := client.SearchRawTransactionsVerbose(addrPubKeyHash, skip, resultsCount, true, false, nil)
|
|
if err != nil {
|
|
// Break when there are no further txs
|
|
if rpcError, ok := err.(*rpcmodel.RPCError); ok && rpcError.Code == rpcmodel.ErrRPCNoTxInfo {
|
|
break
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
for _, result := range results {
|
|
// Mempool transactions bring about unnecessary complexity, so
|
|
// simply don't bother processing them
|
|
if result.IsInMempool {
|
|
continue
|
|
}
|
|
|
|
tx, err := parseRawTransactionResult(result)
|
|
if err != nil {
|
|
return nil, errors.Errorf("failed to process SearchRawTransactionResult: %s", err)
|
|
}
|
|
if tx == nil {
|
|
continue
|
|
}
|
|
if !isTxMatured(tx, *result.Confirmations) {
|
|
continue
|
|
}
|
|
|
|
txs = append(txs, tx)
|
|
}
|
|
|
|
skip += resultsCount
|
|
}
|
|
return txs, nil
|
|
}
|
|
|
|
func parseRawTransactionResult(result *rpcmodel.SearchRawTransactionsResult) (*wire.MsgTx, error) {
|
|
txBytes, err := hex.DecodeString(result.Hex)
|
|
if err != nil {
|
|
return nil, errors.Errorf("failed to decode transaction bytes: %s", err)
|
|
}
|
|
var tx wire.MsgTx
|
|
reader := bytes.NewReader(txBytes)
|
|
err = tx.Deserialize(reader)
|
|
if err != nil {
|
|
return nil, errors.Errorf("failed to deserialize transaction: %s", err)
|
|
}
|
|
return &tx, nil
|
|
}
|
|
|
|
func isTxMatured(tx *wire.MsgTx, confirmations uint64) bool {
|
|
if !tx.IsCoinBase() {
|
|
return confirmations >= minConfirmations
|
|
}
|
|
return confirmations >= ActiveConfig().NetParams().BlockCoinbaseMaturity
|
|
}
|
|
|
|
func buildUTXOs(txs []*wire.MsgTx) map[wire.Outpoint]*wire.MsgTx {
|
|
utxos := make(map[wire.Outpoint]*wire.MsgTx)
|
|
for _, tx := range txs {
|
|
for i := range tx.TxOut {
|
|
outpoint := wire.NewOutpoint(tx.TxID(), uint32(i))
|
|
utxos[*outpoint] = tx
|
|
}
|
|
}
|
|
for _, tx := range txs {
|
|
for _, input := range tx.TxIn {
|
|
delete(utxos, input.PreviousOutpoint)
|
|
}
|
|
}
|
|
return utxos
|
|
}
|