Bitcoin Forum
April 27, 2024, 04:50:11 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Writing a Miner?  (Read 7007 times)
Reikoku (OP)
Full Member
***
Offline Offline

Activity: 140
Merit: 100


firstbits: 1kwc1p


View Profile
June 30, 2011, 03:36:44 AM
 #1

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?

Rei | 1Kwc1pqv54jCg8jvnm3Gu1dqFQYhS34Bow
Trades So Far: 7
1714193411
Hero Member
*
Offline Offline

Posts: 1714193411

View Profile Personal Message (Offline)

Ignore
1714193411
Reply with quote  #2

1714193411
Report to moderator
"You Asked For Change, We Gave You Coins" -- casascius
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714193411
Hero Member
*
Offline Offline

Posts: 1714193411

View Profile Personal Message (Offline)

Ignore
1714193411
Reply with quote  #2

1714193411
Report to moderator
Zagitta
Full Member
***
Offline Offline

Activity: 302
Merit: 100


Presale is live!


View Profile
June 30, 2011, 06:44:28 PM
 #2

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 Smiley but you might be able to find something on the wiki



fcmatt
Legendary
*
Offline Offline

Activity: 2072
Merit: 1001


View Profile
June 30, 2011, 10:15:42 PM
 #3

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.
Nirth
Newbie
*
Offline Offline

Activity: 15
Merit: 0


View Profile
June 30, 2011, 10:58:34 PM
 #4

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.
Reikoku (OP)
Full Member
***
Offline Offline

Activity: 140
Merit: 100


firstbits: 1kwc1p


View Profile
July 01, 2011, 07:49:45 AM
 #5

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.

Rei | 1Kwc1pqv54jCg8jvnm3Gu1dqFQYhS34Bow
Trades So Far: 7
antares
Hero Member
*****
Offline Offline

Activity: 518
Merit: 500


View Profile
July 01, 2011, 10:02:02 AM
 #6

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
N4rk0
Newbie
*
Offline Offline

Activity: 27
Merit: 0


View Profile
July 01, 2011, 04:39:17 PM
 #7

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.
pwnyboy
Full Member
***
Offline Offline

Activity: 125
Merit: 100


View Profile
July 01, 2011, 05:23:33 PM
 #8

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?
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!