@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); } }
@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<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) { 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) { 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); } }
@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); } }