Hey everyone
Just stumbled upon this Python solo miner for Bitcoin that uses ckpool. It's kinda like a lottery with super low odds, but it's cool for learning. Can't remember where I found it, so no proper credits. Feel free to tweak the code as you like to grasp the basics of Bitcoin mining.
Python Solo Miner for Fun and Learning
19 replies 22 views
How do you even mine on testnet with this? I mean, can it work with any GPU?
I tried installing via pip but got errors
/home/user/Downloads# pip install hashlib
/home/user/Downloads# pip install binascii
No need for pip here, those modules are standard in Python. Just import them directly.
Sounds like it was modified from an Icelandic version or something. But yeah, it's for Python 2.7. Python 3 users can just import as @witcher_sense said. And usually, Python 3 has less dependency issues.
pixel_stakeSenior Member
Posts: 7 · Reputation: 1132
#6Nov 24, 2023, 09:31 PM
You say it’s just for fun, but what’s the fun in mining 24/7 and not hitting anything? Any chance of giving estimates on how long it takes to find a block?
diamond_alphaMember
Posts: 3 · Reputation: 172
#7Nov 25, 2023, 02:15 AM
This code is awesome! Exactly what I was looking for. Any plans to update it? I'm curious why it jumps through nonces instead of just counting up.
I think it's just a design choice, makes the function easier to handle. The code runs a set number of times then resets. If it counted up, it would need to track that nonce, which complicates things.
Congrats on diving into Bitcoin mining!
And it’s cool you’re using a real pool instead of just a regtest node. Ever thought about making a guide for connecting USB miners like AntMiner?
Yeah, it’s not exactly thrilling... more of an educational thing? I’d rather mine some altcoin where results are real. Solo mining on an Antminer these days is a total gamble.
LuckyWalletFull Member
Posts: 4 · Reputation: 627
#11Nov 27, 2023, 05:14 PM
Thanks! I wanna practice coding for CPU/GPU mining too, so this is super helpful.
Quick question: why double hash here?
hash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(blockheader)).digest()).digest()
Is that a mistake?
diamond_alphaMember
Posts: 3 · Reputation: 172
#12Nov 27, 2023, 06:45 PM
If your CPU is burning out, you might need better cooling!
diamond_alphaMember
Posts: 3 · Reputation: 172
#13Nov 27, 2023, 06:57 PM
You could add PyCuda to this code. Would love to see that!
Bitcoin uses double hashes, that’s just how it works. You hash once, then hash again to check for those leading zeros.
diamondhandsHero Member
Posts: 98 · Reputation: 2112
#14Nov 27, 2023, 10:25 PM
Exactly, that’s just Bitcoin's protocol. For instance, the last block hash is calculated like that.
LuckyWalletFull Member
Posts: 4 · Reputation: 627
#15Nov 28, 2023, 03:20 AM
I checked it out and... I was wrong! Thanks for clearing that up!
LuckyWalletFull Member
Posts: 4 · Reputation: 627
#16Nov 28, 2023, 08:44 AM
Made a few tweaks:
1) I called noncework 2**32 times instead of just 10 million.
2) Used the loop value as the nonce instead of random numbers.
Could also integrate time to reset the root merkle less frequently.
LuckyWalletFull Member
Posts: 4 · Reputation: 627
#17Nov 28, 2023, 10:29 AM
Another small tweak: I switched from hashlib to a pure Python implementation of SHA-256. Slower by about 3.5 times, but it helps understand the encoding better. Planning to use it as a base for my mining algorithm.
diamond2020Hero Member
Posts: 4 · Reputation: 2268
#18Nov 28, 2023, 02:28 PM
Ran this on one core of an i5 and cranked out 10 million hashes in about 56 seconds. That’s 178k hashes/s, which equals 65 billion years to find a block. That’s like 14 times the Earth’s age!
Seems like more of a fun project for learning than serious mining. It’s a good way to see what’s being hashed and sent to the pool. This script isn’t really optimized, right? Doesn’t seem multithreaded?
diamond2020Hero Member
Posts: 4 · Reputation: 2268
#20Nov 28, 2023, 06:45 PM
I’m curious why you picked (2**32) for noncework. In my tests, it takes around 2.5 hours, by then a block might be found already. Shouldn’t you be getting info sooner?
Also, what’s the benefit of noncework(k) over noncework()?