コード例 #1
0
def test_hex_to_binary():
    num = 4562
    hex_num = hex(num)[2:]
    bin_num = hex_to_binary(hex_num)
    decoded_num = int(bin_num, 2)

    assert decoded_num == num
コード例 #2
0
ファイル: block.py プロジェクト: briandcase/python-blockchain
    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')
コード例 #3
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.')
コード例 #4
0
def test_hex_to_binary():
    original_number = 666
    # [2:] cuts off the 0x prefix
    hex_number = hex(original_number)[2:]
    binary_number = hex_to_binary(hex_number)

    assert int(binary_number, 2) == original_number
コード例 #5
0
def test_hex_to_binary():
    orig_num = 1234567890
    hex_num = hex(orig_num)[2:]
    bin_num = hex_to_binary(hex_num)
    # Assert that the binary returned from hex_to_binary is indeed the correct
    # representation for the original number.
    assert (int(bin_num, 2) == orig_num)
コード例 #6
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')	
コード例 #7
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.')
コード例 #8
0
def test_mine_block(last_block, block):

    assert isinstance(block, Block)
    assert block.data == 'test_data'
    assert block.last_hash == last_block.hash
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == '0' * block.difficulty
コード例 #9
0
    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")
コード例 #10
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')
コード例 #11
0
def test_hex_to_binary():
    original_number = 789
    print(hex(original_number), "\n")
    hex_number = hex(original_number)[2:]
    binary_number = hex_to_binary(hex_number)

    assert int(binary_number, 2) == original_number
コード例 #12
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.')
コード例 #13
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")
コード例 #14
0
def test_hex_to_binary():
    number = 3526
    hex_number = hex(number)[2:]

    binary_number = hex_to_binary(hex_number)
    backtransformed_number = int(binary_number, 2)

    assert backtransformed_number == number
コード例 #15
0
def test_hex_to_binary():
    # Confirm that converting a the binary number back to an int results in the original number

    original_number = 789
    # Remove leading zeros from hex number
    hex_number = hex(original_number)[2:]
    binary_number = hex_to_binary(hex_number)

    assert int(binary_number, 2) == original_number
コード例 #16
0
def test_mine_block_leading_zeros():
    # Set up a block
    last_block = Block.genesis()
    data = "test-data"
    block = Block.mine_block(last_block, data)

    # Check that the block hash has the correct number of leading zeros
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == "0" * block.difficulty
コード例 #17
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)

    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
    assert hex_to_binary(block.hash)[0:block.difficulty] == '0' * block.difficulty
コード例 #18
0
def test_mine():
    previous_block = Block.genesis()
    data = "test data"
    block = Block.mine(previous_block, data)

    assert isinstance(block, Block)
    assert block.data == data
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == "0" * block.difficulty
コード例 #19
0
def test_mine_block():
    previous_block = Block.generate_genesis()
    data = '123'

    next_block = Block.mine_block(previous_block, data)

    assert isinstance(next_block, Block)
    assert next_block.data == data
    assert previous_block.hash == next_block.previous_hash
    assert hex_to_binary(next_block.hash).startswith('0' *
                                                     next_block.difficulty)
コード例 #20
0
    def is_valid_proof(block, block_hash):
        """
        Checks if block hash is valid and meets difficulty requirements
        """
        validate_block = Block(block.index, block.timestamp,
                               block.previous_hash, '', block.data,
                               block.difficulty, block.nonce)

        return hex_to_binary(block_hash).startswith(
            '0' *
            block.difficulty) and block_hash == generate_hash(validate_block)
コード例 #21
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test data'
    block = Block.mine_block(last_block, data)

    # First argument is the object that you want to test, second is the class that you want to match object.
    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == '0' * block.difficulty
コード例 #22
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)
コード例 #23
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)

    assert isinstance(
        block, Block
    )  # Check if the block we created became an instance or object of Block class
    assert block.data == data
    assert block.last_hash == last_block.hash
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == '0' * block.difficulty
コード例 #24
0
 def is_valid_block(last_block, block):
     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('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')
コード例 #25
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)
コード例 #26
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)

    #Ensure that mined block is instance of Block
    assert isinstance(block, Block)
    #data of the block should match the input data
    assert block.data == data
    #last hash should match hash of last block
    assert block.last_hash == last_block.hash
    #make sure that proof of work condition is satisfied
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == '0' * block.difficulty
コード例 #27
0
    def proof_of_work(previous_block, block):
        """
        Continuously generates a new hash using different nonce values until hash meets difficulty requirements
        """
        hash = generate_hash(block)

        while not hex_to_binary(hash).startswith('0' * block.difficulty):
            block.nonce += 1
            block.timestamp = time.time_ns()
            block.difficulty = Block.adjust_difficulty(previous_block,
                                                       block.timestamp)
            hash = generate_hash(block)

        return hash
コード例 #28
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)
コード例 #29
0
ファイル: block.py プロジェクト: invalidexplorer/xtcCoin
    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)
コード例 #30
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)