11
11
12
12
from test_framework .blocktools import create_block , create_coinbase , add_witness_commitment
13
13
from test_framework .messages import BlockTransactions , BlockTransactionsRequest , calculate_shortid , CBlock , CBlockHeader , CInv , COutPoint , CTransaction , CTxIn , CTxInWitness , CTxOut , FromHex , HeaderAndShortIDs , msg_no_witness_block , msg_no_witness_blocktxn , msg_cmpctblock , msg_getblocktxn , msg_getdata , msg_getheaders , msg_headers , msg_inv , msg_sendcmpct , msg_sendheaders , msg_tx , msg_block , msg_blocktxn , MSG_BLOCK , MSG_CMPCT_BLOCK , MSG_WITNESS_FLAG , NODE_NETWORK , P2PHeaderAndShortIDs , PrefilledTransaction , ser_uint256 , ToHex
14
- from test_framework .mininode import mininode_lock , P2PInterface
14
+ from test_framework .mininode import p2p_lock , P2PInterface
15
15
from test_framework .script import CScript , OP_TRUE , OP_DROP
16
16
from test_framework .test_framework import BitcoinTestFramework
17
17
from test_framework .util import assert_equal , wait_until , softfork_active
@@ -48,12 +48,12 @@ def on_inv(self, message):
48
48
self .block_announced = True
49
49
self .announced_blockhashes .add (x .hash )
50
50
51
- # Requires caller to hold mininode_lock
51
+ # Requires caller to hold p2p_lock
52
52
def received_block_announcement (self ):
53
53
return self .block_announced
54
54
55
55
def clear_block_announcement (self ):
56
- with mininode_lock :
56
+ with p2p_lock :
57
57
self .block_announced = False
58
58
self .last_message .pop ("inv" , None )
59
59
self .last_message .pop ("headers" , None )
@@ -73,23 +73,23 @@ def send_header_for_blocks(self, new_blocks):
73
73
def request_headers_and_sync (self , locator , hashstop = 0 ):
74
74
self .clear_block_announcement ()
75
75
self .get_headers (locator , hashstop )
76
- wait_until (self .received_block_announcement , timeout = 30 , lock = mininode_lock )
76
+ wait_until (self .received_block_announcement , timeout = 30 , lock = p2p_lock )
77
77
self .clear_block_announcement ()
78
78
79
79
# Block until a block announcement for a particular block hash is
80
80
# received.
81
81
def wait_for_block_announcement (self , block_hash , timeout = 30 ):
82
82
def received_hash ():
83
83
return (block_hash in self .announced_blockhashes )
84
- wait_until (received_hash , timeout = timeout , lock = mininode_lock )
84
+ wait_until (received_hash , timeout = timeout , lock = p2p_lock )
85
85
86
86
def send_await_disconnect (self , message , timeout = 30 ):
87
87
"""Sends a message to the node and wait for disconnect.
88
88
89
89
This is used when we want to send a message into the node that we expect
90
90
will get us disconnected, eg an invalid block."""
91
91
self .send_message (message )
92
- wait_until (lambda : not self .is_connected , timeout = timeout , lock = mininode_lock )
92
+ wait_until (lambda : not self .is_connected , timeout = timeout , lock = p2p_lock )
93
93
94
94
class CompactBlocksTest (BitcoinTestFramework ):
95
95
def set_test_params (self ):
@@ -154,8 +154,8 @@ def test_sendcmpct(self, test_node, old_node=None):
154
154
# Make sure we get a SENDCMPCT message from our peer
155
155
def received_sendcmpct ():
156
156
return (len (test_node .last_sendcmpct ) > 0 )
157
- wait_until (received_sendcmpct , timeout = 30 , lock = mininode_lock )
158
- with mininode_lock :
157
+ wait_until (received_sendcmpct , timeout = 30 , lock = p2p_lock )
158
+ with p2p_lock :
159
159
# Check that the first version received is the preferred one
160
160
assert_equal (test_node .last_sendcmpct [0 ].version , preferred_version )
161
161
# And that we receive versions down to 1.
@@ -170,7 +170,7 @@ def check_announcement_of_new_block(node, peer, predicate):
170
170
peer .wait_for_block_announcement (block_hash , timeout = 30 )
171
171
assert peer .block_announced
172
172
173
- with mininode_lock :
173
+ with p2p_lock :
174
174
assert predicate (peer ), (
175
175
"block_hash={!r}, cmpctblock={!r}, inv={!r}" .format (
176
176
block_hash , peer .last_message .get ("cmpctblock" , None ), peer .last_message .get ("inv" , None )))
@@ -294,11 +294,11 @@ def test_compactblock_construction(self, test_node, use_witness_address=True):
294
294
block .rehash ()
295
295
296
296
# Wait until the block was announced (via compact blocks)
297
- wait_until (lambda : "cmpctblock" in test_node .last_message , timeout = 30 , lock = mininode_lock )
297
+ wait_until (lambda : "cmpctblock" in test_node .last_message , timeout = 30 , lock = p2p_lock )
298
298
299
299
# Now fetch and check the compact block
300
300
header_and_shortids = None
301
- with mininode_lock :
301
+ with p2p_lock :
302
302
# Convert the on-the-wire representation to absolute indexes
303
303
header_and_shortids = HeaderAndShortIDs (test_node .last_message ["cmpctblock" ].header_and_shortids )
304
304
self .check_compactblock_construction_from_block (version , header_and_shortids , block_hash , block )
@@ -308,11 +308,11 @@ def test_compactblock_construction(self, test_node, use_witness_address=True):
308
308
inv = CInv (MSG_CMPCT_BLOCK , block_hash )
309
309
test_node .send_message (msg_getdata ([inv ]))
310
310
311
- wait_until (lambda : "cmpctblock" in test_node .last_message , timeout = 30 , lock = mininode_lock )
311
+ wait_until (lambda : "cmpctblock" in test_node .last_message , timeout = 30 , lock = p2p_lock )
312
312
313
313
# Now fetch and check the compact block
314
314
header_and_shortids = None
315
- with mininode_lock :
315
+ with p2p_lock :
316
316
# Convert the on-the-wire representation to absolute indexes
317
317
header_and_shortids = HeaderAndShortIDs (test_node .last_message ["cmpctblock" ].header_and_shortids )
318
318
self .check_compactblock_construction_from_block (version , header_and_shortids , block_hash , block )
@@ -378,7 +378,7 @@ def test_compactblock_requests(self, test_node, segwit=True):
378
378
379
379
if announce == "inv" :
380
380
test_node .send_message (msg_inv ([CInv (MSG_BLOCK , block .sha256 )]))
381
- wait_until (lambda : "getheaders" in test_node .last_message , timeout = 30 , lock = mininode_lock )
381
+ wait_until (lambda : "getheaders" in test_node .last_message , timeout = 30 , lock = p2p_lock )
382
382
test_node .send_header_for_blocks ([block ])
383
383
else :
384
384
test_node .send_header_for_blocks ([block ])
@@ -397,7 +397,7 @@ def test_compactblock_requests(self, test_node, segwit=True):
397
397
test_node .send_and_ping (msg_cmpctblock (comp_block .to_p2p ()))
398
398
assert_equal (int (node .getbestblockhash (), 16 ), block .hashPrevBlock )
399
399
# Expect a getblocktxn message.
400
- with mininode_lock :
400
+ with p2p_lock :
401
401
assert "getblocktxn" in test_node .last_message
402
402
absolute_indexes = test_node .last_message ["getblocktxn" ].block_txn_request .to_absolute ()
403
403
assert_equal (absolute_indexes , [0 ]) # should be a coinbase request
@@ -439,7 +439,7 @@ def test_getblocktxn_requests(self, test_node):
439
439
def test_getblocktxn_response (compact_block , peer , expected_result ):
440
440
msg = msg_cmpctblock (compact_block .to_p2p ())
441
441
peer .send_and_ping (msg )
442
- with mininode_lock :
442
+ with p2p_lock :
443
443
assert "getblocktxn" in peer .last_message
444
444
absolute_indexes = peer .last_message ["getblocktxn" ].block_txn_request .to_absolute ()
445
445
assert_equal (absolute_indexes , expected_result )
@@ -504,13 +504,13 @@ def test_tip_after_message(node, peer, msg, tip):
504
504
assert tx .hash in mempool
505
505
506
506
# Clear out last request.
507
- with mininode_lock :
507
+ with p2p_lock :
508
508
test_node .last_message .pop ("getblocktxn" , None )
509
509
510
510
# Send compact block
511
511
comp_block .initialize_from_block (block , prefill_list = [0 ], use_witness = with_witness )
512
512
test_tip_after_message (node , test_node , msg_cmpctblock (comp_block .to_p2p ()), block .sha256 )
513
- with mininode_lock :
513
+ with p2p_lock :
514
514
# Shouldn't have gotten a request for any transaction
515
515
assert "getblocktxn" not in test_node .last_message
516
516
@@ -537,7 +537,7 @@ def test_incorrect_blocktxn_response(self, test_node):
537
537
comp_block .initialize_from_block (block , prefill_list = [0 ], use_witness = (version == 2 ))
538
538
test_node .send_and_ping (msg_cmpctblock (comp_block .to_p2p ()))
539
539
absolute_indexes = []
540
- with mininode_lock :
540
+ with p2p_lock :
541
541
assert "getblocktxn" in test_node .last_message
542
542
absolute_indexes = test_node .last_message ["getblocktxn" ].block_txn_request .to_absolute ()
543
543
assert_equal (absolute_indexes , [6 , 7 , 8 , 9 , 10 ])
@@ -588,10 +588,10 @@ def test_getblocktxn_handler(self, test_node):
588
588
num_to_request = random .randint (1 , len (block .vtx ))
589
589
msg .block_txn_request .from_absolute (sorted (random .sample (range (len (block .vtx )), num_to_request )))
590
590
test_node .send_message (msg )
591
- wait_until (lambda : "blocktxn" in test_node .last_message , timeout = 10 , lock = mininode_lock )
591
+ wait_until (lambda : "blocktxn" in test_node .last_message , timeout = 10 , lock = p2p_lock )
592
592
593
593
[tx .calc_sha256 () for tx in block .vtx ]
594
- with mininode_lock :
594
+ with p2p_lock :
595
595
assert_equal (test_node .last_message ["blocktxn" ].block_transactions .blockhash , int (block_hash , 16 ))
596
596
all_indices = msg .block_txn_request .to_absolute ()
597
597
for index in all_indices :
@@ -611,11 +611,11 @@ def test_getblocktxn_handler(self, test_node):
611
611
# allowed depth for a blocktxn response.
612
612
block_hash = node .getblockhash (current_height )
613
613
msg .block_txn_request = BlockTransactionsRequest (int (block_hash , 16 ), [0 ])
614
- with mininode_lock :
614
+ with p2p_lock :
615
615
test_node .last_message .pop ("block" , None )
616
616
test_node .last_message .pop ("blocktxn" , None )
617
617
test_node .send_and_ping (msg )
618
- with mininode_lock :
618
+ with p2p_lock :
619
619
test_node .last_message ["block" ].block .calc_sha256 ()
620
620
assert_equal (test_node .last_message ["block" ].block .sha256 , int (block_hash , 16 ))
621
621
assert "blocktxn" not in test_node .last_message
@@ -628,21 +628,21 @@ def test_compactblocks_not_at_tip(self, test_node):
628
628
for _ in range (MAX_CMPCTBLOCK_DEPTH + 1 ):
629
629
test_node .clear_block_announcement ()
630
630
new_blocks .append (node .generate (1 )[0 ])
631
- wait_until (test_node .received_block_announcement , timeout = 30 , lock = mininode_lock )
631
+ wait_until (test_node .received_block_announcement , timeout = 30 , lock = p2p_lock )
632
632
633
633
test_node .clear_block_announcement ()
634
634
test_node .send_message (msg_getdata ([CInv (MSG_CMPCT_BLOCK , int (new_blocks [0 ], 16 ))]))
635
- wait_until (lambda : "cmpctblock" in test_node .last_message , timeout = 30 , lock = mininode_lock )
635
+ wait_until (lambda : "cmpctblock" in test_node .last_message , timeout = 30 , lock = p2p_lock )
636
636
637
637
test_node .clear_block_announcement ()
638
638
node .generate (1 )
639
- wait_until (test_node .received_block_announcement , timeout = 30 , lock = mininode_lock )
639
+ wait_until (test_node .received_block_announcement , timeout = 30 , lock = p2p_lock )
640
640
test_node .clear_block_announcement ()
641
- with mininode_lock :
641
+ with p2p_lock :
642
642
test_node .last_message .pop ("block" , None )
643
643
test_node .send_message (msg_getdata ([CInv (MSG_CMPCT_BLOCK , int (new_blocks [0 ], 16 ))]))
644
- wait_until (lambda : "block" in test_node .last_message , timeout = 30 , lock = mininode_lock )
645
- with mininode_lock :
644
+ wait_until (lambda : "block" in test_node .last_message , timeout = 30 , lock = p2p_lock )
645
+ with p2p_lock :
646
646
test_node .last_message ["block" ].block .calc_sha256 ()
647
647
assert_equal (test_node .last_message ["block" ].block .sha256 , int (new_blocks [0 ], 16 ))
648
648
@@ -670,10 +670,10 @@ def test_compactblocks_not_at_tip(self, test_node):
670
670
# (to avoid fingerprinting attacks).
671
671
msg = msg_getblocktxn ()
672
672
msg .block_txn_request = BlockTransactionsRequest (block .sha256 , [0 ])
673
- with mininode_lock :
673
+ with p2p_lock :
674
674
test_node .last_message .pop ("blocktxn" , None )
675
675
test_node .send_and_ping (msg )
676
- with mininode_lock :
676
+ with p2p_lock :
677
677
assert "blocktxn" not in test_node .last_message
678
678
679
679
def test_end_to_end_block_relay (self , listeners ):
@@ -689,8 +689,8 @@ def test_end_to_end_block_relay(self, listeners):
689
689
node .submitblock (ToHex (block ))
690
690
691
691
for l in listeners :
692
- wait_until (lambda : "cmpctblock" in l .last_message , timeout = 30 , lock = mininode_lock )
693
- with mininode_lock :
692
+ wait_until (lambda : "cmpctblock" in l .last_message , timeout = 30 , lock = p2p_lock )
693
+ with p2p_lock :
694
694
for l in listeners :
695
695
l .last_message ["cmpctblock" ].header_and_shortids .header .calc_sha256 ()
696
696
assert_equal (l .last_message ["cmpctblock" ].header_and_shortids .header .sha256 , block .sha256 )
@@ -747,7 +747,7 @@ def announce_cmpct_block(node, peer):
747
747
cmpct_block .initialize_from_block (block )
748
748
msg = msg_cmpctblock (cmpct_block .to_p2p ())
749
749
peer .send_and_ping (msg )
750
- with mininode_lock :
750
+ with p2p_lock :
751
751
assert "getblocktxn" in peer .last_message
752
752
return block , cmpct_block
753
753
0 commit comments