Example #1
0
def test_crypto_hash():
    # it should produce the same hash independent of the params' order
    assert crypto_hash(1, [2], "tres") == crypto_hash("tres", 1, [2])

    # it always produces the same output for the same input
    assert (crypto_hash(1, [2], "tres") ==
            "91dee9cb59dc4cb8b571a4b8c594000a402030a28f80d7299a872074f65fe479")
Example #2
0
def test_crypto_hash():
    # test should create the same hash with arguments given in any order of
    #varying data types
    assert crypto_hash(1, [2], 'three') == crypto_hash('three', 1, [2])
    assert crypto_hash(
        'foo'
    ) == 'b2213295d564916f89a6a42455567c87c3f480fcd7a1c15e220f17d7169a790b'
Example #3
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'
def test_crypto_hash():
    # it should create the same hash, regardless of
    # the args data type and the order.
    assert crypto_hash(1, [2], 'three') == crypto_hash([2], 'three', 1)
    assert crypto_hash(
        'foo'
    ) == 'b2213295d564916f89a6a42455567c87c3f480fcd7a1c15e220f17d7169a790b'
Example #5
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([2], 1, 'three')
    assert crypto_hash(
        'pk'
    ) == '6ded23596234c2e44755814ebda190318ed47845ff95b8e65509d394430a9577'
def test_crypto_hash():
    # It should create the same hash with arguments of different types in any order
    assert crypto_hash(1, [2], "three") == crypto_hash("three", 1, [2])

    assert crypto_hash(
        "foo"
    ) == "b2213295d564916f89a6a42455567c87c3f480fcd7a1c15e220f17d7169a790b"
Example #7
0
 def mine_block(last_block, data):
     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, last_hash, hash, data, difficulty, nonce)
Example #8
0
    def mine_block(last_block, data):
        timestamp = time.time_ns()
        last_hash = last_block.hash
        difficulty = last_block.difficulty
        nonce = 0
        hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

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

        return Block(timestamp, last_hash, hash, data, difficulty, nonce)
Example #9
0
 def mine_block(last_block, data):
     # mine a block based on the given last_block and data, untill a block hash
     # is found that meets the leading o's proof of work requirement
     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, last_hash, hash, data, difficulty, nonce)
Example #10
0
    def is_valid_block(last_block, block):
        """
        Validate a block by enforcing the following rules
        - the block must have the proper last_hash reference
        - the block must meet the proof of work requirement of having the correct number of leading zeroes according to the difficulty set
        - the difficulty must only adjust by 1
        - the block hash must be a valid combination of the block fields
        """
        if block.last_hash != last_block.hash:
            raise Exception(
                'The block last_hash must be equal to last_block.hash')
        if hex_to_binary(
                block.hash)[0:block.difficulty] != '0' * block.difficulty:
            raise Exception(
                'The proof of work requirement was not met. The block"s hash does not have leading zeros equal to the difficulty'
            )
        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.last_hash,
                                         block.data, block.difficulty,
                                         block.nonce)

        if block.hash != reconstructed_hash:
            raise Exception('The block hash must be correct.')
Example #11
0
    def is_valid_block(last_block, block):
        '''
        Validate block by enforcing the following rules:
        - the block must have the proper last_hash 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
        :param last_block:
        :param block:
        :return:
        '''
        if block.last_hash != last_block.hash:
            raise Exception('The block last_hash 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.last_hash,
            block.data,
            block.nonce,
            block.difficulty
        )

        if block.hash != reconstructed_hash:
            raise Exception("The block hash must be correct")
def main():
    number = 13
    hex_no = hex(number)[2:]
    binary = hex_to_bin(hex_no)
    print(f'hex number: {hex_no}, equivalent Binary is: {binary}')
    hex_to_binary_crypto_hash = hex_to_bin(crypto_hash('Yash'))
    print(f'hex_to_binary_crypto_hash: {hex_to_binary_crypto_hash}')
    def mine_block(last_block, data):
        """
        Function to create a new block using the last block and current data 
        """
        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_bin(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, last_hash, hash, data, difficulty, nonce)
    def is_valid_block(last_block, block):
        """
        Validate block by enforcing the following rules:
        1. The block must have the proper last_hash reference
        2. The block must meet the proof of work requirement
        3. The difficulty must only adjust by 1
        4. The block hash must be a valid combination of the block fields
        """
        if block.last_hash != last_block.hash:
            raise Exception("The block last_hash 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.last_hash,
            block.data,
            block.nonce,
            block.difficulty,
        )

        if block.hash != reconstructed_hash:
            raise Exception("The block hash must be correct")
