Skip to content

Commit

Permalink
Merge bitcoin#14101: qa: Use named args in validation acceptance tests
Browse files Browse the repository at this point in the history
fa782a3 qa: Use named args in some tests (MarcoFalke)
b4d3309 scripted-diff: Use named arguments in feature_block (MarcoFalke)
749ba35 scripted-diff: Pass node into p2p_segwit acceptance tests (MarcoFalke)

Pull request description:

  It is confusing to use a list of arguments such as `False, False, 16, ...` where it is unclear what each of them means.

  Run some scripted diffs to put meaning to them.

Tree-SHA512: d768df2375ea3c77145ebb1bf4c2d690581a379031449ded7ae160022d975eb13890aa8c6a44a5eebda8791cb2910a599326e431af76ed9e60afe1d182ada65c

# Conflicts:
#	test/functional/feature_block.py
#	test/functional/mining_basic.py
#	test/functional/p2p_segwit.py
  • Loading branch information
laanwj authored and Munkybooty committed Jul 2, 2021
1 parent f5a46e2 commit 025a33b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
76 changes: 38 additions & 38 deletions test/functional/feature_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def run_test(self):
self.log.info("Reject a block where the miner creates too much coinbase reward")
self.move_tip(6)
b9 = self.next_block(9, spend=out[4], additional_coinbase_value=1)
self.send_blocks([b9], False, 16, b'bad-cb-amount', reconnect=False)
self.send_blocks([b9], success=False, reject_code=16, reject_reason=b'bad-cb-amount', reconnect=False)

# Create a fork that ends in a block with too much fee (the one that causes the reorg)
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand All @@ -190,7 +190,7 @@ def run_test(self):
self.send_blocks([b10], False)

b11 = self.next_block(11, spend=out[4], additional_coinbase_value=1)
self.send_blocks([b11], False, 16, b'bad-cb-amount', reconnect=False)
self.send_blocks([b11], success=False, reject_code=16, reject_reason=b'bad-cb-amount', reconnect=False)

# Try again, but with a valid fork first
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand All @@ -203,7 +203,7 @@ def run_test(self):
b13 = self.next_block(13, spend=out[4])
self.save_spendable_output()
b14 = self.next_block(14, spend=out[5], additional_coinbase_value=1)
self.send_blocks([b12, b13, b14], False, 16, b'bad-cb-amount', reconnect=False)
self.send_blocks([b12, b13, b14], success=False, reject_code=16, reject_reason=b'bad-cb-amount', reconnect=False)

# New tip should be b13.
assert_equal(node.getbestblockhash(), b13.hash)
Expand All @@ -222,7 +222,7 @@ def run_test(self):
self.log.info("Reject a block with too many checksigs")
too_many_checksigs = CScript([OP_CHECKSIG] * (MAX_BLOCK_SIGOPS))
b16 = self.next_block(16, spend=out[6], script=too_many_checksigs)
self.send_blocks([b16], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b16], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

# Attempt to spend a transaction created on a different fork
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand All @@ -231,7 +231,7 @@ def run_test(self):
self.log.info("Reject a block with a spend from a re-org'ed out tx")
self.move_tip(15)
b17 = self.next_block(17, spend=txout_b3)
self.send_blocks([b17], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b17], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# Attempt to spend a transaction created on a different fork (on a fork this time)
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand All @@ -244,7 +244,7 @@ def run_test(self):
self.send_blocks([b18], False)

b19 = self.next_block(19, spend=out[6])
self.send_blocks([b19], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b19], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# Attempt to spend a coinbase at depth too low
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand All @@ -253,7 +253,7 @@ def run_test(self):
self.log.info("Reject a block spending an immature coinbase.")
self.move_tip(15)
b20 = self.next_block(20, spend=out[7])
self.send_blocks([b20], False, 16, b'bad-txns-premature-spend-of-coinbase')
self.send_blocks([b20], success=False, reject_code=16, reject_reason=b'bad-txns-premature-spend-of-coinbase')

