简单的比特币挖掘算法

本文关键字:算法 简单 | 更新日期: 2023-09-27 18:26:09

我正试图弄清楚简单的比特币挖掘算法是如何在简单的c或c#或一些伪语言中工作的。我在http://pastebin.com/EXDsRbYH,但不幸的是,它的作用还不清楚。我无法运行它。

假设我只有一个输入:一个比特币钱包"abc…",我想用它来开采比特币。我需要一个简单易懂的算法,它将在一台机器上用一个线程在一个cpu上进行比特币挖掘[我知道它需要很长时间才能完成:)]

简单的比特币挖掘算法

超级愚蠢,相当无用,但我做过一次演示:

from hashlib import md5
from random import random
import sys
# what to hash
data = "Bitcoins!"
# This is just a first run to init the variables 
h = md5(data.encode('utf-8'))
v = h.digest()
best = v
best_i = data
best_vhex = h.hexdigest()
# x ist just a helper to only display
# a subset of all updates (calculates faster)
x = 0
step = 100
# In reality, this loop stops when the "h" hash
# is below a certain threshold (called "difficulty")
while True:
  i = data + str(random())
  h = md5(i.encode('utf-8'))
  v = h.digest()
  vhex = h.hexdigest()
  # log progress
  if v < best or x > step:
    msg = "%-25s | %-25s -> %s" % (i, best_i, best_vhex)
    sys.stdout.write(''r' + msg)
    x = 0
  else:
    x += 1
  # check if new best one
  if v < best:
    best_i, best, best_vhex = i, v, vhex
    print