Skip to content

Commit f806584

Browse files
random-zebrafurszy
authored andcommitted
test: move sync_blocks and sync_mempool functions to test_framework.py
>>> backports bitcoin/bitcoin@cc84460 Github-Pull: #2254 Rebased-From: 2488ebc
1 parent d902128 commit f806584

33 files changed

+161
-184
lines changed

test/functional/example_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def setup_network(self):
113113
# sync_all() should not include node2, since we're not expecting it to
114114
# sync.
115115
connect_nodes(self.nodes[0], 1)
116-
self.sync_all([self.nodes[0:1]])
116+
self.sync_all(self.nodes[0:1])
117117

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

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

148148
# Notice above how we called an RPC by calling a method with the same
149149
# name on the node object. Notice also how we used a keyword argument

test/functional/feature_fee_estimation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ def transact_and_mine(self, numblocks, mining_node):
191191
self.memutxo, Decimal("0.05"), MIN_FEE, MIN_FEE)
192192
tx_kbytes = (len(txhex) // 2) / 1000.0
193193
self.fees_per_kb.append(float(fee)/tx_kbytes)
194-
sync_mempools(self.nodes[0:3], wait=.1)
194+
self.sync_mempools(self.nodes[0:3], wait=.1)
195195
mined = mining_node.getblock(mining_node.generate(1)[0],True)["tx"]
196-
sync_blocks(self.nodes[0:3], wait=.1)
196+
self.sync_blocks(self.nodes[0:3], wait=.1)
197197
# update which txouts are confirmed
198198
newmem = []
199199
for utx in self.memutxo:
@@ -270,7 +270,7 @@ def run_test(self):
270270
while len(self.nodes[1].getrawmempool()) > 0:
271271
self.nodes[1].generate(1)
272272

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

test/functional/mempool_packages.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
Decimal,
1313
ROUND_DOWN,
1414
JSONRPCException,
15-
sync_blocks,
16-
sync_mempools
1715
)
1816

1917
def satoshi_round(amount):
@@ -141,7 +139,7 @@ def run_test(self):
141139
# Test reorg handling
142140
# First, the basics:
143141
self.nodes[0].generate(1)
144-
sync_blocks(self.nodes)
142+
self.sync_blocks()
145143
self.nodes[1].invalidateblock(self.nodes[0].getbestblockhash())
146144
self.nodes[1].reconsiderblock(self.nodes[0].getbestblockhash())
147145

@@ -196,12 +194,12 @@ def run_test(self):
196194
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
197195
signedtx = self.nodes[0].signrawtransaction(rawtx)
198196
txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
199-
sync_mempools(self.nodes)
197+
self.sync_mempools()
200198

201199
# Now try to disconnect the tip on each node...
202200
self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
203201
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
204-
sync_blocks(self.nodes)
202+
self.sync_blocks()
205203

206204
if __name__ == '__main__':
207205
MempoolPackagesTest().main()

test/functional/mining_pos_coldStaking.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
p2p_port,
2020
bytes_to_hex_str,
2121
set_node_times,
22-
sync_blocks,
23-
sync_mempools,
2422
)
2523

2624
from decimal import Decimal
@@ -102,7 +100,7 @@ def run_test(self):
102100
for peer in [0, 2]:
103101
for j in range(25):
104102
self.mocktime = self.generate_pow(peer, self.mocktime)
105-
sync_blocks(self.nodes)
103+
self.sync_blocks()
106104

107105
# 2) node[1] sends his entire balance (50 mature rewards) to node[2]
108106
# - node[2] stakes a block - node[1] locks the change
@@ -112,9 +110,9 @@ def run_test(self):
112110
assert_equal(self.nodes[1].getbalance(), 50 * 250)
113111
txid = self.nodes[1].sendtoaddress(self.nodes[2].getnewaddress(), (50 * 250 - 0.01))
114112
assert (txid is not None)
115-
sync_mempools(self.nodes)
113+
self.sync_mempools()
116114
self.mocktime = self.generate_pos(2, self.mocktime)
117-
sync_blocks(self.nodes)
115+
self.sync_blocks()
118116
# lock the change output (so it's not used as stake input in generate_pos)
119117
for x in self.nodes[1].listunspent():
120118
assert (self.nodes[1].lockunspent(False, [{"txid": x['txid'], "vout": x['vout']}]))
@@ -128,7 +126,7 @@ def run_test(self):
128126
self.sync_all()
129127
for i in range(6):
130128
self.mocktime = self.generate_pow(0, self.mocktime)
131-
sync_blocks(self.nodes)
129+
self.sync_blocks()
132130
assert_equal(self.nodes[0].getshieldbalance(), 250)
133131

