Bitcoin Forum

Bitcoin => Mining => Topic started by: Reikoku on June 30, 2011, 03:36:44 AM



Title: Writing a Miner?
Post by: Reikoku on June 30, 2011, 03:36:44 AM
I'm somewhat interested in writing a miner but I have no idea where to start.

I know a few programming languages (Assembly, C++, Perl) and I'm reading into CAL, but I don't actually know where to get information on fetching, processing and returning work.

Can anyone help me out with some sources?


Title: Re: Writing a Miner?
Post by: Zagitta on June 30, 2011, 06:44:28 PM
I can't help you with source code as i program in neither of those languages however requesting work is done over http with JSON-RPC (doesn't change wether it's a pool or the client) and looks like this in case of bitcoin:
Code:
{ "method": "getwork", "params":"", "id": 1}
(getting a json parser for the language you're using is probably the best...) the returned workdata looks like this:

Code:
{"result":{"midstate":"276da6622459d87a45e17e325d31cb8e75966485efb352b539e34aa6a6d6d3af","data":"0000000199cb04e9191f97d409dc884076c468f900934960fe2adcaf000001f100000000cb88f7deb414cbc7b766b3d16e124a72db61127f035b02ae72b7d0bc3798a6d84e0cc2931a0c2a1200000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000","hash1":"00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000","target":"0000000000000000000000000000000000000000000000122a0c000000000000"},"error":null,"id":null}

Long poll protocol looks like this: https://deepbit.net/longpolling.php


Returning the calculated work looks like before except you have replaced data with what you've calculated.

this might be useful: https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29

As for the hashing, no idea :) but you might be able to find something on the wiki




Title: Re: Writing a Miner?
Post by: fcmatt on June 30, 2011, 10:15:42 PM
if it was me.. i would just look at a current miner and make an attempt to get it to perform 3% better (or more).
then share the result here for tips at first.. then release for all once the tips fade away.


Title: Re: Writing a Miner?
Post by: Nirth on June 30, 2011, 10:58:34 PM
3% performance increase is not important. You rather make it as simple as the rest of them but preferably with unique features. For example, an in built hide function or perhaps a merge function with the bitcoin application that you could make a portable version of as well. A statistics section like those on pools that could show estimate of earnings and such for multiple mining set-ups and perhaps over a time frame.


Title: Re: Writing a Miner?
Post by: Reikoku on July 01, 2011, 07:49:45 AM
It's the actual hashing process which I can't find very much about. I'm surprised there isn't a good page on the wiki.


Title: Re: Writing a Miner?
Post by: antares on July 01, 2011, 10:02:02 AM
as far as I remember it's SHA256(SHA256(data))

See This, as it explains a lot and has some reference implementation attached in C:
https://en.bitcoin.it/wiki/Block_hashing_algorithm


Title: Re: Writing a Miner?
Post by: N4rk0 on July 01, 2011, 04:39:17 PM
The miner needs to send a POST http request to the server containing the authentication info and a json getwork struct and get the result like described by Zagitta.
   The data field is a int[32] (big endian ) , the first 20 integers are the actual block header that needs to be hased.
There are  other 12 ints because it's the first step of the sha256 algo(pseudocode here http://en.wikipedia.org/wiki/SHA-2 ). The midstate contains the a,b,c,d,e,f,g,h , registers of the sha256 transform after processing the first 512 bits chunk of data. Notice that for processing the first chunk , the nonce isn't needed, because it is contained in data[19] (last int of the actual header) that's why is handful to have them , because you can calculate the hashes starting from that point (actually you can go some step further because the nonce is in position 19, the processing of the fist chunk finish on 15 , you can process positions 16 17 18 , and start each calculation from there) . Your goal is to find a nonce to be inserted in header[19] for which sha256(sha256(header ) ) has a specified (by the difficulty ) number of final zero bits.
After finding the nonce you need to put it into the data[] field of the json sent by the server , put this data[] in a string , build a json getwork request , add to it a params array, put the string in the arra , send the json to the server. It will rely with an other json whose result field will be true if the solution was accepted.

I just finished writing my miner in c#.
I found really confusing  the endianess of integers for the sha256 transform. The ints in data[] are represented as big endian hex strings. If you parse the string '000000ff'(big endia) it will return the int 0xff , that's written in memory as 0xff000000 (if you use windows or linux) BUT when you perform the arithmetical operations on that number , the operators work as that is a big endina number. So for instance if you have the number int num = 0x1 , it's written in memory as  0x80000000 , but if you write int res = num << 1 , you get res = 2, becase << operate on the big endian representation.  This means that before doing your calculations you need to reverse the bytes of each int of the data array, because the sha256 transform requires to operate on little endian integers.


Title: Re: Writing a Miner?
Post by: pwnyboy on July 01, 2011, 05:23:33 PM
You can find a good overview of the hashing algorithm here:

https://en.bitcoin.it/wiki/Block_hashing_algorithm

What N4rk0 said above was also good advice; I too found the endian changes a bit hard to grapple.

To the original poster, CAL is interesting, and I can't help but think it might speed things up a few percentage points over Phoenix/phatk.  Will you be open-sourcing your miner?