Skip to content

Commit 6839863

Browse files
committed
[tests] p2p_segwit: Make sure each subtest leaves utxos for the next.
1 parent bfe3273 commit 6839863

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

test/functional/p2p_segwit.py

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,17 @@ def run_test(self):
228228
# self.std_node is for testing node1 (fRequireStandard=true)
229229
self.std_node = self.nodes[1].add_p2p_connection(TestP2PConn(), services=NODE_NETWORK | NODE_WITNESS)
230230

231+
for conn in (self.test_node, self.old_node, self.std_node):
232+
conn.wait_for_verack()
233+
234+
assert self.test_node.nServices & NODE_WITNESS != 0
235+
231236
# Keep a place to store utxo's that can be used in later tests
232237
self.utxo = []
233238

234-
# Test logic begins here
235-
self.test_node.wait_for_verack()
236-
237239
self.log.info("Starting tests before segwit lock in:")
238240

239-
self.test_witness_services() # Verifies NODE_WITNESS
240-
self.test_non_witness_transaction() # non-witness tx's are accepted
241+
self.test_non_witness_transaction()
241242
self.test_unnecessary_witness_before_segwit_activation()
242243
self.test_v0_outputs_arent_spendable()
243244
self.test_block_relay(segwit_activated=False)
@@ -297,13 +298,11 @@ def subtest(func): # noqa: N805
297298
def func_wrapper(self, *args, **kwargs):
298299
self.log.info("Subtest: {}".format(func.__name__))
299300
func(self, *args, **kwargs)
301+
# Each subtest should leave some utxos for the next subtest
302+
assert self.utxo
300303

301304
return func_wrapper
302305

303-
def test_witness_services(self):
304-
self.log.info("Verifying NODE_WITNESS service bit")
305-
assert((self.test_node.nServices & NODE_WITNESS) != 0)
306-
307306
@subtest
308307
def test_non_witness_transaction(self):
309308
"""See if sending a regular transaction works, and create a utxo to use in later tests."""
@@ -339,9 +338,6 @@ def test_non_witness_transaction(self):
339338
def test_unnecessary_witness_before_segwit_activation(self):
340339
"""Verify that blocks with witnesses are rejected before activation."""
341340

342-
# For now, rely on earlier tests to have created at least one utxo for
343-
# us to use
344-
assert(len(self.utxo) > 0)
345341
assert(get_bip9_status(self.nodes[0], 'segwit')['status'] != 'active')
346342

347343
tx = CTransaction()
@@ -486,8 +482,6 @@ def test_v0_outputs_arent_spendable(self):
486482
witness, and so can't be spent before segwit activation (the point at which
487483
blocks are permitted to contain witnesses)."""
488484

489-
assert len(self.utxo), "self.utxo is empty"
490-
491485
# Create two outputs, a p2wsh and p2sh-p2wsh
492486
witness_program = CScript([OP_TRUE])
493487
witness_hash = sha256(witness_program)
@@ -632,7 +626,6 @@ def test_witness_tx_relay_before_segwit_activation(self):
632626
# Generate a transaction that doesn't require a witness, but send it
633627
# with a witness. Should be rejected for premature-witness, but should
634628
# not be added to recently rejected list.
635-
assert(len(self.utxo))
636629
tx = CTransaction()
637630
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
638631
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
@@ -676,8 +669,6 @@ def test_standardness_v0(self, segwit_activated):
676669
V0 segwit outputs and inputs are always standard.
677670
V0 segwit inputs may only be mined after activation, but not before."""
678671

679-
assert(len(self.utxo))
680-
681672
witness_program = CScript([OP_TRUE])
682673
witness_hash = sha256(witness_program)
683674
script_pubkey = CScript([OP_0, witness_hash])
@@ -766,8 +757,6 @@ def advance_to_segwit_active(self):
766757
def test_p2sh_witness(self, segwit_activated):
767758
"""Test P2SH wrapped witness programs."""
768759

769-
assert(len(self.utxo))
770-
771760
# Prepare the p2sh-wrapped witness output
772761
witness_program = CScript([OP_DROP, OP_TRUE])
773762
witness_hash = sha256(witness_program)
@@ -861,7 +850,6 @@ def test_witness_commitments(self):
861850
test_witness_block(self.nodes[0].rpc, self.test_node, block_2, accepted=True)
862851

