13

I'm trying to figure out how does simple Bitcoin mining algorithm works in plain simple c or c# or some pseudo language. I've found an example at http://pastebin.com/EXDsRbYH, but unfortunately It isn't clear what it does. I was unable to run it.

Suppose I have only one input: a Bitcoin wallet "abc..." which I would like to be used for the Bitcoins to be mined. I need simple to understand algorithm that will do the bitcoin mining on one machine with one thread on one cpu [I know it will take ages to complete :)]

2 Answers 2

20

The simplest miner would work like this:

  1. Request a new Getwork from a given pool/bitcoind with specified credentials
  2. For nonce=0;nonce<0xFFFFFFFF;nonce++
    • Set Getwork Data nonce
    • SHA-256 hash the Getwork Data (block header)
    • Check if hash result is smaller than Getwork Target, if so, submit a share
    • If more than 1 second passed since you received Getwork, stop the loop
  3. GOTO: 1

Now, if you want something that could rise up to par with currently used miners, you need to consider:

  • OpenCL calculations, without this you will never get the high hashrates of graphic cards.
  • Midstate, or in other words hashing optimization. First half of Getwork Data doesn't change, so no point in calculating midstate for each hash.
  • Longpolling, makes you calculate less stale blocks.
  • Rollntime, minimizes communication with the pool.
2
  • Could you please explain why we need to stop the loop if it took more than 1 second?
    – Rasoul
    Dec 27, 2013 at 15:20
  • @Rasoul The basic getwork requests (in bitcoind) time out after 1 second as far as I remember. This is why we have to stop the loop and get new work every second.
    – ThePiachu
    Dec 28, 2013 at 7:49
2

Read the answers to this question and if you have any specific questions about the mining algorithm perhaps you can edit your question to ask them.

If all you want to do is run a mining program, there are lots available. You can study the source and see what they're doing.

The official bitcoin client itself has a basic CPU miner built in. You can examine the code to get an idea of how mining works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.