Esempio n. 1
0
    def valid_proof(transactions, last_hash, proof):
        """Validate a proof of work number and see if it solves the puzzle
        algorithm (two leading 0s)

        Arguments:
            :transactions: The transactions of the block for which the proof
            is created.
            :last_hash: The previous block's hash which will be stored in the
            current block.
            :proof: The proof number we're testing.
        """
        # Create a string with all the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transactions]
                     ) + str(last_hash) + str(proof)).encode()
        # Hash the string
        # IMPORTANT: This is NOT the same hash as will be stored in the
        # previous_hash. It's a not a block's hash. It's only used for the
        # proof-of-work algorithm.
        guess_hash = hash_string_256(guess)
        # Only a hash (which is based on the above inputs) which starts with
        # two 0s is treated as valid
        # This condition is of course defined by you. You could also require
        # 10 leading 0s - this would take significantly longer (and this
        # allows you to control the speed at which new blocks can be added)
        return guess_hash[0:2] == '00'
Esempio n. 2
0
 def valid_proof(transactions, last_hash, proof):
     # Create a string with all the hash inputs
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     # Hash the string
     guess_hash = hash_string_256(guess)
     return guess_hash[0:4] == '0000'
Esempio n. 3
0
 def valid_proof(transactions, last_hash, proof):
     # be carefull while passing the arguments make sure hashing will cause issue for the same item (list or string matters!!str([tx....]))
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     guess_hash = hash_string_256(guess)
     print(guess_hash)
     return guess_hash[0:2] == '00'
Esempio n. 4
0
 def valid_proof(transactions, last_hash, proof):
     """Checks whether proof is valid according to some criteria."""
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) +
              str(proof)).encode()
     guess_hash = hash_string_256(guess)
     return guess_hash[0:2] == '00'
Esempio n. 5
0
    def valid_proof(transactions, last_hash, proof):
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()

        guess_hash = hash_string_256(guess)
        # print(guess_hash)
        return guess_hash[0:2] == '00'
Esempio n. 6
0
    def valid_proof(votes, last_hash, proof):
        guess = (str([vt.to_order_dict() for vt in votes]) + str(last_hash) +
                 str(proof)).encode('utf8')

        print(guess)
        guess_hash = hash_string_256(guess)
        print(guess_hash)
        return guess_hash[0:2] == '00'
Esempio n. 7
0
    def valid_proof(transcations, last_hash, proof):

        #creating a string with all the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transcations]) +
                 str(last_hash) + str(proof)).encode()
        guess_hash = hash_string_256(guess)
        print(guess_hash)
        return guess_hash[0:2] == '00'
Esempio n. 8
0
 def valid_proof(transactions, last_hash, proof):
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     print(guess)
     # Hash the string
     # IMPORTANT: This is NOT the same hash as will be stored in the previous_hash. It's a not a block's hash. It's only used for the proof-of-work algorithm.
     guess_hash = hash_string_256(guess)
     print(guess_hash)
     # Our requirement for a valid hash
     return guess_hash[0:2] == '00'
Esempio n. 9
0
 def valid_proof(transactions, last_hash, proof):
     """ Algorithm which generates a new hash and checks 
     weather it fulfills our difficulty criteria that its
     a valid new hash
     """
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     guess_hash = hash_string_256(guess)
     # print(guess_hash)
     return guess_hash[0:2] == '00'
 def valid_proof(transactions, last_hash, proof):
     # Create a string with all the hash inputs
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     # Hash the string; This hash is used for POW Algorithm and is not the same as stored in previous_hash
     guess_hash = hash_string_256(guess)
     # Only a hash (which is based on the above inputs) which starts with two 0s is treated as valid
     # If 10 0's are used instead of '00', this allows to control the speed at which new blocks are created
     # so more 0's mean more time will be required to create a new block
     return guess_hash[0:2] == '00'
Esempio n. 11
0
    def valid_proof(transactions, last_hash, proof):
        """Validate a proof of work number and see if it solves the puzzle algorithm (two leading 0s)

        Arguments:
            :transactions: The transactions of the block for which the proof is created.
            :last_hash: The previous block's hash which will be stored in the current block.
            :proof: The proof number we're testing.
        """
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()
        guess_hash = hash_string_256(guess)
        return guess_hash[0:2] == '00'
