Skip to content

Commit

Permalink
[test] Add test for cfilters.
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#18876
Rebased-From: 1e8a696
  • Loading branch information
jimpo authored and luke-jr committed May 14, 2020
1 parent f250151 commit a34126e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
69 changes: 67 additions & 2 deletions test/functional/p2p_blockfilters.py
Expand Up @@ -13,6 +13,7 @@
hash256,
msg_getcfcheckpt,
msg_getcfheaders,
msg_getcfilters,
ser_uint256,
uint256_from_str,
)
Expand All @@ -25,6 +26,21 @@
wait_until,
)

class CFiltersClient(P2PInterface):
def __init__(self):
super().__init__()
# Store the cfilters received.
self.cfilters = []

def pop_cfilters(self):
cfilters = self.cfilters
self.cfilters = []
return cfilters

def on_cfilter(self, message):
"""Store cfilters received in a list."""
self.cfilters.append(message)

class CompactFiltersTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
Expand All @@ -37,8 +53,8 @@ def set_test_params(self):

def run_test(self):
# Node 0 supports COMPACT_FILTERS, node 1 does not.
node0 = self.nodes[0].add_p2p_connection(P2PInterface())
node1 = self.nodes[1].add_p2p_connection(P2PInterface())
node0 = self.nodes[0].add_p2p_connection(CFiltersClient())
node1 = self.nodes[1].add_p2p_connection(CFiltersClient())

# Nodes 0 & 1 share the same first 999 blocks in the chain.
self.nodes[0].generate(999)
Expand Down Expand Up @@ -134,6 +150,44 @@ def run_test(self):
int(stale_cfcheckpt, 16)
)

self.log.info("Check that peers can fetch cfilters.")
stop_hash = self.nodes[0].getblockhash(10)
request = msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=1,
stop_hash=int(stop_hash, 16)
)
node0.send_message(request)
node0.sync_with_ping()
response = node0.pop_cfilters()
assert_equal(len(response), 10)

self.log.info("Check that cfilter responses are correct.")
for cfilter, cfhash, height in zip(response, main_cfhashes, range(1, 11)):
block_hash = self.nodes[0].getblockhash(height)
assert_equal(cfilter.filter_type, FILTER_TYPE_BASIC)
assert_equal(cfilter.block_hash, int(block_hash, 16))
computed_cfhash = uint256_from_str(hash256(cfilter.filter_data))
assert_equal(computed_cfhash, cfhash)

self.log.info("Check that peers can fetch cfilters for stale blocks.")
stop_hash = self.nodes[0].getblockhash(10)
request = msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=1000,
stop_hash=int(stale_block_hash, 16)
)
node0.send_message(request)
node0.sync_with_ping()
response = node0.pop_cfilters()
assert_equal(len(response), 1)

cfilter = response[0]
assert_equal(cfilter.filter_type, FILTER_TYPE_BASIC)
assert_equal(cfilter.block_hash, int(stale_block_hash, 16))
computed_cfhash = uint256_from_str(hash256(cfilter.filter_data))
assert_equal(computed_cfhash, stale_cfhashes[999])

self.log.info("Requests to node 1 without NODE_COMPACT_FILTERS results in disconnection.")
requests = [
msg_getcfcheckpt(
Expand All @@ -145,6 +199,11 @@ def run_test(self):
start_height=1000,
stop_hash=int(main_block_hash, 16)
),
msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=1000,
stop_hash=int(main_block_hash, 16)
),
]
for request in requests:
node1 = self.nodes[1].add_p2p_connection(P2PInterface())
Expand All @@ -153,6 +212,12 @@ def run_test(self):

self.log.info("Check that invalid requests result in disconnection.")
requests = [
# Requesting too many filters results in disconnection.
msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=0,
stop_hash=int(main_block_hash, 16)
),
# Requesting too many filter headers results in disconnection.
msg_getcfheaders(
filter_type=FILTER_TYPE_BASIC,
Expand Down
51 changes: 51 additions & 0 deletions test/functional/test_framework/messages.py
Expand Up @@ -1500,6 +1500,57 @@ class msg_no_witness_blocktxn(msg_blocktxn):
def serialize(self):
return self.block_transactions.serialize(with_witness=False)


class msg_getcfilters:
__slots__ = ("filter_type", "start_height", "stop_hash")
command = b"getcfilters"

def __init__(self, filter_type, start_height, stop_hash):
self.filter_type = filter_type
self.start_height = start_height
self.stop_hash = stop_hash

def deserialize(self, f):
self.filter_type = struct.unpack("<B", f.read(1))[0]
self.start_height = struct.unpack("<I", f.read(4))[0]
self.stop_hash = deser_uint256(f)

def serialize(self):
r = b""
r += struct.pack("<B", self.filter_type)
r += struct.pack("<I", self.start_height)
r += ser_uint256(self.stop_hash)
return r

def __repr__(self):
return "msg_getcfilters(filter_type={:#x}, start_height={}, stop_hash={:x})".format(
self.filter_type, self.start_height, self.stop_hash)

class msg_cfilter:
__slots__ = ("filter_type", "block_hash", "filter_data")
command = b"cfilter"

def __init__(self, filter_type=None, block_hash=None, filter_data=None):
self.filter_type = filter_type
self.block_hash = block_hash
self.filter_data = filter_data

def deserialize(self, f):
self.filter_type = struct.unpack("<B", f.read(1))[0]
self.block_hash = deser_uint256(f)
self.filter_data = deser_string(f)

def serialize(self):
r = b""
r += struct.pack("<B", self.filter_type)
r += ser_uint256(self.block_hash)
r += ser_string(self.filter_data)
return r

def __repr__(self):
return "msg_cfilter(filter_type={:#x}, block_hash={:x})".format(
self.filter_type, self.block_hash)

class msg_getcfheaders:
__slots__ = ("filter_type", "start_height", "stop_hash")
command = b"getcfheaders"
Expand Down
3 changes: 3 additions & 0 deletions test/functional/test_framework/mininode.py
Expand Up @@ -28,6 +28,7 @@
msg_block,
MSG_BLOCK,
msg_blocktxn,
msg_cfilter,
msg_cfheaders,
msg_cfcheckpt,
msg_cmpctblock,
Expand Down Expand Up @@ -66,6 +67,7 @@
b"addr": msg_addr,
b"block": msg_block,
b"blocktxn": msg_blocktxn,
b"cfilter": msg_cfilter,
b"cfheaders": msg_cfheaders,
b"cfcheckpt": msg_cfcheckpt,
b"cmpctblock": msg_cmpctblock,
Expand Down Expand Up @@ -328,6 +330,7 @@ def on_close(self):
def on_addr(self, message): pass
def on_block(self, message): pass
def on_blocktxn(self, message): pass
def on_cfilter(self, message): pass
def on_cfheaders(self, message): pass
def on_cfcheckpt(self, message): pass
def on_cmpctblock(self, message): pass
Expand Down

0 comments on commit a34126e

Please sign in to comment.