Skip to content

Commit 48b4514

Browse files
random-zebrafurszy
authored andcommitted
[Tests] Tier2 tests: minor fixes
1 parent 4f7bdf7 commit 48b4514

File tree

3 files changed

+76
-62
lines changed

3 files changed

+76
-62
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ def get_mn_status(self, node, mnTxHash):
11101110
mnData = node.listmasternodes(mnTxHash)
11111111
if len(mnData) == 0:
11121112
return ""
1113+
assert_equal(len(mnData), 1)
11131114
return mnData[0]["status"]
11141115

11151116

@@ -1119,36 +1120,56 @@ def advance_mocktime(self, secs):
11191120
time.sleep(1)
11201121

11211122

1122-
def wait_until_mnsync_finished(self, _timeout):
1123-
for i in range(self.num_nodes):
1124-
try:
1125-
wait_until(lambda: self.nodes[i].mnsync("status")["RequestedMasternodeAssets"] == 999,
1126-
timeout=_timeout, mocktime=self.advance_mocktime)
1127-
except AssertionError:
1128-
raise AssertionError("Unable to complete mnsync on node %d" % i)
1123+
def wait_until_mnsync_finished(self):
1124+
SYNC_FINISHED = [999] * self.num_nodes
1125+
synced = [-1] * self.num_nodes
1126+
time.sleep(2)
1127+
timeout = time.time() + 300
1128+
while synced != SYNC_FINISHED and time.time() < timeout:
1129+
for i in range(self.num_nodes):
1130+
if synced[i] != SYNC_FINISHED[i]:
1131+
synced[i] = self.nodes[i].mnsync("status")["RequestedMasternodeAssets"]
1132+
if synced != SYNC_FINISHED:
1133+
time.sleep(5)
1134+
if synced != SYNC_FINISHED:
1135+
raise AssertionError("Unable to complete mnsync: %s" % str(synced))
11291136

11301137

11311138
def wait_until_mn_status(self, status, mnTxHash, _timeout, orEmpty=False, with_ping_mns=[]):
1132-
def _sendpings():
1133-
if len(with_ping_mns) == 0:
1134-
return
1135-
else:
1136-
self.send_pings(with_ping_mns)
1139+
nodes_status = [None] * self.num_nodes
11371140

1138-
for i in range(self.num_nodes):
1139-
try:
1140-
wait_until(lambda: (self.get_mn_status(self.nodes[i], mnTxHash) == status or
1141-
(orEmpty and self.get_mn_status(self.nodes[i], mnTxHash) == "")),
1142-
timeout=_timeout, sendpings=_sendpings, mocktime=self.advance_mocktime)
1143-
except AssertionError:
1144-
strErr = "Unable to get status \"%s\" on node %d for mnode %s" % (status, i, mnTxHash)
1145-
raise AssertionError(strErr)
1141+
def node_synced(i):
1142+
return nodes_status[i] == status or (orEmpty and nodes_status[i] == "")
1143+
1144+
def all_synced():
1145+
for i in range(self.num_nodes):
1146+
if not node_synced(i):
1147+
return False
1148+
return True
1149+
1150+
time.sleep(2)
1151+
timeout = time.time() + _timeout
1152+
while not all_synced() and time.time() < timeout:
1153+
for i in range(self.num_nodes):
1154+
if not node_synced(i):
1155+
nodes_status[i] = self.get_mn_status(self.nodes[i], mnTxHash)
1156+
if not all_synced():
1157+
time.sleep(2)
1158+
self.send_pings(with_ping_mns)
1159+
if not all_synced():
1160+
strErr = "Unable to get get status \"%s\" on all nodes for mnode %s. Current: %s" % (
1161+
status, mnTxHash, str(nodes_status))
1162+
raise AssertionError(strErr)
11461163

11471164

11481165
def wait_until_mn_enabled(self, mnTxHash, _timeout, _with_ping_mns=[]):
11491166
self.wait_until_mn_status("ENABLED", mnTxHash, _timeout, with_ping_mns=_with_ping_mns)
11501167

11511168

1169+
def wait_until_mn_preenabled(self, mnTxHash, _timeout, _with_ping_mns=[]):
1170+
self.wait_until_mn_status("PRE_ENABLED", mnTxHash, _timeout, with_ping_mns=_with_ping_mns)
1171+
1172+
11521173
def wait_until_mn_vinspent(self, mnTxHash, _timeout, _with_ping_mns=[]):
11531174
self.wait_until_mn_status("VIN_SPENT", mnTxHash, _timeout, orEmpty=True, with_ping_mns=_with_ping_mns)
11541175

