Solo CPU Mining with Python | Learn the Basics of Bitcoin Mining | Just for Fun

19 replies 20 views
0xAtlasFull Member
Posts: 175 · Reputation: 722
#1Jan 21, 2018, 04:05 PM
Hey everyone! I stumbled upon this Python-based solo bitcoin miner that uses ckpool, and I thought I'd share it. You can also hook it up to other pools if that's your thing. It’s pretty much like playing the lottery with a tiny chance of winning, but it's a neat proof of concept. I might have seen this somewhere before, but I can't remember where, so I can’t give the original creator a shout-out. Feel free to tweak the code however you like. This is a good starting point for anyone wanting to grasp the basics of bitcoin mining. Setting Up Dependencies Make sure you have Python installed, preferably older versions first. To install dependencies, just run: If you find any missing dependencies, you can easily install them using pip install DependencyName. Editing the Code You’ll need to modify the code to input your Bitcoin address for receiving mining rewards, plus set the pool host and port. Save the modified file with a .py extension. One popular solo pool is solo.ckpool.org, which has a low fee around 2%, so you keep 98% of your rewards. How to Run It To run the code, open a command prompt in the same folder where your file is saved and use the command: Troubleshooting Dependencies and Errors As I mentioned, use pip install to get any missing dependencies. If you run into errors, a quick Google search might help. If you still can’t figure it out, it could be that your Python version is incompatible, in which case you might wanna try a different version. Future Updates If you come up with any improvements to the code, please share them here so everyone can benefit, and I’ll update the original code too. Honorable Mentions for Contributors 1.2.3. Acknowledgment Just wanted to give a nod to the original creator of the code. I’m not the one who made it.
5 Reply Quote Share
ben21Member
Posts: 2 · Reputation: 99
#2Jan 21, 2018, 04:25 PM
how to mine testnet any GPU speed, python mining
2 Reply Quote Share
ben21Member
Posts: 2 · Reputation: 99
#3Jan 23, 2018, 09:42 AM
pip install  error /home/user/Downloads# pip install hashlib /home/user/Downloads# pip install binascii
2 Reply Quote Share
Posts: 34 · Reputation: 220
#4Jan 23, 2018, 12:18 PM
You don't need to install from pip, these modules are from standard python library, which means they come with interpreter. Just import them like this:
6 Reply Quote Share
0xAtlasFull Member
Posts: 175 · Reputation: 722
#5Jan 23, 2018, 03:30 PM
Yeah I guess its from iceland with few modifications. Yes but this code was initially written for Python 2.7 so I described that method. For python3 users can directly import as suggested by @witcher_sense. And usually people don't get missing dependecy errors in Python3 as it comes with multiple pre installed libraries. For python3 pip3 install works fine in many cases.
1 Reply Quote Share
dave.falconFull Member
Posts: 163 · Reputation: 447
#6Jan 23, 2018, 04:25 PM
You say just for fun, I wonder where is the fun if we start mining with our CPU 24/7 and never mine anything, could you provide some probability estimations on a single block found/time?
2 Reply Quote Share
lord21Member
Posts: 3 · Reputation: 111
#7Jan 23, 2018, 04:39 PM
This code is great! Its a good start to what I was looking for. Any intentions on updating it? I'm curious, why does it randomly go through nonces instead of just counting through them by counting up one by one?
2 Reply Quote Share
Posts: 34 · Reputation: 220
#8Jan 23, 2018, 05:59 PM
It may be that I am missing something but I think it is just an implementation detail that doesn't affect the chances of finding a correct hash but makes the function a little bit easier to design and reason about. As you can see from the code, the algorithm performs a predetermined number of operations and then starts over by invoking the main function. If it were incrementing nonce instead of taking a random value, it would need to keep track of the actual value, pass it back to the main function, and reset it after each found block. Using random numbers saves you from having to choose the right moment to relaunch the nonce management algorithm. The only problem I see is that the range of random values is tiny compared to all possible values.
4 Reply Quote Share
LuckyCoinLegendary
Posts: 832 · Reputation: 4795
#9Jan 23, 2018, 08:05 PM
Congratulations on learning how to mine bitcoins! I also appreciate the fact that you're not connecting to some regtest node, but rather, you are using a live mining pool (ckpool, in this case). Have you considered making a guide to connecting a USB miner such as the Compac F or AntMiner U1/2 to a mining pool?
3 Reply Quote Share
RogueMinerFull Member
Posts: 58 · Reputation: 306
#10Jan 23, 2018, 10:14 PM
Yeah, doesn't sound fun... for educational purposes only? I'd prefer to mine some shitcoin using my CPU or GPU but with some real results/mining involved. Solo mining even on an Antminer S19 Pro is a lottery now already - 0.000065% chance of catching the block every 10 minutes (around 30 years to solo mine a block), CPU mining is not even a lottery, you're just losing money on electricity and burning out your CPU.
2 Reply Quote Share
Posts: 4 · Reputation: 79
#11Jan 24, 2018, 12:17 AM
thanks, i also want to practice some code for cpu/gpu bitcoin mining, so i very gladly looked at your work. a question regarding this line of code: hash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(blockheader)).digest()).digest() why are you calling sha256 twice? so you hash the hash of the header. maybe it's a mistake?
2 Reply Quote Share
lord21Member
Posts: 3 · Reputation: 111
#12Jan 24, 2018, 03:16 AM
If you're "burning out" your CPU, you need a better cooling solution .
4 Reply Quote Share
lord21Member
Posts: 3 · Reputation: 111
#13Jan 24, 2018, 06:27 AM
Should be able to implement PyCuda into that code. I'd love to see that myself.  Bitcoin double hashes. That's how it works. When you hash the block and think you're done, you're not. You have to hash it a second time to see if it has the requisite zeros.
0 Reply Quote Share
sage777Full Member
Posts: 102 · Reputation: 305
#14Jan 24, 2018, 12:31 PM
Because Bitcoin works in that way. For example, the latest block hash (at the moment of writing) is calculated in this way:
1 Reply Quote Share
Posts: 4 · Reputation: 79
#15Jan 26, 2018, 11:25 PM
I did so.e investigation and....i Was wrong. Really thanks, this helped me a lot!!
4 Reply Quote Share
Posts: 4 · Reputation: 79
#16Jan 27, 2018, 04:20 AM
I made some small improvements: 1) I called the noncework function 2**32 times, not just 10 million million times 2) every time i called the noncework function, i passed the loop value k, using that number as the nounce and not a random number I could also have used the time parameter, another 5000 or 6000 times cycle, so that the root merkle only resets once every 5000*2**32. In that case, if we had a 100 TH/S asic instead of a CPU, it would have to recalculate the nounce every 4 or 5 seconds, if I'm not mistaken.
3 Reply Quote Share
Posts: 4 · Reputation: 79
#17Jan 27, 2018, 07:52 AM
Another small improvement, the last one I think. I didn't use hashlib to hash the nounce, but I used this 100% python implementation: https://github.com/keanemind/python-sha-256. In this way the code is about 3.5 slower (but that's just to understand, performances aren't important) but you can understand how the hash is encoded. It sure is useful to me, and I will use it as a starting point for my own serious mining algorithm that I am building (https://bitcointalk.org/index.php?topic=5446391.0). I hope  it is useful to others too.
0 Reply Quote Share
Posts: 6 · Reputation: 74
#18Jan 27, 2018, 01:53 PM
I've just fired this up on one core of a i5, running through 10MM hashes in around 56 seconds. That gives me 178,000 hashes/s or 0.178M/h, which presently equates to 65 BILLION years to find a block. That's between 14 and 15 times the age of the Earth, and slightly less than 5 times the age of the universe and everything in it. Still, gotta be in it to win it!
4 Reply Quote Share
HyperRavenFull Member
Posts: 175 · Reputation: 633
#19Jan 29, 2018, 07:48 AM
Looks to be more of a fun project to learn about Bitcoin mining, rather than doing so seriously. It's an easy way to see what is being hashed and being sent over to the pool. This script isn't particularly optimized, don't think it is multithreaded? If you want to do so, then there are more optimized CPU miners to use, but even so you shouldn't expect anything at all.
3 Reply Quote Share
Posts: 6 · Reputation: 74
#20Jan 29, 2018, 11:08 AM
I'm interested to understand why you chose (2**32) as the target for noncework. Given the end-start times when I tried your script, the noncework function takes around 2.5 hours. In that time, it's almost certain that a block would be found and you'd be looking at coinbase etc information from a non-current block. Am I wrong here, or should you be regaining information much sooner? Also, can you explain the advantage of noncework(k) instead of noncework() ? Thanks! I'm trying to optimize the script as a bit of fun, given I'll never mine a block, my goal is to improve my Mh/s figure as much as possible and learn more about the technical background underpinning mining.
4 Reply Quote Share

Related topics