@@ -768,6 +768,34 @@ def announce_cmpct_block(node, peer):
768
768
stalling_peer .send_and_ping (msg )
769
769
assert_equal (int (node .getbestblockhash (), 16 ), block .sha256 )
770
770
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
+
771
799
def run_test (self ):
772
800
# Setup the p2p connections
773
801
self .segwit_node = self .nodes [0 ].add_p2p_connection (TestP2PConn (cmpct_version = 2 ))
@@ -823,6 +851,9 @@ def run_test(self):
823
851
self .log .info ("Testing invalid index in cmpctblock message..." )
824
852
self .test_invalid_cmpctblock_message ()
825
853
854
+ self .log .info ("Testing high-bandwidth mode states via getpeerinfo..." )
855
+ self .test_highbandwidth_mode_states_via_getpeerinfo ()
856
+
826
857
827
858
if __name__ == '__main__' :
828
859
CompactBlocksTest ().main ()
0 commit comments