# Attempt to spend a coinbase at depth too low (on a fork this time)
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand All @@ -266,7 +266,7 @@ def run_test(self):
self.send_blocks([b21], False)

b22 = self.next_block(22, spend=out[5])
self.send_blocks([b22], False, 16, b'bad-txns-premature-spend-of-coinbase')
self.send_blocks([b22], success=False, reject_code=16, reject_reason=b'bad-txns-premature-spend-of-coinbase')

# Create a block on either side of MAX_BLOCK_SIZE and make sure its accepted/rejected
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
Expand Down Expand Up @@ -295,7 +295,7 @@ def run_test(self):
tx.vout = [CTxOut(0, script_output)]
b24 = self.update_block(24, [tx])
assert_equal(len(b24.serialize()), MAX_BLOCK_SIZE + 1)
self.send_blocks([b24], False, 16, b'bad-blk-length', reconnect=True)
self.send_blocks([b24], success=False, reject_code=16, reject_reason=b'bad-blk-length', reconnect=True)

b25 = self.next_block(25, spend=out[7])
self.send_blocks([b25], False, request_block=False, reconnect=True)
Expand All @@ -313,7 +313,7 @@ def run_test(self):
# update_block causes the merkle root to get updated, even with no new
# transactions, and updates the required state.
b26 = self.update_block(26, [])
self.send_blocks([b26], False, 16, b'bad-cb-length', reconnect=True)
self.send_blocks([b26], success=False, reject_code=16, reject_reason=b'bad-cb-length', reconnect=True)

# Extend the b26 chain to make sure dashd isn't accepting b26
b27 = self.next_block(27, spend=out[7])
Expand All @@ -325,7 +325,7 @@ def run_test(self):
b28.vtx[0].vin[0].scriptSig = b'\x00' * 101
b28.vtx[0].rehash()
b28 = self.update_block(28, [])
self.send_blocks([b28], False, 16, b'bad-cb-length', reconnect=True)
self.send_blocks([b28], success=False, reject_code=16, reject_reason=b'bad-cb-length', reconnect=True)