Esempio n. 12
0
    def valid_proof(transactions, last_hash, proof):
        """Validate a proof of work number and see if it solves the puzzle algorithm

        Arguments:
            :transactions: The transactions for the last block for which the proof is being validated
            :last_hash: The previous block's hash which will be stored in the current block
            :proof: The proof number we're testing
        """
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                str(last_hash) + str(proof)).encode()
        # print(guess)
        guess_hash = hash_string_256(guess)
        # print(guess_hash)
        return guess_hash[0:2] == "00"
Esempio n. 13
0
    def valid_proof(transactions, last_hash, proof):
        """
        Checks if a combination of transactions, last_hash, and a number that it's represented by the proof
        argument pass the conditions of the proof of work algorithm.
        :param transactions: A list of transactions that we want to add to the blockchain
        :param last_hash: The hash of the last block
        :param proof: The number that we want to check as proof of work.
        :return:
        """
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()
        guess_hash = hash_string_256(guess)
        print('Trying hash: ', guess_hash)

        return guess_hash[0:2] == '00'
Esempio n. 14
0
 def valid_proof(transactions, last_hash, proof):
     """Validate a proof of work number and see if it solves the puzzle algorithm (two leading 0s)
     
     Arguments:
         :transactions: The Transactions of the block for which the proof is created.
         :last_hash: The previous block's hash which will be stored in the current block.
         : proof: The proof number tested."""
     # Create a string with all the hash inputs
     guess = (str([tx.to_ordered_dict() for tx in transactions]) + str(last_hash) +str(proof)).encode()
     # Hash string
     # NOTE: This is a different hash stored in the previous hash. Only used for proof of work algorithms.
     guess_hash = hash_string_256(guess)
     # Only a hash (based on the above inputs) which starts with two 0s is treated as valid.
     # Check if hash fulfills condition
     return guess_hash[0:2] == '00'
Esempio n. 15
0
    def valid_proof(transactions, last_hash, proof):
        """Validate a proof of work number and see if it solves the puzzle algorithm (two leading 0s)

        Arguments:
            :transactions: The transactions of the block for which the proof is created.
            :last_hash: The previous block's hash which will be stored in the current block.
            :proof: The proof number we're testing.
        """
        # Create a string with all the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transactions]) + str(last_hash) + str(proof)).encode()
        # Hash the string
        # IMPORTANT: This is NOT the same hash as will be stored in the previous_hash. It's a not a block's hash. It's only used for the proof-of-work algorithm.
        guess_hash = hash_string_256(guess)
        # Only a hash (which is based on the above inputs) which starts with two 0s is treated as valid
        return guess_hash[0:2] == '00'
Esempio n. 16
0
    def valid_proof(transactions, last_hash, proof_number):
        """ Proof of work: (decided by th eimplmenter of the crypto currency)
            Transactions - Previous Hash - Proof (Nonce) (random number) : implemented in a loop 
            We combine the 3 above values in a hash (1 string with 64 chars)
            If hash starts with a "n" amount of leading chars (this is up to you).
            If the block is chnaged the POW would need to be recaclualted for that block and all susequent blocks. 
            This is time consuming  and therefore makes it really hard to change as time and power is needed.
        """
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof_number)).encode()
        guess_hash = hash_string_256(guess)

        return guess_hash[
            0:
            2] == "00"  # for simplicity we will on looking for 2 leading zeroes
Esempio n. 17
0
    def valid_proof(transactions, last_hash, proof):
        """
        This staticmethod method calculates the proof of all the transactions based on the previous hash and the proof's number.

        :param transactions: All of the transactions
        :param last_hash: the previous hash of the transactions
        :param proof: the proof's number
        :var guess str: The concatenation of all transactions, the last hash and the proof of work encoded in UTF8.
        :var guess_hash str: Hashes the guess var.
        :returns bool: True if the proof is valid, false if not.
        """

        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()
        guess_hash = hash_string_256(guess)
        return guess_hash[0:2] == '00'
