I programmed a simple SHA256 blockchain mining algorithm in python using hashlib. What I am trying to do here is changing a random number in my data and then calculating the SHA256 hash. Whenever the hash has the predefined amount of zeroes in the beginning, it prints the hash and its corresponding nonce to the console. The goal is to have 11 zeroes in the beginning.
import hashlib
import threading
#bn=block number d=input data, ph=previous hash
bn = 1
d = "mydata"
ph = "0000000000000000000000000000000000000000000000000000000000000000"
class MyThread(threading.Thread):
def __init__(self, threadID, begin, end):
threading.Thread.__init__(self)
self.threadID = threadID
self.end = end
self.begin = begin
def run(self):
for val in range(self.begin, self.end):
mine(self.threadID, val)
print("done " + str(self.threadID))
#hashing function
def mine(id, x):
hash = hashlib.sha256((str(bn) + str(x) + str(d) + str(ph)).encode('utf-8')).hexdigest()
if hash.find("0000000", 0, 7) > -1:
# print(id)
print(x)
print(hash)
#now break it up and distribute it to the threads
#Possible SHA256 Combinations 8388608000000
pc = 8388608000000
#Number of desired Threads (more threads help more?)
nt = 704
#calculating the steps
step = round((pc)/(nt))
#starting the threads with each thread calculating a different range of nonces.
for x in range (1, nt):
thread = MyThread((x), (x-1)*(step), (x)*(step))
thread.start()
How can I speed this thing up?
Currently, I run this on an Intel 8250u quadcore and I can get hashes with 7 zeroes in the beginning comfortably. However, 8 zeroes already take 5 hours. Funnily, only around 20-30% of my CPU is used. How can I make it run on all cores with threads running in parallel?
Do you have another Idea how I can speed this thing up? I have better hardware (Threadripper 1920) available but I'm afraid that alone will not give me a 300x - 4000x (best case) speed increase...
My first thought was outsourcing this to the GPU since I know that Bitcoin Mining evolved from CPU to GPU. When looking into Numba I saw that it does not support Hashlib. I also saw that compiling this using Pypy might speed it up? What approach would you guys favour? I'm confused...
Thanks for being patient with me