@Override public void onSuccess(ResultMessage result) { try { BlockHeader header = parseBlockHeader(type, result.getResult().getJSONObject(0)); listener.onBlockUpdate(header); } catch (JSONException e) { log.error("Unexpected JSON format", e); } }
public static ResultMessage fromJson(String json) throws JSONException { return new ResultMessage(json); }
public JSONArray getResult() { if (has("result")) { if (opt("result") instanceof JSONArray) { try { return getJSONArray("result"); } catch (JSONException e) { // Should not happen throw new RuntimeException(e); } } else { JSONArray result = new JSONArray(); try { result.put(get("result")); } catch (JSONException e) { // Should not happen throw new RuntimeException(e); } return result; } } else { return new JSONArray(); } } }
@Test public void testCallCommand() throws Exception { client.startAsync(); client.awaitRunning(5, TimeUnit.SECONDS); CallMessage call = new CallMessage("blockchain.address.get_history", Arrays.asList("mrx4EmF6zHXky3zDoeJ1K7KvYcuNn8Mmc4")); final ListenableFuture<ResultMessage> futureReply = client.call(call); // Check if server got the correct message Assert.assertEquals(call.toString(), new String(serverInput.toByteArray())); // Reply to the client String resultJson = "{\"id\": 0, \"result\": [{" + "\"tx_hash\": \"3aa2a5a9825ca767e092bcc19487aa13969eeb217fd0fba8492543bbb8c30954\", " + "\"height\": 260144}]}"; final ResultMessage serverResult = ResultMessage.fromJson(resultJson); serverResult.setId(call.getId()); serverOutput.write(serverResult.toString().getBytes()); ResultMessage result = futureReply.get(3, TimeUnit.SECONDS); Assert.assertEquals(serverResult.toString(), result.toString()); serverOutput.close(); }
@Test public void testResultMessage() throws JSONException { String resultString = "{\"id\": 1, \"result\": [{" + "\"tx_hash\": \"3aa2a5a9825ca767e092bcc19487aa13969eeb217fd0fba8492543bbb8c30954\", " + "\"height\": 260144}]}"; ResultMessage result = ResultMessage.fromJson(resultString); Assert.assertEquals(result.getId(), 1L); Assert.assertTrue(result.getResult().length() > 0); Assert.assertEquals(result.getResult().getJSONObject(0).getString("tx_hash"), "3aa2a5a9825ca767e092bcc19487aa13969eeb217fd0fba8492543bbb8c30954"); Assert.assertEquals(result.getResult().getJSONObject(0).getInt("height"), 260144); } }
private void handle(BaseMessage message) { if (message instanceof ResultMessage) { ResultMessage reply = (ResultMessage) message; if (callers.containsKey(reply.getId())) { SettableFuture<ResultMessage> future = callers.get(reply.getId()); future.set(reply); callers.remove(reply.getId()); } else { log.error("Received reply from server, but could not find caller", new MessageException("Orphaned reply", reply.toString()));
reply = ResultMessage.fromJson(serverMessage); } else if (reply.isCall()) { reply = CallMessage.fromJson(serverMessage);
@Override public void onSuccess(ResultMessage result) { try { BlockHeader header = parseBlockHeader(type, result.getResult().getJSONObject(0)); listener.onNewBlock(header); } catch (JSONException e) { log.error("Unexpected JSON format", e); } }
@Override public void onSuccess(@Nullable ResultMessage result) { if (log.isDebugEnabled()) { try { log.debug("Server {} version {} OK", type.getName(), checkNotNull(result).getResult().get(0)); } catch (Exception ignore) { } } }
@Override public void onSuccess(ResultMessage result) { JSONArray resTxs = result.getResult(); ImmutableList.Builder<HistoryTx> historyTxs = ImmutableList.builder(); try { for (int i = 0; i < resTxs.length(); i++) { historyTxs.add(new HistoryTx(resTxs.getJSONObject(i))); } } catch (JSONException e) { onFailure(e); return; } listener.onTransactionHistory(status, historyTxs.build()); }
@Override public void onSuccess(ResultMessage result) { JSONArray resTxs = result.getResult(); ImmutableList.Builder<UnspentTx> utxes = ImmutableList.builder(); try { for (int i = 0; i < resTxs.length(); i++) { utxes.add(new UnspentTx(resTxs.getJSONObject(i))); } } catch (JSONException e) { onFailure(e); return; } listener.onUnspentTransactionUpdate(status, utxes.build()); }
@Override public void onSuccess(ResultMessage result) { AddressStatus status = null; try { if (result.getResult().isNull(0)) { status = new AddressStatus(address, null); } else { status = new AddressStatus(address, result.getResult().getString(0)); } listener.onAddressStatusUpdate(status); } catch (JSONException e) { log.error("Unexpected JSON format", e); } }
@Override public void onSuccess(ResultMessage result) { try { String txId = result.getResult().getString(0); // FIXME could return {u'message': u'', u'code': -25} log.info("got tx {} =?= {}", txId, tx.getHash()); checkState(tx.getHash().toString().equals(txId)); if (listener != null) listener.onTransactionBroadcast(tx); } catch (Exception e) { onFailure(e); } }
@Override public boolean broadcastTxSync(final BitTransaction tx) { checkNotNull(stratumClient); CallMessage message = new CallMessage("blockchain.transaction.broadcast", Arrays.asList(Utils.HEX.encode(tx.bitcoinSerialize()))); try { ResultMessage result = stratumClient.call(message).get(); String txId = result.getResult().getString(0); // FIXME could return {u'message': u'', u'code': -25} log.info("got tx {} =?= {}", txId, tx.getHash()); checkState(tx.getHash().toString().equals(txId)); return true; } catch (Exception e) { log.error("Could not get reply for blockchain.transaction.broadcast", e); } return false; }
@Override public void onSuccess(ResultMessage result) { try { String rawTx = result.getResult().getString(0); byte[] txBytes = Utils.HEX.decode(rawTx); BitTransaction tx = new BitTransaction(type, txBytes); if (!tx.getHash().equals(txHash)) { throw new Exception("Requested TX " + txHash + " but got " + tx.getHashAsString()); } listener.onTransactionUpdate(tx); if (cacheDir != null) { try { Files.write(txBytes, getTxCacheFile(txHash)); } catch (IOException e) { log.warn("Error writing cached transaction", e); } } } catch (Exception e) { onFailure(e); } }