【发布时间】:2022-01-24 16:22:45
【问题描述】:
我正在做一个项目,要求我获得比特币的实时挖掘难度。 因此,我在此页面上阅读了该页面,该页面解释了如何从块的哈希中获取挖掘难度: https://en.bitcoin.it/wiki/Difficulty
所以我制作了这个 python 脚本,它在两个日期之间从 Blockchain api 收集所有哈希值。(https://www.blockchain.com/api) 并根据哈希值计算挖掘难度。
但是,当我绘制结果时,我发现与我在网上看到的所有其他挖矿难度都完全不同。正如您在此处看到的那样,采矿难度真的很混乱:
这是当我将 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