Python Solo Miner for Fun and Learning

19 replies 22 views
ben2015Member
Posts: 6 · Reputation: 181
#1Nov 22, 2023, 08:54 AM
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.
6 Reply Quote Share
madguruHero Member
Posts: 2 · Reputation: 2372
#2Nov 24, 2023, 08:57 AM
How do you even mine on testnet with this? I mean, can it work with any GPU?
4 Reply Quote Share
madguruHero Member
Posts: 2 · Reputation: 2372
#3Nov 24, 2023, 09:26 AM
I tried installing via pip but got errors /home/user/Downloads# pip install hashlib /home/user/Downloads# pip install binascii
3 Reply Quote Share
0xHashMember
Posts: 162 · Reputation: 210
#4Nov 24, 2023, 11:06 AM
No need for pip here, those modules are standard in Python. Just import them directly.
2 Reply Quote Share
ben2015Member
Posts: 6 · Reputation: 181
#5Nov 24, 2023, 03:46 PM
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.
3 Reply Quote Share
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?
2 Reply Quote Share
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.
6 Reply Quote Share
0xHashMember
Posts: 162 · Reputation: 210
#8Nov 27, 2023, 01:27 AM
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.
3 Reply Quote Share
0xNodeMember
Posts: 307 · Reputation: 80
#9Nov 27, 2023, 04:18 AM
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?
3 Reply Quote Share
0xSigmaHero Member
Posts: 36 · Reputation: 2072
#10Nov 27, 2023, 09:01 AM
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.
6 Reply Quote Share
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?
6 Reply Quote Share
Posts: 3 · Reputation: 172
#12Nov 27, 2023, 06:45 PM
If your CPU is burning out, you might need better cooling!
6 Reply Quote Share
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.
4 Reply Quote Share
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.
2 Reply Quote Share
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!
1 Reply Quote Share
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.
4 Reply Quote Share
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.
2 Reply Quote Share
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!
4 Reply Quote Share
cobra51Newbie
Posts: 52 · Reputation: 31
#19Nov 28, 2023, 03:09 PM
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?
1 Reply Quote Share
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()?
2 Reply Quote Share

Related topics