Skip to content

Commit

Permalink
test: move sync_blocks and sync_mempool functions to test_framework.py
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Mar 19, 2021
1 parent d076c81 commit 2488ebc
Show file tree
Hide file tree
Showing 33 changed files with 161 additions and 184 deletions.
4 changes: 2 additions & 2 deletions test/functional/example_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def setup_network(self):
# sync_all() should not include node2, since we're not expecting it to
# sync.
connect_nodes(self.nodes[0], 1)
self.sync_all([self.nodes[0:1]])
self.sync_all(self.nodes[0:1])

# Use setup_nodes() to customize the node start behaviour (for example if
# you don't want to start all nodes at the start of the test).
Expand Down Expand Up @@ -143,7 +143,7 @@ def run_test(self):

# Generating a block on one of the nodes will get us out of IBD
blocks = [int(self.nodes[0].generate(1)[0], 16)]
self.sync_all([self.nodes[0:1]])
self.sync_all(self.nodes[0:1])

# Notice above how we called an RPC by calling a method with the same
# name on the node object. Notice also how we used a keyword argument
Expand Down
6 changes: 3 additions & 3 deletions test/functional/feature_fee_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def transact_and_mine(self, numblocks, mining_node):
self.memutxo, Decimal("0.05"), MIN_FEE, MIN_FEE)
tx_kbytes = (len(txhex) // 2) / 1000.0
self.fees_per_kb.append(float(fee)/tx_kbytes)
sync_mempools(self.nodes[0:3], wait=.1)
self.sync_mempools(self.nodes[0:3], wait=.1)
mined = mining_node.getblock(mining_node.generate(1)[0],True)["tx"]
sync_blocks(self.nodes[0:3], wait=.1)
self.sync_blocks(self.nodes[0:3], wait=.1)
# update which txouts are confirmed
newmem = []
for utx in self.memutxo:
Expand Down Expand Up @@ -270,7 +270,7 @@ def run_test(self):
while len(self.nodes[1].getrawmempool()) > 0:
self.nodes[1].generate(1)

sync_blocks(self.nodes[0:3], wait=.1)
self.sync_blocks(self.nodes[0:3], wait=.1)
self.log.info("Final estimates after emptying mempools")
check_estimates(self.nodes[1], self.fees_per_kb, 2)

Expand Down
8 changes: 3 additions & 5 deletions test/functional/mempool_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
Decimal,
ROUND_DOWN,
JSONRPCException,
sync_blocks,
sync_mempools
)

def satoshi_round(amount):
Expand Down Expand Up @@ -141,7 +139,7 @@ def run_test(self):
# Test reorg handling
# First, the basics:
self.nodes[0].generate(1)
sync_blocks(self.nodes)
self.sync_blocks()
self.nodes[1].invalidateblock(self.nodes[0].getbestblockhash())
self.nodes[1].reconsiderblock(self.nodes[0].getbestblockhash())

Expand Down Expand Up @@ -196,12 +194,12 @@ def run_test(self):
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
signedtx = self.nodes[0].signrawtransaction(rawtx)
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
sync_mempools(self.nodes)
self.sync_mempools()

# Now try to disconnect the tip on each node...
self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
sync_blocks(self.nodes)
self.sync_blocks()

if __name__ == '__main__':
MempoolPackagesTest().main()
47 changes: 21 additions & 26 deletions test/functional/mining_pos_coldStaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
p2p_port,
bytes_to_hex_str,
set_node_times,
sync_blocks,
sync_mempools,
)

