Skip to content

Commit

Permalink
[Tests][Refactoring] Remove magic nubmers in p2p_invalid_* tests
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 5, 2021
1 parent 0ca5c18 commit 9df921a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
16 changes: 11 additions & 5 deletions test/functional/p2p_invalid_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from test_framework.blocktools import create_block, create_coinbase, create_transaction
from test_framework.messages import COIN
from test_framework.mininode import network_thread_start, P2PDataStore
from test_framework.script import (
CScript,
OP_TRUE,
)
from test_framework.test_framework import PivxTestFramework
from test_framework.util import assert_equal

Expand All @@ -24,6 +28,9 @@ def set_test_params(self):
self.setup_clean_chain = True
self.extra_args = [["-whitelist=127.0.0.1"]]

def create_tx(self, spend_tx, n, value):
return create_transaction(spend_tx, n, b"", value, CScript([OP_TRUE]))

def run_test(self):
# Add p2p connection to node0
node = self.nodes[0] # convenience reference to the node
Expand Down Expand Up @@ -65,9 +72,8 @@ def run_test(self):
block2 = create_block(tip, create_coinbase(height), block_time)
block_time += 1

# b'0x51' is OP_TRUE
tx1 = create_transaction(block1.vtx[0], 0, b'\x51', 50 * COIN)
tx2 = create_transaction(tx1, 0, b'\x51', 50 * COIN)
tx1 = self.create_tx(block1.vtx[0], 0, 50 * COIN)
tx2 = self.create_tx(tx1, 0, 50 * COIN)

block2.vtx.extend([tx1, tx2])
block2.hashMerkleRoot = block2.calc_merkle_root()
Expand Down Expand Up @@ -123,7 +129,7 @@ def run_test(self):
# Complete testing of CVE-2018-17144, by checking for the inflation bug.
# Create a block that spends the output of a tx in a previous block.
block4 = create_block(tip, create_coinbase(height), block_time)
tx3 = create_transaction(tx2, 0, b'\x51', 50 * COIN)
tx3 = self.create_tx(tx2, 0, 50 * COIN)

# Duplicates input
tx3.vin.append(tx3.vin[0])
Expand All @@ -137,7 +143,7 @@ def run_test(self):

self.log.info("Test output value > input value out of range")
# Can be removed when 'feature_block.py' is added to the suite.
tx4 = create_transaction(tx2, 0, b'\x51', 260 * COIN)
tx4 = self.create_tx(tx2, 0, 260 * COIN)
block4 = create_block(tip, create_coinbase(height), block_time)
block4.vtx.extend([tx4])
block4.hashMerkleRoot = block4.calc_merkle_root()
Expand Down
46 changes: 24 additions & 22 deletions test/functional/p2p_invalid_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
CTxOut,
)
from test_framework.mininode import network_thread_start, P2PDataStore, network_thread_join
from test_framework.script import (
CScript,
OP_NOTIF,
OP_TRUE,
)
from test_framework.test_framework import PivxTestFramework
from test_framework.util import (
assert_equal,
wait_until,
)


Expand Down Expand Up @@ -45,6 +49,18 @@ def reconnect_p2p(self, **kwargs):
network_thread_join()
self.bootstrap_p2p(**kwargs)

def new_spend_tx(self, prev_hash, prev_n, values):
"""Create a CTransaction spending COutPoint(prev_hash, prev_n)
each amount specified in the 'values' list is sent to an
anyone-can-spend script"""
tx = CTransaction()
tx.vin.append(CTxIn(outpoint=COutPoint(prev_hash, prev_n)))
for value in values:
tx.vout.append(CTxOut(nValue=value, scriptPubKey=CScript([OP_TRUE])))
tx.calc_sha256()
return tx

def run_test(self):
node = self.nodes[0] # convenience reference to the node

Expand All @@ -67,11 +83,10 @@ def run_test(self):
self.log.info("Mature the block.")
self.nodes[0].generate(100)

# b'\x64' is OP_NOTIF
# Transaction will be rejected with code 16 (REJECT_INVALID)
# and we get disconnected immediately
self.log.info('Test a transaction that is rejected')
tx1 = create_transaction(block1.vtx[0], 0, b'\x64', 50 * COIN - 12000)
tx1 = create_transaction(block1.vtx[0], 0, CScript([OP_NOTIF]), 50 * COIN - 12000)
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)

# Make two p2p connections to provide the node with orphans
Expand All @@ -82,32 +97,19 @@ def run_test(self):
self.log.info('Test orphan transaction handling ... ')
# Create a root transaction that we withhold until all dependend transactions
# are sent out and in the orphan cache
tx_withhold = CTransaction()
tx_withhold.vin.append(CTxIn(outpoint=COutPoint(block1.vtx[0].sha256, 0)))
tx_withhold.vout.append(CTxOut(nValue=50 * COIN - 12000, scriptPubKey=b'\x51'))
tx_withhold.calc_sha256()
tx_withhold = self.new_spend_tx(block1.vtx[0].sha256, 0, [50 * COIN - 12000])

# Our first orphan tx with some outputs to create further orphan txs
tx_orphan_1 = CTransaction()
tx_orphan_1.vin.append(CTxIn(outpoint=COutPoint(tx_withhold.sha256, 0)))
tx_orphan_1.vout = [CTxOut(nValue=10 * COIN, scriptPubKey=b'\x51')] * 3
tx_orphan_1.calc_sha256()
# Our first orphan tx with 3 outputs to create further orphan txs
tx_orphan_1 = self.new_spend_tx(tx_withhold.sha256, 0, [10 * COIN] * 3)

# A valid transaction with low fee
tx_orphan_2_no_fee = CTransaction()
tx_orphan_2_no_fee.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 0)))
tx_orphan_2_no_fee.vout.append(CTxOut(nValue=10 * COIN, scriptPubKey=b'\x51'))
tx_orphan_2_no_fee = self.new_spend_tx(tx_orphan_1.sha256, 0, [10 * COIN])

# A valid transaction with sufficient fee
tx_orphan_2_valid = CTransaction()
tx_orphan_2_valid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 1)))
tx_orphan_2_valid.vout.append(CTxOut(nValue=10 * COIN - 12000, scriptPubKey=b'\x51'))
tx_orphan_2_valid.calc_sha256()
tx_orphan_2_valid = self.new_spend_tx(tx_orphan_1.sha256, 1, [10 * COIN - 12000])

# An invalid transaction with negative fee
tx_orphan_2_invalid = CTransaction()
tx_orphan_2_invalid.vin.append(CTxIn(outpoint=COutPoint(tx_orphan_1.sha256, 2)))
tx_orphan_2_invalid.vout.append(CTxOut(nValue=11 * COIN, scriptPubKey=b'\x51'))
tx_orphan_2_invalid = self.new_spend_tx(tx_orphan_1.sha256, 2, [11 * COIN])

self.log.info('Send the orphans ... ')
# Send valid orphan txs from p2ps[0]
Expand Down

0 comments on commit 9df921a

Please sign in to comment.