mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2025-06-18 14:08:52 +00:00
long(), print(), xrange() for Python 3
Also remove trailing whitespace
This commit is contained in:
parent
3c0b24edd7
commit
35fef9a94f
@ -4,38 +4,42 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import time
|
import time
|
||||||
|
|
||||||
max_nonce = 2 ** 32 # 4 billion
|
try:
|
||||||
|
long # Python 2
|
||||||
|
xrange
|
||||||
|
except NameError:
|
||||||
|
long = int # Python 3
|
||||||
|
xrange = range
|
||||||
|
|
||||||
|
max_nonce = 2 ** 32 # 4 billion
|
||||||
|
|
||||||
|
|
||||||
def proof_of_work(header, difficulty_bits):
|
def proof_of_work(header, difficulty_bits):
|
||||||
|
|
||||||
# calculate the difficulty target
|
# calculate the difficulty target
|
||||||
target = 2 ** (256-difficulty_bits)
|
target = 2 ** (256 - difficulty_bits)
|
||||||
|
|
||||||
for nonce in xrange(max_nonce):
|
for nonce in xrange(max_nonce):
|
||||||
hash_result = hashlib.sha256(str(header)+str(nonce)).hexdigest()
|
hash_result = hashlib.sha256(str(header) + str(nonce)).hexdigest()
|
||||||
|
|
||||||
# check if this is a valid result, below the target
|
# check if this is a valid result, below the target
|
||||||
if long(hash_result, 16) < target:
|
if long(hash_result, 16) < target:
|
||||||
print "Success with nonce %d" % nonce
|
print("Success with nonce %d" % nonce)
|
||||||
print "Hash is %s" % hash_result
|
print("Hash is %s" % hash_result)
|
||||||
return (hash_result,nonce)
|
return (hash_result, nonce)
|
||||||
|
|
||||||
print "Failed after %d (max_nonce) tries" % nonce
|
print("Failed after %d (max_nonce) tries" % nonce)
|
||||||
return nonce
|
return nonce
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
nonce = 0
|
nonce = 0
|
||||||
hash_result = ''
|
hash_result = ''
|
||||||
|
|
||||||
# difficulty from 0 to 31 bits
|
# difficulty from 0 to 31 bits
|
||||||
for difficulty_bits in xrange(32):
|
for difficulty_bits in xrange(32):
|
||||||
|
|
||||||
difficulty = 2 ** difficulty_bits
|
difficulty = 2 ** difficulty_bits
|
||||||
print "Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits)
|
print("Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits))
|
||||||
|
print("Starting search...")
|
||||||
print "Starting search..."
|
|
||||||
|
|
||||||
# checkpoint the current time
|
# checkpoint the current time
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
@ -51,13 +55,10 @@ if __name__ == '__main__':
|
|||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
|
|
||||||
elapsed_time = end_time - start_time
|
elapsed_time = end_time - start_time
|
||||||
print "Elapsed Time: %.4f seconds" % elapsed_time
|
print("Elapsed Time: %.4f seconds" % elapsed_time)
|
||||||
|
|
||||||
if elapsed_time > 0:
|
if elapsed_time > 0:
|
||||||
|
|
||||||
# estimate the hashes per second
|
# estimate the hashes per second
|
||||||
hash_power = float(long(nonce)/elapsed_time)
|
hash_power = float(long(nonce) / elapsed_time)
|
||||||
print "Hashing Power: %ld hashes per second" % hash_power
|
print("Hashing Power: %ld hashes per second" % hash_power)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user