Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mute unnecessary logging and minor improvements #4062

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions core/src/main/java/bisq/core/btc/nodes/LocalBitcoinNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import org.bitcoinj.core.PeerAddress;
import org.bitcoinj.core.VersionMessage;
import org.bitcoinj.core.listeners.PeerDisconnectedEventListener;
import org.bitcoinj.net.NioClient;
import org.bitcoinj.net.NioClientManager;

import org.bitcoinj.net.BlockingClient;

import javax.inject.Inject;
import javax.inject.Singleton;

import javax.net.SocketFactory;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -168,7 +170,7 @@ private void handleHandshakeAttempt(Optional<VersionMessage> optionalVersionMess
log.info("Local Bitcoin node found to be well configured (not pruning and allows bloom filters)");
} else {
wellConfigured = false;
log.info("Local Bitcoin node badly configured (it is pruning and/or bloom filters are disabled)");
log.warn("Local Bitcoin node badly configured (it is pruning and/or bloom filters are disabled)");
}
}
}
Expand Down Expand Up @@ -226,16 +228,17 @@ private Optional<VersionMessage> attemptHandshakeForVersionMessage() {
return Optional.empty();
}

// We temporarily silence BitcoinJ NioClient's and NioClientManager's loggers,
// because when a local Bitcoin node is not found they pollute console output
// with "connection refused" error messages.
var originalNioClientLoggerLevel = silence(NioClient.class);
var originalNioClientManagerLoggerLevel = silence(NioClientManager.class);
// We temporarily silence loggers of following BitcoinJ classes, because in some
// cases they pollute console output with misguiding messages.
var originalBlockingClientLoggerLevel = silence(BlockingClient.class);
var originalPeerLoggerLevel = silence(Peer.class);

try {
log.info("Initiating attempt to connect to and handshake with a local " +
"Bitcoin node (which may or may not be running) on port {}.", port);
createClient(peer, port, CONNECTION_TIMEOUT);
// We don't keep a reference, because a handshake is initiated automatically
// and because the client is closed automatically when closing the Peer.
createBlockingClient(peer, port, CONNECTION_TIMEOUT);
} catch (IOException ex) {
log.error("Local bitcoin node handshake attempt was unexpectedly interrupted", ex);
return Optional.empty();
Expand All @@ -253,14 +256,14 @@ private Optional<VersionMessage> attemptHandshakeForVersionMessage() {
} catch (TimeoutException ex) {
optionalPeerVersionMessage = Optional.empty();
log.error("Exploratory handshake attempt with a local Bitcoin node (that may not be there)" +
" unexpectedly timed out. This should never happen; please report this. HANDSHAKE_TIMEOUT" +
" is {} ms. Continuing as if a local BTC node was not found.", HANDSHAKE_TIMEOUT);
" unexpectedly timed out. This should never happen; please report this." +
" Continuing as if a local BTC node was not found.");
}

peer.close();

restoreLoggerLevel(NioClient.class, originalNioClientLoggerLevel);
restoreLoggerLevel(NioClientManager.class, originalNioClientManagerLoggerLevel);
restoreLoggerLevel(BlockingClient.class, originalBlockingClientLoggerLevel);
restoreLoggerLevel(Peer.class, originalPeerLoggerLevel);

return optionalPeerVersionMessage;
}
Expand All @@ -280,19 +283,24 @@ private Peer createLocalPeer(int port) throws UnknownHostException {

var localPeerAddress = new PeerAddress(InetAddress.getLocalHost(), port);

return new Peer(networkParameters, ourVersionMessage, localPeerAddress, null);
var peer = new Peer(networkParameters, ourVersionMessage, localPeerAddress, null);

// We're not interested in the peer's blockchain.
peer.setDownloadData(false);

return peer;
}

/**
* Creates an NioClient that is expected to only be used to coerce a VersionMessage
* Creates a BlockingClient that is expected to only be used to coerce a VersionMessage
* out of a local Bitcoin node and be closed right after.
*/
private static NioClient createClient(Peer peer, int port, int connectionTimeout) throws IOException {
private static BlockingClient createBlockingClient(Peer peer, int port, int connectionTimeout) throws IOException {
InetSocketAddress serverAddress = new InetSocketAddress(InetAddress.getLocalHost(), port);

// This initiates the handshake procedure, which, if successful, will complete
// the peerVersionMessageFuture, or be cancelled, in case of failure.
return new NioClient(serverAddress, peer, connectionTimeout);
return new BlockingClient(serverAddress, peer, connectionTimeout, SocketFactory.getDefault(), null);
}

private static Level silence(Class<?> klass) {
Expand Down