Skip to content

Commit 3ae6afa

Browse files
authored
Make ActiveEth2P2PNetwork aware of seed lookahead period and compute distance (Consensys#8172)
1 parent 60253d6 commit 3ae6afa

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Diff for: networking/eth2/src/main/java/tech/pegasys/teku/networking/eth2/ActiveEth2P2PNetwork.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,21 @@ public void onSyncStateChanged(final boolean isInSync, final boolean isOptimisti
179179
gossipForkManager.onOptimisticHeadChanged(isOptimistic);
180180
}
181181

182-
private boolean isCloseToInSync() {
182+
@VisibleForTesting
183+
boolean isCloseToInSync() {
184+
final Optional<UInt64> currentEpoch = recentChainData.getCurrentEpoch();
185+
if (currentEpoch.isEmpty()) {
186+
return false;
187+
}
188+
189+
final int maxLookaheadEpochs = spec.getSpecConfig(currentEpoch.get()).getMaxSeedLookahead();
190+
final int slotsPerEpoch = spec.slotsPerEpoch(currentEpoch.get());
191+
final int maxLookaheadSlots = slotsPerEpoch * maxLookaheadEpochs;
192+
183193
return recentChainData
184194
.getChainHeadSlotsBehind()
185195
.orElse(UInt64.MAX_VALUE)
186-
.isLessThanOrEqualTo(500);
196+
.isLessThanOrEqualTo(maxLookaheadSlots);
187197
}
188198

189199
private void setTopicScoringParams() {

Diff for: networking/eth2/src/test/java/tech/pegasys/teku/networking/eth2/ActiveEth2P2PNetworkTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public class ActiveEth2P2PNetworkTest {
8181
private Fork altairFork;
8282
private Bytes32 genesisValidatorsRoot;
8383

84+
private final int maxFollowDistanceSlots =
85+
spec.getGenesisSpecConfig().getMaxSeedLookahead()
86+
* spec.slotsPerEpoch(storageSystem.combinedChainDataClient().getCurrentEpoch());
87+
8488
@BeforeEach
8589
public void setup() {
8690
when(discoveryNetwork.start()).thenReturn(SafeFuture.completedFuture(null));
@@ -255,6 +259,41 @@ void onSyncStateChanged_shouldNotResultInMultipleSubscriptions() {
255259
verify(eventChannels, times(1)).subscribe(eq(BlockGossipChannel.class), any());
256260
}
257261

262+
@Test
263+
void isCloseToInSync_shouldCalculateWhenDistanceOutOfRange() {
264+
storageSystem.chainUpdater().setCurrentSlot(UInt64.valueOf(maxFollowDistanceSlots + 1));
265+
assertThat(network.isCloseToInSync()).isFalse();
266+
}
267+
268+
@Test
269+
void isCloseToInSync_shouldCalculateWhenDistanceInRange() {
270+
storageSystem.chainUpdater().setCurrentSlot(UInt64.valueOf(maxFollowDistanceSlots));
271+
assertThat(network.isCloseToInSync()).isTrue();
272+
}
273+
274+
@Test
275+
void isCloseToInSync_shouldReturnFalseWhenEmptyCurrentEpoch() {
276+
final StorageSystem storageSystem = InMemoryStorageSystemBuilder.buildDefault();
277+
final RecentChainData recentChainData = storageSystem.recentChainData();
278+
final ActiveEth2P2PNetwork network =
279+
new ActiveEth2P2PNetwork(
280+
spec,
281+
asyncRunner,
282+
discoveryNetwork,
283+
peerManager,
284+
gossipForkManager,
285+
eventChannels,
286+
recentChainData,
287+
attestationSubnetService,
288+
syncCommitteeSubnetService,
289+
gossipEncoding,
290+
gossipConfigurator,
291+
processedAttestationSubscriptionProvider,
292+
true);
293+
294+
assertThat(network.isCloseToInSync()).isFalse();
295+
}
296+
258297
@SuppressWarnings("unchecked")
259298
private ArgumentCaptor<Iterable<Integer>> subnetIdCaptor() {
260299
return ArgumentCaptor.forClass(Iterable.class);

0 commit comments

Comments
 (0)