134132
# 3) nodes[0] generates a owner address
@@ -193,9 +191,9 @@ def run_test(self):
193191
assert_equal(res["staker_address"], staker_address)
194192
fee = self.nodes[0].viewshieldtransaction(res["txid"])['fee']
195193
# sync and mine 2 blocks
196-
sync_mempools(self.nodes)
194+
self.sync_mempools()
197195
self.mocktime = self.generate_pos(2, self.mocktime)
198-
sync_blocks(self.nodes)
196+
self.sync_blocks()
199197
self.log.info("%d Txes created." % NUM_OF_INPUTS)
200198
# check balances:
201199
self.expected_balance = NUM_OF_INPUTS * INPUT_VALUE
@@ -215,9 +213,9 @@ def run_test(self):
215213
txhash = self.spendUTXOwithNode(u, 0)
216214
assert(txhash != None)
217215
self.log.info("Good. Owner was able to spend - tx: %s" % str(txhash))
218-
sync_mempools(self.nodes)
216+
self.sync_mempools()
219217
self.mocktime = self.generate_pos(2, self.mocktime)
220-
sync_blocks(self.nodes)
218+
self.sync_blocks()
221219
# check tx
222220
self.check_tx_in_chain(0, txhash)
223221
self.check_tx_in_chain(1, txhash)
@@ -251,7 +249,7 @@ def run_test(self):
251249
self.spendUTXOwithNode, u, 1)
252250
self.log.info("Good. Cold staker was NOT able to spend (failed OP_CHECKCOLDSTAKEVERIFY)")
253251
self.mocktime = self.generate_pos(2, self.mocktime)
254-
sync_blocks(self.nodes)
252+
self.sync_blocks()
255253

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

266264
# Verify that nodes[0] accepts it
267-
sync_blocks(self.nodes)
265+
self.sync_blocks()
268266
assert_equal(self.nodes[0].getblockcount(), self.nodes[1].getblockcount())
269267
assert_equal(newblockhash, self.nodes[0].getbestblockhash())
270268
self.log.info("Great. Cold-staked block was accepted!")
@@ -292,7 +290,7 @@ def run_test(self):
292290
assert_equal(new_block.hash, self.nodes[1].getbestblockhash())
293291

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

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

347345
# Verify that nodes[0] rejects it
348-
sync_blocks(self.nodes)
346+
self.sync_blocks()
349347
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblock, new_block.hash)
350348
self.log.info("Great. Malicious cold-staked block was NOT accepted!")
351349
self.checkBalances()
@@ -355,7 +353,7 @@ def run_test(self):
355353
# ----------------------------------------------------------------------------------------
356354
self.log.info("Let's void the contracts.")
357355
self.mocktime = self.generate_pos(2, self.mocktime)
358-
sync_blocks(self.nodes)
356+
self.sync_blocks()
359357
print("*** 13 ***")
360358
self.log.info("Cancel the stake delegation spending the delegated utxos...")
361359
delegated_utxos = getDelegatedUtxos(self.nodes[0].listunspent())
@@ -364,19 +362,19 @@ def run_test(self):
364362
txhash = self.spendUTXOsWithNode(delegated_utxos, 0)
365363
assert(txhash != None)
366364
self.log.info("Good. Owner was able to void the stake delegations - tx: %s" % str(txhash))
367-
sync_mempools(self.nodes)
365+
self.sync_blocks()
368366
self.mocktime = self.generate_pos(2, self.mocktime)
369-
sync_blocks(self.nodes)
367+
self.sync_blocks()
370368

371369
# deactivate SPORK 17 and check that the owner can still spend the last utxo
372370
self.setColdStakingEnforcement(False)
373371
assert (not self.isColdStakingEnforced())
374372
txhash = self.spendUTXOsWithNode([final_spend], 0)
375373
assert(txhash != None)
376374
self.log.info("Good. Owner was able to void a stake delegation (with SPORK 17 disabled) - tx: %s" % str(txhash))
377-
sync_mempools(self.nodes)
375+
self.sync_mempools()
378376
self.mocktime = self.generate_pos(2, self.mocktime)
379-
sync_blocks(self.nodes)
377+
self.sync_blocks()
380378
# check tx
381379
self.check_tx_in_chain(0, txhash)
382380
self.check_tx_in_chain(1, txhash)
@@ -403,17 +401,17 @@ def run_test(self):
403401
for peer in [0, 2]:
404402
for j in range(25):
405403
self.mocktime = self.generate_pos(peer, self.mocktime)
406-
sync_blocks(self.nodes)
404+
self.sync_blocks()
407405
self.expected_balance = self.expected_immature_balance
408406
self.expected_immature_balance = 0
409407
self.checkBalances()
410408
delegated_utxos = getDelegatedUtxos(self.nodes[0].listunspent())
411409
txhash = self.spendUTXOsWithNode(delegated_utxos, 0)
412410
assert (txhash != None)
413411
self.log.info("Good. Owner was able to spend the cold staked coins - tx: %s" % str(txhash))
414-
sync_mempools(self.nodes)
412+
self.sync_mempools()
415413
self.mocktime = self.generate_pos(2, self.mocktime)
416-
sync_blocks(self.nodes)
414+
self.sync_blocks()
417415
# check tx
418416
self.check_tx_in_chain(0, txhash)
419417
self.check_tx_in_chain(1, txhash)
@@ -481,8 +479,5 @@ def add_output_to_coinstake(self, block, value, peer=1):
481479
block.re_sign_block()
482480

