From d96b1e1f15c073e9076bd52f0276ee7ccce646a9 Mon Sep 17 00:00:00 2001 From: Meredith Baxter Date: Wed, 16 Oct 2019 17:09:14 -0400 Subject: [PATCH 1/3] Complete future exceptionally on failure Signed-off-by: Meredith Baxter --- .../netty/NettyConnectionInitializer.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java index d77ffed75fe..21f0b516cc6 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java @@ -14,8 +14,6 @@ */ package org.hyperledger.besu.ethereum.p2p.rlpx.connections.netty; -import static com.google.common.base.Preconditions.checkState; - import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.p2p.config.RlpxConfiguration; import org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer; @@ -114,15 +112,15 @@ public CompletableFuture start() { future -> { final InetSocketAddress socketAddress = (InetSocketAddress) server.channel().localAddress(); - final String message = - String.format( - "Unable start up P2P network on %s:%s. Check for port conflicts.", - config.getBindHost(), config.getBindPort()); - - if (!future.isSuccess()) { + if (!future.isSuccess() || socketAddress == null) { + final String message = + String.format( + "Unable start up P2P network on %s:%s. Check for port conflicts.", + config.getBindHost(), config.getBindPort()); LOG.error(message, future.cause()); + listeningPortFuture.completeExceptionally(new IllegalStateException(message)); + return; } - checkState(socketAddress != null, message); LOG.info("P2P network started and listening on {}", socketAddress); final int listeningPort = socketAddress.getPort(); From 786ff1b37ce95c2e936cd521b4d4594208a0ddf1 Mon Sep 17 00:00:00 2001 From: Meredith Baxter Date: Wed, 16 Oct 2019 17:34:33 -0400 Subject: [PATCH 2/3] Move logging of startup status to RlpxAgent Signed-off-by: Meredith Baxter --- .../besu/ethereum/p2p/rlpx/RlpxAgent.java | 14 +++++++++++++- .../rlpx/connections/ConnectionInitializer.java | 3 ++- .../netty/NettyConnectionInitializer.java | 17 ++++++----------- .../connections/MockConnectionInitializer.java | 7 +++++-- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java index a8cef8a2a57..693e02898ed 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java @@ -125,7 +125,19 @@ public CompletableFuture start() { } setupListeners(); - return connectionInitializer.start(); + return connectionInitializer + .start() + .thenApply( + (socketAddress) -> { + LOG.info("P2P RLPx agent started and listening on {}.", socketAddress); + return socketAddress.getPort(); + }) + .whenComplete( + (res, err) -> { + if (err != null) { + LOG.error("Failed to start P2P RLPx agent.", err); + } + }); } public CompletableFuture stop() { diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java index 3d4264149d3..de364c1b480 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java @@ -17,6 +17,7 @@ import org.hyperledger.besu.ethereum.p2p.peers.Peer; import org.hyperledger.besu.ethereum.p2p.rlpx.ConnectCallback; +import java.net.InetSocketAddress; import java.util.concurrent.CompletableFuture; public interface ConnectionInitializer { @@ -27,7 +28,7 @@ public interface ConnectionInitializer { * * @return The port on which we're listening for incoming connections. */ - CompletableFuture start(); + CompletableFuture start(); /** * Shutdown the connection initializer. Stop listening for incoming connections and stop diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java index 21f0b516cc6..85eccdc6578 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/netty/NettyConnectionInitializer.java @@ -46,12 +46,9 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.util.concurrent.SingleThreadEventExecutor; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; public class NettyConnectionInitializer implements ConnectionInitializer { - private static final Logger LOG = LogManager.getLogger(); private static final int TIMEOUT_SECONDS = 10; private final KeyPair keyPair; @@ -93,8 +90,8 @@ public NettyConnectionInitializer( } @Override - public CompletableFuture start() { - final CompletableFuture listeningPortFuture = new CompletableFuture<>(); + public CompletableFuture start() { + final CompletableFuture listeningPortFuture = new CompletableFuture<>(); if (!started.compareAndSet(false, true)) { listeningPortFuture.completeExceptionally( new IllegalStateException( @@ -115,16 +112,14 @@ public CompletableFuture start() { if (!future.isSuccess() || socketAddress == null) { final String message = String.format( - "Unable start up P2P network on %s:%s. Check for port conflicts.", + "Unable start listening on %s:%s. Check for port conflicts.", config.getBindHost(), config.getBindPort()); - LOG.error(message, future.cause()); - listeningPortFuture.completeExceptionally(new IllegalStateException(message)); + listeningPortFuture.completeExceptionally( + new IllegalStateException(message, future.cause())); return; } - LOG.info("P2P network started and listening on {}", socketAddress); - final int listeningPort = socketAddress.getPort(); - listeningPortFuture.complete(listeningPort); + listeningPortFuture.complete(socketAddress); }); return listeningPortFuture; diff --git a/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/MockConnectionInitializer.java b/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/MockConnectionInitializer.java index a41f59365c9..f3a33afb6d1 100644 --- a/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/MockConnectionInitializer.java +++ b/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/MockConnectionInitializer.java @@ -18,6 +18,7 @@ import org.hyperledger.besu.ethereum.p2p.rlpx.ConnectCallback; import org.hyperledger.besu.util.Subscribers; +import java.net.InetSocketAddress; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -52,8 +53,10 @@ public void simulateIncomingConnection(final PeerConnection incomingConnection) } @Override - public CompletableFuture start() { - return CompletableFuture.completedFuture(NEXT_PORT.incrementAndGet()); + public CompletableFuture start() { + InetSocketAddress socketAddress = + new InetSocketAddress("127.0.0.1", NEXT_PORT.incrementAndGet()); + return CompletableFuture.completedFuture(socketAddress); } @Override From a7ce1fea15269933f83ca7c033597ba0c447d382 Mon Sep 17 00:00:00 2001 From: Meredith Baxter Date: Wed, 16 Oct 2019 17:43:31 -0400 Subject: [PATCH 3/3] Update javadoc Signed-off-by: Meredith Baxter --- .../ethereum/p2p/rlpx/connections/ConnectionInitializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java index de364c1b480..3bc2370cc88 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/ConnectionInitializer.java @@ -26,7 +26,7 @@ public interface ConnectionInitializer { * Start the connection initializer. Begins listening for incoming connections. Start allowing * outbound connections. * - * @return The port on which we're listening for incoming connections. + * @return The address on which we're listening for incoming connections. */ CompletableFuture start();