Example #15
0
    def is_valid_block(last_block, block):
        """
        Validate block by enforcing the following rules:
            -The block must have the proper last_hash
            -The block must meet the proof of work requirement
            -The difficulty must only change by one
            -The block hash must be a valid combination of the block fields
        """

        if block.last_hash != last_block.hash:
            raise Exception('Incorrect last_hash')

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

        if abs(last_block.difficulty - block.difficulty) > 1:
            raise Exception(
                'The block difficulty must only increase/decrease by one')

        test_hash = crypto_hash(block.timestamp, block.last_hash, block.data,
                                block.nonce, block.difficulty)

        if block.hash != test_hash:
            raise Exception('Invalid hash')
Example #16
0
    def block_is_valid(last_block, block):
        """
        In this block, I am trying to validate a blokc by enforcing the following:
            - the block must have the proper last_hash 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.last_hash != last_block.hash:
            raise Exception ('Block `last_hash` Must be Correct')
        
        if convert_hex_to_binary(block.hash)[0:block.difficulty] != '0' * block.difficulty:
            raise Exception('`Proof of Work` Requirement has not Been Met')

        if abs(last_block.difficulty - block.difficulty) > 1:
            raise Exception('Block Difficulty Can Only Be Adjusted by 1')
        
        reconstructed_hash = crypto_hash(
            block.timestamp,
            block.last_hash,
            block.data,
            block.nonce,
            block.difficulty
        )

        if block.hash != reconstructed_hash:
            raise Exception('Block Hash Must be Correct')    
Example #17
0
    def is_valid_block(last_block, block):
        """
        Validate block by enforcing the rules:
        - the block must have the proper last_hash
        - the block must meet the proof of work requirement
        - the difficulty must only adjust by 1
        - the block hash must be a valid combination
        """

        if block.last_hash != last_block.hash:
            raise Exception('The block last_hash 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.last_hash,
                                         block.data, block.nonce,
                                         block.difficulty)

        if block.hash != reconstructed_hash:
            raise Exception('The block hash must be correct')
Example #18
0
	def is_valid_block(last_block, block):
		"""Validate a block by inforcing the following rules
		   - The block must have the proper last_hash 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 (by 
		     regenerating the hash with the block fields must match the block's hash presented)
		"""
		if block.last_hash != last_block.hash:
			raise Exception('The block last_hash must be correct')

		if hex_to_binary(block.hash)[: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 one')

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

		if block.hash != reconstructed_hash:
			raise Exception('The block hash must be correct')	
Example #19
0
    def is_valid(last_block, block):
        '''
            Validate a block by enforcing the following rules:
                - The block must have the proper last_hash 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.last_hash != last_block.hash:
            raise Exception('The block must have a proper last_hash reference.')

        if hex_to_binary(block.hash)[0:block.difficulty] != ('0' * block.difficulty):
            raise Exception('The block did not meet the Proof of Work Requirement.')

        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.last_hash,
            block.data,
            block.nonce,
            block.difficulty
        )

        if block.hash != reconstructed_hash:
            raise Exception('The block must have a proper hash reference.')
Example #20
0
    def is_valid_block(last_block, block):
        """
        Validate a block by enforcing the following rules:
            -the block must have the proper last_hash 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 (timestamp, last_hash, hash, data)
        """
        # make sure the last_block hash matches
        if block.last_hash != last_block.hash:
            raise Exception('The block last_hash must be correct.')

        # make sure hash starts with correct number of leading 0s that match the difficulty
        if hex_to_binary(
                block.hash)[0:block.difficulty] != '0' * block.difficulty:
            raise Exception('The proof of work requirement was not met.')

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

        # make sure the hash of the block must be a hash of all the fields in the block
        reconstructed_hash = crypto_hash(block.timestamp, block.last_hash,
                                         block.data, block.nonce,
                                         block.difficulty)

        if block.hash != reconstructed_hash:
            raise Exception('The block hash must be correct.')
    def is_valid_block(last_block, block):
        """
        Validate the block by enforcing the following rules:
            - the block must have proper last_hash reference
            - the block must see the proof of work requirement
            - difficult must be adjust by 1
            - the block hash must be valid combination of the block fields
        """
        if block.last_hash != last_block.hash:
            raise Exception('the last_hash mustbe correct')

        if hex_to_bin(block.hash)[0:block.difficulty] != '0' * block.difficulty:
            raise Exception('the proof of requirement was not meet')

        if abs(last_block.difficulty - block.difficulty) > 1:
            raise Exception('the block difficulty should change by 1')

        reconstructed_hash = crypto_hash (
            block.time_stamp,
            block.last_hash,
            block.data,
            block.difficulty,
            block.nonce
        )

        if block.hash != reconstructed_hash:
            raise Exception('The block hash must be correct')
Example #22
0
    def mine_block(last_block, data):
        """
        Mine blocks based on the given last_block and data, until hash w/ the correct number of leading 0's is found.
        """
        timestamp = 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_ns()
            difficulty = Block.adjust_difficulty(last_block, timestamp)
            hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

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

        while hex_to_bin(hash)[0:difficulty] != '0' * difficulty:
            nonce += 1
            time_stamp = time.time_ns()
            difficulty = Block.adjust_difficulty(last_block, time_stamp)
            hash = crypto_hash(time_stamp, last_hash, data, difficulty, nonce)
        
        return Block(time_stamp, last_hash, hash, data, difficulty, nonce)
Example #24
0
    def mine_the_block(last_block, data):
        """
        Here I mine a block based on the given last_block and data till the block hash 
        that is found meets requirement of leading O's as stipulated in the proof of work
        """
        timestamp = time.time_ns()
        last_hash = last_block.hash
        difficulty = Block.adjust_mining_difficulty(last_block, timestamp)
        nonce = 0
        hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

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

        return Block(timestamp, last_hash, hash, data, difficulty, nonce)
Example #25
0
    def mine_block(last_block, data):
        """
        Mine a block based on the given last_block and data,
        until a block hash is found that meets the leading 0's requirment.
        """
        timestamp = time.time()
        last_hash = last_block.hash
        difficulty = Block.adjust_difficulity(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()
            difficulty = Block.adjust_difficulity(last_block, timestamp)
            hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

        return Block(timestamp, last_hash, hash, data, difficulty, nonce)
Example #26
0
    def mine_block(last_block, data):
        """
        Mine a block, until a block hash is found that meets the ledaing
        's proof of work requirement
        """
        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, last_hash, hash, data, difficulty, nonce)
Example #27
0
    def mine_block(last_block, data):
        '''
    mines a block based on the given last_block and data until a block has is found that meets the leading 0's proof of work requirement.
    '''
        timestamp = time.time_ns()
        last_hash = last_block.hash
        difficulty = Block.adjust_difficulty(last_block, timestamp)
        nonce = 0
        # hash = f'{timestamp}-{last_hash}'
        hash = crypto_hash(timestamp, last_hash, data, difficulty, nonce)

        while 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, last_hash, hash, data, difficulty, nonce)
    def mine_block(last_block, data):
        """
        Mine a block based on the given last_block and data.
        """
        timestamp = time.time_ns()
        last_hash = last_block.hash
        hash = crypto_hash(timestamp, last_hash, data)

        return Block(timestamp, last_hash, hash, data)
Example #29
0
    def mine(previous_block, data):
        """
        Mine a block based on the previous_block and data, until a block hash is found
        that will satisfy the leading zeros requirement for proof of work.
        """

        timestamp = time.time_ns()
        previous_hash = previous_block.hash
        difficulty = Block.adjust_difficulty(previous_block, timestamp)
        nonce = 0
        hash = crypto_hash(timestamp, previous_hash, data, difficulty, nonce)

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

        return Block(timestamp, previous_hash, hash, data, difficulty, nonce)
Example #30
0
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}')