如何用Python实现区块链的PoW共识机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计408个文字,预计阅读时间需要2分钟。
原始实现(Python 2 版本):pythonimport hashlibimport time
def proof_of_work(target_difficulty): prefix='0' * target_difficulty nonce=0 while True: hash_result=hashlib.sha256(str(nonce).encode()).hexdigest() if hash_result.startswith(prefix): return nonce nonce +=1 time.sleep(0.01)
target_difficulty=5nonce=proof_of_work(target_difficulty)print(Found nonce:, nonce)
改进后的代码(基于 Python 3 特性):pythonimport hashlibimport time
def proof_of_work(target_difficulty): prefix='0' * target_difficulty nonce=0 while True: hash_result=hashlib.sha256(str(nonce).encode()).hexdigest() if hash_result.startswith(prefix): return nonce nonce +=1 time.sleep(0.01)
target_difficulty=5nonce=proof_of_work(target_difficulty)print(Found nonce:, nonce)
原始实现(python2 版本)
github.com/santisiri/proof-of-work
依据python3特性改进后:
#!/usr/bin/env python# example of proof of work algorithm
import hashlib
import time
max_nonce = 2 ** 32 # 4 billion
def proof_of_work(header, difficulty_bits):
target = 2 ** (256-difficulty_bits)
for nonce in range(max_nonce):
hash_result = hashlib.sha256((str(header)+str(nonce)).encode("utf-8")).hexdigest()
if int(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 = ''
for difficulty_bits in range(32):
difficulty = 2 ** difficulty_bits
print( "" )
print( "Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits) )
print( "Starting search..." )
start_time = time.time()
new_block = 'test block with transactions' + hash_result
(hash_result, nonce) = proof_of_work(new_block, difficulty_bits)
end_time = time.time()
elapsed_time = end_time - start_time
print( "Elapsed time: %.4f seconds" % elapsed_time )
if elapsed_time > 0:
hash_power = float(int(nonce)/elapsed_time)
print( "Hashing power: %ld hashes per second" % hash_power )
运行结果:
注:
具体原理不知。
参考:
简述目前相对成熟的区块链共识算法
本文共计408个文字,预计阅读时间需要2分钟。
原始实现(Python 2 版本):pythonimport hashlibimport time
def proof_of_work(target_difficulty): prefix='0' * target_difficulty nonce=0 while True: hash_result=hashlib.sha256(str(nonce).encode()).hexdigest() if hash_result.startswith(prefix): return nonce nonce +=1 time.sleep(0.01)
target_difficulty=5nonce=proof_of_work(target_difficulty)print(Found nonce:, nonce)
改进后的代码(基于 Python 3 特性):pythonimport hashlibimport time
def proof_of_work(target_difficulty): prefix='0' * target_difficulty nonce=0 while True: hash_result=hashlib.sha256(str(nonce).encode()).hexdigest() if hash_result.startswith(prefix): return nonce nonce +=1 time.sleep(0.01)
target_difficulty=5nonce=proof_of_work(target_difficulty)print(Found nonce:, nonce)
原始实现(python2 版本)
github.com/santisiri/proof-of-work
依据python3特性改进后:
#!/usr/bin/env python# example of proof of work algorithm
import hashlib
import time
max_nonce = 2 ** 32 # 4 billion
def proof_of_work(header, difficulty_bits):
target = 2 ** (256-difficulty_bits)
for nonce in range(max_nonce):
hash_result = hashlib.sha256((str(header)+str(nonce)).encode("utf-8")).hexdigest()
if int(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 = ''
for difficulty_bits in range(32):
difficulty = 2 ** difficulty_bits
print( "" )
print( "Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits) )
print( "Starting search..." )
start_time = time.time()
new_block = 'test block with transactions' + hash_result
(hash_result, nonce) = proof_of_work(new_block, difficulty_bits)
end_time = time.time()
elapsed_time = end_time - start_time
print( "Elapsed time: %.4f seconds" % elapsed_time )
if elapsed_time > 0:
hash_power = float(int(nonce)/elapsed_time)
print( "Hashing power: %ld hashes per second" % hash_power )
运行结果:
注:
具体原理不知。
参考:

