async def test_is_consecutive(self): block1 = Block() block1.index = 0 block1.hash = '3' block1.prev_hash = '' block2 = Block() block2.index = 1 block2.hash = '4' block2.prev_hash = '3' blockchain = await Blockchain.init_async([block1, block2]) self.assertTrue(await blockchain.is_consecutive)
async def test_get_difficulty(self): block1 = Block() block1.index = 0 block1.hash = '3000000000000000' block1.prev_hash = '' block2 = Block() block2.index = 1 block2.hash = '4000000000000000' blocks = [block1, block2] blockchain = await Blockchain.init_async(blocks) result = await blockchain.get_difficulty() self.assertEqual( result, 231584178474632390847141970017375815706539969331281128078907097565294011351038 )
async def integrate_block_with_existing_chain(self, block: Block, extra_blocks=None): """Even in case of retrace, this is the only place where we insert a new block into the block collection and update BU""" self.app_log.warning('integrate_block_with_existing_chain') try: # TODO: reorg the checks, to have the faster ones first. # Like, here we begin with checking every tx one by one, when <e did not even check index and provided hash matched previous one. bc = await Blockchain.init_async() result = await bc.test_block(block) if not result: return False # self.mongo.db.blocks.update({'index': block.index}, block.to_dict(), upsert=True) # self.mongo.db.blocks.remove({'index': {"$gt": block.index}}, multi=True) # todo: is this useful? can we have more blocks above? No because if we had, we would have raised just above await self.mongo.async_db.blocks.delete_many( {'index': { "$gte": block.index }}) db_block = block.to_dict() db_block['updated_at'] = time() await self.mongo.async_db.blocks.replace_one( {'index': block.index}, db_block, upsert=True) await self.mongo.async_db.miner_transactions.delete_many({ 'id': { '$in': [x.transaction_signature for x in block.transactions] } }) await self.config.LatestBlock.update_latest_block() self.app_log.info("New block inserted for height: {}".format( block.index)) latest_consensus = await self.mongo.async_db.consensus.find_one({ 'index': block.index + 1, 'block.version': CHAIN.get_version_for_height(block.index + 1), 'ignore': { '$ne': True } }) if not latest_consensus: await self.config.LatestBlock.block_checker( ) # This will trigger mining pool to generate a new block to mine if self.config.mp: await self.config.mp.refresh() await StratumServer.block_checker() if not self.syncing: await self.config.nodeShared().send_block( self.config.LatestBlock.block) return True except Exception as e: from traceback import format_exc self.app_log.warning("{}".format(format_exc()))
async def test_test_inbound_chain(self): block1 = Block() block1.index = 0 block1.hash = '3000000000000000' block1.prev_hash = '' block2 = Block() block2.index = 1 block2.hash = '4000000000000000' blocks = [block1, block2] blockchain = await Blockchain.init_async(blocks) block1 = Block() block1.index = 0 block1.hash = '3' block1.prev_hash = '' block2 = Block() block2.index = 1 block2.hash = '5' block3 = Block() block3.index = 2 block3.hash = '5' inbound_blocks = [block1, block2, block3] inbound_blockchain = await Blockchain.init_async(inbound_blocks) self.assertTrue(await blockchain.test_inbound_blockchain(inbound_blockchain))
async def test_count(self): blocks = [Block(), Block()] blockchain = await Blockchain.init_async(blocks) final_block = await blockchain.final_block self.assertEqual(final_block, blocks[1])
async def test_get_blocks(self): blocks = [Block(), Block()] blockchain = await Blockchain.init_async(blocks) got_blocks = blockchain.get_blocks(0, 2) self.assertEqual([x async for x in got_blocks], blocks)
async def test_get_block(self): blocks = [Block()] blockchain = await Blockchain.init_async(blocks) got_block = await blockchain.get_block(0, 1) self.assertEqual(got_block, blocks[0])
async def test_blocks(self): blocks = [Block()] blockchain = await Blockchain.init_async(blocks) self.assertEqual([x async for x in blockchain.blocks], blocks)