【问题标题】:Calculating Bitcoin mining difficulty计算比特币挖矿难度
【发布时间】:2022-01-24 16:22:45
【问题描述】:

我正在做一个项目,要求我获得比特币的实时挖掘难度。 因此,我在此页面上阅读了该页面,该页面解释了如何从块的哈希中获取挖掘难度: https://en.bitcoin.it/wiki/Difficulty

所以我制作了这个 python 脚本,它在两个日期之间从 Blockchain api 收集所有哈希值。(https://www.blockchain.com/api) 并根据哈希值计算挖掘难度。

但是,当我绘制结果时,我发现与我在网上看到的所有其他挖矿难度都完全不同。正如您在此处看到的那样,采矿难度真的很混乱:

x=时间,y=难度

这是当我将 np.log 应用于难度时: x= 时间,y = np.log(难度)

如您所见,结果确实很混乱。 所以我想知道是否有一位加密专家能够判断我的公式代码有什么问题(或者也许我是对的):)

这是我的代码:

import requests, json
from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

start = "2021-01-01"#The script start to collect hashes from this date
end= "2021-12-01"#And end at this one

timestamp_start = datetime.strptime(start, "%Y-%m-%d").timestamp()
timestamp_end = datetime.strptime(end, "%Y-%m-%d").timestamp()
new_start = timestamp_start

datas = pd.DataFrame([], columns = ["time", "difficulty"])

dec_max_diff = int("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16)#maximum target that is used to calculate mining difficulty

while True:

    if new_start > timestamp_end:
        break

    
    url = f"https://blockchain.info/blocks/{int(new_start*1000)}?format=json"
    response = requests.get(url)#Make a request to the API
    list_block_adress = json.loads(response.text)#Get a json containing the hash and a timestamp
    

    for block_adress in list_block_adress:

        dec_hash = int(block_adress["hash"], 16)

        difficulty = dec_max_diff / dec_hash #Formula to calculate mining difficulty

        data = pd.DataFrame([[block_adress["time"], difficulty]], columns = ["time", "difficulty"])

        datas = pd.concat([datas, data])

    new_start += 60*60*24 #For the loop to continue


#Sorting and cleaning up the datas
datas.sort_values(by='time', inplace= True)
datas.drop_duplicates(subset='time', keep="first")

#Ploting the datas
times = pd.to_datetime(datas["time"], unit= "ms").to_numpy()
difficulties= datas["difficulty"].apply(lambda x: np.log(x)).to_numpy()
plt.plot(times, difficulties)
plt.show()

【问题讨论】:

  • 呃,不是加密专家,只是被两张图的差异弄糊涂了。 top 是别人的输出,bottom 是你的代码的输出?老实说,它们看起来非常相似(请参阅相同位置的主要尖峰具有相同的总体形状)。底部的图有更多的噪音,差异可能是缩放或过滤的东西?
  • 第一个是我绘制 x= 时间和 y= 难度,而第二个是绘制 x= 时间和 y = ln( 难度) :)
  • 你能附上预期结果的图吗?

标签: python blockchain bitcoin


【解决方案1】:

由于哈希实际上是随机的,每个位都是独立的,因此除了 n 工作证明需要 0 之外,还有一些 以下位也为 0。 (即使当时的 n 更高,这些哈希值也是有效的,但这无关紧要。)这里的“动态范围”,相对于噪声的下限,可以作为所需难度,似乎涵盖了大约 e10=22000,这很容易适合开采的区块数。

(实际的 n 不是整数,因为目标不是 2 的幂,但这给出了正确的想法。)

【讨论】:

    【解决方案2】:

    我不是加密专家,但难度是使用区块头中的bits 字段计算的,而不是区块地址哈希。所以你要获取区块头查询https://blockchain.info/rawblock/<block_hash>(见https://www.blockchain.com/api/blockchain_api),解压bits的内容,计算current_target和当前难度。

    仅在height // 2016 == 0 的块中重新计算块难度。 因此,无需查询两个日期之间生成的每个块,而只需查询每个 2016 年 + 2 次。

    另外,请注意list_block_adress 包含time 降序

    【讨论】:

    • 您好,谢谢您的回答!我的问题来自 hash 和 target 之间的误解!你现在说清楚了。 PS:在我看来,你现在看起来像个加密专家;)
    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2020-08-08
    • 2016-03-21
    • 2020-03-04
    • 1970-01-01
    相关资源
    最近更新 更多