use of com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException in project sparrow by sparrowwallet.
the class BatchedElectrumServerRpc method getBlockHeaders.
@Override
@SuppressWarnings("unchecked")
public Map<Integer, String> getBlockHeaders(Transport transport, Wallet wallet, Set<Integer> blockHeights) {
PagedBatchRequestBuilder<Integer, String> batchRequest = PagedBatchRequestBuilder.create(transport, idCounter).keysType(Integer.class).returnType(String.class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving " + blockHeights.size() + " block headers"));
for (Integer height : blockHeights) {
batchRequest.add(height, "blockchain.block.header", height);
}
try {
return batchRequest.execute();
} catch (JsonRpcBatchException e) {
return (Map<Integer, String>) e.getSuccesses();
} catch (Exception e) {
throw new ElectrumServerRpcException("Failed to retrieve block headers for block heights: " + blockHeights, e);
}
}
use of com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException in project sparrow by sparrowwallet.
the class BatchedElectrumServerRpc method getTransactions.
@Override
@SuppressWarnings("unchecked")
public Map<String, String> getTransactions(Transport transport, Wallet wallet, Set<String> txids) {
PagedBatchRequestBuilder<String, String> batchRequest = PagedBatchRequestBuilder.create(transport, idCounter).keysType(String.class).returnType(String.class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving " + txids.size() + " transactions"));
for (String txid : txids) {
batchRequest.add(txid, "blockchain.transaction.get", txid);
}
try {
return batchRequest.execute();
} catch (JsonRpcBatchException e) {
Map<String, String> result = (Map<String, String>) e.getSuccesses();
String strErrorTx = Sha256Hash.ZERO_HASH.toString();
for (Object hash : e.getErrors().keySet()) {
String txhash = (String) hash;
result.put(txhash, strErrorTx);
}
return result;
} catch (Exception e) {
throw new ElectrumServerRpcException("Failed to retrieve transactions for txids: " + txids.stream().map(txid -> "[" + txid.substring(0, 6) + "]").collect(Collectors.toList()), e);
}
}
use of com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException in project sparrow by sparrowwallet.
the class BatchedElectrumServerRpc method getScriptHashHistory.
@Override
@SuppressWarnings("unchecked")
public Map<String, ScriptHashTx[]> getScriptHashHistory(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes, boolean failOnError) {
PagedBatchRequestBuilder<String, ScriptHashTx[]> batchRequest = PagedBatchRequestBuilder.create(transport, idCounter).keysType(String.class).returnType(ScriptHashTx[].class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Loading transactions for " + nodeRangesToString(pathScriptHashes.keySet())));
for (String path : pathScriptHashes.keySet()) {
batchRequest.add(path, "blockchain.scripthash.get_history", pathScriptHashes.get(path));
}
try {
return batchRequest.execute();
} catch (JsonRpcBatchException e) {
if (failOnError) {
throw new ElectrumServerRpcException("Failed to retrieve transaction history for paths: " + nodeRangesToString((Collection<String>) e.getErrors().keySet()), e);
}
Map<String, ScriptHashTx[]> result = (Map<String, ScriptHashTx[]>) e.getSuccesses();
for (Object key : e.getErrors().keySet()) {
result.put((String) key, new ScriptHashTx[] { ScriptHashTx.ERROR_TX });
}
return result;
} catch (Exception e) {
throw new ElectrumServerRpcException("Failed to retrieve transaction history for paths: " + nodeRangesToString(pathScriptHashes.keySet()), e);
}
}
use of com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException in project sparrow by sparrowwallet.
the class BatchedElectrumServerRpc method subscribeScriptHashes.
@Override
@SuppressWarnings("unchecked")
public Map<String, String> subscribeScriptHashes(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes) {
PagedBatchRequestBuilder<String, String> batchRequest = PagedBatchRequestBuilder.create(transport, idCounter).keysType(String.class).returnType(String.class);
EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Finding transactions for " + nodeRangesToString(pathScriptHashes.keySet())));
for (String path : pathScriptHashes.keySet()) {
batchRequest.add(path, "blockchain.scripthash.subscribe", pathScriptHashes.get(path));
}
try {
return batchRequest.execute();
} catch (JsonRpcBatchException e) {
// Even if we have some successes, failure to subscribe for all script hashes will result in outdated wallet view. Don't proceed.
throw new ElectrumServerRpcException("Failed to subscribe to paths: " + nodeRangesToString((Collection<String>) e.getErrors().keySet()), e);
} catch (Exception e) {
throw new ElectrumServerRpcException("Failed to subscribe to paths: " + nodeRangesToString(pathScriptHashes.keySet()), e);
}
}
Aggregations