11#!/usr/bin/env python3
22# Copyright (c) 2016 The Bitcoin Core developers
3+ # Copyright (c) 2017 The Bitcoin developers
34# Distributed under the MIT software license, see the accompanying
45# file COPYING or http://www.opensource.org/licenses/mit-license.php.
56
1314'''
1415CompactBlocksTest -- test compact blocks (BIP 152)
1516
16- Version 1 compact blocks are pre-segwit (txids)
17- Version 2 compact blocks are post-segwit (wtxids)
17+ Only testing Version 1 compact blocks (txids)
1818'''
1919
2020# TestNode: A peer we use to send messages to bitcoind, and store responses.
@@ -114,27 +114,24 @@ class CompactBlocksTest(BitcoinTestFramework):
114114 def __init__ (self ):
115115 super ().__init__ ()
116116 self .setup_clean_chain = True
117- # Node0 = pre-segwit, node1 = segwit-aware
118117 self .num_nodes = 2
119118 self .utxos = []
120119
121120 def setup_network (self ):
122121 self .nodes = []
123122
124- # Start up node0 to be a version 1, pre-segwit node .
123+ # Start up two version 1 CB nodes .
125124 self .nodes = start_nodes (self .num_nodes , self .options .tmpdir ,
126- [["-debug" , "-logtimemicros=1" , "-bip9params=segwit:0:0" ],
125+ [["-debug" , "-logtimemicros=1" ],
127126 ["-debug" , "-logtimemicros" , "-txindex" ]])
128127 connect_nodes (self .nodes [0 ], 1 )
129128
130- def build_block_on_tip (self , node , segwit = False ):
129+ def build_block_on_tip (self , node ):
131130 height = node .getblockcount ()
132131 tip = node .getbestblockhash ()
133132 mtp = node .getblockheader (tip )['mediantime' ]
134133 block = create_block (int (tip , 16 ), create_coinbase (height + 1 ), mtp + 1 )
135134 block .nVersion = 4
136- if segwit :
137- add_witness_commitment (block )
138135 block .solve ()
139136 return block
140137
@@ -214,7 +211,7 @@ def check_announcement_of_new_block(node, peer, predicate):
214211
215212 # Now try a SENDCMPCT message with too-high version
216213 sendcmpct = msg_sendcmpct ()
217- sendcmpct .version = preferred_version + 1
214+ sendcmpct .version = 999 # was: preferred_version+1
218215 sendcmpct .announce = True
219216 test_node .send_and_ping (sendcmpct )
220217 check_announcement_of_new_block (node , test_node , lambda p : p .last_cmpctblock is None )
@@ -259,7 +256,7 @@ def check_announcement_of_new_block(node, peer, predicate):
259256 if old_node is not None :
260257 # Verify that a peer using an older protocol version can receive
261258 # announcements from this node.
262- sendcmpct .version = preferred_version - 1
259+ sendcmpct .version = 1 # preferred_version-1
263260 sendcmpct .announce = True
264261 old_node .send_and_ping (sendcmpct )
265262 # Header sync
@@ -282,29 +279,17 @@ def test_invalid_cmpctblock_message(self):
282279
283280 # Compare the generated shortids to what we expect based on BIP 152, given
284281 # bitcoind's choice of nonce.
285- def test_compactblock_construction (self , node , test_node , version , use_witness_address ):
282+ def test_compactblock_construction (self , node , test_node , version ):
286283 # Generate a bunch of transactions.
287284 node .generate (101 )
288285 num_transactions = 25
289286 address = node .getnewaddress ()
290- if use_witness_address :
291- # Want at least one segwit spend, so move all funds to
292- # a witness address.
293- address = node .addwitnessaddress (address )
294- value_to_send = node .getbalance ()
295- node .sendtoaddress (address , satoshi_round (value_to_send - Decimal (0.1 )))
296- node .generate (1 )
297-
298- segwit_tx_generated = False
287+
299288 for i in range (num_transactions ):
300289 txid = node .sendtoaddress (address , 0.1 )
301290 hex_tx = node .gettransaction (txid )["hex" ]
302291 tx = FromHex (CTransaction (), hex_tx )
303- if not tx .wit .is_null ():
304- segwit_tx_generated = True
305-
306- if use_witness_address :
307- assert (segwit_tx_generated ) # check that our test is not broken
292+ assert (tx .wit .is_null ())
308293
309294 # Wait until we've seen the block announcement for the resulting tip
310295 tip = int (node .getbestblockhash (), 16 )
@@ -399,14 +384,11 @@ def check_compactblock_construction_from_block(self, version, header_and_shortid
399384 # Test that bitcoind requests compact blocks when we announce new blocks
400385 # via header or inv, and that responding to getblocktxn causes the block
401386 # to be successfully reconstructed.
402- # Post-segwit: upgraded nodes would only make this request of cb-version-2,
403- # NODE_WITNESS peers. Unupgraded nodes would still make this request of
404- # any cb-version-1-supporting peer.
405- def test_compactblock_requests (self , node , test_node , version , segwit ):
387+ def test_compactblock_requests (self , node , test_node , version ):
406388 # Try announcing a block with an inv or header, expect a compactblock
407389 # request
408390 for announce in ["inv" , "header" ]:
409- block = self .build_block_on_tip (node , segwit = segwit )
391+ block = self .build_block_on_tip (node )
410392 with mininode_lock :
411393 test_node .last_getdata = None
412394
@@ -715,19 +697,13 @@ def test_compactblocks_not_at_tip(self, node, test_node):
715697 with mininode_lock :
716698 assert (test_node .last_blocktxn is None )
717699
718- def activate_segwit (self , node ):
719- node .generate (144 * 3 )
720- assert_equal (get_bip9_status (node , "segwit" )["status" ], 'active' )
721-
722700 def test_end_to_end_block_relay (self , node , listeners ):
723701 utxo = self .utxos .pop (0 )
724702
725703 block = self .build_block_with_transactions (node , utxo , 10 )
726704
727705 [l .clear_block_announcement () for l in listeners ]
728706
729- # ToHex() won't serialize with witness, but this block has no witnesses
730- # anyway. TODO: repeat this test with witness tx's to a segwit node.
731707 node .submitblock (ToHex (block ))
732708
733709 for l in listeners :
@@ -740,24 +716,19 @@ def test_end_to_end_block_relay(self, node, listeners):
740716
741717 # Test that we don't get disconnected if we relay a compact block with valid header,
742718 # but invalid transactions.
743- def test_invalid_tx_in_compactblock (self , node , test_node , use_segwit ):
719+ def test_invalid_tx_in_compactblock (self , node , test_node ):
744720 assert (len (self .utxos ))
745721 utxo = self .utxos [0 ]
746722
747723 block = self .build_block_with_transactions (node , utxo , 5 )
748724 del block .vtx [3 ]
749725 block .hashMerkleRoot = block .calc_merkle_root ()
750- if use_segwit :
751- # If we're testing with segwit, also drop the coinbase witness,
752- # but include the witness commitment.
753- add_witness_commitment (block )
754- block .vtx [0 ].wit .vtxinwit = []
755726 block .solve ()
756727
757728 # Now send the compact block with all transactions prefilled, and
758729 # verify that we don't get disconnected.
759730 comp_block = HeaderAndShortIDs ()
760- comp_block .initialize_from_block (block , prefill_list = [0 , 1 , 2 , 3 , 4 ], use_witness = use_segwit )
731+ comp_block .initialize_from_block (block , prefill_list = [0 , 1 , 2 , 3 , 4 ], use_witness = False )
761732 msg = msg_cmpctblock (comp_block .to_p2p ())
762733 test_node .send_and_ping (msg )
763734
@@ -828,17 +799,17 @@ def announce_cmpct_block(node, peer):
828799 def run_test (self ):
829800 # Setup the p2p connections and start up the network thread.
830801 self .test_node = TestNode ()
831- self .segwit_node = TestNode ()
832- self .old_node = TestNode () # version 1 peer <--> segwit node
802+ self .ex_softfork_node = TestNode ()
803+ self .old_node = TestNode () # version 1 peer
833804
834805 connections = []
835806 connections .append (NodeConn ('127.0.0.1' , p2p_port (0 ), self .nodes [0 ], self .test_node ))
836807 connections .append (NodeConn ('127.0.0.1' , p2p_port (1 ), self .nodes [1 ],
837- self .segwit_node , services = NODE_NETWORK | NODE_WITNESS ))
808+ self .ex_softfork_node , services = NODE_NETWORK ))
838809 connections .append (NodeConn ('127.0.0.1' , p2p_port (1 ), self .nodes [1 ],
839810 self .old_node , services = NODE_NETWORK ))
840811 self .test_node .add_connection (connections [0 ])
841- self .segwit_node .add_connection (connections [1 ])
812+ self .ex_softfork_node .add_connection (connections [1 ])
842813 self .old_node .add_connection (connections [2 ])
843814
844815 NetworkThread ().start () # Start up network handling in another thread
@@ -849,120 +820,69 @@ def run_test(self):
849820 # We will need UTXOs to construct transactions in later tests.
850821 self .make_utxos ()
851822
852- print ("Running tests, pre-segwit activation :" )
823+ print ("Running tests:" )
853824
854825 print ("\t Testing SENDCMPCT p2p message... " )
855826 self .test_sendcmpct (self .nodes [0 ], self .test_node , 1 )
856827 sync_blocks (self .nodes )
857- self .test_sendcmpct (self .nodes [1 ], self .segwit_node , 2 , old_node = self .old_node )
828+ self .test_sendcmpct (self .nodes [1 ], self .ex_softfork_node , 1 , old_node = self .old_node )
858829 sync_blocks (self .nodes )
859830
860831 print ("\t Testing compactblock construction..." )
861- self .test_compactblock_construction (self .nodes [0 ], self .test_node , 1 , False )
832+ self .test_compactblock_construction (self .nodes [0 ], self .test_node , 1 )
862833 sync_blocks (self .nodes )
863- self .test_compactblock_construction (self .nodes [1 ], self .segwit_node , 2 , False )
834+ self .test_compactblock_construction (self .nodes [1 ], self .ex_softfork_node , 1 )
864835 sync_blocks (self .nodes )
865836
866837 print ("\t Testing compactblock requests... " )
867- self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 , False )
838+ self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 )
868839 sync_blocks (self .nodes )
869- self .test_compactblock_requests (self .nodes [1 ], self .segwit_node , 2 , False )
840+ self .test_compactblock_requests (self .nodes [1 ], self .ex_softfork_node , 2 )
870841 sync_blocks (self .nodes )
871842
872843 print ("\t Testing getblocktxn requests..." )
873844 self .test_getblocktxn_requests (self .nodes [0 ], self .test_node , 1 )
874845 sync_blocks (self .nodes )
875- self .test_getblocktxn_requests (self .nodes [1 ], self .segwit_node , 2 )
846+ self .test_getblocktxn_requests (self .nodes [1 ], self .ex_softfork_node , 2 )
876847 sync_blocks (self .nodes )
877848
878849 print ("\t Testing getblocktxn handler..." )
879850 self .test_getblocktxn_handler (self .nodes [0 ], self .test_node , 1 )
880851 sync_blocks (self .nodes )
881- self .test_getblocktxn_handler (self .nodes [1 ], self .segwit_node , 2 )
852+ self .test_getblocktxn_handler (self .nodes [1 ], self .ex_softfork_node , 2 )
882853 self .test_getblocktxn_handler (self .nodes [1 ], self .old_node , 1 )
883854 sync_blocks (self .nodes )
884855
885856 print ("\t Testing compactblock requests/announcements not at chain tip..." )
886857 self .test_compactblocks_not_at_tip (self .nodes [0 ], self .test_node )
887858 sync_blocks (self .nodes )
888- self .test_compactblocks_not_at_tip (self .nodes [1 ], self .segwit_node )
859+ self .test_compactblocks_not_at_tip (self .nodes [1 ], self .ex_softfork_node )
889860 self .test_compactblocks_not_at_tip (self .nodes [1 ], self .old_node )
890861 sync_blocks (self .nodes )
891862
892863 print ("\t Testing handling of incorrect blocktxn responses..." )
893864 self .test_incorrect_blocktxn_response (self .nodes [0 ], self .test_node , 1 )
894865 sync_blocks (self .nodes )
895- self .test_incorrect_blocktxn_response (self .nodes [1 ], self .segwit_node , 2 )
866+ self .test_incorrect_blocktxn_response (self .nodes [1 ], self .ex_softfork_node , 2 )
896867 sync_blocks (self .nodes )
897868
898869 # End-to-end block relay tests
899870 print ("\t Testing end-to-end block relay..." )
900871 self .request_cb_announcements (self .test_node , self .nodes [0 ], 1 )
901872 self .request_cb_announcements (self .old_node , self .nodes [1 ], 1 )
902- self .request_cb_announcements (self .segwit_node , self .nodes [1 ], 2 )
903- self .test_end_to_end_block_relay (self .nodes [0 ], [self .segwit_node , self .test_node , self .old_node ])
904- self .test_end_to_end_block_relay (self .nodes [1 ], [self .segwit_node , self .test_node , self .old_node ])
873+ self .request_cb_announcements (self .ex_softfork_node , self .nodes [1 ], 2 )
874+ self .test_end_to_end_block_relay (self .nodes [0 ], [self .ex_softfork_node , self .test_node , self .old_node ])
875+ self .test_end_to_end_block_relay (self .nodes [1 ], [self .ex_softfork_node , self .test_node , self .old_node ])
905876
906877 print ("\t Testing handling of invalid compact blocks..." )
907- self .test_invalid_tx_in_compactblock (self .nodes [0 ], self .test_node , False )
908- self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .segwit_node , False )
909- self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .old_node , False )
878+ self .test_invalid_tx_in_compactblock (self .nodes [0 ], self .test_node )
879+ self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .ex_softfork_node )
880+ self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .old_node )
910881
911882 print ("\t Testing reconstructing compact blocks from all peers..." )
912- self .test_compactblock_reconstruction_multiple_peers (self .nodes [1 ], self .segwit_node , self .old_node )
913- sync_blocks (self .nodes )
914-
915- # Advance to segwit activation
916- print ("\n Advancing to segwit activation\n " )
917- self .activate_segwit (self .nodes [1 ])
918- print ("Running tests, post-segwit activation..." )
919-
920- print ("\t Testing compactblock construction..." )
921- self .test_compactblock_construction (self .nodes [1 ], self .old_node , 1 , True )
922- self .test_compactblock_construction (self .nodes [1 ], self .segwit_node , 2 , True )
923- sync_blocks (self .nodes )
924-
925- print ("\t Testing compactblock requests (unupgraded node)... " )
926- self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 , True )
927-
928- print ("\t Testing getblocktxn requests (unupgraded node)..." )
929- self .test_getblocktxn_requests (self .nodes [0 ], self .test_node , 1 )
930-
931- # Need to manually sync node0 and node1, because post-segwit activation,
932- # node1 will not download blocks from node0.
933- print ("\t Syncing nodes..." )
934- assert (self .nodes [0 ].getbestblockhash () != self .nodes [1 ].getbestblockhash ())
935- while (self .nodes [0 ].getblockcount () > self .nodes [1 ].getblockcount ()):
936- block_hash = self .nodes [0 ].getblockhash (self .nodes [1 ].getblockcount ()+ 1 )
937- self .nodes [1 ].submitblock (self .nodes [0 ].getblock (block_hash , False ))
938- assert_equal (self .nodes [0 ].getbestblockhash (), self .nodes [1 ].getbestblockhash ())
939-
940- print ("\t Testing compactblock requests (segwit node)... " )
941- self .test_compactblock_requests (self .nodes [1 ], self .segwit_node , 2 , True )
942-
943- print ("\t Testing getblocktxn requests (segwit node)..." )
944- self .test_getblocktxn_requests (self .nodes [1 ], self .segwit_node , 2 )
883+ self .test_compactblock_reconstruction_multiple_peers (self .nodes [1 ], self .ex_softfork_node , self .old_node )
945884 sync_blocks (self .nodes )
946885
947- print ("\t Testing getblocktxn handler (segwit node should return witnesses)..." )
948- self .test_getblocktxn_handler (self .nodes [1 ], self .segwit_node , 2 )
949- self .test_getblocktxn_handler (self .nodes [1 ], self .old_node , 1 )
950-
951- # Test that if we submitblock to node1, we'll get a compact block
952- # announcement to all peers.
953- # (Post-segwit activation, blocks won't propagate from node0 to node1
954- # automatically, so don't bother testing a block announced to node0.)
955- print ("\t Testing end-to-end block relay..." )
956- self .request_cb_announcements (self .test_node , self .nodes [0 ], 1 )
957- self .request_cb_announcements (self .old_node , self .nodes [1 ], 1 )
958- self .request_cb_announcements (self .segwit_node , self .nodes [1 ], 2 )
959- self .test_end_to_end_block_relay (self .nodes [1 ], [self .segwit_node , self .test_node , self .old_node ])
960-
961- print ("\t Testing handling of invalid compact blocks..." )
962- self .test_invalid_tx_in_compactblock (self .nodes [0 ], self .test_node , False )
963- self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .segwit_node , True )
964- self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .old_node , True )
965-
966886 print ("\t Testing invalid index in cmpctblock message..." )
967887 self .test_invalid_cmpctblock_message ()
968888
0 commit comments