# 工作量证明算法

区块链中一种工作量证明算法
#!/usr/bin/env python
# example of proof-of-work algorithm
import hashlib
import time
max_nonce = 2 ** 32 # 4 billion
# calculate the difficulty target
target = 2 ** (256-difficulty_bits)
for nonce in xrange(max_nonce):
# check if this is a valid result, below the target
if long(hash_result, 16) < target:
print "Success with nonce %d" % nonce
print "Hash is %s" % hash_result
return (hash_result,nonce)
print "Failed after %d (max_nonce) tries" % nonce
return nonce
if __name__ == '__main__':
nonce = 0
hash_result = ''
# difficulty from 0 to 31 bits
for difficulty_bits in xrange(32):
difficulty = 2 ** difficulty_bits
print "Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits)
print "Starting search..."
# checkpoint the current time
start_time = time.time()
# make a new block which includes the hash from the previous block
# we fake a block of transactions - just a string
new_block = 'test block with transactions' + hash_result
# find a valid nonce for the new block
(hash_result, nonce) = proof_of_work(new_block, difficulty_bits)
# checkpoint how long it took to find a result
end_time = time.time()
elapsed_time = end_time - start_time
print "Elapsed Time: %.4f seconds" % elapsed_time
if elapsed_time > 0:
# estimate the hashes per second
hash_power = float(long(nonce)/elapsed_time)
print "Hashing Power: %ld hashes per second" % hash_power


• 本文已收录于以下专栏：

## 工作量证明算法

[python] view plain copy   区块链中一种工作量证明算法   [python] view plain copy   #!/usr/bin/env...

## IFPUG工作量算法总结.pdf

• 2011年03月27日 16:34
• 142KB
• 下载

## 不当工作的奴隶：如何与上司沟通工作量太大

1. Take the first step to approach your boss and discuss your problem. Collect facts on your current...
• artwebs
• 2012年09月17日 09:33
• 808

举报原因： 您举报文章：工作量证明算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)