Esempio n. 18
0
    def valid_proof(transactions, last_hash, proof):
        """ Validate a proof of work number and see if it solves the puzzle

        Arguments:
            :transactions: The transactions of the block for which the proof
            of work working on.
            :last_hash: The previous block's hash which will be stored.
            :proof: The proof of work number we're testing.
        """
        # Create a string with the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()
        # print(guess)
        guess_hash = hash_string_256(guess)
        # print(guess_hash)
        return guess_hash[0:2] == '00'
Esempio n. 19
0
    def valid_proof(transactions, last_hash, proof):
        """Validate a proof of work number and see if it solves the puzzle algorithm (two leading 0s)

        Arguments:
            :transactions: The transactions of the block for which the proof is created.
            :last_hash: The previous block's hash which will be stored in the current block.
            :proof: The proof number we're testing.
        """
        # Create a string with all the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transactions]
                     ) + str(last_hash) + str(proof)).encode()
        # Hash the string
        # IMPORTANT: This is NOT the same hash as will be stored in the previous_hash. It's a not a block's hash. It's only used for the proof-of-work algorithm.
        guess_hash = hash_string_256(guess)
        # Only a hash (which is based on the above inputs) which starts with two 0s is treated as valid
        # This condition is of course defined by you. You could also require 10 leading 0s - this would take significantly longer (and this allows you to control the speed at which new blocks can be added)
        return guess_hash[0:2] == '00'
Esempio n. 20
0
    def valid_proof(transactions, last_hash, proof):
        """To validate a proof of work number and see if it solves the puzzle algorithm

        Arguments:
            :transactions: The transactions of the block for which the proof is being generated for
            :last_hash: The previous blocks hash which will be stored in the current block
            :proof: The proof number we're testing
        """
        # create a string with all the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transactions]
                     ) + str(last_hash) + str(proof)).encode()
        # hash the string
        # IMPORTANT: This is NOT the hash as will be stored in the previous_hash. It's not a blocks hash, it's just to get the proof
        guess_hash = hash_string_256(guess)
        # print(guess_hash)
        # only a hash which starts with two zeros will pass the return condition
        return guess_hash[0:2] == '00'
Esempio n. 21
0
    def valid_proof(transactions, last_hash, proof):
        """ Validate a proof of work number and see if it solves the puzzle algorithm.

        Arguments:
            :transactions: The transactions of the block for which the proof is
            :last_hash: The previous block's hash which will be stored in the current
            :proof: The proof number we're testing.
        """

        # Create a string with all the hash inputs
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()
        # Hash the string
        # IMPORTANT: This is NOT the same hash as will be stored in the previous_hash
        guess_hash = hash_string_256(guess)
        # Only a hash (which is based on the above inputs) which starts with two
        # This condition is of course defined by you. You could also require 10
        return guess_hash[0:2] == '00'
