@@ -43,6 +43,7 @@ def __init__(self):
43
43
self .last_pong = msg_pong (0 )
44
44
self .sleep_time = 0.05
45
45
self .getdataset = set ()
46
+ self .last_reject = None
46
47
47
48
def add_connection (self , conn ):
48
49
self .connection = conn
@@ -68,7 +69,7 @@ def on_pong(self, conn, message):
68
69
69
70
def on_reject (self , conn , message ):
70
71
self .last_reject = message
71
- #print message
72
+ #print ( message)
72
73
73
74
# Syncing helpers
74
75
def sync (self , test_function , timeout = 60 ):
@@ -136,13 +137,17 @@ def request_block(self, blockhash, inv_type, timeout=60):
136
137
self .wait_for_block (blockhash , timeout )
137
138
return self .last_block
138
139
139
- def test_transaction_acceptance (self , tx , with_witness , accepted ):
140
+ def test_transaction_acceptance (self , tx , with_witness , accepted , reason = None ):
140
141
tx_message = msg_tx (tx )
141
142
if with_witness :
142
143
tx_message = msg_witness_tx (tx )
143
144
self .send_message (tx_message )
144
145
self .sync_with_ping ()
145
146
assert_equal (tx .hash in self .connection .rpc .getrawmempool (), accepted )
147
+ if (reason != None and not accepted ):
148
+ # Check the rejection reason as well.
149
+ with mininode_lock :
150
+ assert_equal (self .last_reject .reason , reason )
146
151
147
152
# Test whether a witness block had the correct effect on the tip
148
153
def test_witness_block (self , block , accepted , with_witness = True ):
@@ -277,9 +282,52 @@ def test_unnecessary_witness_before_segwit_activation(self):
277
282
self .test_node .sync_with_ping ()
278
283
assert_equal (self .nodes [0 ].getbestblockhash (), block .hash )
279
284
285
+ sync_blocks (self .nodes )
286
+
287
+ # Create a p2sh output -- this is so we can pass the standardness
288
+ # rules (an anyone-can-spend OP_TRUE would be rejected, if not wrapped
289
+ # in P2SH).
290
+ p2sh_program = CScript ([OP_TRUE ])
291
+ p2sh_pubkey = hash160 (p2sh_program )
292
+ scriptPubKey = CScript ([OP_HASH160 , p2sh_pubkey , OP_EQUAL ])
293
+
294
+ # Now check that unnecessary witnesses can't be used to blind a node
295
+ # to a transaction, eg by violating standardness checks.
296
+ tx2 = CTransaction ()
297
+ tx2 .vin .append (CTxIn (COutPoint (tx .sha256 , 0 ), b"" ))
298
+ tx2 .vout .append (CTxOut (tx .vout [0 ].nValue - 1000 , scriptPubKey ))
299
+ tx2 .rehash ()
300
+ self .test_node .test_transaction_acceptance (tx2 , False , True )
301
+ self .nodes [0 ].generate (1 )
302
+ sync_blocks (self .nodes )
303
+
304
+ # We'll add an unnecessary witness to this transaction that would cause
305
+ # it to be too large according to IsStandard.
306
+ tx3 = CTransaction ()
307
+ tx3 .vin .append (CTxIn (COutPoint (tx2 .sha256 , 0 ), CScript ([p2sh_program ])))
308
+ tx3 .vout .append (CTxOut (tx2 .vout [0 ].nValue - 1000 , scriptPubKey ))
309
+ tx3 .wit .vtxinwit .append (CTxinWitness ())
310
+ tx3 .wit .vtxinwit [0 ].scriptWitness .stack = [b'a' * 400000 ]
311
+ tx3 .rehash ()
312
+ self .std_node .test_transaction_acceptance (tx3 , True , False , b'no-witness-yet' )
313
+
314
+ # If we send without witness, it should be accepted.
315
+ self .std_node .test_transaction_acceptance (tx3 , False , True )
316
+
317
+ # Now create a new anyone-can-spend utxo for the next test.
318
+ tx4 = CTransaction ()
319
+ tx4 .vin .append (CTxIn (COutPoint (tx3 .sha256 , 0 ), CScript ([p2sh_program ])))
320
+ tx4 .vout .append (CTxOut (tx3 .vout [0 ].nValue - 1000 , CScript ([OP_TRUE ])))
321
+ tx4 .rehash ()
322
+ self .test_node .test_transaction_acceptance (tx3 , False , True )
323
+ self .test_node .test_transaction_acceptance (tx4 , False , True )
324
+
325
+ self .nodes [0 ].generate (1 )
326
+ sync_blocks (self .nodes )
327
+
280
328
# Update our utxo list; we spent the first entry.
281
329
self .utxo .pop (0 )
282
- self .utxo .append (UTXO (tx .sha256 , 0 , tx .vout [0 ].nValue ))
330
+ self .utxo .append (UTXO (tx4 .sha256 , 0 , tx4 .vout [0 ].nValue ))
283
331
284
332
285
333
# Mine enough blocks for segwit's vb state to be 'started'.
0 commit comments