/** * Helper for forcing a connection to localhost. Useful when using regtest mode. Returns the peer object. */ @Nullable public Peer connectToLocalHost() { lock.lock(); try { final PeerAddress localhost = PeerAddress.localhost(params); backoffMap.put(localhost, new ExponentialBackoff(peerBackoffParams)); return connectTo(localhost, true, vConnectTimeoutMillis); } finally { lock.unlock(); } }
/** * Helper for forcing a connection to localhost. Useful when using regtest mode. Returns the peer object. */ @Nullable public Peer connectToLocalHost() { lock.lock(); try { final PeerAddress localhost = PeerAddress.localhost(params); backoffMap.put(localhost, new ExponentialBackoff(peerBackoffParams)); return connectTo(localhost, true, vConnectTimeoutMillis); } finally { lock.unlock(); } }
/** * Helper for forcing a connection to localhost. Useful when using regtest mode. Returns the peer object. */ @Nullable public Peer connectToLocalHost() { lock.lock(); try { final PeerAddress localhost = PeerAddress.localhost(params); backoffMap.put(localhost, new ExponentialBackoff(peerBackoffParams)); return connectTo(localhost, true, vConnectTimeoutMillis); } finally { lock.unlock(); } }
/** * Helper for forcing a connection to localhost. Useful when using regtest mode. Returns the peer object. */ @Nullable public Peer connectToLocalHost() { lock.lock(); try { final PeerAddress localhost = PeerAddress.localhost(params); backoffMap.put(localhost, new ExponentialBackoff(peerBackoffParams)); return connectTo(localhost, true, vConnectTimeoutMillis); } finally { lock.unlock(); } }
@Test public void preferLocalPeer() throws IOException { // Because we are using the same port (8333 or 18333) that is used by Bitcoin Core // We have to consider 2 cases: // 1. Test are executed on the same machine that is running a full node // 2. Test are executed without any full node running locally // We have to avoid to connecting to real and external services in unit tests // So we skip this test in case we have already something running on port PARAMS.getPort() // Check that if we have a localhost port 8333 or 18333 then it's used instead of the p2p network. ServerSocket local = null; try { local = new ServerSocket(PARAMS.getPort(), 100, InetAddresses.forString("127.0.0.1")); } catch(BindException e) { // Port already in use, skipping this test. return; } try { peerGroup.setUseLocalhostPeerWhenPossible(true); peerGroup.start(); local.accept().close(); // Probe connect local.accept(); // Real connect // If we get here it used the local peer. Check no others are in use. assertEquals(1, peerGroup.getMaxConnections()); assertEquals(PeerAddress.localhost(PARAMS), peerGroup.getPendingPeers().get(0).getAddress()); } finally { local.close(); } }