Skip to content

Commit 17845e7

Browse files
committed
rpc: add utxo's blockhash and number of confirmations to scantxoutset output
1 parent 8754d05 commit 17845e7

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/rpc/blockchain.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ static RPCHelpMan scantxoutset()
21902190
RPCResult{"when action=='start'; only returns after scan completes", RPCResult::Type::OBJ, "", "", {
21912191
{RPCResult::Type::BOOL, "success", "Whether the scan was completed"},
21922192
{RPCResult::Type::NUM, "txouts", "The number of unspent transaction outputs scanned"},
2193-
{RPCResult::Type::NUM, "height", "The current block height (index)"},
2193+
{RPCResult::Type::NUM, "height", "The block height at which the scan was done"},
21942194
{RPCResult::Type::STR_HEX, "bestblock", "The hash of the block at the tip of the chain"},
21952195
{RPCResult::Type::ARR, "unspents", "",
21962196
{
@@ -2203,6 +2203,8 @@ static RPCHelpMan scantxoutset()
22032203
{RPCResult::Type::STR_AMOUNT, "amount", "The total amount in " + CURRENCY_UNIT + " of the unspent output"},
22042204
{RPCResult::Type::BOOL, "coinbase", "Whether this is a coinbase output"},
22052205
{RPCResult::Type::NUM, "height", "Height of the unspent transaction output"},
2206+
{RPCResult::Type::STR_HEX, "blockhash", "Blockhash of the unspent transaction output"},
2207+
{RPCResult::Type::NUM, "confirmations", "Number of confirmations of the unspent transaction output when the scan was done"},
22062208
}},
22072209
}},
22082210
{RPCResult::Type::STR_AMOUNT, "total_amount", "The total amount of all found unspent outputs in " + CURRENCY_UNIT},
@@ -2292,17 +2294,20 @@ static RPCHelpMan scantxoutset()
22922294
const COutPoint& outpoint = it.first;
22932295
const Coin& coin = it.second;
22942296
const CTxOut& txo = coin.out;
2297+
const CBlockIndex& coinb_block{*CHECK_NONFATAL(tip->GetAncestor(coin.nHeight))};
22952298
input_txos.push_back(txo);
22962299
total_in += txo.nValue;
22972300

22982301
UniValue unspent(UniValue::VOBJ);
22992302
unspent.pushKV("txid", outpoint.hash.GetHex());
2300-
unspent.pushKV("vout", (int32_t)outpoint.n);
2303+
unspent.pushKV("vout", outpoint.n);
23012304
unspent.pushKV("scriptPubKey", HexStr(txo.scriptPubKey));
23022305
unspent.pushKV("desc", descriptors[txo.scriptPubKey]);
23032306
unspent.pushKV("amount", ValueFromAmount(txo.nValue));
23042307
unspent.pushKV("coinbase", coin.IsCoinBase());
2305-
unspent.pushKV("height", (int32_t)coin.nHeight);
2308+
unspent.pushKV("height", coin.nHeight);
2309+
unspent.pushKV("blockhash", coinb_block.GetBlockHash().GetHex());
2310+
unspent.pushKV("confirmations", tip->nHeight - coin.nHeight + 1);
23062311

23072312
unspents.push_back(std::move(unspent));
23082313
}

test/functional/rpc_scantxoutset.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ def run_test(self):
120120
assert_equal(self.nodes[0].scantxoutset("status"), None)
121121
assert_equal(self.nodes[0].scantxoutset("abort"), False)
122122

123-
# check that first arg is needed
123+
# Check that the blockhash and confirmations fields are correct
124+
self.generate(self.nodes[0], 2)
125+
unspent = self.nodes[0].scantxoutset("start", ["addr(mpQ8rokAhp1TAtJQR6F6TaUmjAWkAWYYBq)"])["unspents"][0]
126+
blockhash = self.nodes[0].getblockhash(info["height"])
127+
assert_equal(unspent["height"], info["height"])
128+
assert_equal(unspent["blockhash"], blockhash)
129+
assert_equal(unspent["confirmations"], 3)
130+
131+
# Check that first arg is needed
124132
assert_raises_rpc_error(-1, "scantxoutset \"action\" ( [scanobjects,...] )", self.nodes[0].scantxoutset)
125133

126134
# Check that second arg is needed for start

0 commit comments

Comments
 (0)