Search in sources :

Example 1 with JsonRpcClient

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);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BigInteger(java.math.BigInteger) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)

Example 2 with JsonRpcClient

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);
    }
}
Also used : JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)

Example 3 with JsonRpcClient

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;
}
Also used : JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)

Example 4 with JsonRpcClient

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;
}
Also used : WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)

Example 5 with JsonRpcClient

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;
}
Also used : Transaction(com.sparrowwallet.drongo.protocol.Transaction) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)

Aggregations

JsonRpcClient (com.github.arteam.simplejsonrpc.client.JsonRpcClient)11 JsonRpcException (com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)11 WalletHistoryStatusEvent (com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent)5 BigInteger (java.math.BigInteger)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 JsonRpcBatchException (com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)2 Transaction (com.sparrowwallet.drongo.protocol.Transaction)2 Transport (com.github.arteam.simplejsonrpc.client.Transport)1 Utils (com.sparrowwallet.drongo.Utils)1 Sha256Hash (com.sparrowwallet.drongo.protocol.Sha256Hash)1 Wallet (com.sparrowwallet.drongo.wallet.Wallet)1 AppServices (com.sparrowwallet.sparrow.AppServices)1 EventManager (com.sparrowwallet.sparrow.EventManager)1 java.util (java.util)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1