def test_is_block_valid_with_maladjusted_difficulty(prev_block, block):
    adjusted_difficulty = 10
    block.difficulty = adjusted_difficulty
    block.hash = f"{'0' * adjusted_difficulty}abc123"

    with pytest.raises(Exception, match="Difficulty adjustment error."):
        Block.is_block_valid(prev_block, block)
def test_is_block_valid_with_invalid_hash(prev_block, block):
    block.hash = "000000000000000abc123"

    with pytest.raises(
            Exception,
            match="Value error. A Proof of Work requirement has not been met."
    ):
        Block.is_block_valid(prev_block, block)
    def is_chain_valid(chain):
        """
		A Blockchain is said to be valid if:
		1. The Genesis block is valid.
		2. All the blocks in the blockchain are valid.
		"""
        if chain[0] != Block.genesis():
            raise Exception("Genesis block was invalid")
        for i in range(1, len(chain)):
            last_block = chain[i - 1]
            block = chain[i]
            Block.is_block_valid(last_block, block)
Esempio n. 4
0
    def is_chain_valid(chain):
        """
        Validate the chain
        """

        if chain[0] != Block.genesis_block():
            raise Exception("The genesis block is not valid")

        for i in range(1, len(chain)):
            block = chain[i]
            last_block = chain[i - 1]
            Block.is_block_valid(last_block, block)
        BlockChain.is_transaction_chain_valid(chain)
    def is_chain_valid(blockchain):
        """
        Validate incoming blockchain qua the following ruleset:
            - the blockchain must begin with a valid genesis block
            - blocks must be formatted properly
        """
        if (blockchain[0] != Block.genesis()):
            raise Exception("The genesis block must be valid.")

        for i in range(1, len(blockchain)):
            block = blockchain[i]
            prev_block = blockchain[i - 1]
            Block.is_block_valid(prev_block, block)
Esempio n. 6
0
def test_is_block_valid():
    last_block = Block.genesis_block()
    block = Block.mine_block(last_block, "dummy data")
    Block.is_block_valid(last_block, block)

    # bad last hash check
    block = Block.mine_block(last_block, "dummy data")
    block.last_hash = "evil_last_hash"
    with pytest.raises(
            Exception,
            match=
            "The last block hash and the last hash of the new block should match"
    ):
        Block.is_block_valid(last_block, block)

    # bad proof of work
    block = Block.mine_block(last_block, "dummy data")
    block.hash = "aaaaa"
    with pytest.raises(Exception, match="Proof of work requirement not met"):
        Block.is_block_valid(last_block, block)

    # difficulty tampered
    difficulty = 10
    block = Block.mine_block(last_block, "dummy data")
    block.difficulty = difficulty
    block.hash = f"{'0' * difficulty}1111a"

    with pytest.raises(Exception,
                       match="The block difficulty must only adjust by 1"):
        Block.is_block_valid(last_block, block)
def test_block_valid_wrong_hash(last_block, mined_block):
    mined_block.hash = "0x%sabcd" % ('0' * mined_block.difficulty)
    with pytest.raises(Exception, match='The block hash was not valid'):
        Block.is_block_valid(last_block, mined_block)
def test_block_valid_wrong_difficulty(last_block, mined_block):
    jumped_difficulty = 10
    mined_block.difficulty = jumped_difficulty
    mined_block.hash = "0x%sabcd" % ('0' * jumped_difficulty)
    with pytest.raises(Exception, match='The block difficulty was not valid'):
        Block.is_block_valid(last_block, mined_block)
def test_block_valid_wrong_pow(last_block, mined_block):
    mined_block.hash = '0xabcd'
    with pytest.raises(Exception,
                       match='The proof of work requirement was not met'):
        Block.is_block_valid(last_block, mined_block)
def test_block_valid_wrong_last_hash(last_block, mined_block):
    mined_block.last_hash = 'wrong_last_hash'
    with pytest.raises(Exception,
                       match='The hash of last block did not match'):
        Block.is_block_valid(last_block, mined_block)
def test_block_valid(last_block, mined_block):
    Block.is_block_valid(last_block, mined_block)
def test_is_block_valid_with_invalid_prev_hash(prev_block, block):
    block.prev_hash = "invalidate"

    with pytest.raises(Exception, match="Value previous hash is invalid."):
        Block.is_block_valid(prev_block, block)
def test_is_block_valid(prev_block, block):
    Block.is_block_valid(prev_block, block)