Merge #16285: rpc: Improve scantxoutset response and help message

bdd6a4fd5d qa: Check scantxoutset result against gettxoutsetinfo (João Barbosa)
fc0c410d6e rpc: Improve scantxoutset response and help message (João Barbosa)

Pull request description:

  The new response keys `height` and `bestblock` allow the client to know at what point the scan took place.

  The help message now has all the response keys (`result` and `txouts` were missing) and it's improved a bit. Note that `searched_items` key is renamed to `txouts`, considering `scantxoutset` is marked experimental.

ACKs for top commit:
  laanwj:
    ACK bdd6a4fd5d

Tree-SHA512: 6bb7c3464b19857b756b8bc491ab7c58b0d948aad8c005b26ed27c55a1278f5639217e11a315bb505b4f44ebe86f413068c1e539c8a5f7a4007735586cc6443c
This commit is contained in:
Wladimir J. van der Laan 2019-09-09 08:08:32 +02:00
commit 08bb4c3156
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
2 changed files with 26 additions and 10 deletions

View file

@ -2064,17 +2064,21 @@ UniValue scantxoutset(const JSONRPCRequest& request)
}, },
RPCResult{ RPCResult{
"{\n" "{\n"
" \"success\": true|false, (boolean) Whether the scan was completed\n"
" \"txouts\": n, (numeric) The number of unspent transaction outputs scanned\n"
" \"height\": n, (numeric) The current block height (index)\n"
" \"bestblock\": \"hex\", (string) The hash of the block at the tip of the chain\n"
" \"unspents\": [\n" " \"unspents\": [\n"
" {\n" " {\n"
" \"txid\" : \"transactionid\", (string) The transaction id\n" " \"txid\": \"hash\", (string) The transaction id\n"
" \"vout\": n, (numeric) the vout value\n" " \"vout\": n, (numeric) The vout value\n"
" \"scriptPubKey\" : \"script\", (string) the script key\n" " \"scriptPubKey\": \"script\", (string) The script key\n"
" \"desc\" : \"descriptor\", (string) A specialized descriptor for the matched scriptPubKey\n" " \"desc\": \"descriptor\", (string) A specialized descriptor for the matched scriptPubKey\n"
" \"amount\" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " of the unspent output\n" " \"amount\": x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " of the unspent output\n"
" \"height\" : n, (numeric) Height of the unspent transaction output\n" " \"height\": n, (numeric) Height of the unspent transaction output\n"
" }\n" " }\n"
" ,...], \n" " ,...],\n"
" \"total_amount\" : x.xxx, (numeric) The total amount of all found unspent outputs in " + CURRENCY_UNIT + "\n" " \"total_amount\": x.xxx, (numeric) The total amount of all found unspent outputs in " + CURRENCY_UNIT + "\n"
"]\n" "]\n"
}, },
RPCExamples{""}, RPCExamples{""},
@ -2128,15 +2132,20 @@ UniValue scantxoutset(const JSONRPCRequest& request)
g_scan_progress = 0; g_scan_progress = 0;
int64_t count = 0; int64_t count = 0;
std::unique_ptr<CCoinsViewCursor> pcursor; std::unique_ptr<CCoinsViewCursor> pcursor;
CBlockIndex* tip;
{ {
LOCK(cs_main); LOCK(cs_main);
::ChainstateActive().ForceFlushStateToDisk(); ::ChainstateActive().ForceFlushStateToDisk();
pcursor = std::unique_ptr<CCoinsViewCursor>(::ChainstateActive().CoinsDB().Cursor()); pcursor = std::unique_ptr<CCoinsViewCursor>(::ChainstateActive().CoinsDB().Cursor());
assert(pcursor); assert(pcursor);
tip = ::ChainActive().Tip();
assert(tip);
} }
bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins); bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins);
result.pushKV("success", res); result.pushKV("success", res);
result.pushKV("searched_items", count); result.pushKV("txouts", count);
result.pushKV("height", tip->nHeight);
result.pushKV("bestblock", tip->GetBlockHash().GetHex());
for (const auto& it : coins) { for (const auto& it : coins) {
const COutPoint& outpoint = it.first; const COutPoint& outpoint = it.first;

View file

@ -58,6 +58,13 @@ class ScantxoutsetTest(BitcoinTestFramework):
self.start_node(0) self.start_node(0)
self.nodes[0].generate(110) self.nodes[0].generate(110)
scan = self.nodes[0].scantxoutset("start", [])
info = self.nodes[0].gettxoutsetinfo()
assert_equal(scan['success'], True)
assert_equal(scan['height'], info['height'])
assert_equal(scan['txouts'], info['txouts'])
assert_equal(scan['bestblock'], info['bestblock'])
self.restart_node(0, ['-nowallet']) self.restart_node(0, ['-nowallet'])
self.log.info("Test if we have found the non HD unspent outputs.") self.log.info("Test if we have found the non HD unspent outputs.")
assert_equal(self.nodes[0].scantxoutset("start", [ "pkh(" + pubk1 + ")", "pkh(" + pubk2 + ")", "pkh(" + pubk3 + ")"])['total_amount'], Decimal("0.002")) assert_equal(self.nodes[0].scantxoutset("start", [ "pkh(" + pubk1 + ")", "pkh(" + pubk2 + ")", "pkh(" + pubk3 + ")"])['total_amount'], Decimal("0.002"))