def __init__(self, ip, port, name, bootstrap, difficulty): self.name = name self.port = port self.peers = bootstrap self.difficulty = difficulty self.chain = BlockUtils.Chain(BlockUtils.genisisBlock(), []) self.outbox = Queue() self.inbox = Queue() self.inboxServer = InboxServer(self.inbox, ip, port) self.loop = asyncio.get_event_loop()
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 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)
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)