(转)《精通比特币》原码分析: pow机制

https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/proof-of-work-example.py

下面代码可以运行:

#!/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):

    # calculate the difficulty target
    target = 2 ** (256-difficulty_bits)

    for nonce in range(max_nonce):
        encode_str = (str(header)+str(nonce)).encode('utf-8')
        hash_result = hashlib.sha256(encode_str).hexdigest()
        # check if this is a valid result, below the target
        if int(hash_result, 16) < target: # 16进制字符串转成10进制int
            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 range(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(nonce/elapsed_time)
            print("Hashing Power: %ld hashes per second" % hash_power)

你可能感兴趣的:(区块链,python)