From 593d58dc684ac1ac3806f654451c69e7784b5eb7 Mon Sep 17 00:00:00 2001 From: Tom Briar Date: Wed, 6 Dec 2023 15:55:30 -0500 Subject: [PATCH] node: Added vector based coin lookup --- src/node/coin.cpp | 19 +++++++++++++++++++ src/node/coin.h | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/node/coin.cpp b/src/node/coin.cpp index 221854c5f67d5..056c68a9b39af 100644 --- a/src/node/coin.cpp +++ b/src/node/coin.cpp @@ -23,4 +23,23 @@ void FindCoins(const NodeContext& node, std::map& coins) } } } + +std::vector FindCoins(const node::NodeContext& node, const std::vector& outpoints) +{ + assert(node.mempool); + assert(node.chainman); + LOCK2(cs_main, node.mempool->cs); + CCoinsViewCache& chain_view = node.chainman->ActiveChainstate().CoinsTip(); + CCoinsViewMemPool mempool_view(&chain_view, *node.mempool); + std::vector coins; + for (auto& outpoint : outpoints) { + Coin coin; + if (!mempool_view.GetCoin(outpoint, coin)) { + // Either the coin is not in the CCoinsViewCache or is spent. Clear it. + coin.Clear(); + } + coins.push_back(coin); + } + return coins; +} } // namespace node diff --git a/src/node/coin.h b/src/node/coin.h index b32e410e1cceb..9c3213b69efa9 100644 --- a/src/node/coin.h +++ b/src/node/coin.h @@ -6,6 +6,7 @@ #define BITCOIN_NODE_COIN_H #include +#include class COutPoint; class Coin; @@ -22,6 +23,18 @@ struct NodeContext; * @param[in,out] coins map to fill */ void FindCoins(const node::NodeContext& node, std::map& coins); + +/** + * Look up unspent output information. Returns coins in the mempool and in the + * current chain UTXO set. Iterates through all the outpoints and + * returns the corresponding coins. + * + * @param[in] node The node context to use for lookup + * @param[in] outpoints A Vector containing outpoints + */ +std::vector FindCoins(const node::NodeContext& node, const std::vector& outpoints); } // namespace node + + #endif // BITCOIN_NODE_COIN_H