1+ #!/usr/bin/env python3
2+
3+ from test_framework .test_framework import PivxTestFramework
4+ from test_framework .util import *
5+ import time
6+
7+ """
8+ Test checking:
9+ 1) Masternode setup/creation.
10+ 2) Tier two network sync (masternode broadcasting).
11+ 3) Masternode activation.
12+ """
13+
14+ class MasternodeActivationTest (PivxTestFramework ):
15+
16+ def set_test_params (self ):
17+ self .num_nodes = 3
18+ self .extra_args = [[], [], []]
19+ self .setup_clean_chain = True
20+
21+ def check_mnsync_finished (self ):
22+ for i in range (0 , 2 ):
23+ status = self .nodes [i ].mnsync ("status" )["RequestedMasternodeAssets" ]
24+ assert_equal (status , 999 ) # sync finished
25+
26+ def check_first_mn_in_mnlist_enabled (self , node ):
27+ listMNs = node .listmasternodes ()
28+ assert (len (listMNs ) > 0 )
29+ assert_equal (listMNs [0 ]["status" ], "ENABLED" )
30+
31+ def run_test (self ):
32+ mnOwner = self .nodes [0 ]
33+ mnRemote = self .nodes [1 ]
34+ miner = self .nodes [2 ]
35+
36+ print ("generating 301 blocks.." )
37+ miner .generate (301 )
38+
39+ print ("adding balance to the mn owner.." )
40+ mnOwnerAddr = mnOwner .getnewaddress ()
41+ # send to the owner the collateral tx cost
42+ miner .sendtoaddress (mnOwnerAddr , Decimal ('10001' ))
43+ sync_mempools (self .nodes )
44+ # confirm the tx
45+ miner .generate (1 )
46+ self .sync_all ()
47+ # verify reception
48+ assert_equal (mnOwner .getbalance (), Decimal ('10001' ))
49+
50+ # get the remote MN port
51+ remotePort = p2p_port (1 )
52+
53+ print ("all good, creating the masternode.." )
54+ ## Owner side
55+
56+ # Let's create the masternode
57+ masternodePrivKey = mnOwner .createmasternodekey ()
58+ masternodeAlias = "masternode1"
59+ mnAddress = mnOwner .getnewaddress (masternodeAlias )
60+ collateralTxId = mnOwner .sendtoaddress (mnAddress , Decimal ('10000' ))
61+
62+ sync_mempools (self .nodes )
63+ miner .generate (2 )
64+ self .sync_all ()
65+ # check if tx got confirmed
66+ assert (mnOwner .getrawtransaction (collateralTxId , 1 )["confirmations" ] > 0 )
67+
68+ # get the collateral output using the RPC command
69+ mnCollateralOutput = mnOwner .getmasternodeoutputs ()[0 ]
70+ assert_equal (mnCollateralOutput ["txhash" ], collateralTxId )
71+
72+ print ("collateral accepted.. updating masternode.conf and stopping the node" )
73+
74+ # verify collateral confirmed
75+
76+ # create the masternode.conf and add it
77+ confData = masternodeAlias + " 127.0.0.1:" + str (remotePort ) + " " + str (masternodePrivKey ) + " " + str (mnCollateralOutput ["txhash" ]) + " " + str (mnCollateralOutput ["outputidx" ])
78+ destinationDirPath = os .path .join (self .options .tmpdir , "node0" , "regtest" )
79+ destPath = os .path .join (destinationDirPath , "masternode.conf" )
80+ with open (destPath , "a+" ) as file_object :
81+ file_object .write ("\n " )
82+ file_object .write (confData )
83+
84+
85+ ## Remote side
86+ self .stop_node (1 )
87+
88+ # change the .conf
89+ destinationDirPath = os .path .join (self .options .tmpdir , "node1" )
90+ destPath = os .path .join (destinationDirPath , "pivx.conf" )
91+ with open (destPath , "a+" ) as file_object :
92+ file_object .write ("\n " )
93+ file_object .write ("listen=1\n " )
94+ file_object .write ("masternode=1\n " )
95+ file_object .write ("externalip=127.0.0.1\n " )
96+ file_object .write ("masternodeaddr=127.0.0.1:" + str (remotePort )+ "\n " )
97+ file_object .write ("masternodeprivkey=" + str (masternodePrivKey ))
98+
99+ print ("starting nodes again.." )
100+ self .restart_node (0 )
101+ self .start_node (1 )
102+ connect_nodes (self .nodes [0 ], 1 )
103+ connect_nodes (self .nodes [1 ], 0 )
104+ connect_nodes (self .nodes [1 ], 2 )
105+ connect_nodes (self .nodes [2 ], 1 )
106+
107+ print ("syncing tier two across recently started peers.." )
108+ # let the nodes sync the tier two
109+ time .sleep (20 )
110+ miner .generate (1 )
111+ sync_blocks (self .nodes )
112+
113+ # wait a little bit until the tier two is synced.
114+ time .sleep (20 )
115+ self .check_mnsync_finished ()
116+
117+ print ("tier two synced! starting masternode.." )
118+
119+ ## Now everything is set, start the masternode
120+ ret = mnOwner .startmasternode ("alias" , "false" , masternodeAlias )
121+ assert_equal (ret ["result" ], "success" )
122+
123+ print ("masternode started, waiting 125 seconds until it's enabled.." )
124+ for i in range (1 , 5 ):
125+ miner .generate (1 )
126+ sync_blocks (self .nodes )
127+ time .sleep (25 )
128+
129+ # check masternode enabled
130+ self .check_first_mn_in_mnlist_enabled (mnRemote )
131+ self .check_first_mn_in_mnlist_enabled (mnOwner )
132+ self .check_first_mn_in_mnlist_enabled (miner )
133+
134+ print ("masternode enabled and running properly!" )
135+
136+
137+
138+ if __name__ == '__main__' :
139+ MasternodeActivationTest ().main ()
0 commit comments