Search in sources :

Example 1 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent 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);
    }
}
Also used : BigInteger(java.math.BigInteger) WalletNode.nodeRangesToString(com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException) WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)

Example 2 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent 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);
    }
}
Also used : WalletNode.nodeRangesToString(com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException) WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)

Example 3 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent 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 4 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent 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 5 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent 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);
    }
}
Also used : WalletNode.nodeRangesToString(com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException) WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)

Aggregations

JsonRpcException (com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)8 WalletHistoryStatusEvent (com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent)8 JsonRpcClient (com.github.arteam.simplejsonrpc.client.JsonRpcClient)4 JsonRpcBatchException (com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)4 WalletNode.nodeRangesToString (com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString)4 BigInteger (java.math.BigInteger)2