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 :(

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.

  • I tried that. no luck either. :( by the way I am connecting to a monero pool URL if you see. – user1223879 Jan 20 '18 at 8:00
  • You have to use whatever protocol the pool is using. They probably use stratum, not getwork or getblocktemplate. – Andrew Chow Jan 20 '18 at 16:49
  • looks like they use getblocktemplate from this link : getmonero.org/resources/developer-guides/daemon-rpc.html – user1223879 Jan 20 '18 at 17:11
  • 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 Jan 20 '18 at 17:17
  • ok. let me try to use stratum and update the post. Thanks for your suggestions:) – user1223879 Jan 20 '18 at 17:39
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.

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.