From 546076ea480e31d2a60bf2064e2d614eb192e3ea Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 30 Aug 2020 13:29:42 +0200 Subject: [PATCH] avoid displaying any server response in error dialogs --- .../sparrowwallet/sparrow/AppController.java | 3 +- .../sparrow/net/SimpleElectrumServerRpc.java | 50 +++++++++++-------- .../transaction/HeadersController.java | 2 +- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index f730bacd..629960c1 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -485,7 +485,8 @@ public class AppController implements Initializable { transactionReferenceService.setOnFailed(failEvent -> { Platform.runLater(() -> { Throwable e = failEvent.getSource().getException(); - showErrorDialog("Error fetching transaction", e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); + log.error("Error fetching transaction " + txId.toString(), e); + showErrorDialog("Error fetching transaction", "The server returned an error when fetching the transaction. The server response is contained in sparrow.log"); }); }); transactionReferenceService.start(); diff --git a/src/main/java/com/sparrowwallet/sparrow/net/SimpleElectrumServerRpc.java b/src/main/java/com/sparrowwallet/sparrow/net/SimpleElectrumServerRpc.java index 252b5614..6c3d442c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/SimpleElectrumServerRpc.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/SimpleElectrumServerRpc.java @@ -167,31 +167,37 @@ public class SimpleElectrumServerRpc implements ElectrumServerRpc { try { VerboseTransaction verboseTransaction = client.createRequest().returnAs(VerboseTransaction.class).method("blockchain.transaction.get").id(txid).params(txid, true).execute(); result.put(txid, verboseTransaction); - } catch(IllegalStateException | IllegalArgumentException e) { - log.warn("Error retrieving transaction: " + txid + " (" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()) + ")"); - String rawTxHex = client.createRequest().returnAs(String.class).method("blockchain.transaction.get").id(txid).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(id).params(scriptHash).execute(); - for(ScriptHashTx scriptHashTx : scriptHashTxes) { - if(scriptHashTx.tx_hash.equals(id)) { - height = scriptHashTx.height; - break; + } catch(Exception e) { + //electrs 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(txid).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(id).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 : AppController.getCurrentBlockHeight() - height + 1); - verboseTransaction.blockhash = Sha256Hash.ZERO_HASH.toString(); - result.put(txid, verboseTransaction); - } catch(JsonRpcException e) { - log.warn("Error retrieving transaction: " + txid + " (" + e.getErrorMessage() + ")"); + VerboseTransaction verboseTransaction = new VerboseTransaction(); + verboseTransaction.txid = id; + verboseTransaction.hex = rawTxHex; + verboseTransaction.confirmations = (height <= 0 ? 0 : AppController.getCurrentBlockHeight() - height + 1); + verboseTransaction.blockhash = Sha256Hash.ZERO_HASH.toString(); + result.put(txid, verboseTransaction); + } catch(Exception ex) { + throw new ElectrumServerRpcException("Error retrieving transaction: ", ex); + } } } diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index 4d5c2f26..24d132b3 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -749,7 +749,7 @@ public class HeadersController extends TransactionFormController implements Init broadcastTransactionService.setOnFailed(workerStateEvent -> { broadcastProgressBar.setProgress(0); log.error("Error broadcasting transaction", workerStateEvent.getSource().getException()); - AppController.showErrorDialog("Error broadcasting transaction", workerStateEvent.getSource().getException().getMessage()); + AppController.showErrorDialog("Error broadcasting transaction", "The server returned an error when broadcasting the transaction. The server response is contained in sparrow.log"); broadcastButton.setDisable(false); });