from decimal import Decimal
Expand Down Expand Up @@ -102,7 +100,7 @@ def run_test(self):
for peer in [0, 2]:
for j in range(25):
self.mocktime = self.generate_pow(peer, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()

# 2) node[1] sends his entire balance (50 mature rewards) to node[2]
# - node[2] stakes a block - node[1] locks the change
Expand All @@ -112,9 +110,9 @@ def run_test(self):
assert_equal(self.nodes[1].getbalance(), 50 * 250)
txid = self.nodes[1].sendtoaddress(self.nodes[2].getnewaddress(), (50 * 250 - 0.01))
assert (txid is not None)
sync_mempools(self.nodes)
self.sync_mempools()
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
# lock the change output (so it's not used as stake input in generate_pos)
for x in self.nodes[1].listunspent():
assert (self.nodes[1].lockunspent(False, [{"txid": x['txid'], "vout": x['vout']}]))
Expand All @@ -128,7 +126,7 @@ def run_test(self):
self.sync_all()
for i in range(6):
self.mocktime = self.generate_pow(0, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
assert_equal(self.nodes[0].getshieldbalance(), 250)

# 3) nodes[0] generates a owner address
Expand Down Expand Up @@ -193,9 +191,9 @@ def run_test(self):
assert_equal(res["staker_address"], staker_address)
fee = self.nodes[0].viewshieldtransaction(res["txid"])['fee']
# sync and mine 2 blocks
sync_mempools(self.nodes)
self.sync_mempools()
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
self.log.info("%d Txes created." % NUM_OF_INPUTS)
# check balances:
self.expected_balance = NUM_OF_INPUTS * INPUT_VALUE
Expand All @@ -215,9 +213,9 @@ def run_test(self):
txhash = self.spendUTXOwithNode(u, 0)
assert(txhash != None)
self.log.info("Good. Owner was able to spend - tx: %s" % str(txhash))
sync_mempools(self.nodes)
self.sync_mempools()
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
# check tx
self.check_tx_in_chain(0, txhash)
self.check_tx_in_chain(1, txhash)
Expand Down Expand Up @@ -251,7 +249,7 @@ def run_test(self):
self.spendUTXOwithNode, u, 1)
self.log.info("Good. Cold staker was NOT able to spend (failed OP_CHECKCOLDSTAKEVERIFY)")
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()

# 9) check that the staker can use the coins to stake a block with internal miner.
# --------------------------------------------------------------------------------
Expand All @@ -264,7 +262,7 @@ def run_test(self):
self.log.info("Block %s submitted" % newblockhash)

# Verify that nodes[0] accepts it
sync_blocks(self.nodes)
self.sync_blocks()
assert_equal(self.nodes[0].getblockcount(), self.nodes[1].getblockcount())
assert_equal(newblockhash, self.nodes[0].getbestblockhash())
self.log.info("Great. Cold-staked block was accepted!")
Expand Down Expand Up @@ -292,7 +290,7 @@ def run_test(self):
assert_equal(new_block.hash, self.nodes[1].getbestblockhash())

# Verify that nodes[0] accepts it
sync_blocks(self.nodes)
self.sync_blocks()
assert_equal(self.nodes[0].getblockcount(), self.nodes[1].getblockcount())
assert_equal(new_block.hash, self.nodes[0].getbestblockhash())
self.log.info("Great. Cold-staked block was accepted!")
Expand Down Expand Up @@ -321,7 +319,7 @@ def run_test(self):
assert("rejected" in ret)

# Verify that nodes[0] rejects it
sync_blocks(self.nodes)
self.sync_blocks()
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblock, new_block.hash)
self.log.info("Great. Malicious cold-staked block was NOT accepted!")
self.checkBalances()
Expand All @@ -345,7 +343,7 @@ def run_test(self):
assert_equal(ret, "bad-p2cs-outs")

# Verify that nodes[0] rejects it
sync_blocks(self.nodes)
self.sync_blocks()
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblock, new_block.hash)
self.log.info("Great. Malicious cold-staked block was NOT accepted!")
self.checkBalances()
Expand All @@ -355,7 +353,7 @@ def run_test(self):
# ----------------------------------------------------------------------------------------
self.log.info("Let's void the contracts.")
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
print("*** 13 ***")
self.log.info("Cancel the stake delegation spending the delegated utxos...")
delegated_utxos = getDelegatedUtxos(self.nodes[0].listunspent())
Expand All @@ -364,19 +362,19 @@ def run_test(self):
txhash = self.spendUTXOsWithNode(delegated_utxos, 0)
assert(txhash != None)
self.log.info("Good. Owner was able to void the stake delegations - tx: %s" % str(txhash))
sync_mempools(self.nodes)
self.sync_blocks()
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()