# Extend the b28 chain to make sure dashd isn't accepting b28
b29 = self.next_block(29, spend=out[7])
Expand Down Expand Up @@ -361,7 +361,7 @@ def run_test(self):
too_many_multisigs = CScript([OP_CHECKMULTISIG] * (MAX_BLOCK_SIGOPS // 20))
b32 = self.next_block(32, spend=out[9], script=too_many_multisigs)
assert_equal(get_legacy_sigopcount_block(b32), MAX_BLOCK_SIGOPS + 1)
self.send_blocks([b32], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b32], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

# CHECKMULTISIGVERIFY
self.log.info("Accept a block with the max number of OP_CHECKMULTISIGVERIFY sigops")
Expand All @@ -374,7 +374,7 @@ def run_test(self):
self.log.info("Reject a block with too many OP_CHECKMULTISIGVERIFY sigops")
too_many_multisigs = CScript([OP_CHECKMULTISIGVERIFY] * (MAX_BLOCK_SIGOPS // 20))
b34 = self.next_block(34, spend=out[10], script=too_many_multisigs)
self.send_blocks([b34], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b34], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

# CHECKSIGVERIFY
self.log.info("Accept a block with the max number of OP_CHECKSIGVERIFY sigops")
Expand All @@ -387,7 +387,7 @@ def run_test(self):
self.log.info("Reject a block with too many OP_CHECKSIGVERIFY sigops")
too_many_checksigs = CScript([OP_CHECKSIGVERIFY] * (MAX_BLOCK_SIGOPS))
b36 = self.next_block(36, spend=out[11], script=too_many_checksigs)
self.send_blocks([b36], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b36], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

# Check spending of a transaction in a block which failed to connect
#
Expand All @@ -404,12 +404,12 @@ def run_test(self):
txout_b37 = PreviousSpendableOutput(b37.vtx[1], 0)
tx = self.create_and_sign_transaction(out[11].tx, out[11].n, 0)
b37 = self.update_block(37, [tx])
self.send_blocks([b37], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b37], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# attempt to spend b37's first non-coinbase tx, at which point b37 was still considered valid
self.move_tip(35)
b38 = self.next_block(38, spend=txout_b37)
self.send_blocks([b38], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b38], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# Check P2SH SigOp counting
#
Expand Down Expand Up @@ -501,7 +501,7 @@ def run_test(self):
tx.rehash()
new_txs.append(tx)
self.update_block(40, new_txs)
self.send_blocks([b40], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b40], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

# same as b40, but one less sigop
self.log.info("Accept a block with the max number of P2SH sigops")
Expand Down Expand Up @@ -564,7 +564,7 @@ def run_test(self):
self.block_heights[b45.sha256] = self.block_heights[self.tip.sha256] + 1
self.tip = b45
self.blocks[45] = b45
self.send_blocks([b45], False, 16, b'bad-cb-missing', reconnect=True)
self.send_blocks([b45], success=False, reject_code=16, reject_reason=b'bad-cb-missing', reconnect=True)

self.log.info("Reject a block with no transactions")
self.move_tip(44)
Expand All @@ -579,7 +579,7 @@ def run_test(self):
self.tip = b46
assert 46 not in self.blocks
self.blocks[46] = b46
self.send_blocks([b46], False, 16, b'bad-blk-length', reconnect=True)
self.send_blocks([b46], success=False, reject_code=16, reject_reason=b'bad-blk-length', reconnect=True)

self.log.info("Reject a block with invalid work")
self.move_tip(44)
Expand All @@ -602,7 +602,7 @@ def run_test(self):
b49 = self.next_block(49)
b49.hashMerkleRoot += 1
b49.solve()
self.send_blocks([b49], False, 16, b'bad-txnmrklroot', reconnect=True)
self.send_blocks([b49], success=False, reject_code=16, reject_reason=b'bad-txnmrklroot', reconnect=True)

self.log.info("Reject a block with incorrect POW limit")
self.move_tip(44)
Expand All @@ -616,15 +616,15 @@ def run_test(self):
b51 = self.next_block(51)
cb2 = create_coinbase(51, self.coinbase_pubkey)
b51 = self.update_block(51, [cb2])
self.send_blocks([b51], False, 16, b'bad-cb-multiple', reconnect=True)
self.send_blocks([b51], success=False, reject_code=16, reject_reason=b'bad-cb-multiple', reconnect=True)

self.log.info("Reject a block with duplicate transactions")
# Note: txns have to be in the right position in the merkle tree to trigger this error
self.move_tip(44)
b52 = self.next_block(52, spend=out[15])
tx = self.create_tx(b52.vtx[1], 0, 1)
b52 = self.update_block(52, [tx, tx])
self.send_blocks([b52], False, 16, b'bad-txns-duplicate', reconnect=True)
self.send_blocks([b52], success=False, reject_code=16, reject_reason=b'bad-txns-duplicate', reconnect=True)

# Test block timestamps
# -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15)
Expand Down Expand Up @@ -691,7 +691,7 @@ def run_test(self):
assert_equal(len(b56.vtx), 3)
b56 = self.update_block(56, [tx1])
assert_equal(b56.hash, b57.hash)
self.send_blocks([b56], False, 16, b'bad-txns-duplicate', reconnect=True)
self.send_blocks([b56], success=False, reject_code=16, reject_reason=b'bad-txns-duplicate', reconnect=True)

# b57p2 - a good block with 6 tx'es, don't submit until end
self.move_tip(55)
Expand All @@ -711,7 +711,7 @@ def run_test(self):
assert_equal(b56p2.hash, b57p2.hash)
assert_equal(len(b56p2.vtx), 6)
b56p2 = self.update_block("b56p2", [tx3, tx4])
self.send_blocks([b56p2], False, 16, b'bad-txns-duplicate', reconnect=True)
self.send_blocks([b56p2], success=False, reject_code=16, reject_reason=b'bad-txns-duplicate', reconnect=True)

self.move_tip("57p2")
self.send_blocks([b57p2], True)
Expand All @@ -736,15 +736,15 @@ def run_test(self):
tx.vout.append(CTxOut(0, b""))
tx.calc_sha256()
b58 = self.update_block(58, [tx])
self.send_blocks([b58], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b58], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# tx with output value > input value
self.log.info("Reject a block with a transaction with outputs > inputs")
self.move_tip(57)
b59 = self.next_block(59)
tx = self.create_and_sign_transaction(out[17].tx, out[17].n, 510 * COIN)
b59 = self.update_block(59, [tx])
self.send_blocks([b59], False, 16, b'bad-txns-in-belowout', reconnect=True)
self.send_blocks([b59], success=False, reject_code=16, reject_reason=b'bad-txns-in-belowout', reconnect=True)

# reset to good chain
self.move_tip(57)
Expand All @@ -768,7 +768,7 @@ def run_test(self):
b61.vtx[0].rehash()
b61 = self.update_block(61, [])
assert_equal(b60.vtx[0].serialize(), b61.vtx[0].serialize())
self.send_blocks([b61], False, 16, b'bad-txns-BIP30', reconnect=True)
self.send_blocks([b61], success=False, reject_code=16, reject_reason=b'bad-txns-BIP30', reconnect=True)

# Test tx.isFinal is properly rejected (not an exhaustive tx.isFinal test, that should be in data-driven transaction tests)
#
Expand All @@ -786,7 +786,7 @@ def run_test(self):
assert(tx.vin[0].nSequence < 0xffffffff)
tx.calc_sha256()
b62 = self.update_block(62, [tx])
self.send_blocks([b62], False, 16, b'bad-txns-nonfinal')
self.send_blocks([b62], success=False, reject_code=16, reject_reason=b'bad-txns-nonfinal')

# Test a non-final coinbase is also rejected
#
Expand All @@ -800,7 +800,7 @@ def run_test(self):
b63.vtx[0].vin[0].nSequence = 0xDEADBEEF
b63.vtx[0].rehash()
b63 = self.update_block(63, [])
self.send_blocks([b63], False, 16, b'bad-txns-nonfinal')
self.send_blocks([b63], success=False, reject_code=16, reject_reason=b'bad-txns-nonfinal')

# This checks that a block with a bloated VARINT between the block_header and the array of tx such that
# the block is > MAX_BLOCK_SIZE with the bloated varint, but <= MAX_BLOCK_SIZE without the bloated varint,
Expand Down Expand Up @@ -834,7 +834,7 @@ def run_test(self):
tx.vin.append(CTxIn(COutPoint(b64a.vtx[1].sha256, 0)))
b64a = self.update_block("64a", [tx])
assert_equal(len(b64a.serialize()), MAX_BLOCK_SIZE + 8)
self.send_blocks([b64a], False, 1, b'error parsing message')
self.send_blocks([b64a], success=False, reject_code=1, reject_reason=b'error parsing message')

# dashd doesn't disconnect us for sending a bloated block, but if we subsequently
# resend the header message, it won't send us the getdata message again. Just
Expand Down Expand Up @@ -876,7 +876,7 @@ def run_test(self):
tx1 = self.create_and_sign_transaction(out[20].tx, out[20].n, out[20].tx.vout[0].nValue)
tx2 = self.create_and_sign_transaction(tx1, 0, 1)
b66 = self.update_block(66, [tx2, tx1])
self.send_blocks([b66], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b66], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# Attempt to double-spend a transaction created in a block
#
Expand All @@ -891,7 +891,7 @@ def run_test(self):
tx2 = self.create_and_sign_transaction(tx1, 0, 1)
tx3 = self.create_and_sign_transaction(tx1, 0, 2)
b67 = self.update_block(67, [tx1, tx2, tx3])
self.send_blocks([b67], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b67], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# More tests of block subsidy
#
Expand All @@ -910,7 +910,7 @@ def run_test(self):
b68 = self.next_block(68, additional_coinbase_value=10)
tx = self.create_and_sign_transaction(out[20].tx, out[20].n, out[20].tx.vout[0].nValue - 9)
b68 = self.update_block(68, [tx])
self.send_blocks([b68], False, 16, b'bad-cb-amount', reconnect=False)
self.send_blocks([b68], success=False, reject_code=16, reject_reason=b'bad-cb-amount', reconnect=False)

self.log.info("Accept a block claiming the correct subsidy in the coinbase transaction")
self.move_tip(65)
Expand All @@ -934,7 +934,7 @@ def run_test(self):
tx.vin.append(CTxIn(COutPoint(bogus_tx.sha256, 0), b"", 0xffffffff))
tx.vout.append(CTxOut(1, b""))
b70 = self.update_block(70, [tx])
self.send_blocks([b70], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b70], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

# Test accepting an invalid block which has the same hash as a valid one (via merkle tree tricks)
#
Expand All @@ -959,7 +959,7 @@ def run_test(self):
assert_equal(b72.sha256, b71.sha256)

self.move_tip(71)
self.send_blocks([b71], False, 16, b'bad-txns-duplicate', reconnect=True)
self.send_blocks([b71], success=False, reject_code=16, reject_reason=b'bad-txns-duplicate', reconnect=True)

self.move_tip(72)
self.send_blocks([b72], True)
Expand Down Expand Up @@ -997,7 +997,7 @@ def run_test(self):
tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a))
b73 = self.update_block(73, [tx])
assert_equal(get_legacy_sigopcount_block(b73), MAX_BLOCK_SIGOPS + 1)
self.send_blocks([b73], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b73], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

# b74/75 - if we push an invalid script element, all prevous sigops are counted,
# but sigops after the element are not counted.
Expand All @@ -1021,7 +1021,7 @@ def run_test(self):
a[MAX_BLOCK_SIGOPS + 4] = 0xff
tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a))
b74 = self.update_block(74, [tx])
self.send_blocks([b74], False, 16, b'bad-blk-sigops', reconnect=True)
self.send_blocks([b74], success=False, reject_code=16, reject_reason=b'bad-blk-sigops', reconnect=True)