Esempio n. 22
0
    def valid_proof(transactions, last_hash, proof):
        """
        Valide un nombre de preuve de travail et voit si il résout l'énigme algorithmique (007)

        Arguments:
            :transactions: Les transactions du bloc pour lequel la preuve est crée.
            :last_hash: Le hash du bloc précédent qui sera stocké dans le bloc en cours.
            :proof: Le nombre preuve que nous testons.
        """
        # Crée une chaîne (string) avec le hash de toutes les entrées
        guess = (str([tx.to_ordered_dict() for tx in transactions]) + str(last_hash) + str(proof)).encode()
        # Hash la chaîne (string)
        # IMPORTANT : ce n'est PAS le même hash qui sera stocké dans le previous_hash.
        # Ce n'est pas un hash de bloc. C'est uniquement utilisé pour l'algorithme de preuve de travail.
        guess_hash = hash_string_256(guess)
        # Uniquement un hash (qui est basé sur les entrées au-dessus) qui commence par 007
        # est traité comme valide
        # Cette condition est bien sûr arbitraire et définie par vous.
        # Vous pourriez aussi requérir 10 zéros - cela serait significativement plus long (et cela
        # vous permet de contrôler la vitesse avec laquelle de nouveaux blocs peuvent être ajoutés)
        return guess_hash[0:3] == '007'
    def valid_proof(transactions, last_hash, proof):
        """ Generates a valid new hashes by checking to see if it fits our difficulty criteria.
        In our case its two leading 0's. Ideally this would get harder with time.

        Arguments:
            :transactions: Transactions of new block for which the proof
            is created
            :last_hash: Hash of previous block in the blockchain, will be
            stored in the current block
            :proof: Proof number
        """
        # Hash not the same as the previous hash since index is not considered.
        # The string is encoded into 'utf-8' characters.
        # IMPORTANT this converts transactions, which is an ordered dict. This means it is
        #           is converted into a string with '[OrderedDict()]' at the start of the
        #           the list of transactions
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof)).encode()
        # Calculate hash of new guess using custom function defined in hash_util
        guess_hash = hash_string_256(guess)
        # Only a hash (which is based on the above inputs) which starts with two 00's is a valid hash.
        # This is the hash difficulty and can be changed to be more difficult.
        return guess_hash[0:2] == '00'
    def valid_proof(transactions, last_hash, proof):
        """ Validate a proof of work number and see if it solves the puzzle algorithm

        Arguments:
            :transactions: The transactions of the block for which the proof
            is being calculated.
            :last_hash: The previous block's hash which will be stored in the
            current block.
            :proof: The proof number we're testing.
        """
        # Create a string with all the hash inputs
        guess = ((str([tx.to_ordered_dict() for tx in transactions]) +
                  str(last_hash) +
                  str(proof)).encode())
        # print(guess)
        # Hash the string
        # IMPORTANT: This is NOT the same hash as will be stored in
        # the previous_block
        guess_hash = hash_string_256(guess)
        # print(guess_hash)
        # Only a hash (which is based on the above inputs) which meets
        # the requirements is considered valid
        # In this case it is 2 leading zeroes
        return guess_hash[0:2] == '00'
Esempio n. 25
0
 def valid_proof(transactions, last_hash, proof):
     """Creates a hash of the combined string of transactions last_hash and proof"""
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     guess_hash = hash_string_256(guess)
     return guess_hash[0:2] == '00'
Esempio n. 26
0
    def valid_proof(transactions, last_hash, proof_number):
        guess = (str([tx.to_ordered_dict() for tx in transactions]) +
                 str(last_hash) + str(proof_number)).encode()
        hash_guess = hash_string_256(guess)

        return hash_guess[0:2] == "00"
Esempio n. 27
0
 def valid_proof(txns, previous_hash, proof):
     guess = (str([tx.get_ordered_dict for tx in txns]) +
              str(previous_hash) + str(proof)).encode()
     guessed_hash = hash_string_256(guess)
     return guessed_hash[0:2] == '00'
Esempio n. 28
0
 def valid_proof(transactions, last_hash, proof):
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     guess_hash = hash_string_256(guess)
     return guess_hash[0:Verification.DIFFICULTY] == (
         '0' * Verification.DIFFICULTY)
Esempio n. 29
0
 def valid_proof(rezultate, last_hash, proof):
     guess = (str(rezultate) + str(last_hash) + str(proof)).encode()
     guess_hash = hash_string_256(guess)
     isValid = guess_hash[:2] == '0' * dificultate
     return isValid
Esempio n. 30
0
 def valid_proof(transaction, last_hash, proof):
     guess = (str([tx.ordered_tx for tx in transaction]) + str(last_hash) +
              str(proof)).encode()
     guess_hash = hash_string_256(guess)
     return guess_hash[0:2] == '00'
Esempio n. 31
0
 def valid_proof(transactions, last_hash, proof):
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     # This hash is used only for the proof-of-work algorithm.
     guess_hash = hash_string_256(guess)
     return guess_hash[0:2] == '00'
Esempio n. 32
0
 def valid_proof(transactions, last_hash, proof):
     """Validate a proof of work number and see if it solves the puzzle"""
     guess = (str([tx.to_ordered_dict() for tx in transactions]) +
              str(last_hash) + str(proof)).encode()
     guess_hash = hash_string_256(guess)
     return guess_hash[0:2] == "00"