def testIsAncestor(self): b10 = BU.generateBlock("b10", 10) foreign = BU.generateBlock("foreign", 5) cur = foreign while (cur.prev != None): self.assertEqual(BU.isAncestorOf(cur, b10), False) cur = cur.prev cur = b10 while (cur.prev != None): self.assertEqual(BU.isAncestorOf(cur, b10), True) cur = cur.prev self.assertEqual(BU.isAncestorOf(BU.genisisBlock(), b10), True)
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)
def testGrowBlock(self): base = BU.generateBlock("me", 15) self.assertEqual(base.height, 15) top = BU.growBlock(base, "he", 23) self.assertEqual(top.height, 15 + 23) self.assertEqual(BU.isValidBlock(top), True) self.assertEqual(BU.growBlock(base, "he", 0).uid, base.uid)
def testGenerateBlock(self): self.assertEqual(BU.generateBlock("me", 6).height, 6) self.assertEqual(BU.generateBlock("me", 0), None) self.assertEqual(BU.generateBlock("me", 1), BU.genisisBlock()) self.assertEqual(BU.isValidBlock(BU.generateBlock("me", 50)), True)
def testBlockToDict(self): block = BU.generateBlock("me", 3) d = BU.blockToDict(block) self.assertEqual(d["height"], 3)
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()