Search in sources :

Example 6 with JsonRpcClient

use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method getBlockHeaders.

@Override
public Map<Integer, String> getBlockHeaders(Transport transport, Wallet wallet, Set<Integer> blockHeights) {
    JsonRpcClient client = new JsonRpcClient(transport);
    Map<Integer, String> result = new LinkedHashMap<>();
    for (Integer blockHeight : blockHeights) {
        EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving block at height " + blockHeight));
        try {
            String blockHeader = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(String.class).method("blockchain.block.header").id(idCounter.incrementAndGet()).params(blockHeight).execute());
            result.put(blockHeight, blockHeader);
        } catch (ServerException e) {
            // If there is an error with the server connection, don't keep trying - this may take too long given many blocks
            throw new ElectrumServerRpcException("Failed to retrieve block header for block height: " + blockHeight, e);
        } catch (JsonRpcException e) {
            log.warn("Failed to retrieve block header for block height: " + blockHeight + " (" + e.getErrorMessage() + ")");
        } catch (Exception e) {
            log.warn("Failed to retrieve block header for block height: " + blockHeight + " (" + e.getMessage() + ")");
        }
    }
    return result;
}
Also used : BigInteger(java.math.BigInteger) WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)

Example 7 with JsonRpcClient

use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method getTransactions.

@Override
public Map<String, String> getTransactions(Transport transport, Wallet wallet, Set<String> txids) {
    JsonRpcClient client = new JsonRpcClient(transport);
    Map<String, String> result = new LinkedHashMap<>();
    for (String txid : txids) {
        EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving transaction [" + txid.substring(0, 6) + "]"));
        try {
            String rawTxHex = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(String.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid).execute());
            result.put(txid, rawTxHex);
        } catch (ServerException e) {
            // If there is an error with the server connection, don't keep trying - this may take too long given many txids
            throw new ElectrumServerRpcException("Failed to retrieve transaction for txid [" + txid.substring(0, 6) + "]", e);
        } catch (Exception e) {
            result.put(txid, Sha256Hash.ZERO_HASH.toString());
        }
    }
    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 8 with JsonRpcClient

use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method ping.

@Override
public void ping(Transport transport) {
    try {
        JsonRpcClient client = new JsonRpcClient(transport);
        new RetryLogic<>(MAX_RETRIES, RETRY_DELAY, 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)

Example 9 with JsonRpcClient

use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method subscribeScriptHashes.

@Override
public Map<String, String> subscribeScriptHashes(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes) {
    JsonRpcClient client = new JsonRpcClient(transport);
    Map<String, String> result = new LinkedHashMap<>();
    for (String path : pathScriptHashes.keySet()) {
        EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Finding transactions for " + path));
        try {
            String scriptHash = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(String.class).method("blockchain.scripthash.subscribe").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).executeNullable());
            result.put(path, scriptHash);
        } catch (Exception 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 path: " + path, e);
        }
    }
    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 10 with JsonRpcClient

use of com.github.arteam.simplejsonrpc.client.JsonRpcClient in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method getFeeEstimates.

@Override
public Map<Integer, Double> getFeeEstimates(Transport transport, List<Integer> targetBlocks) {
    JsonRpcClient client = new JsonRpcClient(transport);
    Map<Integer, Double> result = new LinkedHashMap<>();
    for (Integer targetBlock : targetBlocks) {
        if (targetBlock <= MAX_TARGET_BLOCKS) {
            try {
                Double targetBlocksFeeRateBtcKb = new RetryLogic<Double>(MAX_RETRIES, RETRY_DELAY, IllegalStateException.class).getResult(() -> client.createRequest().returnAs(Double.class).method("blockchain.estimatefee").id(idCounter.incrementAndGet()).params(targetBlock).execute());
                result.put(targetBlock, targetBlocksFeeRateBtcKb);
            } catch (JsonRpcException e) {
                throw new ElectrumServerRpcException("Failed to retrieve fee rate for target blocks: " + targetBlock, e);
            } catch (Exception e) {
                log.warn("Failed to retrieve fee rate for target blocks: " + targetBlock + " (" + e.getMessage() + ")");
                result.put(targetBlock, result.values().stream().mapToDouble(v -> v).min().orElse(0.0001d));
            }
        } else {
            result.put(targetBlock, result.values().stream().mapToDouble(v -> v).min().orElse(0.0001d));
        }
    }
    return result;
}
Also used : BigInteger(java.math.BigInteger) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) Transaction(com.sparrowwallet.drongo.protocol.Transaction) java.util(java.util) Logger(org.slf4j.Logger) Wallet(com.sparrowwallet.drongo.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) Transport(com.github.arteam.simplejsonrpc.client.Transport) AtomicLong(java.util.concurrent.atomic.AtomicLong) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) AppServices(com.sparrowwallet.sparrow.AppServices) Utils(com.sparrowwallet.drongo.Utils) EventManager(com.sparrowwallet.sparrow.EventManager) BigInteger(java.math.BigInteger) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) 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