@@ -1205,7 +1226,7 @@ def setupMasternode(self,
12051226
assert_equal(mnCollateralOutput["txhash"], collateralTxId)
12061227
mnCollateralOutputIndex = mnCollateralOutput["outputidx"]
12071228

1208-
self.log.info("collateral accepted for "+ masternodeAlias +".. updating masternode.conf and stopping the node")
1229+
self.log.info("collateral accepted for "+ masternodeAlias +". Updating masternode.conf...")
12091230

12101231
# verify collateral confirmed
12111232
confData = masternodeAlias + " 127.0.0.1:" + str(p2p_port(mnRemotePos)) + " " + str(masternodePrivKey) + " " + str(mnCollateralOutput["txhash"]) + " " + str(mnCollateralOutputIndex)
@@ -1269,7 +1290,6 @@ def set_test_params(self):
12691290
[],
12701291
["-listen", "-externalip=127.0.0.1"],
12711292
["-sporkkey=932HEevBSujW2ud7RfB1YF91AFygbBRQj3de3LyaCRqNzKKgWXi"]]
1272-
self.setup_clean_chain = True
12731293
self.enable_mocktime()
12741294

12751295
self.ownerOnePos = 0
@@ -1293,6 +1313,7 @@ def set_test_params(self):
12931313
self.mnOneTxHash = ""
12941314
self.mnTwoTxHash = ""
12951315

1316+
12961317
def send_3_pings(self):
12971318
self.advance_mocktime(30)
12981319
self.send_pings([self.remoteOne, self.remoteTwo])
@@ -1307,10 +1328,12 @@ def stake(self, num_blocks, with_ping_mns=[]):
13071328
def controller_start_all_masternodes(self):
13081329
self.controller_start_masternode(self.ownerOne, self.masternodeOneAlias)
13091330
self.controller_start_masternode(self.ownerTwo, self.masternodeTwoAlias)
1331+
self.wait_until_mn_preenabled(self.mnOneTxHash, 40)
1332+
self.wait_until_mn_preenabled(self.mnTwoTxHash, 40)
13101333
self.log.info("masternodes started, waiting until both get enabled..")
13111334
self.send_3_pings()
1312-
self.wait_until_mn_enabled(self.mnOneTxHash, 60, [self.remoteOne, self.remoteTwo])
1313-
self.wait_until_mn_enabled(self.mnTwoTxHash, 60, [self.remoteOne, self.remoteTwo])
1335+
self.wait_until_mn_enabled(self.mnOneTxHash, 120, [self.remoteOne, self.remoteTwo])
1336+
self.wait_until_mn_enabled(self.mnTwoTxHash, 120, [self.remoteOne, self.remoteTwo])
13141337
self.log.info("masternodes enabled and running properly!")
13151338

13161339
def advance_mocktime_and_stake(self, secs_to_add):
@@ -1361,12 +1384,10 @@ def setup_2_masternodes_network(self):
13611384
remoteTwoPort = p2p_port(self.remoteOnePos)
13621385
self.remoteOne.initmasternode(self.mnOnePrivkey, "127.0.0.1:"+str(remoteOnePort))
13631386
self.remoteTwo.initmasternode(self.mnTwoPrivkey, "127.0.0.1:"+str(remoteTwoPort))
1364-
self.advance_mocktime(3)
13651387

13661388
# wait until mnsync complete on all nodes
13671389
self.stake(1)
1368-
time.sleep(20)
1369-
self.wait_until_mnsync_finished(35)
1390+
self.wait_until_mnsync_finished()
13701391
self.log.info("tier two synced! starting masternodes..")
13711392

13721393
# Now everything is set, can start both masternodes

test/functional/tiertwo_masternode_activation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,18 @@ class MasternodeActivationTest(PivxTier2TestFramework):
3030

3131
def disconnect_remotes(self):
3232
for i in [self.remoteOnePos, self.remoteTwoPos]:
33-
disconnect_nodes(self.nodes[i + 1], i)
34-
disconnect_nodes(self.nodes[i], i - 1)
33+
for j in range(self.num_nodes):
34+
if i != j:
35+
disconnect_nodes(self.nodes[i], j)
3536

3637
def reconnect_remotes(self):
37-
# !TODO: for some reason we need two-way connections now...
3838
connect_nodes_clique(self.nodes)
3939
self.sync_all()
4040

4141
def reconnect_and_restart_masternodes(self):
4242
self.log.info("Reconnecting nodes and sending start message again...")
4343
self.reconnect_remotes()
44-
time.sleep(2)
45-
self.wait_until_mnsync_finished(35)
44+
self.wait_until_mnsync_finished()
4645
self.controller_start_all_masternodes()
4746

4847
def spend_collateral(self):

