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 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 __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()
Beispiel #4
0
def main():
    q = Queue()
    bcVM = BlockchainViewmodel(BlockUtils.generateChain("Jacob", 5))
    vm = ViewDrawer(500,500, q)
    t = Thread(target = vm.run)
    t.start()
    q.put(bcVM)
 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 mineLoop(self):
     try:
         while (True):
             # simulate mining by rolling dice till 0
             if (random.randint(0, self.difficulty) == 0):
                 self.chain = BlockUtils.mineNewBlock(self.chain, self.name)
                 self.broadcastNewBlock()
             self.checkForNewBlocks()
     except KeyboardInterrupt:
         self.inboxServer.shutDown()
    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 testAddForkToChain(self):
     chain = BU.generateChain("me", 5)
     b3 = chain.top.prev.prev
     fork = BU.growBlock(b3, "me", 3)
     fChain = BU.addForkToChain(chain, fork)
     self.assertEqual(fChain.top.height, 6)
     self.assertEqual(BU.isValidChain(fChain), True)
     self.assertEqual(len(fChain.orphans), 1)
     fChain = BU.addForkToChain(fChain,
                                BU.growBlock(fChain.orphans[0], "me", 2))
     self.assertEqual(BU.isValidChain(fChain), True)
     self.assertEqual(len(fChain.orphans), 1)
 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 testBlockToDict(self):
     block = BU.generateBlock("me", 3)
     d = BU.blockToDict(block)
     self.assertEqual(d["height"], 3)
 def testMineNewBlock(self):
     chain = BU.generateChain("me", 5)
     newChain = BU.mineNewBlock(chain, "me")
     self.assertEqual(BU.isValidChain(newChain), True)
     self.assertEqual(len(newChain.orphans), 0)
 def checkForNewBlocks(self):
     blocks = list()
     while (not self.inbox.empty()):
         blocks.append(self.inbox.get())
     self.chain = BlockUtils.addNewBlocks(self.chain, blocks)
 def testGenerateChain(self):
     self.assertEqual(BU.isValidChain(BU.generateChain("me", 34)), True)
     self.assertEqual(BU.generateChain("me", -5), None)
 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 testBlockTuple(self):
     time = hash(datetime.datetime.now())
     block = BU.Block("name", None, 1, time)
     self.assertEqual(block.miner, "name")
 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()
 async def listenForBlocks(self, reader, writer):
     data = await reader.read(InboxServer.MAXLEN)
     block = BlockUtils.jsonToBlock(data.decode())
     print(block)
     self.inbox.put(block)
     writer.close()