self.move_tip(72)
b75 = self.next_block(75)
Expand Down Expand Up @@ -1170,7 +1170,7 @@ def run_test(self):
b89a = self.next_block("89a", spend=out[32])
tx = self.create_tx(tx1, 0, 0, CScript([OP_TRUE]))
b89a = self.update_block("89a", [tx])
self.send_blocks([b89a], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
self.send_blocks([b89a], success=False, reject_code=16, reject_reason=b'bad-txns-inputs-missingorspent', reconnect=True)

self.log.info("Test a re-org of ~2 days' worth of blocks (1088 blocks)")

Expand Down
6 changes: 3 additions & 3 deletions test/functional/p2p_invalid_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_test(self):
# Save the coinbase for later
block1 = block
tip = block.sha256
node.p2p.send_blocks_and_test([block1], node, True)
node.p2p.send_blocks_and_test([block1], node, success=True)

self.log.info("Mature the block.")
node.generate(100)
Expand Down Expand Up @@ -82,7 +82,7 @@ def run_test(self):
assert_equal(orig_hash, block2.rehash())
assert(block2_orig.vtx != block2.vtx)

node.p2p.send_blocks_and_test([block2], node, False, False, 16, b'bad-txns-duplicate')
node.p2p.send_blocks_and_test([block2], node, success=False, request_block=False, reject_code=16, reject_reason=b'bad-txns-duplicate')

self.log.info("Test very broken block.")

Expand All @@ -95,7 +95,7 @@ def run_test(self):
block3.rehash()
block3.solve()

node.p2p.send_blocks_and_test([block3], node, False, False, 16, b'bad-cb-amount')
node.p2p.send_blocks_and_test([block3], node, success=False, request_block=False, reject_code=16, reject_reason=b'bad-cb-amount')

if __name__ == '__main__':
InvalidBlockRequestTest().main()
Loading

0 comments on commit 025a33b

Please sign in to comment.