From 1f04509aabf0270ca7188ccb0a0015e08d412539 Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Wed, 19 Oct 2022 23:20:03 +1000 Subject: [PATCH] P2P: Connect to maintained connections at startup (#4543) * Also adds a one time call to initiate connection to nodes in static nodes list after local node is ready. In the absence of this call, the connection attempt is made after the configured interval (default 60 seconds) to check maintained connections. This is to make sure the node doesn't have to wait for almost a minute before it can start interacting with peers (and sync blocks etc.) Signed-off-by: Vinod Damle Signed-off-by: Sally MacFarlane Co-authored-by: Vinod Damle --- CHANGELOG.md | 3 ++- .../besu/ethereum/p2p/network/DefaultP2PNetwork.java | 4 ++++ .../besu/ethereum/p2p/network/DefaultP2PNetworkTest.java | 9 ++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffa0c7aa11..dd4328496e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,8 @@ - Avoid a cyclic reference while printing EngineExchangeTransitionConfigurationParameter [#4357](https://github.com/hyperledger/besu/pull/4357) - Corrects treating a block as bad on internal error [#4512](https://github.com/hyperledger/besu/issues/4512) - In GraphQL update scalar parsing to be variable friendly [#4522](https://github.com/hyperledger/besu/pull/4522) -- update appache-commons-text to 1.10.0 to address CVE-2022-42889 [#4542](https://github.com/hyperledger/besu/pull/4542) +- Initiate connection to maintained peers soon after startup. [#4469](https://github.com/hyperledger/besu/pull/4469) +- Update apache-commons-text to 1.10.0 to address CVE-2022-42889 [#4542](https://github.com/hyperledger/besu/pull/4542) ### Download Links diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java index ca675891ec..1b4690786f 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetwork.java @@ -259,6 +259,10 @@ public class DefaultP2PNetwork implements P2PNetwork { peerBondedObserverId = OptionalLong.of(peerDiscoveryAgent.observePeerBondedEvents(this::handlePeerBondedEvent)); + // Call checkMaintainedConnectionPeers() now that the local node is up, for immediate peer + // additions + checkMaintainedConnectionPeers(); + // Periodically check maintained connections final int checkMaintainedConnectionsSec = config.getCheckMaintainedConnectionsFrequencySec(); peerConnectionScheduler.scheduleWithFixedDelay( diff --git a/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java b/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java index c3b8a0d733..5c101fc894 100644 --- a/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java +++ b/ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/DefaultP2PNetworkTest.java @@ -186,10 +186,11 @@ public final class DefaultP2PNetworkTest { public void checkMaintainedConnectionPeers_unconnectedPeer() { final DefaultP2PNetwork network = network(); final Peer peer = PeerTestHelper.createPeer(); - maintainedPeers.add(peer); network.start(); + maintainedPeers.add(peer); + verify(rlpxAgent, times(0)).connect(peer); network.checkMaintainedConnectionPeers(); @@ -200,10 +201,11 @@ public final class DefaultP2PNetworkTest { public void checkMaintainedConnectionPeers_connectedPeer() { final DefaultP2PNetwork network = network(); final Peer peer = PeerTestHelper.createPeer(); - maintainedPeers.add(peer); network.start(); + maintainedPeers.add(peer); + // Don't connect to an already connected peer final CompletableFuture connectionFuture = CompletableFuture.completedFuture(MockPeerConnection.create(peer)); @@ -216,10 +218,11 @@ public final class DefaultP2PNetworkTest { public void checkMaintainedConnectionPeers_connectingPeer() { final DefaultP2PNetwork network = network(); final Peer peer = PeerTestHelper.createPeer(); - maintainedPeers.add(peer); network.start(); + maintainedPeers.add(peer); + // Don't connect when connection is already pending. final CompletableFuture connectionFuture = new CompletableFuture<>(); when(rlpxAgent.getPeerConnection(peer)).thenReturn(Optional.of(connectionFuture));