Title: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: irsada on February 09, 2023, 08:20:06 PM Hello Bitcoiners
I want to share a python based solo bitcoin miner which uses ckpool. You can use other pools as well if you want. It is basically like a lottery which has extremly low chances to win but it can be used as a proof of concept. Maybe it has already been shared somewhere and I do not remember its true origin so cannot give proper reference. You can make changes as you want. This code can help users understand true basics of mining bitcoins. Installing Dependencies You must have python. Try with old versions first. Install dependencies with Code: pip install hashlib Code Code: # -*- coding: utf-8 -*- Edit Code You can edit code. Set your Bitcoin address to receive your mining rewards. Set pool host and port. Save code in a .py file. One of popular solo pool is solo.ckpool.org which has low fee of like 2%. You get 98% of your mining reward. Run Code You can run code by opening command prompt in same folder of file and run command Code: python FileName.py Missing Dependencies and errors As decribed above you can use pip install command to install any missing dependency. You can google errors and still if you did not find solution maybe your python version is not compatible. Try changing python version. Future Work If you improve this code then share here so others can benefit as well and I will update this code as well. Honor List for Code Improvers
Tribute I want to pay tribute to original author of this code. I am not original author. Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: cixegz on February 10, 2023, 05:10:00 AM Hello Bitcoiners I want to share a python based solo bitcoin miner which uses ckpool. You can use other pools as well if you want. It is basically like a lottery which has extremly low chances to win but it can be used as a proof of concept. Maybe it has already been shared somewhere and I do not remember its true origin so cannot give proper reference. You can make changes as you want. This code can help users understand true basics of mining bitcoins. Installing Dependencies You must have python. Try with old versions first. Install dependencies with Code: pip install hashlib Code Code: # -*- coding: utf-8 -*- Edit Code You can edit code. Set your Bitcoin address to receive your mining rewards. Set pool host and port. Save code in a .py file. One of popular solo pool is solo.ckpool.org which has low fee of like 2%. You get 98% of your mining reward. Run Code You can run code by opening command prompt in same folder of file and run command Code: python FileName.py Missing Dependencies and errors As decribed above you can use pip install command to install any missing dependency. You can google errors and still if you did not find solution maybe your python version is not compatible. Try changing python version. Future Work If you improve this code then share here so others can benefit as well and I will update this code as well. Honor List for Code Improvers
Tribute I want to pay tribute to original author of this code. I am not original author. how to mine testnet any GPU speed, python mining Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: cixegz on February 10, 2023, 05:20:16 AM pip install error
/home/user/Downloads# pip install hashlib Code: Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ /home/user/Downloads# pip install binascii Code: Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: witcher_sense on February 10, 2023, 09:37:23 AM pip install error You don't need to install from pip, these modules are from standard python library, which means they come with interpreter./home/user/Downloads# pip install hashlib /home/user/Downloads# pip install binascii Just import them like this: Code: import binascii Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: irsada on February 10, 2023, 06:44:34 PM Quote My quick search your code might be based on one of these repository, https://github.com/iceland2k14/solominer (https://github.com/iceland2k14/solominer) https://github.com/Pymmdrza/SoloMiner (https://github.com/Pymmdrza/SoloMiner) Yeah I guess its from iceland with few modifications. Quote This only applies to Python 2, your code is in Python 3. 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. Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: digaran on February 10, 2023, 07:24:12 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?
Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: number435398 on April 04, 2023, 04:43:14 AM 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?
Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: witcher_sense on April 04, 2023, 06:08:46 AM I'm curious, why does it randomly go through nonces instead of just counting through them by counting up one by one? 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.Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: NotATether on April 04, 2023, 07:18:45 AM 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? Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: serveria.com on April 04, 2023, 08:53:55 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? 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. ;D Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: Sha256explorer on April 04, 2023, 09:08:02 PM 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? Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: number435398 on April 04, 2023, 11:56:04 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? 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. ;D If you're "burning out" your CPU, you need a better cooling solution ;). Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: number435398 on April 04, 2023, 11:57:37 PM 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? 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. Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: garlonicon on April 05, 2023, 04:44:47 AM Quote why are you calling sha256 twice? so you hash the hash of the header. Because Bitcoin works in that way. For example, the latest block hash (at the moment of writing) is calculated in this way:Code: SHA-256(00000020ceee9013d84c76a7e5a980df9dec537f313a3259da2d02000000000000000000b90235771c6c90b9d13e4b3670b489cb0fadc6bcbf610f4d0ead0c1d1c8d068deaf22c643e020617cb4b5c51)=88b46b0817fd187eba000740d5b727fd961bcb8df91fb4a7564610a4fe2fc76c Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: Sha256explorer on April 05, 2023, 06:53:11 AM Quote why are you calling sha256 twice? so you hash the hash of the header. Because Bitcoin works in that way. For example, the latest block hash (at the moment of writing) is calculated in this way:Code: SHA-256(00000020ceee9013d84c76a7e5a980df9dec537f313a3259da2d02000000000000000000b90235771c6c90b9d13e4b3670b489cb0fadc6bcbf610f4d0ead0c1d1c8d068deaf22c643e020617cb4b5c51)=88b46b0817fd187eba000740d5b727fd961bcb8df91fb4a7564610a4fe2fc76c Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: Sha256explorer on April 20, 2023, 08:56:20 PM 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. Code: # -*- coding: utf-8 -*- Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: Sha256explorer on April 22, 2023, 07:37:01 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. Code: # -*- coding: utf-8 -*- Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: vshoes on February 26, 2024, 04:11:59 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? 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! Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: ranochigo on February 27, 2024, 03:13:22 AM 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? 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. Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: vshoes on February 29, 2024, 11:37:02 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. Code: # omitted for brevity 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. Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: vshoes on March 07, 2024, 12:32:46 PM One more q for anyone who might be able to answer;
Code: if hash < target : Why are we evaluating hash < target? Shouldn't that be Code: if hash == target: Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: Flavatron on March 07, 2024, 01:05:29 PM One more q for anyone who might be able to answer; Code: if hash < target : Why are we evaluating hash < target? Shouldn't that be Code: if hash == target: No, it should be < the target ( not equal to it). When mining Bitcoin , the goal is to find a hash that is not equal to the target(near impossible), but less than the target value(probalistic). The target is a 256-bit number that the network adjusts every 2016 blocks to ensure that the time between blocks remains approximately 10 minutes on average. A lower target means a higher difficulty in finding a valid block. The hashing process is essentially a race to find a nonce value that, when combined with the block's header data(which itself contains the Merkle root of the previous blocks tx) and passed through the SHA-256 hashing algorithm twice, produces a hash value that is less than the network-defined target, which everyone can easily verify for themselves too. Therefore the condition, in the educational example, if hash < target: is correct because you are looking for a hash that is numerically less than the target. Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: vshoes on March 08, 2024, 09:56:49 AM No, it should be < the target ( not equal to it). When mining Bitcoin , the goal is to find a hash that is not equal to the target(near impossible), but less than the target value(probalistic). The target is a 256-bit number that the network adjusts every 2016 blocks to ensure that the time between blocks remains approximately 10 minutes on average. A lower target means a higher difficulty in finding a valid block. The hashing process is essentially a race to find a nonce value that, when combined with the block's header data(which itself contains the Merkle root of the previous blocks tx) and passed through the SHA-256 hashing algorithm twice, produces a hash value that is less than the network-defined target, which everyone can easily verify for themselves too. Therefore the condition, in the educational example, if hash < target: is correct because you are looking for a hash that is numerically less than the target. Thank you so much! I have just had to destroy my entire (wrong) understanding of how mining works, and remake it again from scratch! Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: rostep on April 09, 2024, 11:48:34 PM Helo,
In the script, during the loop on the nonce, merkle_root and ctx.ntime keep the same value. But, I discover that, during this loop, while continue listening socket, ctx.job_id, ctx.coinb1, ctx.coinb2, ctx.ntime, and ctx.merkle_branch change the value. So, during the loop we have an very old value of merkle_root and ctx.ntime which are not updated. Do you think that, we have to always listening the socket during the loop and update the value of merkle_root and ctx.ntime, according to what we receive? Title: Re: Python based Solo miner for CPU | Learn Basic Bitcoin Mining | Just for fun Post by: Nexus9090 on April 11, 2024, 03:37:48 PM I find this thread really quite interesting.
I've been wondering what the payloads look like going back and forth. This simple script goes a long way to explaining it, thank you. I may even give it a go, just to see how slow it is ;) Thanks G. |