Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_invalid_tx.py
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
if template.expect_disconnect: | if template.expect_disconnect: | ||||
self.log.info("Reconnecting to peer") | self.log.info("Reconnecting to peer") | ||||
self.reconnect_p2p() | self.reconnect_p2p() | ||||
# Make two p2p connections to provide the node with orphans | # Make two p2p connections to provide the node with orphans | ||||
# * p2ps[0] will send valid orphan txs (one with low fee) | # * p2ps[0] will send valid orphan txs (one with low fee) | ||||
# * p2ps[1] will send an invalid orphan tx (and is later disconnected for that) | # * p2ps[1] will send an invalid orphan tx (and is later disconnected for that) | ||||
self.reconnect_p2p(num_connections=2) | self.reconnect_p2p(num_connections=2) | ||||
SCRIPT_PUB_KEY_OP_TRUE = b'\x51' | |||||
self.log.info('Test orphan transaction handling ... ') | self.log.info('Test orphan transaction handling ... ') | ||||
# Create a root transaction that we withold until all dependend transactions | # Create a root transaction that we withold until all dependend transactions | ||||
# are sent out and in the orphan cache | # are sent out and in the orphan cache | ||||
tx_withhold = CTransaction() | tx_withhold = CTransaction() | ||||
tx_withhold.vin.append( | tx_withhold.vin.append( | ||||
CTxIn(outpoint=COutPoint(block1.vtx[0].sha256, 0))) | CTxIn(outpoint=COutPoint(block1.vtx[0].sha256, 0))) | ||||
tx_withhold.vout.append( | tx_withhold.vout.append( | ||||
CTxOut(nValue=50 * COIN - 12000, scriptPubKey=b'\x51')) | CTxOut(nValue=50 * COIN - 12000, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)) | ||||
pad_tx(tx_withhold) | pad_tx(tx_withhold) | ||||
tx_withhold.calc_sha256() | tx_withhold.calc_sha256() | ||||
# Our first orphan tx with some outputs to create further orphan txs | # Our first orphan tx with some outputs to create further orphan txs | ||||
tx_orphan_1 = CTransaction() | tx_orphan_1 = CTransaction() | ||||
tx_orphan_1.vin.append( | tx_orphan_1.vin.append( | ||||
CTxIn(outpoint=COutPoint(tx_withhold.sha256, 0))) | CTxIn(outpoint=COutPoint(tx_withhold.sha256, 0))) | ||||
tx_orphan_1.vout = [ | tx_orphan_1.vout = [ | ||||
CTxOut( | CTxOut( | ||||
nValue=10 * COIN, | nValue=10 * COIN, | ||||
scriptPubKey=b'\x51')] * 3 | scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)] * 3 | ||||
pad_tx(tx_orphan_1) | pad_tx(tx_orphan_1) | ||||
tx_orphan_1.calc_sha256() | tx_orphan_1.calc_sha256() | ||||
# A valid transaction with low fee | # A valid transaction with low fee | ||||
tx_orphan_2_no_fee = CTransaction() | tx_orphan_2_no_fee = CTransaction() | ||||
tx_orphan_2_no_fee.vin.append( | tx_orphan_2_no_fee.vin.append( | ||||
CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 0))) | CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 0))) | ||||
tx_orphan_2_no_fee.vout.append( | tx_orphan_2_no_fee.vout.append( | ||||
CTxOut(nValue=10 * COIN, scriptPubKey=b'\x51')) | CTxOut(nValue=10 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)) | ||||
pad_tx(tx_orphan_2_no_fee) | pad_tx(tx_orphan_2_no_fee) | ||||
# A valid transaction with sufficient fee | # A valid transaction with sufficient fee | ||||
tx_orphan_2_valid = CTransaction() | tx_orphan_2_valid = CTransaction() | ||||
tx_orphan_2_valid.vin.append( | tx_orphan_2_valid.vin.append( | ||||
CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 1))) | CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 1))) | ||||
tx_orphan_2_valid.vout.append( | tx_orphan_2_valid.vout.append( | ||||
CTxOut(nValue=10 * COIN - 12000, scriptPubKey=b'\x51')) | CTxOut(nValue=10 * COIN - 12000, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)) | ||||
tx_orphan_2_valid.calc_sha256() | tx_orphan_2_valid.calc_sha256() | ||||
pad_tx(tx_orphan_2_valid) | pad_tx(tx_orphan_2_valid) | ||||
# An invalid transaction with negative fee | # An invalid transaction with negative fee | ||||
tx_orphan_2_invalid = CTransaction() | tx_orphan_2_invalid = CTransaction() | ||||
tx_orphan_2_invalid.vin.append( | tx_orphan_2_invalid.vin.append( | ||||
CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2))) | CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2))) | ||||
tx_orphan_2_invalid.vout.append( | tx_orphan_2_invalid.vout.append( | ||||
CTxOut(nValue=11 * COIN, scriptPubKey=b'\x51')) | CTxOut(nValue=11 * COIN, scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)) | ||||
pad_tx(tx_orphan_2_invalid) | pad_tx(tx_orphan_2_invalid) | ||||
tx_orphan_2_invalid.calc_sha256() | tx_orphan_2_invalid.calc_sha256() | ||||
self.log.info('Send the orphans ... ') | self.log.info('Send the orphans ... ') | ||||
# Send valid orphan txs from p2ps[0] | # Send valid orphan txs from p2ps[0] | ||||
node.p2p.send_txs_and_test( | node.p2p.send_txs_and_test( | ||||
[tx_orphan_1, tx_orphan_2_no_fee, tx_orphan_2_valid], node, success=False) | [tx_orphan_1, tx_orphan_2_no_fee, tx_orphan_2_valid], node, success=False) | ||||
# Send invalid tx from p2ps[1] | # Send invalid tx from p2ps[1] | ||||
Show All 30 Lines | def run_test(self): | ||||
self.log.info('Test orphan pool overflow') | self.log.info('Test orphan pool overflow') | ||||
orphan_tx_pool = [CTransaction() for _ in range(101)] | orphan_tx_pool = [CTransaction() for _ in range(101)] | ||||
for i in range(len(orphan_tx_pool)): | for i in range(len(orphan_tx_pool)): | ||||
orphan_tx_pool[i].vin.append(CTxIn(outpoint=COutPoint(i, 333))) | orphan_tx_pool[i].vin.append(CTxIn(outpoint=COutPoint(i, 333))) | ||||
orphan_tx_pool[i].vout.append( | orphan_tx_pool[i].vout.append( | ||||
CTxOut( | CTxOut( | ||||
nValue=11 * COIN, | nValue=11 * COIN, | ||||
scriptPubKey=b'\x51')) | scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)) | ||||
pad_tx(orphan_tx_pool[i]) | pad_tx(orphan_tx_pool[i]) | ||||
with node.assert_debug_log(['mapOrphan overflow, removed 1 tx']): | with node.assert_debug_log(['mapOrphan overflow, removed 1 tx']): | ||||
node.p2p.send_txs_and_test(orphan_tx_pool, node, success=False) | node.p2p.send_txs_and_test(orphan_tx_pool, node, success=False) | ||||
rejected_parent = CTransaction() | rejected_parent = CTransaction() | ||||
rejected_parent.vin.append( | rejected_parent.vin.append( | ||||
CTxIn( | CTxIn( | ||||
outpoint=COutPoint( | outpoint=COutPoint( | ||||
tx_orphan_2_invalid.sha256, | tx_orphan_2_invalid.sha256, | ||||
0))) | 0))) | ||||
rejected_parent.vout.append( | rejected_parent.vout.append( | ||||
CTxOut( | CTxOut( | ||||
nValue=11 * COIN, | nValue=11 * COIN, | ||||
scriptPubKey=b'\x51')) | scriptPubKey=SCRIPT_PUB_KEY_OP_TRUE)) | ||||
pad_tx(rejected_parent) | pad_tx(rejected_parent) | ||||
rejected_parent.rehash() | rejected_parent.rehash() | ||||
with node.assert_debug_log(['not keeping orphan with rejected parents {}'.format(rejected_parent.hash)]): | with node.assert_debug_log(['not keeping orphan with rejected parents {}'.format(rejected_parent.hash)]): | ||||
node.p2p.send_txs_and_test([rejected_parent], node, success=False) | node.p2p.send_txs_and_test([rejected_parent], node, success=False) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
InvalidTxRequestTest().main() | InvalidTxRequestTest().main() |