Skip to content

Commit fa54d30

Browse files
committed
test: check for false-positives in rpc_scanblocks.py
1 parent 3bca6cd commit fa54d30

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/functional/rpc_scanblocks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the scanblocks RPC call."""
6+
from test_framework.blockfilter import (
7+
bip158_basic_element_hash,
8+
bip158_relevant_scriptpubkeys,
9+
)
610
from test_framework.messages import COIN
711
from test_framework.test_framework import BitcoinTestFramework
812
from test_framework.util import (
@@ -71,6 +75,28 @@ def run_test(self):
7175
assert(blockhash in node.scanblocks(
7276
"start", [{"desc": f"pkh({parent_key}/*)", "range": [0, 100]}], height)['relevant_blocks'])
7377

78+
# check that false-positives are included in the result now; note that
79+
# finding a false-positive at runtime would take too long, hence we simply
80+
# use a pre-calculated one that collides with the regtest genesis block's
81+
# coinbase output and verify that their BIP158 ranged hashes match
82+
genesis_blockhash = node.getblockhash(0)
83+
genesis_spks = bip158_relevant_scriptpubkeys(node, genesis_blockhash)
84+
assert_equal(len(genesis_spks), 1)
85+
genesis_coinbase_spk = list(genesis_spks)[0]
86+
false_positive_spk = bytes.fromhex("001400000000000000000000000000000000000cadcb")
87+
88+
genesis_coinbase_hash = bip158_basic_element_hash(genesis_coinbase_spk, 1, genesis_blockhash)
89+
false_positive_hash = bip158_basic_element_hash(false_positive_spk, 1, genesis_blockhash)
90+
assert_equal(genesis_coinbase_hash, false_positive_hash)
91+
92+
assert(genesis_blockhash in node.scanblocks(
93+
"start", [{"desc": f"raw({genesis_coinbase_spk.hex()})"}], 0, 0)['relevant_blocks'])
94+
assert(genesis_blockhash in node.scanblocks(
95+
"start", [{"desc": f"raw({false_positive_spk.hex()})"}], 0, 0)['relevant_blocks'])
96+
97+
# TODO: after an "accurate" mode for scanblocks is implemented (e.g. PR #26325)
98+
# check here that it filters out the false-positive
99+
74100
# test node with disabled blockfilterindex
75101
assert_raises_rpc_error(-1, "Index is not enabled for filtertype basic",
76102
self.nodes[1].scanblocks, "start", [f"addr({addr_1})"])

0 commit comments

Comments
 (0)