Skip to content

Instantly share code, notes, and snippets.

@yyforyongyu
Created February 3, 2019 19:42
Show Gist options
  • Save yyforyongyu/02fc007e28e41dc885ca2a5cb84f1943 to your computer and use it in GitHub Desktop.
Save yyforyongyu/02fc007e28e41dc885ca2a5cb84f1943 to your computer and use it in GitHub Desktop.
MAX_TEST_MINE_ATTEMPTS = 1000
def mine_pow_nonce(block_number: int, mining_hash: Hash32, difficulty: int) -> Tuple[bytes, bytes]:
cache = get_cache(block_number)
for nonce in range(MAX_TEST_MINE_ATTEMPTS):
mining_output = hashimoto_light(block_number, cache, mining_hash, nonce)
result = big_endian_to_int(mining_output[b'result'])
result_cap = 2**256 // difficulty
if result <= result_cap:
return nonce.to_bytes(8, 'big'), mining_output[b'mix digest']
raise Exception("Too many attempts at POW mining, giving up")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment