Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stratum connection for bitcoin pool mining

Tags:

java

json

bitcoin

I'm programming a Bitcoin miner that mines in a pool using the stratum protocol (see documentation here.

The stratum protocol uses JSON-RPC 2.0 as it's encoding and according to the JSON-RPC 2.0 specification(specification here) I should use sockets to create a connection to the pool.

My problem is that I don't seem to be able to receive a response back from the pool. JSON-RPC 2.0 states that for every Request object that I send, I must receive a response back.

Here is my code:

public static void main(String[] args) 
{
    connectToPool("stratum.slushpool.com", 3333);
}    
static void connectToPool(String host, int port)
{
    try
    {
        InetAddress address = InetAddress.getByName(host);
        out.println("Atempting to connect to " + address.toString() + " on port " + port + ".");

        socket = new Socket(address, port);
        String message1 = "{\"jsonrpc\" : \"2.0\", \"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}";

        PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        output.write((message1 + "\\n"));
        out.println(input.readLine());//Hangs here.
    }
    catch (IOException e) 
    {
        out.println(e.getMessage());
        out.println("Error. Can't connect to Pool.");
        System.exit(-2);
    }
}
like image 218
acedogblast Avatar asked Oct 17 '22 13:10

acedogblast


1 Answers

After hours of tinkering around I have found the solution. Apparently the JSON string shouldn't have any spaces. So instead of:

String message1 = "{\"jsonrpc\" : \"2.0\", \"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}";

It should be:

String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
like image 66
acedogblast Avatar answered Oct 21 '22 02:10

acedogblast