Skip to content

Commit 2d10e2d

Browse files
committed
update current or next slot rule
1 parent 72c585e commit 2d10e2d

File tree

4 files changed

+44
-46
lines changed

4 files changed

+44
-46
lines changed

ethereum/statetransition/src/main/java/tech/pegasys/teku/statetransition/validation/ExecutionPayloadBidGossipValidator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ public SafeFuture<InternalValidationResult> validate(
104104
* [IGNORE] bid.slot is the current slot or the next slot.
105105
*/
106106

107-
// This check considers the gossip clock disparity allowance and hence accepts bids with current
108-
// slot, next slot but not too early, previous slot but not too late
109-
if (!gossipValidationHelper.isCurrentSlotWithGossipDisparityAllowance(bid.getSlot())) {
107+
if (!gossipValidationHelper.isSlotCurrentOrNext(bid.getSlot())) {
110108
LOG.trace("Bid must be for current or next slot but was for slot {}", bid.getSlot());
111109
return completedFuture(
112110
ignore("Bid must be for current or next slot but was for slot %s", bid.getSlot()));

ethereum/statetransition/src/main/java/tech/pegasys/teku/statetransition/validation/GossipValidationHelper.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package tech.pegasys.teku.statetransition.validation;
1515

16+
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE;
17+
1618
import java.util.Optional;
1719
import org.apache.tuweni.bytes.Bytes;
1820
import org.apache.tuweni.bytes.Bytes32;
@@ -165,29 +167,20 @@ public SafeFuture<Optional<BeaconState>> getStateAtSlotAndBlockRoot(
165167
}
166168

167169
public boolean isCurrentSlotWithGossipDisparityAllowance(final UInt64 slot) {
168-
final int maximumGossipClockDisparityMillis =
169-
spec.getNetworkingConfig().getMaximumGossipClockDisparity();
170-
return isTimeWithinSlotWindow(slot, maximumGossipClockDisparityMillis);
170+
return isTimeWithinSlotWindow(slot, maxOffsetTimeInMillis);
171+
}
172+
173+
public boolean isSlotCurrentOrNext(final UInt64 slot) {
174+
return getCurrentSlot()
175+
.map(currentSlot -> slot.equals(currentSlot) || slot.equals(currentSlot.plus(ONE)))
176+
.orElse(false);
171177
}
172178

173179
public boolean isValidatorInPayloadTimelinessCommittee(
174180
final UInt64 validatorIndex, final BeaconState state, final UInt64 slot) {
175181
return spec.getPtc(state, slot).contains(validatorIndex.intValue());
176182
}
177183

178-
private boolean isTimeWithinSlotWindow(final UInt64 slot, final int disparity) {
179-
if (recentChainData.getCurrentSlot().isEmpty()) {
180-
return false;
181-
}
182-
final UInt64 slotStartTimeMillis =
183-
spec.computeTimeMillisAtSlot(slot, recentChainData.getGenesisTimeMillis());
184-
final UInt64 slotEndTimeMillis = slotStartTimeMillis.plus(spec.getSlotDurationMillis(slot));
185-
final UInt64 currentTimeMillis = recentChainData.getStore().getTimeInMillis();
186-
187-
return currentTimeMillis.isGreaterThanOrEqualTo(slotStartTimeMillis.minusMinZero(disparity))
188-
&& currentTimeMillis.isLessThanOrEqualTo(slotEndTimeMillis.plus(disparity));
189-
}
190-
191184
public boolean hasBuilderWithdrawalCredential(
192185
final UInt64 builderIndex, final BeaconState state, final UInt64 slot) {
193186
return MiscHelpersGloas.required(spec.atSlot(slot).miscHelpers())
@@ -208,4 +201,21 @@ public boolean isBlockHashKnown(final Bytes32 blockHash, final Bytes32 blockRoot
208201
recentChainData.getExecutionBlockHashForBlockRoot(blockRoot);
209202
return maybeBlockHash.isPresent() && blockHash.equals(maybeBlockHash.get());
210203
}
204+
205+
private Optional<UInt64> getCurrentSlot() {
206+
return recentChainData.getCurrentSlot();
207+
}
208+
209+
private boolean isTimeWithinSlotWindow(final UInt64 slot, final int disparity) {
210+
if (getCurrentSlot().isEmpty()) {
211+
return false;
212+
}
213+
final UInt64 slotStartTimeMillis =
214+
spec.computeTimeMillisAtSlot(slot, recentChainData.getGenesisTimeMillis());
215+
final UInt64 slotEndTimeMillis = slotStartTimeMillis.plus(spec.getSlotDurationMillis(slot));
216+
final UInt64 currentTimeMillis = recentChainData.getStore().getTimeInMillis();
217+
218+
return currentTimeMillis.isGreaterThanOrEqualTo(slotStartTimeMillis.minusMinZero(disparity))
219+
&& currentTimeMillis.isLessThanOrEqualTo(slotEndTimeMillis.plus(disparity));
220+
}
211221
}

ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/validation/ExecutionPayloadBidGossipValidatorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void setup(final TestSpecInvocationContextProvider.SpecContext specContext) {
7070
parentBlockHash = bid.getParentBlockHash();
7171
postState = dataStructureUtil.randomBeaconState();
7272

73-
when(gossipValidationHelper.isCurrentSlotWithGossipDisparityAllowance(slot)).thenReturn(true);
73+
when(gossipValidationHelper.isSlotCurrentOrNext(slot)).thenReturn(true);
7474
when(gossipValidationHelper.isBlockHashKnown(parentBlockHash, parentBlockRoot))
7575
.thenReturn(true);
7676
when(gossipValidationHelper.getSlotForBlockRoot(parentBlockRoot))
@@ -114,8 +114,8 @@ void shouldReject_whenExecutionPaymentIsNonZero() {
114114
}
115115

116116
@TestTemplate
117-
void shouldIgnore_whenSlotIsNotWithinGossipWindow() {
118-
when(gossipValidationHelper.isCurrentSlotWithGossipDisparityAllowance(slot)).thenReturn(false);
117+
void shouldIgnore_whenSlotIsNotCurrentOrNext() {
118+
when(gossipValidationHelper.isSlotCurrentOrNext(slot)).thenReturn(false);
119119
assertThatSafeFuture(bidValidator.validate(signedBid))
120120
.isCompletedWithValue(
121121
ignore("Bid must be for current or next slot but was for slot %s", slot));

ethereum/statetransition/src/test/java/tech/pegasys/teku/statetransition/validation/GossipValidationHelperTest.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -312,36 +312,26 @@ void currentFinalizedCheckpointIsAncestorOfBlock_shouldReturnValid() {
312312
}
313313

314314
@TestTemplate
315-
void isForCurrentSlot_shouldRejectOutsideLowerBound() {
315+
void isForCurrentSlotWithGossipDisparityAllowance() {
316316
final UInt64 slot = UInt64.valueOf(1000);
317317
final UInt64 slotStartTimeMillis = getSlotStartTimeMillis(slot);
318-
final UInt64 currentTime = slotStartTimeMillis.minus(maximumGossipClockDisparity).decrement();
319-
assertIsCurrentSlot(slot, currentTime, false);
320-
}
321-
322-
@TestTemplate
323-
void isForCurrentSlot_shouldAcceptLowerBound() {
324-
final UInt64 slot = UInt64.valueOf(1000);
325-
final UInt64 slotStartTimeMillis = getSlotStartTimeMillis(slot);
326-
final UInt64 currentTime = slotStartTimeMillis.minus(maximumGossipClockDisparity);
327-
assertIsCurrentSlot(slot, currentTime, true);
328-
}
329-
330-
@TestTemplate
331-
void isForCurrentSlot_shouldAcceptUpperBound() {
332-
final UInt64 slot = UInt64.valueOf(1000);
318+
assertIsCurrentSlot(
319+
slot, slotStartTimeMillis.minus(maximumGossipClockDisparity).decrement(), false);
320+
assertIsCurrentSlot(slot, slotStartTimeMillis.minus(maximumGossipClockDisparity), true);
333321
final UInt64 nextSlotStartTimeMillis = getSlotStartTimeMillis(slot.increment());
334-
final UInt64 currentTime = nextSlotStartTimeMillis.plus(maximumGossipClockDisparity);
335-
assertIsCurrentSlot(slot, currentTime, true);
322+
assertIsCurrentSlot(slot, nextSlotStartTimeMillis.plus(maximumGossipClockDisparity), true);
323+
assertIsCurrentSlot(
324+
slot, nextSlotStartTimeMillis.plus(maximumGossipClockDisparity).increment(), false);
336325
}
337326

338327
@TestTemplate
339-
void isForCurrentSlot_shouldRejectOutsideUpperBound() {
340-
final UInt64 slot = UInt64.valueOf(1000);
341-
final UInt64 nextSlotStartTimeMillis = getSlotStartTimeMillis(slot.increment());
342-
final UInt64 currentTime =
343-
nextSlotStartTimeMillis.plus(maximumGossipClockDisparity).increment();
344-
assertIsCurrentSlot(slot, currentTime, false);
328+
void isCurrentOrNextSlot() {
329+
final UInt64 currentSlot = UInt64.valueOf(10);
330+
storageSystem.chainUpdater().setCurrentSlot(currentSlot);
331+
assertThat(gossipValidationHelper.isSlotCurrentOrNext(currentSlot)).isTrue();
332+
assertThat(gossipValidationHelper.isSlotCurrentOrNext(currentSlot.plus(ONE))).isTrue();
333+
assertThat(gossipValidationHelper.isSlotCurrentOrNext(currentSlot.minus(ONE))).isFalse();
334+
assertThat(gossipValidationHelper.isSlotCurrentOrNext(currentSlot.plus(2))).isFalse();
345335
}
346336

347337
private UInt64 getSlotStartTimeMillis(final UInt64 slot) {

0 commit comments

Comments
 (0)