Skip to content

Commit bfe3273

Browse files
committed
[tests] p2p_segwit: wrap subtests with subtest wrapper.
The subtest wrapper logs the name of the subtest.
1 parent 2af4e39 commit bfe3273

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

test/functional/p2p_segwit.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,24 @@ def run_test(self):
292292

293293
# Individual tests
294294

295+
def subtest(func): # noqa: N805
296+
"""Wraps the subtests for logging and state assertions."""
297+
def func_wrapper(self, *args, **kwargs):
298+
self.log.info("Subtest: {}".format(func.__name__))
299+
func(self, *args, **kwargs)
300+
301+
return func_wrapper
302+
295303
def test_witness_services(self):
296304
self.log.info("Verifying NODE_WITNESS service bit")
297305
assert((self.test_node.nServices & NODE_WITNESS) != 0)
298306

307+
@subtest
299308
def test_non_witness_transaction(self):
300309
"""See if sending a regular transaction works, and create a utxo to use in later tests."""
301310
# Mine a block with an anyone-can-spend coinbase,
302311
# let it mature, then try to spend it.
303-
self.log.info("Testing non-witness transaction")
312+
304313
block = self.build_next_block(version=1)
305314
block.solve()
306315
self.test_node.send_message(msg_block(block))
@@ -326,9 +335,10 @@ def test_non_witness_transaction(self):
326335
self.utxo.append(UTXO(tx.sha256, 0, 49 * 100000000))
327336
self.nodes[0].generate(1)
328337

338+
@subtest
329339
def test_unnecessary_witness_before_segwit_activation(self):
330340
"""Verify that blocks with witnesses are rejected before activation."""
331-
self.log.info("Testing behavior of unnecessary witnesses")
341+
332342
# For now, rely on earlier tests to have created at least one utxo for
333343
# us to use
334344
assert(len(self.utxo) > 0)
@@ -368,12 +378,12 @@ def test_unnecessary_witness_before_segwit_activation(self):
368378
self.utxo.pop(0)
369379
self.utxo.append(UTXO(tx.sha256, 0, tx.vout[0].nValue))
370380

381+
@subtest
371382
def test_block_relay(self, segwit_activated):
372383
"""Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG.
373384
374385
This is true regardless of segwit activation.
375386
Also test that we don't ask for blocks from unupgraded peers."""
376-
self.log.info("Testing block relay")
377387

378388
blocktype = 2 | MSG_WITNESS_FLAG
379389

@@ -464,6 +474,7 @@ def test_block_relay(self, segwit_activated):
464474
self.old_node.announce_tx_and_wait_for_getdata(block4.vtx[0])
465475
assert(block4.sha256 not in self.old_node.getdataset)
466476

477+
@subtest
467478
def test_v0_outputs_arent_spendable(self):
468479
"""Test that v0 outputs aren't spendable before segwit activation.
469480
@@ -475,8 +486,6 @@ def test_v0_outputs_arent_spendable(self):
475486
witness, and so can't be spent before segwit activation (the point at which
476487
blocks are permitted to contain witnesses)."""
477488

478-
self.log.info("Testing that v0 witness program outputs aren't spendable before activation")
479-
480489
assert len(self.utxo), "self.utxo is empty"
481490

482491
# Create two outputs, a p2wsh and p2sh-p2wsh
@@ -542,6 +551,7 @@ def test_v0_outputs_arent_spendable(self):
542551
self.utxo.pop(0)
543552
self.utxo.append(UTXO(txid, 2, value))
544553

554+
@subtest
545555
def advance_to_segwit_started(self):
546556
"""Mine enough blocks for segwit's vb state to be 'started'."""
547557
height = self.nodes[0].getblockcount()
@@ -553,8 +563,8 @@ def advance_to_segwit_started(self):
553563
self.nodes[0].generate(VB_PERIOD - height - 1)
554564
assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'started')
555565