test/functional/tiertwo_masternode_ping.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
from test_framework.util import (
88
assert_equal,
99
assert_greater_than,
10-
connect_nodes_clique,
11-
disconnect_nodes,
1210
Decimal,
1311
p2p_port,
14-
satoshi_round,
1512
sync_blocks,
16-
wait_until,
1713
)
1814

1915
import os
@@ -49,18 +45,26 @@ def run_test(self):
4945
masternodeAlias = "mnode"
5046
mnAddress = owner.getnewaddress(masternodeAlias)
5147
collateralTxId = miner.sendtoaddress(mnAddress, Decimal('10000'))
52-
miner.generate(1)
48+
miner.generate(2)
5349
sync_blocks(self.nodes)
50+
time.sleep(1)
51+
collateral_rawTx = owner.getrawtransaction(collateralTxId, 1)
5452
assert_equal(owner.getbalance(), Decimal('10000'))
55-
assert_greater_than(owner.getrawtransaction(collateralTxId, 1)["confirmations"], 0)
53+
assert_greater_than(collateral_rawTx["confirmations"], 0)
54+
55+
# Block time can be up to median time past +1. We might need to wait...
56+
wait_time = collateral_rawTx["time"] - int(time.time())
57+
if wait_time > 0:
58+
self.log.info("Sleep %d seconds to catch up with the chain..." % wait_time)
59+
time.sleep(wait_time)
5660

5761
# Setup controller
5862
self.log.info("controller setup...")
5963
o = owner.getmasternodeoutputs()
6064
assert_equal(len(o), 1)
6165
assert_equal(o[0]["txhash"], collateralTxId)
6266
vout = o[0]["outputidx"]
63-
self.log.info("collateral accepted for "+ masternodeAlias +".. updating masternode.conf and stopping the node")
67+
self.log.info("collateral accepted for "+ masternodeAlias +". Updating masternode.conf...")
6468
confData = masternodeAlias + " 127.0.0.1:" + str(p2p_port(2)) + " " + \
6569
str(mnPrivkey) + " " + str(collateralTxId) + " " + str(vout)
6670
destPath = os.path.join(self.options.tmpdir, "node1", "regtest", "masternode.conf")
@@ -72,18 +76,14 @@ def run_test(self):
7276
self.log.info("initializing remote masternode...")
7377
remote.initmasternode(mnPrivkey, "127.0.0.1:" + str(p2p_port(2)))
7478

75-
# Wait until mnsync is complete (max 30 seconds)
76-
self.log.info("waiting complete mnsync...")
77-
synced = [False] * 3
78-
timeout = time.time() + 30
79-
while time.time() < timeout:
80-
for i in range(3):
81-
if not synced[i]:
82-
synced[i] = (self.nodes[i].mnsync("status")["RequestedMasternodeAssets"] == 999)
83-
if synced != [True] * 3:
84-
time.sleep(1)
85-
if synced != [True] * 3:
86-
raise AssertionError("Unable to complete mnsync: %s" % str(synced))
79+
# Wait until mnsync is complete (max 120 seconds)
80+
self.log.info("waiting to complete mnsync...")
81+
start_time = time.time()
82+
self.wait_until_mnsync_finished()
83+
self.log.info("MnSync completed in %d seconds" % (time.time() - start_time))
84+
miner.generate(1)
85+
sync_blocks(self.nodes)
86+
time.sleep(1)
8787

8888
# Send Start message
8989
self.log.info("sending masternode broadcast...")
@@ -92,18 +92,12 @@ def run_test(self):
9292
sync_blocks(self.nodes)
9393
time.sleep(1)
9494

95-
# Wait until masternode is enabled anywhere (max 100 secs)
95+
# Wait until masternode is enabled everywhere (max 180 secs)
9696
self.log.info("waiting till masternode gets enabled...")
97-
enabled = [""] * 3
98-
timeout = time.time() + 100
99-
while time.time() < timeout:
100-
for i in range(3):
101-
if enabled[i] != "ENABLED":
102-
enabled[i] = self.get_mn_status(self.nodes[i], collateralTxId)
103-
if enabled != ["ENABLED"] * 3:
104-
time.sleep(1)
105-
if enabled != ["ENABLED"] * 3:
106-
raise AssertionError("Unable to get to \"ENABLED\" state: %s" % str(enabled))
97+
start_time = time.time()
98+
time.sleep(5)
99+
self.wait_until_mn_enabled(collateralTxId, 180)
100+
self.log.info("Masternode enabled in %d seconds" % (time.time() - start_time))
107101
self.log.info("Good. Masternode enabled")
108102
miner.generate(1)
109103
sync_blocks(self.nodes)

0 commit comments

Comments
 (0)