Example #1
0
def test_crypto_hash():
    # It should create the same hash with arguments of different data types
    # in any order
    assert crypto_hash(1, [2], 'three') == crypto_hash('three', 1, [2])
    assert crypto_hash(
        'foo'
    ) == 'b2213295d564916f89a6a42455567c87c3f480fcd7a1c15e220f17d7169a790b'
Example #2
0
    def is_valid_block(last_block, block):
        """
        Validate block by enforcing the following rules:
            - The block's last_hash must be equal to last_block's hash
            - The block must meet the proof of work requirement
            - The difficulty must only adjust by 1 (decrease or increase)
            - The block hash must be a valid combination of the block's fields
        """
        # Rule 1: The block's last hash must be equal to last_block's hash
        if block.last_hash != last_block.hash:
            raise Exception('The block last_hash must be correct')

        # Rule 2: The block must meet the proof of work requirement
        if hex_to_binary(
                block.hash)[0:block.difficulty] != '0' * block.difficulty:
            raise Exception('The proof of work requirement was not met')

        # Rule 3: The difficulty must only adjust by 1
        if abs(last_block.difficulty - block.difficulty) > 1:
            raise Exception('The difficulty must only adjust by 1')

        # Rule 4: The block hash must be a valid combination of the block's fields
        reconstructed_hash = crypto_hash(block.timestamp, block.last_hash,
                                         block.data, block.difficulty,
                                         block.nonce)

        if block.hash != reconstructed_hash:
            raise Exception('The block hash must be correct')
Example #3
0
    def mine_block(last_block, data):
        """
        Mines a block based on the given last_block and data until a block hash is found that meets the leading 0's proof of work requirement.
        """
        timestamp = time.time_ns()
        lasthash = last_block.hash
        difficulty = Block.adjust_difficulty(last_block, timestamp)
        nonce = 0
        hash = crypto_hash(timestamp, lasthash, data, difficulty, nonce)

        while hex_to_binary(hash)[0:difficulty] != '0' * difficulty:
            nonce += 1
            timestamp = time.time_ns()
            difficulty = Block.adjust_difficulty(last_block, timestamp)
            hash = crypto_hash(timestamp, lasthash, data, difficulty, nonce)

        return Block(timestamp, lasthash, hash, data, difficulty, nonce)
Example #4
0
    def mine_block(last_block, data):
        """
        Mine a block based on the last_block and data given
        """
        timestamp = time.time_ns()
        last_hash = last_block.hash
        hash = crypto_hash(timestamp, last_hash, data)

        return Block(timestamp, last_hash, hash, data)
Example #5
0
    def mine_block(last_block, data):
        """
        Mine a block with the details of last block and the data of the block to be created.
        Hash of the new block to be created should match the proof of work requirement of leading zeros.

        """
        timestamp = time.time_ns()
        last_hash = last_block.hash
        difficulty = Block.adjust_difficulty(last_block, timestamp)
        nonce = 0
        hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

        while hex_to_binary(hash)[0:difficulty] != '0' * difficulty:
            nonce += 1
            timestamp = time.time_ns()
            difficulty = Block.adjust_difficulty(last_block, timestamp)
            hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

        return Block(timestamp, hash, last_hash, data, difficulty, nonce)
def main():
    number = 451
    hex_number = hex(number)[2:]
    print(f'hex_number: {hex_number}')

    binary_number = hex_to_binary(hex_number)
    print(f'binary_number: {binary_number}')

    original_number = int(binary_number, 2)
    print(f'original_number: {original_number}')

    hex_to_binary_crypto_hash = hex_to_binary(crypto_hash('test-data'))
    print(f'hex_to_binary_crypto_hash: {hex_to_binary_crypto_hash}')
Example #7
0
 def is_block_valid(last_block, block):
     """
     Check if the block is valid
     """
     if block.last_hash != last_block.hash:
         raise Exception(
             "The last block hash and the last hash of the new block should match"
         )
     if hex_to_binary(
             block.hash)[0:block.difficulty] != "0" * block.difficulty:
         raise Exception("Proof of work requirement not met")
     if abs(last_block.difficulty - block.difficulty) > 1:
         raise Exception('The block difficulty must only adjust by 1')
     rebuilded_hash = crypto_hash(block.timestamp, block.last_hash,
                                  block.data, block.difficulty, block.nonce)
     if block.hash != rebuilded_hash:
         raise Exception(f"The hash of the block: {block.hash} is invalid")
Example #8
0
    def is_valid_block(last_block, block):
        """
        Validate block by enforcing the following rules:
        - The block must have the proper lasthash reference
        - The block must meet the proof of work requirement
        - The difficulty must only adjust by 1
        - The block hash must be a valid combination of the block fields
        """
        if block.lasthash != last_block.hash:
            raise Exception('The block lasthash must be correct')

        if hex_to_binary(
                block.hash)[0:block.difficulty] != '0' * block.difficulty:
            raise Exception('The proof of work requirement was not met')

        if abs(last_block.difficulty - block.difficulty) > 1:
            raise Exception('The block difficulty must only adjust by 1')

        reconstructed_hash = crypto_hash(block.timestamp, block.lasthash,
                                         block.data, block.nonce,
                                         block.difficulty)

        if block.hash != reconstructed_hash:
            raise Exception('The block hash must be correct')
Example #9
0
def test_crypto_hash():
    assert crypto_hash(1, 2, "one") == crypto_hash("one", 2, 1)