566+
@subtest
556567
def test_getblocktemplate_before_lockin(self):
557-
self.log.info("Testing getblocktemplate setting of segwit versionbit (before lockin)")
558568
# Node0 is segwit aware, node2 is not.
559569
for node in [self.nodes[0], self.nodes[2]]:
560570
gbt_results = node.getblocktemplate()
@@ -600,6 +610,7 @@ def test_getblocktemplate_before_lockin(self):
600610
self.nodes[0].setmocktime(0)
601611
self.nodes[2].setmocktime(0)
602612

613+
@subtest
603614
def advance_to_segwit_lockin(self):
604615
"""Mine enough blocks to lock in segwit, but don't activate."""
605616
# TODO: we could verify that lockin only happens at the right threshold of
@@ -615,8 +626,9 @@ def advance_to_segwit_lockin(self):
615626
self.nodes[0].generate(1)
616627
assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'locked_in')
617628

629+
@subtest
618630
def test_witness_tx_relay_before_segwit_activation(self):
619-
self.log.info("Testing relay of witness transactions")
631+
620632
# Generate a transaction that doesn't require a witness, but send it
621633
# with a witness. Should be rejected for premature-witness, but should
622634
# not be added to recently rejected list.
@@ -657,13 +669,13 @@ def test_witness_tx_relay_before_segwit_activation(self):
657669
self.utxo.pop(0)
658670
self.utxo.append(UTXO(tx_hash, 0, tx_value))
659671

672+
@subtest
660673
def test_standardness_v0(self, segwit_activated):
661674
"""Test V0 txout standardness.
662675
663676
V0 segwit outputs and inputs are always standard.
664677
V0 segwit inputs may only be mined after activation, but not before."""
665678

666-
self.log.info("Testing standardness of v0 outputs (%s activation)" % ("after" if segwit_activated else "before"))
667679
assert(len(self.utxo))
668680

669681
witness_program = CScript([OP_TRUE])
@@ -737,6 +749,7 @@ def test_standardness_v0(self, segwit_activated):
737749
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
738750
assert_equal(len(self.nodes[1].getrawmempool()), 0)
739751

752+
@subtest
740753
def advance_to_segwit_active(self):
741754
"""Mine enough blocks to activate segwit."""
742755
# TODO: we could verify that activation only happens at the right threshold
@@ -749,9 +762,9 @@ def advance_to_segwit_active(self):
749762
self.nodes[0].generate(1)
750763
assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'active')
751764

765+
@subtest
752766
def test_p2sh_witness(self, segwit_activated):
753767
"""Test P2SH wrapped witness programs."""
754-
self.log.info("Testing P2SH witness transactions")
755768

756769
assert(len(self.utxo))
757770

@@ -819,13 +832,12 @@ def test_p2sh_witness(self, segwit_activated):
819832
self.utxo.pop(0)
820833
self.utxo.append(UTXO(spend_tx.sha256, 0, spend_tx.vout[0].nValue))
821834

835+
@subtest
822836
def test_witness_commitments(self):
823837
"""Test witness commitments.
824838
825839
This test can only be run after segwit has activated."""
826840

827-
self.log.info("Testing witness commitments")
828-
829841
# First try a correct witness commitment.
830842
block = self.build_next_block()
831843
add_witness_commitment(block)
@@ -911,8 +923,8 @@ def test_witness_commitments(self):
911923
self.utxo.pop(0)
912924
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
913925

926+
@subtest
914927
def test_block_malleability(self):
915-
self.log.info("Testing witness block malleability")
916928

917929
# Make sure that a block that has too big a virtual size
918930
# because of a too-large coinbase witness is not permanently
@@ -951,8 +963,8 @@ def test_block_malleability(self):
951963
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(0)]
952964
test_witness_block(self.nodes[0].rpc, self.test_node, block, accepted=True)
953965

