def test_complex_methods(self): from pypoptools.pypopminer import MockMiner, PublicationData m = MockMiner() last_known_btc_hash = m.btcTip.getHash() m.mineBtcBlocks(1) endorsed_block = m.vbkTip vbk_pop_tx = m.createVbkPopTxEndorsingVbkBlock(endorsed_block, last_known_btc_hash) self.assertEqual(vbk_pop_tx.publishedBlock.getHash(), endorsed_block.getHash()) self.assertEqual(len(vbk_pop_tx.blockOfProofContext), 1) last_known_vbk_hash = m.vbkTip.getHash() m.mineVbkBlocks(1, [vbk_pop_tx], True) endorsed_block = m.vbkTip vtb = m.endorseVbkBlock(endorsed_block, last_known_btc_hash) self.assertEqual(vtb.transaction.publishedBlock.getHash(), endorsed_block.getHash()) self.assertEqual(len(vtb.transaction.blockOfProofContext), 2) pub_data = PublicationData() pub_data.identifier = 1337 pub_data.header = "0011223344" pub_data.payoutInfo = "0014aaddff" pop_data = m.endorseAltBlock(pub_data, last_known_vbk_hash) self.assertEqual(len(pop_data.atvs), 1) self.assertEqual(len(pop_data.vtbs), 2) self.assertEqual(len(pop_data.context), 2)
def test_atomic_methods(self): from pypoptools.pypopminer import MockMiner, PublicationData m = MockMiner() last_known_btc_hash = m.btcTip.getHash() last_known_vbk_hash = m.vbkTip.getHash() m.mineBtcBlocks(1) m.mineVbkBlocks(1) endorsed_block = m.vbkTip btc_tx = m.createBtcTxEndorsingVbkBlock(endorsed_block) btc_block_of_proof = m.mineBtcBlocks(1, [btc_tx]) vbk_pop_tx = m.createVbkPopTxEndorsingVbkBlock(btc_block_of_proof, btc_tx, endorsed_block, last_known_btc_hash) self.assertEqual(vbk_pop_tx.publishedBlock.toHex(), endorsed_block.toHex()) self.assertEqual(vbk_pop_tx.bitcoinTransaction.getHash(), btc_tx.getHash()) self.assertEqual(vbk_pop_tx.blockOfProof.getHash(), btc_block_of_proof.getHash()) self.assertEqual(len(vbk_pop_tx.blockOfProofContext), 1) vbk_containing_block = m.mineVbkBlocks(1, [vbk_pop_tx], True) vtb = m.createVTB(vbk_containing_block, vbk_pop_tx) self.assertEqual(vtb.transaction.getHash(), vbk_pop_tx.getHash()) self.assertEqual(vtb.containingBlock.getHash(), vbk_containing_block.getHash()) pub_data = PublicationData() pub_data.identifier = 1337 pub_data.header = "0011223344" pub_data.payoutInfo = "0014aaddff" vbk_tx = m.createVbkTxEndorsingAltBlock(pub_data) vbk_block_of_proof = m.mineVbkBlocks(1, [vbk_tx], False) atv = m.createATV(vbk_block_of_proof, vbk_tx) self.assertEqual(atv.transaction.getHash(), vbk_tx.getHash()) self.assertEqual(atv.blockOfProof.getHash(), vbk_block_of_proof.getHash()) pop_data = m.createPopDataEndorsingAltBlock(vbk_block_of_proof, vbk_tx, last_known_vbk_hash) self.assertEqual(len(pop_data.atvs), 1) self.assertEqual(len(pop_data.vtbs), 1) self.assertEqual(len(pop_data.context), 2)
def run_test(self): from pypoptools.pypopminer import MockMiner, PublicationData apm = MockMiner() self._assert_nodes_peer_info() vbk_blocks_amount = 100 self.log.info("generate vbk blocks on node0, amount {}".format( vbk_blocks_amount)) vbk_blocks = [] for i in range(vbk_blocks_amount): vbk_blocks.append(apm.mineVbkBlocks(1)) assert len(vbk_blocks) == vbk_blocks_amount vtbs_amount = 20 self.log.info("generate vtbs on node0, amount {}".format(vtbs_amount)) for i in range(vtbs_amount): apm.endorseVbkBlock(apm.vbkTip, apm.btcTip.getHash(), 1) self.nodes[0].generate(nblocks=10) last_block = self.nodes[0].getblockcount() assert last_block >= 5 self.log.info("endorse {} alt block".format(last_block - 5)) endorse_block(self.nodes[0], apm, last_block - 5) self._assert_nodes_peer_info() containing_block_hash = self.nodes[0].generate(nblocks=1)[0] containing_block = self.nodes[0].getblock(containing_block_hash) assert len(containing_block.containingVTBs) == vtbs_amount assert len(containing_block.containingVBKs ) == vbk_blocks_amount + vtbs_amount + 1 assert last_block >= 6 self.log.info("endorse {} alt block".format(last_block - 6)) endorse_block(self.nodes[0], apm, last_block - 6) self._assert_nodes_peer_info() self.nodes[0].generate(nblocks=1) time.sleep(5) self.log.info("sync all nodes") sync_all(self.nodes) self._assert_nodes_peer_info()
def test_mock_miner_can_produce_publications(self): from pypoptools.pypopminer import MockMiner, PublicationData m = MockMiner() self.assertEqual(m.vbkTip.previousBlock, "000000000000000000000000") self.assertEqual(m.vbkTip.height, 0) endorsed = m.mineVbkBlocks(100) self.assertEqual(endorsed.height, 100) m.endorseVbkBlock(endorsed, m.btcTip.getHash()) p = PublicationData() p.identifier = 1337 p.header = "0011223344" p.payoutInfo = "0014aaddff" payloads = m.endorseAltBlock(p, endorsed.getHash()) print(repr(payloads)) self.assertEqual(len(payloads.vtbs), 1) self.assertEqual(m.vbkTip.height, 102)
def run_test(self): from pypoptools.pypopminer import MockMiner apm = MockMiner() start = time.time() # disconnect all nodes disconnect_all(self.nodes) self.log.info("node[0] mine {} blocks".format(self.to_mine)) for i in range(self.to_mine): self.log.info("node[0] mine block #{}".format(i + 1)) mine_alt_block(self.nodes[0], nblocks=1) assert self.nodes[0].getblockcount() >= self.to_mine elapsed1 = time.time() - start print("{} blocks mined in {:.3f} sec".format(self.to_mine, elapsed1)) for e in range(self.payloads_to_mine): self.log.info("generate vbk blocks on node0, amount {}".format( self.vbks_in_payload)) vbk_blocks = [] for i in range(self.vbks_in_payload): vbk_blocks.append(apm.mineVbkBlocks(1)) assert len(vbk_blocks) == self.vbks_in_payload self.log.info("generate vtbs on node0, amount {}".format( self.vtbs_in_payload)) for i in range(self.vtbs_in_payload): apm.endorseVbkBlock(apm.vbkTip, apm.btcTip.getHash()) last_block = self.nodes[0].getblockcount() assert last_block >= self.atvs_in_payload + 5 self.log.info("generate atvs on node0, amount {}".format( self.atvs_in_payload)) for i in range(self.atvs_in_payload): to_endorse = last_block - self.atvs_in_payload - 5 + i endorse_block(self.nodes[0], apm, to_endorse) self.nodes[0].generate(nblocks=1) containing_block = self.nodes[0].getbestblock() assert len(containing_block.containingATVs) == self.atvs_in_payload assert len(containing_block.containingVTBs) == self.vtbs_in_payload assert len( containing_block.containingVBKs ) == self.vbks_in_payload + self.vtbs_in_payload + self.atvs_in_payload node_0_tip = self.nodes[0].getbestblock() node_1_tip = self.nodes[1].getbestblock() assert node_0_tip != node_1_tip elapsed2 = time.time() - start - elapsed1 print("{} payloads generated in {:.3f} sec".format( self.payloads_to_mine, elapsed2)) self.log.info("restart all nodes") for node in self.nodes: node.restart() assert node_0_tip == self.nodes[0].getbestblock() assert node_1_tip == self.nodes[1].getbestblock() self.log.info("connect all nodes") connect_all(self.nodes) sync_all(self.nodes, timeout=300) assert self.nodes[0].getbestblock() == self.nodes[1].getbestblock() elapsed3 = time.time() - start - elapsed2 - elapsed1 print("{} nodes synced in {:.3f} sec".format(self.num_nodes, elapsed3))