Skip to content

Commit

Permalink
use read timeout to improve disconnection behaviour, especially while…
Browse files Browse the repository at this point in the history
… refreshing a wallet
  • Loading branch information
craigraw committed Apr 29, 2021
1 parent b6f7483 commit 5518116
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/sparrowwallet/sparrow/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,9 @@ public void walletHistoryFailed(WalletHistoryFailedEvent event) {
walletHistoryFinished(new WalletHistoryFinishedEvent(event.getWallet()));
tabs.getTabs().stream().filter(tab -> tab.getUserData() instanceof WalletTabData && ((WalletTabData) tab.getUserData()).getWallet() == event.getWallet()).forEach(this::tabLabelAddFailure);
if(getOpenWallets().containsKey(event.getWallet())) {
statusUpdated(new StatusEvent("Error retrieving wallet history" + (Config.get().getServerType() == ServerType.PUBLIC_ELECTRUM_SERVER ? ", " + TRYING_ANOTHER_SERVER_MESSAGE : "")));
if(AppServices.isConnected()) {
statusUpdated(new StatusEvent("Error retrieving wallet history" + (Config.get().getServerType() == ServerType.PUBLIC_ELECTRUM_SERVER ? ", " + TRYING_ANOTHER_SERVER_MESSAGE : "")));
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,15 @@ protected Task<Boolean> createTask() {
protected Boolean call() throws ServerException {
walletSynchronizeLocks.putIfAbsent(wallet, new Object());
synchronized(walletSynchronizeLocks.get(wallet)) {
ElectrumServer electrumServer = new ElectrumServer();
Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap = (nodes == null ? electrumServer.getHistory(wallet) : electrumServer.getHistory(wallet, nodes));
electrumServer.getReferencedTransactions(wallet, nodeTransactionMap);
electrumServer.calculateNodeHistory(wallet, nodeTransactionMap);
return true;
if(isConnected()) {
ElectrumServer electrumServer = new ElectrumServer();
Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap = (nodes == null ? electrumServer.getHistory(wallet) : electrumServer.getHistory(wallet, nodes));
electrumServer.getReferencedTransactions(wallet, nodeTransactionMap);
electrumServer.calculateNodeHistory(wallet, nodeTransactionMap);
return true;
}

return false;
}
}
};
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.net.ssl.SSLHandshakeException;
import java.io.*;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand All @@ -24,6 +25,7 @@ public class TcpTransport implements Transport, Closeable {

public static final int DEFAULT_PORT = 50001;
private static final int[] READ_TIMEOUT_SECS = {3, 8, 16, 34};
public static final int SOCKET_READ_TIMEOUT_MILLIS = 5000;

protected final HostAndPort server;
protected final SocketFactory socketFactory;
Expand Down Expand Up @@ -181,7 +183,7 @@ public void readInputLoop() throws ServerException {
}

protected String readInputStream(BufferedReader in) throws IOException {
String response = in.readLine();
String response = readLine(in);

if(response == null) {
throw new IOException("Could not connect to server at " + Config.get().getServerAddress());
Expand All @@ -190,9 +192,22 @@ protected String readInputStream(BufferedReader in) throws IOException {
return response;
}

private String readLine(BufferedReader in) throws IOException {
while(!socket.isClosed()) {
try {
return in.readLine();
} catch(SocketTimeoutException e) {
//ignore and continue
}
}

return null;
}

public void connect() throws ServerException {
try {
socket = createSocket();
socket.setSoTimeout(SOCKET_READ_TIMEOUT_MILLIS);
running = true;
} catch(SSLHandshakeException e) {
throw new TlsServerException(server, e);
Expand All @@ -216,7 +231,6 @@ protected Socket createSocket() throws IOException {
@Override
public void close() throws IOException {
if(socket != null) {
running = false;
socket.close();
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,18 @@ public void refreshHistory(Integer blockHeight, Wallet pastWallet, WalletNode no
log.debug(node == null ? wallet.getName() + " refreshing full wallet history" : wallet.getName() + " requesting node wallet history for " + node.getDerivationPath());
ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet, getWalletTransactionNodes(node));
historyService.setOnSucceeded(workerStateEvent -> {
EventManager.get().post(new WalletHistoryFinishedEvent(wallet));
updateWallet(blockHeight, pastWallet, previousWallet);
if(historyService.getValue()) {
EventManager.get().post(new WalletHistoryFinishedEvent(wallet));
updateWallet(blockHeight, pastWallet, previousWallet);
}
});
historyService.setOnFailed(workerStateEvent -> {
log.error("Error retrieving wallet history", workerStateEvent.getSource().getException());
if(AppServices.isConnected()) {
log.error("Error retrieving wallet history", workerStateEvent.getSource().getException());
} else {
log.debug("Disconnected while retrieving wallet history", workerStateEvent.getSource().getException());
}

EventManager.get().post(new WalletHistoryFailedEvent(wallet, workerStateEvent.getSource().getException()));
});

Expand Down

0 comments on commit 5518116

Please sign in to comment.