966+
@subtest
954967
def test_witness_block_size(self):
955-
self.log.info("Testing witness block size limit")
956968
# TODO: Test that non-witness carrying blocks can't exceed 1MB
957969
# Skipping this test for now; this is covered in p2p-fullblocktest.py
958970

@@ -1029,6 +1041,7 @@ def test_witness_block_size(self):
10291041
self.utxo.pop(0)
10301042
self.utxo.append(UTXO(block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue))
10311043

1044+
@subtest
10321045
def test_submit_block(self):
10331046
"""Test that submitblock adds the nonce automatically when possible."""
10341047
block = self.build_next_block()
@@ -1064,9 +1077,9 @@ def test_submit_block(self):
10641077
# Tip should not advance!
10651078
assert(self.nodes[0].getbestblockhash() != block_2.hash)
10661079

1080+
@subtest
10671081
def test_extra_witness_data(self):
10681082
"""Test extra witness data in a transaction."""
1069-
self.log.info("Testing extra witness data in tx")
10701083

10711084
assert(len(self.utxo) > 0)
10721085

@@ -1139,9 +1152,10 @@ def test_extra_witness_data(self):
11391152
self.utxo.pop(0)
11401153
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
11411154

1155+
@subtest
11421156
def test_max_witness_push_length(self):
11431157
"""Test that witness stack can only allow up to 520 byte pushes."""
1144-
self.log.info("Testing maximum witness push size")
1158+
11451159
MAX_SCRIPT_ELEMENT_SIZE = 520
11461160
assert(len(self.utxo))
11471161

@@ -1178,9 +1192,10 @@ def test_max_witness_push_length(self):
11781192
self.utxo.pop()
11791193
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
11801194

1195+
@subtest
11811196
def test_max_witness_program_length(self):
11821197
"""Test that witness outputs greater than 10kB can't be spent."""
1183-
self.log.info("Testing maximum witness program length")
1198+
11841199
assert(len(self.utxo))
11851200
MAX_PROGRAM_LENGTH = 10000
11861201

@@ -1226,9 +1241,10 @@ def test_max_witness_program_length(self):
12261241
self.utxo.pop()
12271242
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
12281243

1244+
@subtest
12291245
def test_witness_input_length(self):
12301246
"""Test that vin length must match vtxinwit length."""
1231-
self.log.info("Testing witness input length")
1247+
12321248
assert(len(self.utxo))
12331249

12341250
witness_program = CScript([OP_DROP, OP_TRUE])
@@ -1309,6 +1325,7 @@ def serialize_with_witness(self):
13091325
self.utxo.pop()
13101326
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
13111327

1328+
@subtest
13121329
def test_tx_relay_after_segwit_activation(self):
13131330
"""Test transaction relay after segwit activation.
13141331
@@ -1317,7 +1334,6 @@ def test_tx_relay_after_segwit_activation(self):
13171334
- accepts transactions with valid witnesses
13181335
and that witness transactions are relayed to non-upgraded peers."""
13191336

1320-
self.log.info("Testing relay of witness transactions")
13211337
# Generate a transaction that doesn't require a witness, but send it
13221338
# with a witness. Should be rejected because we can't use a witness
13231339
# when spending a non-witness output.
@@ -1403,13 +1419,13 @@ def test_tx_relay_after_segwit_activation(self):
14031419
self.utxo.pop(0)
14041420
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
14051421

1422+
@subtest
14061423
def test_segwit_versions(self):
14071424
"""Test validity of future segwit version transactions.
14081425
14091426
Future segwit version transactions are non-standard, but valid in blocks.
14101427
Can run this before and after segwit activation."""
14111428

1412-
self.log.info("Testing standardness/consensus for segwit versions (0-16)")
14131429
assert(len(self.utxo))
14141430
num_tests = 17 # will test OP_0, OP1, ..., OP_16
14151431
if (len(self.utxo) < num_tests):
@@ -1491,8 +1507,9 @@ def test_segwit_versions(self):
14911507
# Add utxo to our list
14921508
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
14931509

