Unable to execute getwork logic in java

1

I was going through this post in stack exchange How can I code a Bitcoin JSON-RPC “getwork” request in Java?

I tried to write a simple snippet for just the getwork json rpc.

public static void main(String[] args) throws Exception {

    String request = "{\"method\": \"getwork\", \"params\": [], \"id\":0}";
    URL url = new URL("http://de01.supportxmr.com:7777");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    if (conn .getConnectTimeout() == 0)
        conn.setConnectTimeout(1000);
    if (conn.getReadTimeout() == 0)
        conn.setReadTimeout(1000);
    conn.setRequestMethod("POST");
    String encoded = Base64.getEncoder().encodeToString(("<my_wallet_addr>:x").getBytes(StandardCharsets.UTF_8));  //Java 8
    conn.setRequestProperty("Authorization", "Basic "+encoded);
    conn.setRequestProperty("Accept", "application/json");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Content-Length", Integer.toString(request.getBytes().length));
    conn.setRequestProperty("X-Mining-Extensions", "midstate");
    conn.setAllowUserInteraction(false);
    conn.setUseCaches(false);
    conn.setDoOutput(true);

    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(request);
    wr.close();

    InputStream is = conn.getInputStream();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;
    byte[] buffer = new byte[4096];
    while ((len = is.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
    String content = bos.toString();
    is.close();

    System.out.println(content);

}

when I run this code, I get an error

Exception in thread "main" java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:789)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at org.litecoinpool.miner.Test.main(Test.java:42)

What am I missing here? Is stratum proxy necessary to be running on the machine? If so how do I specify the parameters to run in the java code?

I tried a direct TCP connection also to the server.

public static void main(String[] args) throws Exception {
    String message1 = "{\"id\":1,\"method\":\"mining.subscribe\",\"params\":[]}";
    String authorizemessage = "{\"params\": [\"<wallet_address>\", \"x\"], \"id\": 2, \"method\": \"mining.authorize\"}";


    Socket soc = new Socket("de01.supportxmr.com", 7777);
    System.out.println("connected");
    OutputStream outputStream = soc.getOutputStream();
    outputStream.write(authorizemessage.getBytes());
    outputStream.flush();

    BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    JSONObject json = new JSONObject(in.readLine());

    System.out.println("json response: " + json.toString());

    outputStream.write(message1.getBytes());
    outputStream.flush();

    in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    json = new JSONObject(in.readLine());

    System.out.println("json response: " + json.toString());

}

but no luck again :(

user1223879

Posted 2018-01-19T18:29:03.877

Reputation: 11

Answers

3

The getwork RPC call was removed from Bitcoin Core. It was deprecated and then superceded by the getblocktemplate RPC call.


It looks like the pool that you are using uses an unspecified protocol. It's just a tcp connection where you are sending JSON formatted strings and receiving JSON formatted strings from the pool server. I figured this out by digging through this mining software's source code.

Andrew Chow

Posted 2018-01-19T18:29:03.877

Reputation: 40 910

I tried that. no luck either. :( by the way I am connecting to a monero pool URL if you see.user1223879 2018-01-20T08:00:06.233

You have to use whatever protocol the pool is using. They probably use stratum, not getwork or getblocktemplate.Andrew Chow 2018-01-20T16:49:59.257

looks like they use getblocktemplate from this link : https://getmonero.org/resources/developer-guides/daemon-rpc.html

user1223879 2018-01-20T17:11:58.253

That's not necessarily true. That link points to the official monero node software documentation. But that is not the same as the pool that you are using. They don't necessarily have that exposed to miners to connect to, they may (probably) use stratum which is run by a software external to the node.Andrew Chow 2018-01-20T17:17:39.860

ok. let me try to use stratum and update the post. Thanks for your suggestions:)user1223879 2018-01-20T17:39:02.463

Thanks again for your analysis. Even i suspected this and I tried direct connection but could not read anything from the input stream. Please check the edited question section for the tcp code at the end.user1223879 2018-01-21T16:20:01.287

0

Finally I found the right string to be passed to the pool.

String message1 = "{\"id\":1,\"method\":\"getblocktemplate\",\"params\":[\"wallet_address\":\"<wallet_address>\", \"reserve_size\" : 60]}\n";

String authorizemessage = "{\"params\":{\"login\":\"<wallet_address>\", \"pass\":\"x\"}, \"id\": 2, \"method\": \"login\"}\n";

After that we can first authorize using the authorizemessage and later pass message1 through the OutputStream of the socket.

user1223879

Posted 2018-01-19T18:29:03.877

Reputation: 11