def genesis() -> Block: args = (0, "0", int(time.time()), "Genesis Block") nonce = 0 # suppose this target's difficulty = 1 target = "00000ffff0000000000000000000000000000000000000000000000000000000" while True: hash = Block.calculate_hash(*args, nonce=nonce, target=target) if Block.validate_difficulty(hash, target): break else: nonce += 1 return Block(*args, nonce=nonce, target=target, hash=hash)
def generate_next(self, data: str) -> Block: lb = self.latest_block args = (lb.index + 1, lb.hash, int(time.time()), data) nonce = 0 target = self.retarget() while True: hash = Block.calculate_hash(*args, nonce=nonce, target=target) if Block.validate_difficulty(hash, target): break else: nonce += 1 return Block(*args, nonce=nonce, target=target, hash=hash)