863852
# Now test commitments with actual transactions
864-
assert (len(self.utxo) > 0)
865853
tx = CTransaction()
866854
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
867855

@@ -1081,8 +1069,6 @@ def test_submit_block(self):
10811069
def test_extra_witness_data(self):
10821070
"""Test extra witness data in a transaction."""
10831071

1084-
assert(len(self.utxo) > 0)
1085-
10861072
block = self.build_next_block()
10871073

10881074
witness_program = CScript([OP_DROP, OP_TRUE])
@@ -1157,7 +1143,6 @@ def test_max_witness_push_length(self):
11571143
"""Test that witness stack can only allow up to 520 byte pushes."""
11581144

11591145
MAX_SCRIPT_ELEMENT_SIZE = 520
1160-
assert(len(self.utxo))
11611146

11621147
block = self.build_next_block()
11631148

@@ -1196,7 +1181,6 @@ def test_max_witness_push_length(self):
11961181
def test_max_witness_program_length(self):
11971182
"""Test that witness outputs greater than 10kB can't be spent."""
11981183

1199-
assert(len(self.utxo))
12001184
MAX_PROGRAM_LENGTH = 10000
12011185

12021186
# This program is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
@@ -1245,8 +1229,6 @@ def test_max_witness_program_length(self):
12451229
def test_witness_input_length(self):
12461230
"""Test that vin length must match vtxinwit length."""
12471231

1248-
assert(len(self.utxo))
1249-
12501232
witness_program = CScript([OP_DROP, OP_TRUE])
12511233
witness_hash = sha256(witness_program)
12521234
script_pubkey = CScript([OP_0, witness_hash])
@@ -1337,7 +1319,6 @@ def test_tx_relay_after_segwit_activation(self):
13371319
# Generate a transaction that doesn't require a witness, but send it
13381320
# with a witness. Should be rejected because we can't use a witness
13391321
# when spending a non-witness output.
1340-
assert(len(self.utxo))
13411322
tx = CTransaction()
13421323
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
13431324
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
@@ -1426,7 +1407,6 @@ def test_segwit_versions(self):
14261407
Future segwit version transactions are non-standard, but valid in blocks.
14271408
Can run this before and after segwit activation."""
14281409

1429-
assert(len(self.utxo))
14301410
num_tests = 17 # will test OP_0, OP1, ..., OP_16
14311411
if (len(self.utxo) < num_tests):
14321412
tx = CTransaction()
@@ -1557,7 +1537,6 @@ def test_uncompressed_pubkey(self):
15571537
pubkey = CPubKey(key.get_pubkey())
15581538
assert_equal(len(pubkey), 65) # This should be an uncompressed pubkey
15591539

1560-
assert(len(self.utxo) > 0)
15611540
utxo = self.utxo.pop(0)
15621541

15631542
# Test 1: P2WPKH
@@ -1661,7 +1640,6 @@ def test_signature_version_1(self):
16611640
script_pubkey = CScript([OP_0, witness_hash])
16621641

16631642
# First create a witness output for use in the tests.
1664-
assert(len(self.utxo))
16651643
tx = CTransaction()
16661644
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
16671645
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
@@ -1835,7 +1813,6 @@ def test_signature_version_1(self):
18351813
@subtest
18361814
def test_non_standard_witness_blinding(self):
18371815
"""Test behavior of unnecessary witnesses in transactions does not blind the node for the transaction"""
1838-
assert (len(self.utxo) > 0)
18391816

18401817
# Create a p2sh output -- this is so we can pass the standardness
18411818
# rules (an anyone-can-spend OP_TRUE would be rejected, if not wrapped
@@ -1901,7 +1878,6 @@ def test_non_standard_witness(self):
19011878

19021879
p2wsh_scripts = []
19031880

1904-
assert(len(self.utxo))
19051881
tx = CTransaction()
19061882
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
19071883

@@ -2019,8 +1995,6 @@ def test_upgrade_after_activation(self, node_id):
20191995
def test_witness_sigops(self):
20201996
"""Test sigop counting is correct inside witnesses."""
20211997

2022-
assert(len(self.utxo))
2023-
20241998
# Keep this under MAX_OPS_PER_SCRIPT (201)
20251999
witness_program = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKMULTISIG] * 5 + [OP_CHECKSIG] * 193 + [OP_ENDIF])
20262000
witness_hash = sha256(witness_program)

0 commit comments

Comments
 (0)