Skip to content

Commit 343dc47

Browse files
committed
test: add test for high-bandwidth mode states in getpeerinfo
1 parent dab6583 commit 343dc47

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

test/functional/p2p_compactblocks.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,34 @@ def announce_cmpct_block(node, peer):
768768
stalling_peer.send_and_ping(msg)
769769
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
770770

771+
def test_highbandwidth_mode_states_via_getpeerinfo(self):
772+
# create new p2p connection for a fresh state w/o any prior sendcmpct messages sent
773+
hb_test_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=2))
774+
775+
# assert the RPC getpeerinfo boolean fields `bip152_hb_{to, from}`
776+
# match the given parameters for the last peer of a given node
777+
def assert_highbandwidth_states(node, hb_to, hb_from):
778+
peerinfo = node.getpeerinfo()[-1]
779+
assert_equal(peerinfo['bip152_hb_to'], hb_to)
780+
assert_equal(peerinfo['bip152_hb_from'], hb_from)
781+
782+
# initially, neither node has selected the other peer as high-bandwidth yet
783+
assert_highbandwidth_states(self.nodes[0], hb_to=False, hb_from=False)
784+
785+
# peer requests high-bandwidth mode by sending sendcmpct(1)
786+
hb_test_node.send_and_ping(msg_sendcmpct(announce=True, version=2))
787+
assert_highbandwidth_states(self.nodes[0], hb_to=False, hb_from=True)
788+
789+
# peer generates a block and sends it to node, which should
790+
# select the peer as high-bandwidth (up to 3 peers according to BIP 152)
791+
block = self.build_block_on_tip(self.nodes[0])
792+
hb_test_node.send_and_ping(msg_block(block))
793+
assert_highbandwidth_states(self.nodes[0], hb_to=True, hb_from=True)
794+
795+
# peer requests low-bandwidth mode by sending sendcmpct(0)
796+
hb_test_node.send_and_ping(msg_sendcmpct(announce=False, version=2))
797+
assert_highbandwidth_states(self.nodes[0], hb_to=True, hb_from=False)
798+
771799
def run_test(self):
772800
# Setup the p2p connections
773801
self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=2))
@@ -823,6 +851,9 @@ def run_test(self):
823851
self.log.info("Testing invalid index in cmpctblock message...")
824852
self.test_invalid_cmpctblock_message()
825853

854+
self.log.info("Testing high-bandwidth mode states via getpeerinfo...")
855+
self.test_highbandwidth_mode_states_via_getpeerinfo()
856+
826857

827858
if __name__ == '__main__':
828859
CompactBlocksTest().main()

0 commit comments

Comments
 (0)