5

I'm not entirely sure whether to ask this question on Bitcoin-SE or Stack Overflow, but I decided to post it here due to the direct relevancy of the question to Bitcoin and the general prominence of programming experience I have observed on Bitcoin-SE. If Bitcoin-SE is the incorrect place to post it, I apologize in advance; I'm new to SE and this was my best guess.

I'm attempting, in order to better understand how Bitcoin mining works, to code a simple Bitcoin CPU miner in Java, which I have some but not much experience with. All the online resources on JSON-RPC implementation in Java I have found have either been out of date and no longer functional or incomprehensible for someone with limited Java experience. How, in the most simplistic way possible, would I code a JSON-RPC getwork request (to a pool server) in Java and correctly parse the output into the variables required for mining?

  • +1 This is the right forum to ask. (And bitcoinj would be a good place to implement this feature, not sure if they have it already). – Thilo Mar 15 '12 at 8:15
  • @Thilo Thanks, glad I posted it in the right place. BitcoinJ implements a client-only node; it won't process RPC requests of any sort. Good idea, but I'm afraid it won't do the job. – BinaryMage Mar 16 '12 at 1:15
  • It won't right now, but (at least) for an RPC client it should offer support one day (IMHO). – Thilo Mar 16 '12 at 2:00
  • Sorry if the "client-only node" was misleading; that's just what's stated on their Google Code page. I looked through the documentation and found nothing whatsoever on RPC. I agree it should implement RPC client support, but that doesn't mean it will. In any case, it doesn't currently. – BinaryMage Mar 16 '12 at 2:07
  • Yes, we can agree on that ;-) Please update this post with an answer of your own if you find out how to do RPC with Java (even if you have to hand-roll it with just the JDK libs). – Thilo Mar 16 '12 at 2:25
3

The JSON-RPC protocol is very simple. You POST to the mining pool (or the bitcoind client):

{"method": "getwork", "params": [], "id": "anything"}

and get back:

{"result": {"data": "hex bytes....", "target": "hex bytes...", ...}, ... }

You just need the first 80 bytes from result.data - you change the last 4 bytes (nonce) and compute the hashes. Then, when you find a hash that is less than "target" (generally a hash with the high order 32 bits equal to zero), you call getwork again to report your hash (and earn a share in the pool):

{"method": "getwork", "params": ["hex-header"], "id": "anything"}

There a bunch on wonky byte-order encoding in the standard getwork call. You'll generally want to reverse the bytes is each long integer (4 bytes) before hashing it, and do the same before reporting your found block back to the pool. See the sample pyminer for a simple reference implementation of a miner (in python). Inside the Miner.work() function is where the magic happens. The work function takes two parameters: datastr, this is the value stored inside result.data and targetstr this is the value stored inside result.target.

One other thing - most pools require that you use Basic Authentication when you POST, so you'll need to include your username and password in the header of your request.

1

For everyone else, who stumble upon this question :

I've written a Java wrapper around the JSON-RPC provided by bitcoin/litecoin.

https://github.com/coding-idiot/Litecoin-Bitcoin-RPC-Java-Connector

It uses Htmlunit & GoogleGson and quite easy to understand and extend.

  • Looks good. Will be testing it – marscom Apr 19 '14 at 23:39
1

Take a look at the DiabloMiner source code (Java): https://github.com/Diablo-D3/DiabloMiner

It's a GPU miner, but it has the JSON-RPC getwork stuff you need.

After you get basic getwork going you may want to look at implementing extensions: https://en.bitcoin.it/wiki/Getwork

  • I have looked Diablo's code over; I was looking for something easier to understand. (Diablo doesn't comment at all, and I have little Java experience) If no one else pipes up, I'll go over that more closely and try to simplify it myself. Thanks for the suggestion in any case. – BinaryMage Mar 17 '12 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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