def testBlock(size): block = BU.generateBlock("me", size) nBlock = BU.jsonToBlock(BU.blockToJson(block)) self.assertEqual(nBlock.miner, block.miner) self.assertEqual(nBlock.prev, block.prev) self.assertEqual(nBlock.height, block.height) self.assertEqual(nBlock.uid, block.uid)
async def writeBlock(): for peer in peers: reader, writer = await asyncio.open_connection(peer.ip, peer.port, loop=loop) write.write(BlockUtils.blockToJson(block).encode()) #response = await reader.read(100) print("sent block") writer.close()
def testBlockToFromData(self): def testBlock(size): block = BU.generateBlock("me", size) nBlock = BU.jsonToBlock(BU.blockToJson(block)) self.assertEqual(nBlock.miner, block.miner) self.assertEqual(nBlock.prev, block.prev) self.assertEqual(nBlock.height, block.height) self.assertEqual(nBlock.uid, block.uid) block = BU.genisisBlock() nBlock = BU.jsonToBlock(BU.blockToJson(block)) self.assertEqual(nBlock.miner, block.miner) self.assertEqual(nBlock.prev, block.prev) self.assertEqual(nBlock.height, block.height) self.assertEqual(nBlock.uid, block.uid) testBlock(5) testBlock(10) testBlock(15) testBlock(16) testBlock(50) testBlock(100)
async def sendBlock(ip, port, loop, height): block = BlockUtils.generateBlock("me", height) reader, writer = await asyncio.open_connection(ip, port, loop = loop) writer.write(BlockUtils.blockToJson(block).encode()) print("sent block") writer.close()