Miner-Configuration

無法在java中執行getwork邏輯

  • January 22, 2018

我在堆棧交換中瀏覽了這篇文章如何用 Java 編寫比特幣 JSON-RPC “getwork”請求?

我試圖為 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);

}

當我執行此程式碼時,出現錯誤

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)

我在這裡想念什麼?是否需要在機器上執行地層代理?如果是這樣,我如何指定要在 java 程式碼中執行的參數?

我也嘗試了與伺服器的直接 TCP 連接。

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());

}

但又沒有運氣了:(

getworkRPC 呼叫已從 Bitcoin Core 中刪除。它已被棄用,然後被getblocktemplateRPC 呼叫取代。


看起來您正在使用的池使用未指定的協議。這只是一個 tcp 連接,您在其中發送 JSON 格式的字元串並從池伺服器接收 JSON 格式的字元串。我通過探勘這個探勘軟體的原始碼發現了這一點。

最後我找到了要傳遞給池的正確字元串。

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";

之後,我們可以先使用 authorizemessage 進行授權,然後通過 socket 的 OutputStream 傳遞 message1。

引用自:https://bitcoin.stackexchange.com/questions/69010