def sc_sync_nodes(self, sc_nodes): sync_sc_blocks(sc_nodes) sync_sc_mempools(sc_nodes)
def sc_sync_all(self): sync_sc_blocks(self.sc_nodes) sync_sc_mempools(self.sc_nodes)
def run_test(self): #Generate 1-block coinbase for nodes 0 and 1 for simple test print("Generate 1-block coinbase for MC Nodes 0 and 1...") self.nodes[0].generate(1) self.sync_all() self.nodes[1].generate(1) self.sync_all() self.nodes[2].generate(100) self.sync_all() print("OK\n") #Saving MC Nodes information and setting tx amount print("Nodes initialization...") mcnode0name = "node0" mcnode1name = "node1" mcnode2name = "node2" mcnode1address = self.nodes[1].getnewaddress() mcnode0balance = self.nodes[0].getbalance() mcnode1balance = self.nodes[1].getbalance() print("-->MC Node 0 balance: {0}".format(mcnode0balance)) print("-->MC Node 1 balance: {0}".format(mcnode1balance)) mc_amount = Decimal("5.00000000") #Saving SC Nodes information and setting tx amount and fee scnode0name = "node0" scnode1name = "node1" scnode2name = "node2" scnode1address = self.sc_nodes[1].wallet_balances()["publicKeys"][0] scnode0balance = int( self.sc_nodes[0].wallet_balances()["totalBalance"]) scnode1balance = int( self.sc_nodes[1].wallet_balances()["totalBalance"]) print("-->SC Node 0 balance: {0}".format(scnode0balance)) print("-->SC Node 1 balance: {0}".format(scnode1balance)) sc_amount = random.randint(1, scnode0balance - 100) sc_fee = random.randint(1, 99) print("OK\n") #MC Node0 sends tx to MC Node1. The same happens in SC side print("Sending transactions...") print( "-->MC Node 0 sends to MC Node 1 address {0}, {1} coins...".format( str(mcnode1address), mc_amount)) mctxid = self.create_mc_tx(self.nodes[0], mcnode1address, mc_amount) print( "-->SC Node 0 sends to SC Node 1 address {0}, {1} coins with fee {2} coins..." .format(str(scnode1address), sc_amount, sc_fee)) sctxid = self.create_sc_tx(self.sc_nodes[0], scnode1address, sc_amount, sc_fee) print("OK\n") #Synchronizing MC nodes mempools and checks that the new transaction is in all of them. Do the same for SC nodes print( "Synchronizing nodes' mempools and check tx is in all of them...") sync_mempools(self.nodes) self.check_tx_in_mc_mempool(self.nodes[0], mcnode0name, mctxid) self.check_tx_in_mc_mempool(self.nodes[1], mcnode1name, mctxid) self.check_tx_in_mc_mempool(self.nodes[2], mcnode2name, mctxid) sync_sc_mempools(self.sc_nodes) self.check_tx_in_sc_mempool(self.sc_nodes[0], scnode0name, sctxid) self.check_tx_in_sc_mempool(self.sc_nodes[1], scnode1name, sctxid) self.check_tx_in_sc_mempool(self.sc_nodes[2], scnode2name, sctxid) print("OK\n") #MC Node2 generate a new block, then sync all the nodes. Do the same for SC Node2. print("Generating new blocks...") print("-->MC Node 2 generates a block...") blocks = self.nodes[2].generate(1) assert_equal(len(blocks), 1, "MC Node2 couldn't generate a block") self.sync_all() print("-->SC Node 2 generates a block...") assert_equal("ok", str(self.sc_nodes[2].debug_startMining()["response"]), "SC Node 2 couldn't start mining") '''We wait for the new blocks to appear in Node 2 block count. This is not necessary in zend because the generate call is synchronous with respect to the effective block generation, while in Hybrid App, and also with our modification, it's not.''' wait_for_next_sc_blocks(self.sc_nodes[2], 4) self.sc_sync_all() print("OK\n") #Checking that the tx is in the newly created block for both MC and SC print("Checking block inclusion...") self.check_tx_in_mc_block(self.nodes[0], mcnode0name, mctxid) self.check_tx_in_mc_block(self.nodes[1], mcnode1name, mctxid) self.check_tx_in_mc_block(self.nodes[2], mcnode2name, mctxid) self.check_tx_in_sc_block(self.sc_nodes[0], scnode0name, sctxid) self.check_tx_in_sc_block(self.sc_nodes[1], scnode1name, sctxid) self.check_tx_in_sc_block(self.sc_nodes[2], scnode2name, sctxid) print("OK\n") print("Checking mempools empty...") assert_equal(0, self.nodes[0].getmempoolinfo()["size"]) assert_equal(0, self.nodes[1].getmempoolinfo()["size"]) assert_equal(0, self.nodes[2].getmempoolinfo()["size"]) assert_equal(0, self.sc_nodes[0].nodeView_pool()["size"]) assert_equal(0, self.sc_nodes[1].nodeView_pool()["size"]) assert_equal(0, self.sc_nodes[2].nodeView_pool()["size"]) print("OK\n") #Checking that MC Node0 balance has decreased by amount-fee and that MC Node1 balance has increased of amount. Do the same for SC nodes 0 and 1 print("Checking balance changed for MC & SC Nodes 0 and 1...") mcnode0newbalance = self.nodes[0].getbalance() mcnode1newbalance = self.nodes[1].getbalance() mc_fee = self.nodes[0].gettransaction(mctxid)["fee"] assert_equal(mcnode0balance - (mc_amount - mc_fee), mcnode0newbalance, "Coins sent/total amount mismatch for MC Node0") assert_equal(mcnode1balance + mc_amount, mcnode1newbalance, "Coins received/total amount mismatch for MC Node1") print("-->MC Node 0 new balance: {0}".format(mcnode0newbalance)) print("-->MC Node 1 new balance: {0}".format(mcnode1newbalance)) node0newbalance = int( self.sc_nodes[0].wallet_balances()["totalBalance"]) node1newbalance = int( self.sc_nodes[1].wallet_balances()["totalBalance"]) assert_equal(scnode0balance - (sc_amount + sc_fee), node0newbalance, "Coins sent/total sc_amount mismatch for Node0") assert_equal(scnode1balance + sc_amount, node1newbalance, "Coins received/total sc_amount mismatch for Node1") print("-->SC Node 0 new balance: {0}".format(node0newbalance)) print("-->SC Node 1 new balance: {0}".format(node1newbalance)) print("OK\n")
def run_test(self): #Saving basic information and setting tx fee and amount print("Nodes initialization...") scnode0name = "node0" scnode1name = "node1" scnode2name = "node2" scnodeadresses = self.sc_nodes[1].wallet_allPublicKeys() scnode1address = self.sc_nodes[1].wallet_allPublicKeys( )["result"]["propositions"][0]["publicKey"] boxes_request_on_regular_boxes = json.dumps( {"boxType": "com.horizen.box.RegularBox"}) sc_node_0_regular_box_balance = int(self.sc_nodes[0].wallet_balance( boxes_request_on_regular_boxes)["result"]["balance"]) sc_node_1_regular_box_balance = int(self.sc_nodes[1].wallet_balance( boxes_request_on_regular_boxes)["result"]["balance"]) sc_node_2_regular_box_balance = int(self.sc_nodes[2].wallet_balance( boxes_request_on_regular_boxes)["result"]["balance"]) print("-->SC Node 0 regular boxes balance: {0}".format( sc_node_0_regular_box_balance)) print("-->SC Node 1 regular boxes balance: {0}".format( sc_node_1_regular_box_balance)) print("-->SC Node 2 regular boxes balance: {0}".format( sc_node_2_regular_box_balance)) balance_request_on_forger_boxes = json.dumps( {"boxType": "com.horizen.box.ForgerBox"}) sc_node_0_forger_box_balance = int(self.sc_nodes[0].wallet_balance( balance_request_on_forger_boxes)["result"]["balance"]) sc_node_1_forger_box_balance = int(self.sc_nodes[1].wallet_balance( balance_request_on_forger_boxes)["result"]["balance"]) sc_node_2_forger_box_balance = int(self.sc_nodes[2].wallet_balance( balance_request_on_forger_boxes)["result"]["balance"]) print("-->SC Node 0 forger boxes balance: {0}".format( sc_node_0_forger_box_balance)) print("-->SC Node 1 forger boxes balance: {0}".format( sc_node_1_forger_box_balance)) print("-->SC Node 2 forger boxes balance: {0}".format( sc_node_2_forger_box_balance)) sc_amount = random.randint(sc_node_0_forger_box_balance - 1000, sc_node_0_forger_box_balance - 100) sc_fee = 0 print("OK\n") #Node0 do a tx to Node1 print("Sending transactions...") boxes_request_on_forger_boxes = json.dumps( {"boxTypeClass": "com.horizen.box.ForgerBox"}) sc_node_0_first_forger_box = self.sc_nodes[0].wallet_allBoxes( boxes_request_on_forger_boxes)["result"]["boxes"][0] sc_node_0_first_forger_box_id = sc_node_0_first_forger_box["id"] sc_node_0_first_forger_box_pub_key = sc_node_0_first_forger_box[ "proposition"]["publicKey"] sc_node_0_first_forger_box_vrf_pub_key = sc_node_0_first_forger_box[ "vrfPubKey"]["publicKey"] print("Found forger box with id {forgerBxId} for node 0".format( forgerBxId=sc_node_0_first_forger_box_id)) print( "-->SC Node 0 sends to SC Node 1 address {0}, {1} coins with fee {2} coins..." .format(str(scnode1address), sc_amount, sc_fee)) sctxid = self.send_forger_stake_coins( self.sc_nodes[0], sc_node_0_first_forger_box_id, scnode1address, sc_node_0_first_forger_box_pub_key, sc_node_0_first_forger_box_vrf_pub_key, sc_amount, sc_node_0_forger_box_balance) print("OK\n") #Check tx appears in all nodes' mempools print( "Synchronizing nodes' mempools and check tx is in all of them...") sync_sc_mempools(self.sc_nodes) self.check_tx_in_mempool(self.sc_nodes[0], scnode0name, sctxid) self.check_tx_in_mempool(self.sc_nodes[1], scnode1name, sctxid) self.check_tx_in_mempool(self.sc_nodes[2], scnode2name, sctxid) print("OK\n") #Node 2 generates a block, then checking that block appeared in chain and synchronize everything print("Generating new blocks...") print("-->SC Node 2 generates a block...") blocks = generate_next_blocks(self.sc_nodes[2], scnode2name, 1) #TODO check implementation #wait_for_next_sc_blocks(self.sc_nodes[2], 4, wait_for = 60) print("Synchronizing everything...") self.sc_sync_all() print("OK\n") #Check tx is in block for each node print("Checking block inclusion...") #TODO change implementation to support multiple blocks for block in blocks: self.check_tx_in_block(self.sc_nodes[0], scnode0name, block, sctxid) self.check_tx_in_block(self.sc_nodes[1], scnode1name, block, sctxid) self.check_tx_in_block(self.sc_nodes[2], scnode2name, block, sctxid) print("OK\n") #Check that tx isn't in nodes' mempools anymore print("Checking mempools empty...") assert_equal( 0, len(self.sc_nodes[0].transaction_allTransactions()["result"] ["transactions"])) assert_equal( 0, len(self.sc_nodes[1].transaction_allTransactions()["result"] ["transactions"])) assert_equal( 0, len(self.sc_nodes[2].transaction_allTransactions()["result"] ["transactions"])) print("OK\n") #Checking that node0 balance has decreased by amount-fee, node1 balance has increased by amount and node2 balance has increased of blockreward+txfee print("Checking balance changed...") scblockreward = 1 node_0_new_forger_balance = int(self.sc_nodes[0].wallet_balance( balance_request_on_forger_boxes)["result"]["balance"]) node_1_new_forger_balance = int(self.sc_nodes[1].wallet_balance( balance_request_on_forger_boxes)["result"]["balance"]) node_2_new_forger_balance = int(self.sc_nodes[2].wallet_balance( balance_request_on_forger_boxes)["result"]["balance"]) assert_equal(sc_node_0_forger_box_balance - (sc_amount + sc_fee), node_0_new_forger_balance, "Coins sent/total sc_amount mismatch for Node0") #@TODO verify that balance forger boxes for node 1 and node 2 are not changed after node 0 forger box spent spent print("-->Node 0 new forger boxes balance: {0}".format( node_0_new_forger_balance)) print("-->Node 1 new forger boxes balance: {0}".format( node_1_new_forger_balance)) print("-->Node 2 new forger boxes balance: {0}".format( node_2_new_forger_balance)) print("OK\n") node_0_new_regular_balance = int(self.sc_nodes[0].wallet_balance( boxes_request_on_regular_boxes)["result"]["balance"]) node_1_new_regular_balance = int(self.sc_nodes[1].wallet_balance( boxes_request_on_regular_boxes)["result"]["balance"]) node_2_new_regular_balance = int(self.sc_nodes[2].wallet_balance( boxes_request_on_regular_boxes)["result"]["balance"]) assert_equal(sc_node_0_regular_box_balance, node_0_new_regular_balance, "Coins sent/total sc_amount mismatch for Node0") assert_equal(sc_node_1_regular_box_balance + sc_amount, node_1_new_regular_balance, "Coins received/total sc_amount mismatch for Node1") assert_equal(sc_node_2_regular_box_balance, node_2_new_regular_balance, "Coins received/total sc_amount mismatch for Node2") print("-->Node 0 new regular boxes balance: {0}".format( node_0_new_regular_balance)) print("-->Node 1 new regular boxes balance: {0}".format( node_1_new_regular_balance)) print("-->Node 2 new regular boxes balance: {0}".format( node_2_new_regular_balance))
def run_test(self): #Saving basic information and setting tx fee and amount print("Nodes initialization...") scnode0name = "node0" scnode1name = "node1" scnode2name = "node2" scnodeadresses = self.sc_nodes[1].wallet_allPublicKeys() scnode1address = self.sc_nodes[1].wallet_allPublicKeys( )["result"]["propositions"][0]["publicKey"] scnode0balance = int( self.sc_nodes[0].wallet_balance()["result"]["balance"]) scnode1balance = int( self.sc_nodes[1].wallet_balance()["result"]["balance"]) scnode2balance = int( self.sc_nodes[2].wallet_balance()["result"]["balance"]) print("-->SC Node 0 balance: {0}".format(scnode0balance)) print("-->SC Node 1 balance: {0}".format(scnode1balance)) print("-->SC Node 2 balance: {0}".format(scnode2balance)) sc_amount = random.randint(scnode0balance - 1000, scnode0balance - 100) sc_fee = 0 print("OK\n") #Node0 do a tx to Node1 print("Sending transactions...") print( "-->SC Node 0 sends to SC Node 1 address {0}, {1} coins with fee {2} coins..." .format(str(scnode1address), sc_amount, sc_fee)) sctxid = self.send_coins(self.sc_nodes[0], scnode1address, sc_amount, sc_fee) print("OK\n") #Check tx appears in all nodes' mempools print( "Synchronizing nodes' mempools and check tx is in all of them...") sync_sc_mempools(self.sc_nodes) self.check_tx_in_mempool(self.sc_nodes[0], scnode0name, sctxid) self.check_tx_in_mempool(self.sc_nodes[1], scnode1name, sctxid) self.check_tx_in_mempool(self.sc_nodes[2], scnode2name, sctxid) print("OK\n") #Node 2 generates a block, then checking that block appeared in chain and synchronize everything print("Generating new blocks...") print("-->SC Node 2 generates a block...") blocks = self.generate_blocks(self.sc_nodes[2], scnode2name, 1) #TODO check implementation #wait_for_next_sc_blocks(self.sc_nodes[2], 4, wait_for = 60) print("Synchronizing everything...") self.sc_sync_all() print("OK\n") #Check tx is in block for each node print("Checking block inclusion...") #TODO change implementation to support multiple blocks for block in blocks: self.check_tx_in_block(self.sc_nodes[0], scnode0name, block, sctxid) self.check_tx_in_block(self.sc_nodes[1], scnode1name, block, sctxid) self.check_tx_in_block(self.sc_nodes[2], scnode2name, block, sctxid) print("OK\n") #Check that tx isn't in nodes' mempools anymore print("Checking mempools empty...") assert_equal( 0, len(self.sc_nodes[0].transaction_allTransactions()["result"] ["transactions"])) assert_equal( 0, len(self.sc_nodes[1].transaction_allTransactions()["result"] ["transactions"])) assert_equal( 0, len(self.sc_nodes[2].transaction_allTransactions()["result"] ["transactions"])) print("OK\n") #Checking that node0 balance has decreased by amount-fee, node1 balance has increased by amount and node2 balance has increased of blockreward+txfee print("Checking balance changed...") scblockreward = 1 node0newbalance = int( self.sc_nodes[0].wallet_balance()["result"]["balance"]) node1newbalance = int( self.sc_nodes[1].wallet_balance()["result"]["balance"]) node2newbalance = int( self.sc_nodes[2].wallet_balance()["result"]["balance"]) assert_equal(scnode0balance - (sc_amount + sc_fee), node0newbalance, "Coins sent/total sc_amount mismatch for Node0") assert_equal(scnode1balance + sc_amount, node1newbalance, "Coins received/total sc_amount mismatch for Node1") assert_equal(scnode2balance, node2newbalance, "Coins received/total sc_amount mismatch for Node2") print("-->Node 0 new balance: {0}".format(node0newbalance)) print("-->Node 1 new balance: {0}".format(node1newbalance)) print("-->Node 2 new balance: {0}".format(node2newbalance)) print("OK\n")