Exemple #1
0
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)
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
def testGenesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)
    # uzyskujemy atrybut
    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
Exemple #5
0
    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')
Exemple #6
0
def testIsValidBadProofOfWork(lastBlock, block):
    block.hash = 'fff'

    with pytest.raises(Exception,
                       match='proof of work requirement was not met'):
        Block.isValidBlock(lastBlock, block)
Exemple #7
0
def testIsValidBlockBadLastHash(lastBlock, block):
    block.lasthash = 'evil_LastHash'

    with pytest.raises(Exception, match='lastHash must be correct'):
        Block.isValidBlock(lastBlock, block)
Exemple #8
0
def testIsValidBlock(lastBlock, block):
    Block.isValidBlock(lastBlock, block)
Exemple #9
0
def block(lastBlock):
    return Block.mineBlock(lastBlock, 'test-data')
Exemple #10
0
def lastBlock():
    return Block.genesis()
Exemple #11
0
def testSlowlyMineBlock():
    lastBlock = Block.mineBlock(Block.genesis(), 'foo')
    time.sleep(MINE_RATE / SEC)
    mineBlock = Block.mineBlock(lastBlock, 'bar')

    assert mineBlock.dificulty == lastBlock.dificulty - 1
Exemple #12
0
def testQuicklyMineBlock():
    lastBlock = Block.mineBlock(Block.genesis(), 'foo')
    mineBlock = Block.mineBlock(lastBlock, 'bar')

    assert mineBlock.dificulty == lastBlock.dificulty + 1
Exemple #13
0
def testIsValidBlockBadHash(lastBlock, block):
    block.hash = '0000000000000000bbbabc'

    with pytest.raises(Exception, match='block hash must be correct'):
        Block.isValidBlock(lastBlock, block)