Skip to content

Commit

Permalink
make sure we start gossip when peers connects and we are in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Nov 29, 2024
1 parent c6b48cd commit 33155be
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ private synchronized void startup() {
if (recentChainData.isCloseToInSync()) {
startGossip();
}
peerManager.subscribeConnect(peer -> onPeerConnected());
}

private void onPeerConnected() {
if (gossipStarted.get() || state.get() != State.RUNNING) {
return;
}
if (recentChainData.isCloseToInSync()) {
startGossip();
}
}

private synchronized void startGossip() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding;
import tech.pegasys.teku.networking.eth2.gossip.forks.GossipForkManager;
import tech.pegasys.teku.networking.eth2.gossip.topics.ProcessedAttestationSubscriptionProvider;
import tech.pegasys.teku.networking.eth2.peers.Eth2Peer;
import tech.pegasys.teku.networking.eth2.peers.Eth2PeerManager;
import tech.pegasys.teku.networking.p2p.discovery.DiscoveryNetwork;
import tech.pegasys.teku.networking.p2p.peer.PeerConnectedSubscriber;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener;
Expand Down Expand Up @@ -185,6 +187,33 @@ public void unsubscribeFromSyncCommitteeSubnetId_shouldUpdateDiscoveryENR() {
assertThat(capturedValues.get(3)).containsExactlyInAnyOrder(1, 3);
}

@Test
void shouldStartGossipOnPeerConnect() {
@SuppressWarnings("unchecked")
final ArgumentCaptor<PeerConnectedSubscriber<Eth2Peer>> peerManagerCaptor =
ArgumentCaptor.forClass(PeerConnectedSubscriber.class);
// Current slot is a long way beyond the chain head
storageSystem.chainUpdater().setCurrentSlot(UInt64.valueOf(64));

assertThat(network.start()).isCompleted();
verify(peerManager).subscribeConnect(peerManagerCaptor.capture());

network.onSyncStateChanged(false);
// based on network time we know we're too far behind, so we don't start gossip
verify(gossipForkManager, never()).configureGossipForEpoch(any());

// we are still too far behind, so on peer connect gossip is not started
peerManagerCaptor.getValue().onConnected(mock(Eth2Peer.class));
verify(gossipForkManager, never()).configureGossipForEpoch(any());

// Advance the chain
storageSystem.chainUpdater().updateBestBlock(storageSystem.chainUpdater().advanceChain(64));

// on peer connect gossip is started
peerManagerCaptor.getValue().onConnected(mock(Eth2Peer.class));
verify(gossipForkManager).configureGossipForEpoch(any());
}

@Test
void onSyncStateChanged_shouldEnableGossipWhenInSync() {
// Current slot is a long way beyond the chain head
Expand Down

0 comments on commit 33155be

Please sign in to comment.