2013-09-07 21:11:42 +00:00
|
|
|
#!/usr/bin/env python
|
2013-08-18 21:50:41 +00:00
|
|
|
# example of proof-of-work algorithm
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import time
|
|
|
|
|
2017-08-24 16:37:02 +00:00
|
|
|
try:
|
|
|
|
long # Python 2
|
|
|
|
xrange
|
|
|
|
except NameError:
|
|
|
|
long = int # Python 3
|
|
|
|
xrange = range
|
|
|
|
|
|
|
|
max_nonce = 2 ** 32 # 4 billion
|
|
|
|
|
2013-08-18 21:50:41 +00:00
|
|
|
|
2013-09-07 21:11:42 +00:00
|
|
|
def proof_of_work(header, difficulty_bits):
|
2014-09-25 16:02:07 +00:00
|
|
|
# calculate the difficulty target
|
2017-08-24 16:37:02 +00:00
|
|
|
target = 2 ** (256 - difficulty_bits)
|
|
|
|
|
2013-08-18 21:50:41 +00:00
|
|
|
for nonce in xrange(max_nonce):
|
2018-03-16 15:02:21 +00:00
|
|
|
hash_result = hashlib.sha256((str(header) + str(nonce)).encode()).hexdigest()
|
2017-08-24 16:37:02 +00:00
|
|
|
|
2014-09-25 16:02:07 +00:00
|
|
|
# check if this is a valid result, below the target
|
2013-08-18 21:50:41 +00:00
|
|
|
if long(hash_result, 16) < target:
|
2017-08-24 16:37:02 +00:00
|
|
|
print("Success with nonce %d" % nonce)
|
|
|
|
print("Hash is %s" % hash_result)
|
|
|
|
return (hash_result, nonce)
|
|
|
|
|
|
|
|
print("Failed after %d (max_nonce) tries" % nonce)
|
2013-08-18 21:50:41 +00:00
|
|
|
return nonce
|
|
|
|
|
2017-08-24 16:37:02 +00:00
|
|
|
|
2013-08-18 21:50:41 +00:00
|
|
|
if __name__ == '__main__':
|
2013-09-07 21:11:42 +00:00
|
|
|
nonce = 0
|
|
|
|
hash_result = ''
|
2017-08-24 16:37:02 +00:00
|
|
|
|
|
|
|
# difficulty from 0 to 31 bits
|
2013-09-07 21:11:42 +00:00
|
|
|
for difficulty_bits in xrange(32):
|
|
|
|
difficulty = 2 ** difficulty_bits
|
2017-08-24 16:37:02 +00:00
|
|
|
print("Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits))
|
|
|
|
print("Starting search...")
|
|
|
|
|
2014-09-25 16:02:07 +00:00
|
|
|
# checkpoint the current time
|
2013-09-07 21:11:42 +00:00
|
|
|
start_time = time.time()
|
2017-08-24 16:37:02 +00:00
|
|
|
|
2014-09-25 16:02:07 +00:00
|
|
|
# make a new block which includes the hash from the previous block
|
|
|
|
# we fake a block of transactions - just a string
|
2017-08-24 16:37:02 +00:00
|
|
|
new_block = 'test block with transactions' + hash_result
|
|
|
|
|
2014-09-25 16:02:07 +00:00
|
|
|
# find a valid nonce for the new block
|
2017-08-24 16:37:02 +00:00
|
|
|
(hash_result, nonce) = proof_of_work(new_block, difficulty_bits)
|
|
|
|
|
2014-09-25 16:02:07 +00:00
|
|
|
# checkpoint how long it took to find a result
|
2013-09-07 21:11:42 +00:00
|
|
|
end_time = time.time()
|
2017-08-24 16:37:02 +00:00
|
|
|
|
2013-09-07 21:11:42 +00:00
|
|
|
elapsed_time = end_time - start_time
|
2017-08-24 16:37:02 +00:00
|
|
|
print("Elapsed Time: %.4f seconds" % elapsed_time)
|
|
|
|
|
2013-09-07 21:11:42 +00:00
|
|
|
if elapsed_time > 0:
|
2017-08-24 16:37:02 +00:00
|
|
|
|
2014-09-25 16:02:07 +00:00
|
|
|
# estimate the hashes per second
|
2017-08-24 16:37:02 +00:00
|
|
|
hash_power = float(long(nonce) / elapsed_time)
|
|
|
|
print("Hashing Power: %ld hashes per second" % hash_power)
|