Skip to content

Commit c133cdc

Browse files
committed
Cap listsinceblock target_confirmations param
Previously, listsinceblock would fail with error code -1 when the target_confirmations exceeded the number of confirmations of the genesis block. This commit allows target_confirmations to refer to a lastblock hash with more confirmations than exist in the chain by setting the lastblock hash to the genesis hash in this case. This allows for `listsinceblock "" 6` to not fail if the block count is less than 5 which may happen on regtest. Includes update to the functional test for listsinceblock to test for this case.
1 parent 007e15d commit c133cdc

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
14841484
{RPCResult::Type::ARR, "removed", "<structure is the same as \"transactions\" above, only present if include_removed=true>\n"
14851485
"Note: transactions that were re-added in the active chain will appear as-is in this array, and may thus have a positive confirmation count."
14861486
, {{RPCResult::Type::ELISION, "", ""},}},
1487-
{RPCResult::Type::STR_HEX, "lastblock", "The hash of the block (target_confirmations-1) from the best block on the main chain. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones"},
1487+
{RPCResult::Type::STR_HEX, "lastblock", "The hash of the block (target_confirmations-1) from the best block on the main chain, or the genesis hash if the referenced block does not exist yet. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones"},
14881488
}
14891489
},
14901490
RPCExamples{
@@ -1567,6 +1567,7 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
15671567
}
15681568

15691569
uint256 lastblock;
1570+
target_confirms = std::min(target_confirms, wallet.GetLastBlockHeight() + 1);
15701571
CHECK_NONFATAL(wallet.chain().findAncestorByHeight(wallet.GetLastBlockHash(), wallet.GetLastBlockHeight() + 1 - target_confirms, FoundBlock().hash(lastblock)));
15711572

15721573
UniValue ret(UniValue::VOBJ);

test/functional/wallet_listsinceblock.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def run_test(self):
3636
self.test_double_spend()
3737
self.test_double_send()
3838
self.double_spends_filtered()
39+
self.test_targetconfirmations()
3940

4041
def test_no_blockhash(self):
4142
self.log.info("Test no blockhash")
@@ -74,6 +75,27 @@ def test_invalid_blockhash(self):
7475
assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'Z000000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].listsinceblock,
7576
"Z000000000000000000000000000000000000000000000000000000000000000")
7677

78+
def test_targetconfirmations(self):
79+
'''
80+
This tests when the value of target_confirmations exceeds the number of
81+
blocks in the main chain. In this case, the genesis block hash should be
82+
given for the `lastblock` property. If target_confirmations is < 1, then
83+
a -8 invalid parameter error is thrown.
84+
'''
85+
self.log.info("Test target_confirmations")
86+
blockhash, = self.nodes[2].generate(1)
87+
blockheight = self.nodes[2].getblockheader(blockhash)['height']
88+
self.sync_all()
89+
90+
assert_equal(
91+
self.nodes[0].getblockhash(0),
92+
self.nodes[0].listsinceblock(blockhash, blockheight + 1)['lastblock'])
93+
assert_equal(
94+
self.nodes[0].getblockhash(0),
95+
self.nodes[0].listsinceblock(blockhash, blockheight + 1000)['lastblock'])
96+
assert_raises_rpc_error(-8, "Invalid parameter",
97+
self.nodes[0].listsinceblock, blockhash, 0)
98+
7799
def test_reorg(self):
78100
'''
79101
`listsinceblock` did not behave correctly when handed a block that was

0 commit comments

Comments
 (0)