-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce LocalBitcoinNode and tests
This new class encapsulates all functionality related to detecting a local Bitcoin node and reporting whether or not it was detected. Previously this functionality was spread across the Config class (formerly BisqEnvironment) with its mutable static isLocalBitcoinNodeRunning property and the BisqSetup class with its checkIfLocalHostNodeIsRunning method. All of this functionality now lives within the LocalBitcoinNode class, an instance of which is wired up via Guice and injected wherever necessary. Note that the code for detecting whether the node is running has been simplified, in that it is no longer wrapped in its own dedicated Thread. There appears to be no performance benefit from doing so, and leaving it in place would have made testing more difficult than necessary. Several methods in BisqSetup have also been refactored to accept callbacks indicating which step should be run next. This has the effect of clarifying when the step2()-step5() methods will be called.
- Loading branch information
Showing
13 changed files
with
181 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
core/src/main/java/bisq/core/btc/nodes/LocalBitcoinNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package bisq.core.btc.nodes; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
|
||
import java.io.IOException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Detects whether a Bitcoin node is running on localhost. | ||
* @see bisq.common.config.Config#isIgnoreLocalBtcNode() | ||
*/ | ||
public class LocalBitcoinNode { | ||
|
||
public static final String LOCAL_BITCOIN_NODE_PORT = "localBitcoinNodePort"; | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LocalBitcoinNode.class); | ||
private static final int CONNECTION_TIMEOUT = 5000; | ||
|
||
private final int port; | ||
private boolean detected = false; | ||
|
||
@Inject | ||
public LocalBitcoinNode(@Named(LOCAL_BITCOIN_NODE_PORT) int port) { | ||
this.port = port; | ||
} | ||
|
||
/** | ||
* Detect whether a Bitcoin node is running on localhost by attempting to connect | ||
* to the node's port and run the given callback regardless of whether the connection | ||
* was successful. If the connection is successful, subsequent calls to | ||
* {@link #isDetected()} will return {@code true}. | ||
* @param callback code to run after detecting whether the node is running | ||
*/ | ||
public void detectAndRun(Runnable callback) { | ||
try (Socket socket = new Socket()) { | ||
socket.connect(new InetSocketAddress("127.0.0.1", port), CONNECTION_TIMEOUT); | ||
log.info("Local Bitcoin node detected on port {}", port); | ||
detected = true; | ||
} catch (IOException ex) { | ||
log.info("No local Bitcoin node detected on port {}.", port); | ||
} | ||
callback.run(); | ||
} | ||
|
||
/** | ||
* Returns whether or not a Bitcoin node was running on localhost at the time | ||
* {@link #detectAndRun(Runnable)} was called. No further monitoring is performed, so | ||
* if the node goes up or down in the meantime, this method will continue to return | ||
* its original value. See {@code MainViewModel#setupBtcNumPeersWatcher} to understand | ||
* how disconnection and reconnection of the local Bitcoin node is actually handled. | ||
*/ | ||
public boolean isDetected() { | ||
return detected; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
@ManfredKarrer Just to be on the safe side. Was there a reason why you did the node detection in a separate thread?