# deactivate SPORK 17 and check that the owner can still spend the last utxo
self.setColdStakingEnforcement(False)
assert (not self.isColdStakingEnforced())
txhash = self.spendUTXOsWithNode([final_spend], 0)
assert(txhash != None)
self.log.info("Good. Owner was able to void a stake delegation (with SPORK 17 disabled) - tx: %s" % str(txhash))
sync_mempools(self.nodes)
self.sync_mempools()
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
# check tx
self.check_tx_in_chain(0, txhash)
self.check_tx_in_chain(1, txhash)
Expand All @@ -403,17 +401,17 @@ def run_test(self):
for peer in [0, 2]:
for j in range(25):
self.mocktime = self.generate_pos(peer, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
self.expected_balance = self.expected_immature_balance
self.expected_immature_balance = 0
self.checkBalances()
delegated_utxos = getDelegatedUtxos(self.nodes[0].listunspent())
txhash = self.spendUTXOsWithNode(delegated_utxos, 0)
assert (txhash != None)
self.log.info("Good. Owner was able to spend the cold staked coins - tx: %s" % str(txhash))
sync_mempools(self.nodes)
self.sync_mempools()
self.mocktime = self.generate_pos(2, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
# check tx
self.check_tx_in_chain(0, txhash)
self.check_tx_in_chain(1, txhash)
Expand Down Expand Up @@ -481,8 +479,5 @@ def add_output_to_coinstake(self, block, value, peer=1):
block.re_sign_block()





if __name__ == '__main__':
PIVX_ColdStakingTest().main()
7 changes: 3 additions & 4 deletions test/functional/mining_pos_fakestake.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
from test_framework.messages import COutPoint
from test_framework.test_framework import PivxTestFramework
from test_framework.util import (
sync_blocks,
assert_equal,
bytes_to_hex_str,
set_node_times
Expand Down Expand Up @@ -93,7 +92,7 @@ def run_test(self):
self.log.info("Mining 50 blocks to reach PoS phase...")
for i in range(50):
self.mocktime = self.generate_pow(0, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()

# Check Tests 1-3
self.test_1()
Expand All @@ -116,7 +115,7 @@ def test_1(self):
self.log.info("Mining 5 blocks as fork depth...")
for i in range(5):
self.mocktime = self.generate_pow(0, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()

# nodes[1] spams 3 blocks with height 256 --> [REJECTED]
assert_equal(self.nodes[1].getblockcount(), 255)
Expand All @@ -138,7 +137,7 @@ def test_2(self):
self.log.info("Mining 5 blocks to include the spends...")
for i in range(5):
self.mocktime = self.generate_pow(0, self.mocktime)
sync_blocks(self.nodes)
self.sync_blocks()
self.check_tx_in_chain(0, txid)
assert_equal(self.nodes[1].getbalance(), 0)

Expand Down
8 changes: 3 additions & 5 deletions test/functional/mining_pos_reorg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import PivxTestFramework
from test_framework.util import (
sync_blocks,
assert_equal,
assert_raises_rpc_error,
connect_nodes,
Expand Down Expand Up @@ -113,7 +111,7 @@ def findUtxoInList(txid, vout, utxo_list):
# Connect with node 2 and sync
self.log.info("Reconnecting node 0 and node 2")
connect_nodes(self.nodes[0], 2)
sync_blocks([self.nodes[i] for i in [0, 2]])
self.sync_blocks([self.nodes[i] for i in [0, 2]])

# verify that the stakeinput can't be spent
stakeinput_tx_json = self.nodes[0].getrawtransaction(stakeinput["txid"], True)
Expand Down Expand Up @@ -143,7 +141,7 @@ def findUtxoInList(txid, vout, utxo_list):
self.log.info("Connecting and syncing nodes...")
set_node_times(self.nodes, block_time_1)
connect_nodes_clique(self.nodes)
sync_blocks(self.nodes)
self.sync_blocks()
for i in [0, 2]:
assert_equal(self.nodes[i].getbestblockhash(), new_best_hash)

Expand All @@ -158,7 +156,7 @@ def findUtxoInList(txid, vout, utxo_list):
stakeinput["txid"][:9], stakeinput["txid"][-4:], stakeinput["vout"]))
self.nodes[0].sendrawtransaction(rawtx["hex"])
self.nodes[1].generate(1)
sync_blocks(self.nodes)
self.sync_blocks()
res, utxo = findUtxoInList(stakeinput["txid"], stakeinput["vout"], self.nodes[0].listunspent())
assert (not res or not utxo["spendable"])

Expand Down
4 changes: 2 additions & 2 deletions test/functional/p2p_feefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_test(self):
node0 = self.nodes[0]
# Get out of IBD
node1.generate(1)
sync_blocks(self.nodes)
self.sync_blocks()

# Setup the p2p connections and start up the network thread.
self.nodes[0].add_p2p_connection(TestNode())
Expand All @@ -69,7 +69,7 @@ def run_test(self):
# Change tx fee rate to 10 sat/byte and test they are no longer received
node1.settxfee(float(0.00010000))
[node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
sync_mempools(self.nodes) # must be sure node 0 has received all txs
self.sync_mempools() # must be sure node 0 has received all txs

# Send one transaction from node0 that should be received, so that we
# we can sync the test on receipt (if node1's txs were relayed, they'd
Expand Down
4 changes: 2 additions & 2 deletions test/functional/p2p_sendheaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def mine_blocks(self, count):
# return the list of block hashes newly mined
def mine_reorg(self, length):
self.nodes[0].generate(length) # make sure all invalidated blocks are node0's
sync_blocks(self.nodes, wait=0.1)
self.sync_blocks(self.nodes, wait=0.1)
for x in self.p2p_connections:
x.wait_for_block_announcement(int(self.nodes[0].getbestblockhash(), 16))
x.clear_last_announcement()
Expand All @@ -256,7 +256,7 @@ def mine_reorg(self, length):
hash_to_invalidate = self.nodes[1].getblockhash(tip_height-(length-1))
self.nodes[1].invalidateblock(hash_to_invalidate)
all_hashes = self.nodes[1].generate(length+1) # Must be longer than the orig chain
sync_blocks(self.nodes, wait=0.1)
self.sync_blocks(self.nodes, wait=0.1)
return [int(x, 16) for x in all_hashes]

def run_test(self):
Expand Down
2 changes: 1 addition & 1 deletion test/functional/p2p_unrequested_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def run_test(self):

# 9. Connect node1 to node0 and ensure it is able to sync
connect_nodes(self.nodes[0], 1)
sync_blocks([self.nodes[0], self.nodes[1]])
self.sync_blocks([self.nodes[0], self.nodes[1]])
self.log.info("Successfully synced nodes 1 and 0")

if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion test/functional/rpc_getchaintips.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def run_test (self):
self.split_network ()
self.nodes[0].generate(10)
self.nodes[2].generate(20)
self.sync_all([self.nodes[:2], self.nodes[2:]])
self.sync_all(self.nodes[:2])
self.sync_all(self.nodes[2:])

tips = self.nodes[1].getchaintips ()
assert_equal (len (tips), 1)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/rpc_invalidateblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run_test(self):

self.log.info("Connect nodes to force a reorg")
connect_nodes(self.nodes[0], 1)
sync_blocks(self.nodes[0:2])
self.sync_blocks(self.nodes[0:2])
assert_equal(self.nodes[0].getblockcount(), 6)
badhash = self.nodes[1].getblockhash(2)

Expand All @@ -40,7 +40,7 @@ def run_test(self):
self.log.info("Make sure we won't reorg to a lower work chain:")
connect_nodes(self.nodes[1], 2)
self.log.info("Sync node 2 to node 1 so both have 6 blocks")
sync_blocks(self.nodes[1:3])
self.sync_blocks(self.nodes[1:3])
assert_equal(self.nodes[2].getblockcount(), 6)
self.log.info("Invalidate block 5 on node 1 so its tip is now at 4")
self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))
Expand Down
Loading

0 comments on commit 2488ebc

Please sign in to comment.