Beispiel #1
0
 def get_hash(self):
     concat_str = str(
         self.number
     ) + self.hash_parent + self.hash_state + self.hash_txns + str(
         self.time)
     if self.data:
         concat_str += self.data
     concat_bytes = concat_str.encode()
     return hash_message(concat_bytes)
Beispiel #2
0
 def calculate_hash(self):
     concat = []
     for state_key, state_bytes in self.db:
         if state_key == b"hash_state":
             continue
         concat.append(state_key + state_bytes)
     if not concat:
         return
     concat_bytes = b"|".join(concat)
     return hash_message(concat_bytes)
Beispiel #3
0
def proof_of_work(difficulty, mining_hash, start_nonce=0, rounds=10000000):
    """
    Simple hashimoto proof of work.
    :param difficulty: mining difficulty
    :type difficulty: int
    :param mining_hash: mining hash
    :type mining_hash: str
    :return: tuple of nonce and proof-of-work hash
    :rtype: tuple[int, str]
    """
    nonce = start_nonce - 1
    isvalid = False
    pow_hash = None
    while isvalid is False and nonce <= rounds:
        concat_str = "%s%s" % (nonce + 1, mining_hash)
        pow_hash = hash_message(concat_str.encode(), hex=True)
        isvalid = is_valid(difficulty, pow_hash)
        nonce += 1
        if nonce % 10000 == 0:
            print(nonce, "attempts", mining_hash, difficulty, pow_hash)
    if isvalid:
        return nonce, pow_hash
    return None, None
Beispiel #4
0
def verify(difficulty, mining_hash, nonce, expected_pow_hash):
    concat_str = "%s%s" % (nonce, mining_hash)
    actual_pow_hash = hash_message(concat_str.encode())
    if actual_pow_hash != expected_pow_hash:
        raise False
    return is_valid(difficulty, expected_pow_hash)
Beispiel #5
0
 def get_pow_hash(self, nonce, block_hash):
     concat_str = "%s%s" % (nonce, block_hash)
     return hash_message(concat_str.encode())
Beispiel #6
0
 def calc_hash(self):
     if not self.txns:
         return settings.BLANK_SHA_256
     txn_ids = [txn.id for txn in self.txns]
     return hash_message(msgpack.packb(txn_ids))