use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.
the class BatchedElectrumServerRpc method getFeeRateHistogram.
@Override
public Map<Long, Long> getFeeRateHistogram(Transport transport) {
try {
JsonRpcClient client = new JsonRpcClient(transport);
BigInteger[][] feesArray = new RetryLogic<BigInteger[][]>(DEFAULT_MAX_ATTEMPTS, RETRY_DELAY_SECS, IllegalStateException.class).getResult(() -> client.createRequest().returnAs(BigInteger[][].class).method("mempool.get_fee_histogram").id(idCounter.incrementAndGet()).execute());
Map<Long, Long> feeRateHistogram = new TreeMap<>();
for (BigInteger[] feePair : feesArray) {
if (feePair[0].longValue() > 0) {
feeRateHistogram.put(feePair[0].longValue(), feePair[1].longValue());
}
}
return feeRateHistogram;
} catch (Exception e) {
throw new ElectrumServerRpcException("Error getting fee rate histogram", e);
}
}
use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.
the class BatchedElectrumServerRpc method ping.
@Override
public void ping(Transport transport) {
try {
JsonRpcClient client = new JsonRpcClient(transport);
new RetryLogic<>(DEFAULT_MAX_ATTEMPTS, RETRY_DELAY_SECS, IllegalStateException.class).getResult(() -> client.createRequest().method("server.ping").id(idCounter.incrementAndGet()).executeNullable());
} catch (Exception e) {
throw new ElectrumServerRpcException("Error pinging server", e);
}
}
use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.
the class SimpleElectrumServerRpc method getScriptHashMempool.
@Override
public Map<String, ScriptHashTx[]> getScriptHashMempool(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes, boolean failOnError) {
JsonRpcClient client = new JsonRpcClient(transport);
Map<String, ScriptHashTx[]> result = new LinkedHashMap<>();
for (String path : pathScriptHashes.keySet()) {
try {
ScriptHashTx[] scriptHashTxes = new RetryLogic<ScriptHashTx[]>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(ScriptHashTx[].class).method("blockchain.scripthash.get_mempool").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).execute());
result.put(path, scriptHashTxes);
} catch (Exception e) {
if (failOnError) {
throw new ElectrumServerRpcException("Failed to retrieve mempool transactions for path: " + path, e);
}
result.put(path, new ScriptHashTx[] { ScriptHashTx.ERROR_TX });
}
}
return result;
}
use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.
the class SimpleElectrumServerRpc method getScriptHashHistory.
@Override
public Map<String, ScriptHashTx[]> getScriptHashHistory(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes, boolean failOnError) {
JsonRpcClient client = new JsonRpcClient(transport);
Map<String, ScriptHashTx[]> result = new LinkedHashMap<>();
for (String path : pathScriptHashes.keySet()) {
EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Loading transactions for " + path));
try {
ScriptHashTx[] scriptHashTxes = new RetryLogic<ScriptHashTx[]>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(ScriptHashTx[].class).method("blockchain.scripthash.get_history").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).execute());
result.put(path, scriptHashTxes);
} catch (Exception e) {
if (failOnError) {
throw new ElectrumServerRpcException("Failed to retrieve transaction history for path: " + path, e);
}
result.put(path, new ScriptHashTx[] { ScriptHashTx.ERROR_TX });
}
}
return result;
}
use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.
the class SimpleElectrumServerRpc method getVerboseTransactions.
@Override
public Map<String, VerboseTransaction> getVerboseTransactions(Transport transport, Set<String> txids, String scriptHash) {
JsonRpcClient client = new JsonRpcClient(transport);
Map<String, VerboseTransaction> result = new LinkedHashMap<>();
for (String txid : txids) {
try {
// The server may return an error if the transaction has not yet been broadcasted - this is a valid state so only try once
VerboseTransaction verboseTransaction = new RetryLogic<VerboseTransaction>(1, RETRY_DELAY, IllegalStateException.class).getResult(() -> client.createRequest().returnAs(VerboseTransaction.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid, true).execute());
result.put(txid, verboseTransaction);
} catch (Exception e) {
// electrs-esplora does not currently support the verbose parameter, so try to fetch an incomplete VerboseTransaction without it
// Note that without the script hash associated with the transaction, we can't get a block height as there is no way in the Electrum RPC protocol to do this
// We mark this VerboseTransaction as incomplete by assigning it a Sha256Hash.ZERO_HASH blockhash
log.debug("Error retrieving transaction: " + txid + " (" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()) + ")");
try {
String rawTxHex = client.createRequest().returnAs(String.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid).execute();
Transaction tx = new Transaction(Utils.hexToBytes(rawTxHex));
String id = tx.getTxId().toString();
int height = 0;
if (scriptHash != null) {
ScriptHashTx[] scriptHashTxes = client.createRequest().returnAs(ScriptHashTx[].class).method("blockchain.scripthash.get_history").id(idCounter.incrementAndGet()).params(scriptHash).execute();
for (ScriptHashTx scriptHashTx : scriptHashTxes) {
if (scriptHashTx.tx_hash.equals(id)) {
height = scriptHashTx.height;
break;
}
}
}
VerboseTransaction verboseTransaction = new VerboseTransaction();
verboseTransaction.txid = id;
verboseTransaction.hex = rawTxHex;
verboseTransaction.confirmations = (height <= 0 ? 0 : AppServices.getCurrentBlockHeight() - height + 1);
verboseTransaction.blockhash = Sha256Hash.ZERO_HASH.toString();
result.put(txid, verboseTransaction);
} catch (Exception ex) {
// ignore
}
}
}
return result;
}
Aggregations