1510+
@subtest
14941511
def test_premature_coinbase_witness_spend(self):
1495-
self.log.info("Testing premature coinbase witness spend")
1512+
14961513
block = self.build_next_block()
14971514
# Change the output of the block to be a witness output.
14981515
witness_program = CScript([OP_TRUE])
@@ -1525,13 +1542,13 @@ def test_premature_coinbase_witness_spend(self):
15251542
test_witness_block(self.nodes[0].rpc, self.test_node, block2, accepted=True)
15261543
sync_blocks(self.nodes)
15271544

1545+
@subtest
15281546
def test_uncompressed_pubkey(self):
15291547
"""Test uncompressed pubkey validity in segwit transactions.
15301548
15311549
Uncompressed pubkeys are no longer supported in default relay policy,
15321550
but (for now) are still valid in blocks."""
15331551

1534-
self.log.info("Testing uncompressed pubkeys")
15351552
# Segwit transactions using uncompressed pubkeys are not accepted
15361553
# under default policy, but should still pass consensus.
15371554
key = CECKey()
@@ -1632,8 +1649,9 @@ def test_uncompressed_pubkey(self):
16321649
test_witness_block(self.nodes[0].rpc, self.test_node, block, accepted=True)
16331650
self.utxo.append(UTXO(tx5.sha256, 0, tx5.vout[0].nValue))
16341651

1652+
@subtest
16351653
def test_signature_version_1(self):
1636-
self.log.info("Testing segwit signature hash version 1")
1654+
16371655
key = CECKey()
16381656
key.set_secretbytes(b"9")
16391657
pubkey = CPubKey(key.get_pubkey())
@@ -1814,8 +1832,9 @@ def test_signature_version_1(self):
18141832
for i in range(len(tx.vout)):
18151833
self.utxo.append(UTXO(tx.sha256, i, tx.vout[i].nValue))
18161834

1835+
@subtest
18171836
def test_non_standard_witness_blinding(self):
1818-
self.log.info("Testing behavior of unnecessary witnesses in transactions does not blind the node for the transaction")
1837+
"""Test behavior of unnecessary witnesses in transactions does not blind the node for the transaction"""
18191838
assert (len(self.utxo) > 0)
18201839

18211840
# Create a p2sh output -- this is so we can pass the standardness
@@ -1868,8 +1887,9 @@ def test_non_standard_witness_blinding(self):
18681887
self.utxo.pop(0)
18691888
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
18701889

1890+
@subtest
18711891
def test_non_standard_witness(self):
1872-
self.log.info("Testing detection of non-standard P2WSH witness")
1892+
"""Test detection of non-standard P2WSH witness"""
18731893
pad = chr(1).encode('latin-1')
18741894

18751895
# Create scripts for tests
@@ -1968,9 +1988,9 @@ def test_non_standard_witness(self):
19681988

19691989
self.utxo.pop(0)
19701990

1991+
@subtest
19711992
def test_upgrade_after_activation(self, node_id):
19721993
"""Test the behavior of starting up a segwit-aware node after the softfork has activated."""
1973-
self.log.info("Testing software upgrade after softfork activation")
19741994

19751995
assert(node_id != 0) # node0 is assumed to be a segwit-active bitcoind
19761996

@@ -1995,9 +2015,9 @@ def test_upgrade_after_activation(self, node_id):
19952015
assert_equal(self.nodes[0].getblock(block_hash), self.nodes[node_id].getblock(block_hash))
19962016
height -= 1
19972017

2018+
@subtest
19982019
def test_witness_sigops(self):
19992020
"""Test sigop counting is correct inside witnesses."""
2000-
self.log.info("Testing sigops limit")
20012021

20022022
assert(len(self.utxo))
20032023

0 commit comments

Comments
 (0)