def testIsValidBlockJumpDifficulty(lastBlock, block): jumpDifficulty = 10 block.dificulty = jumpDifficulty block.hash = f'{"0" * jumpDifficulty}111abc' with pytest.raises(Exception, match='difficulty must only adjust by 1'): Block.isValidBlock(lastBlock, block)
def testMineBlockDicifultyLimitRaTE_1(): lastblock = Block(time.time_ns(), 'testLastHash', 'testHash', 'testData', 1, 0) time.sleep(MINE_RATE / SEC) mineBlock = Block.mineBlock(lastblock, "bar") assert mineBlock.dificulty == 1
def testMineBlock(): lastBlock = Block.genesis() data = 'test-data' block = Block.mineBlock(lastBlock, data) assert isinstance(block, Block) assert block.data == data assert block.lasthash == lastBlock.hash assert HexToBinary(block.hash)[0:block.dificulty] == '0' * block.dificulty
def testGenesis(): genesis = Block.genesis() assert isinstance(genesis, Block) # uzyskujemy atrybut for key, value in GENESIS_DATA.items(): getattr(genesis, key) == value
def msg(self, pubnub, msgObject): print(f'\n-- Channel: {msgObject.channel} | Message:{msgObject.msg}') if msgObject.channel == Channels['BLOCK']: block = Block.from_json(msgObject.msg) potentialChain = self.blockchain.chain[:] potentialChain.append(block) try: self.blockchain.replaceChain(potentialChain) self.transactionPool.clearTransaction(self.blockchain) print('\n -- Succesful replacing the chain') except Exception as e: print(f'\n -- Did not replace chain:{e}') elif msgObject.channel == Channels['TRANSACTION']: transaction = Trasaction.from_json(msgObject.msg) self.transactionPool.setTransaction(transaction) print(f'\n-- Set the new transaction in the transaction pool')
def testIsValidBadProofOfWork(lastBlock, block): block.hash = 'fff' with pytest.raises(Exception, match='proof of work requirement was not met'): Block.isValidBlock(lastBlock, block)
def testIsValidBlockBadLastHash(lastBlock, block): block.lasthash = 'evil_LastHash' with pytest.raises(Exception, match='lastHash must be correct'): Block.isValidBlock(lastBlock, block)
def testIsValidBlock(lastBlock, block): Block.isValidBlock(lastBlock, block)
def block(lastBlock): return Block.mineBlock(lastBlock, 'test-data')
def lastBlock(): return Block.genesis()
def testSlowlyMineBlock(): lastBlock = Block.mineBlock(Block.genesis(), 'foo') time.sleep(MINE_RATE / SEC) mineBlock = Block.mineBlock(lastBlock, 'bar') assert mineBlock.dificulty == lastBlock.dificulty - 1
def testQuicklyMineBlock(): lastBlock = Block.mineBlock(Block.genesis(), 'foo') mineBlock = Block.mineBlock(lastBlock, 'bar') assert mineBlock.dificulty == lastBlock.dificulty + 1
def testIsValidBlockBadHash(lastBlock, block): block.hash = '0000000000000000bbbabc' with pytest.raises(Exception, match='block hash must be correct'): Block.isValidBlock(lastBlock, block)