483481

484-
485-
486-
487482
if __name__ == '__main__':
488483
PIVX_ColdStakingTest().main()

test/functional/mining_pos_fakestake.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
from test_framework.messages import COutPoint
5151
from test_framework.test_framework import PivxTestFramework
5252
from test_framework.util import (
53-
sync_blocks,
5453
assert_equal,
5554
bytes_to_hex_str,
5655
set_node_times
@@ -93,7 +92,7 @@ def run_test(self):
9392
self.log.info("Mining 50 blocks to reach PoS phase...")
9493
for i in range(50):
9594
self.mocktime = self.generate_pow(0, self.mocktime)
96-
sync_blocks(self.nodes)
95+
self.sync_blocks()
9796

9897
# Check Tests 1-3
9998
self.test_1()
@@ -116,7 +115,7 @@ def test_1(self):
116115
self.log.info("Mining 5 blocks as fork depth...")
117116
for i in range(5):
118117
self.mocktime = self.generate_pow(0, self.mocktime)
119-
sync_blocks(self.nodes)
118+
self.sync_blocks()
120119

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

test/functional/mining_pos_reorg.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
from test_framework.authproxy import JSONRPCException
76
from test_framework.test_framework import PivxTestFramework
87
from test_framework.util import (
9-
sync_blocks,
108
assert_equal,
119
assert_raises_rpc_error,
1210
connect_nodes,
@@ -113,7 +111,7 @@ def findUtxoInList(txid, vout, utxo_list):
113111
# Connect with node 2 and sync
114112
self.log.info("Reconnecting node 0 and node 2")
115113
connect_nodes(self.nodes[0], 2)
116-
sync_blocks([self.nodes[i] for i in [0, 2]])
114+
self.sync_blocks([self.nodes[i] for i in [0, 2]])
117115

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

@@ -158,7 +156,7 @@ def findUtxoInList(txid, vout, utxo_list):
158156
stakeinput["txid"][:9], stakeinput["txid"][-4:], stakeinput["vout"]))
159157
self.nodes[0].sendrawtransaction(rawtx["hex"])
160158
self.nodes[1].generate(1)
161-
sync_blocks(self.nodes)
159+
self.sync_blocks()
162160
res, utxo = findUtxoInList(stakeinput["txid"], stakeinput["vout"], self.nodes[0].listunspent())
163161
assert (not res or not utxo["spendable"])
164162

test/functional/p2p_feefilter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def run_test(self):
4545
node0 = self.nodes[0]
4646
# Get out of IBD
4747
node1.generate(1)
48-
sync_blocks(self.nodes)
48+
self.sync_blocks()
4949

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

7474
# Send one transaction from node0 that should be received, so that we
7575
# we can sync the test on receipt (if node1's txs were relayed, they'd

test/functional/p2p_sendheaders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def mine_blocks(self, count):
247247
# return the list of block hashes newly mined
248248
def mine_reorg(self, length):
249249
self.nodes[0].generate(length) # make sure all invalidated blocks are node0's
250-
sync_blocks(self.nodes, wait=0.1)
250+
self.sync_blocks(self.nodes, wait=0.1)
251251
for x in self.p2p_connections:
252252
x.wait_for_block_announcement(int(self.nodes[0].getbestblockhash(), 16))
253253
x.clear_last_announcement()
@@ -256,7 +256,7 @@ def mine_reorg(self, length):
256256
hash_to_invalidate = self.nodes[1].getblockhash(tip_height-(length-1))
257257
self.nodes[1].invalidateblock(hash_to_invalidate)
258258
all_hashes = self.nodes[1].generate(length+1) # Must be longer than the orig chain
259-
sync_blocks(self.nodes, wait=0.1)
259+
self.sync_blocks(self.nodes, wait=0.1)
260260
return [int(x, 16) for x in all_hashes]
261261

262262
def run_test(self):

test/functional/p2p_unrequested_blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def run_test(self):
316316

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

322322
if __name__ == '__main__':

test/functional/rpc_getchaintips.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def run_test (self):
2828
self.split_network ()
2929
self.nodes[0].generate(10)
3030
self.nodes[2].generate(20)
31-
self.sync_all([self.nodes[:2], self.nodes[2:]])
31+
self.sync_all(self.nodes[:2])
32+
self.sync_all(self.nodes[2:])
3233

3334
tips = self.nodes[1].getchaintips ()
3435
assert_equal (len (tips), 1)

0 commit comments

Comments
 (0)