简体   繁体   English

我们如何证明比特币区块总是可以解决的?

[英]How can we prove that a bitcoin block is always solvable?

I'm trying to implement a simple cryptocurrency similar to bitcoin, just to understand it deeply down to the code level. 我试图实现一种类似于比特币的简单加密货币,只是为了深入了解代码级别。

I understand that a bitcoin block contains a hash of the previous block, many transactions and an reward transaction for the solver. 我了解到,比特币区块包含前一个区块的哈希,许多交易以及求解程序的奖励交易。

the miner basically runs SHA256 on this candidate block combined with an random number. 矿工基本上在结合了随机数的候选区块上运行SHA256。 As long as the first certain digits of a hash result are zeros, we say this block is solved, and we broadcast the result to the entire network to claim the reward. 只要哈希结果的前几个数字为零,我们就说该区块已解决,然后将结果广播到整个网络以要求奖励。

but I have never seen anyone proving that a block is solvable at all. 但我从未见过有人证明一个块完全可以解决。 I guess this is guaranteed by SHA256? 我想这是SHA256保证的吗? because the solution size is fixed, after trying enough inputs, you are guaranteed to hit every hash result? 因为解决方案的大小是固定的,所以在尝试了足够的输入之后,您是否保证会击中每个哈希结果? but how can you prove that the solution distribution of a block is even (uniform), so that you can indeed cover all hash results? 但是如何证明一个块的解决方案分布是均匀的(均匀的),从而可以覆盖所有哈希结果呢?

now, suppose a block is indeed always solvable, can I assume that using 64bit for the random integer is enough to solve it? 现在,假设一个块确实总是可以解决的,我可以假设对随机整数使用64位就足以解决它吗? how about 32bit? 32bit怎么样? or I have to use an infinite bit integer? 还是我必须使用无限位整数?

for example, in the basiccoin project: 例如,在basiccoin项目中:

the code for proof of work is the following: 工作证明的代码如下:

    def POW(block, hashes):
    halfHash = tools.det_hash(block)
    block[u'nonce'] = random.randint(0, 10000000000000000000000000000000000000000)
    count = 0
    while tools.det_hash({u'nonce': block['nonce'],
                          u'halfHash': halfHash}) > block['target']:
        count += 1
        block[u'nonce'] += 1
        if count > hashes:
            return {'error': False}
        if restart_signal.is_set():
            restart_signal.clear()
            return {'solution_found': True}
        ''' for testing sudden loss in hashpower from miners.
        if block[u'length']>150:
        else: time.sleep(0.01)
        '''
    return block

this code randoms a number between [0, 10000000000000000000000000000000000000000] as a start point, and then it just increases the value one by one: 这段代码将[0,10000000000000000000000000000000000000000000000]之间的数字作为起点随机分配,然后将值逐个递增:

block[u'nonce'] += 1

I'm not a python programmer, I don't know how python handles the type of the integer. 我不是python程序员,我不知道python如何处理整数类型。 there is no handling of integer overflow. 没有整数溢出的处理。

I'm trying to implement similar thing with c++, I don't know what kind of integer can guarantee a solution. 我正在尝试用c ++实现类似的事情,我不知道哪种整数可以保证解决方案。

but how can you prove that the solution distribution of a block is even (uniform), so that you can indeed cover all hash results? 但是如何证明一个块的解决方案分布是均匀的(均匀的),从而可以覆盖所有哈希结果呢?

SHA256 is deterministic so if you rehash the txns it will always provide the same 256 hash. SHA256是确定性的,因此,如果重新哈希txns,它将始终提供相同的256哈希。 The client nodes keep all the txn and the hashes in the merkle tree for the network clients to propagate and verify the longest possible block chain. 客户端节点将所有txn和哈希保留在merkle树中,以供网络客户端传播并验证最长的可能区块链。

The merkle tree is the essential data structure for recording the hashes of previous blocks. merkle树是用于记录先前块的哈希值的基本数据结构。 From there the chain of hash confirmations can be tracked from the origin (genesis) block. 从那里可以源(生成)块跟踪哈希确认链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM