Skip to content

Commit

Permalink
Add weak subjectivity period check when starting Teku with an existin…
Browse files Browse the repository at this point in the history
…g database (Consensys#7740)
  • Loading branch information
lucassaldanha authored Nov 21, 2023
1 parent f98e4dc commit b345751
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,14 @@ public void warnFlagDeprecation(final String oldFlag, final String newFlag) {
Color.YELLOW);
}

public void warnIgnoringWeakSubjectivityPeriod() {
log.warn(
print(
"Ignoring weak subjectivity period check (--ignore-weak-subjectivity-period-enabled). Syncing "
+ "from outside of the weak subjectivity period is considered UNSAFE.",
Color.YELLOW));
}

private void logWithColorIfLevelGreaterThanInfo(
final Level level, final String msg, final ColorConsolePrinter.Color color) {
final boolean useColor = level.compareTo(Level.INFO) < 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,24 @@ protected SafeFuture<?> initialize() {
spec))
.thenCompose(
client -> {
if (isAllowSyncOutsideWeakSubjectivityPeriod()) {
STATUS_LOG.warnIgnoringWeakSubjectivityPeriod();
}

// Setup chain storage
this.recentChainData = client;
if (recentChainData.isPreGenesis()) {
setupInitialState(client);
} else if (beaconConfig
.eth2NetworkConfig()
.getNetworkBoostrapConfig()
.isUsingCustomInitialState()) {
STATUS_LOG.warnInitialStateIgnored();
return SafeFuture.completedFuture(client);
} else {
if (isUsingCustomInitialState()) {
STATUS_LOG.warnInitialStateIgnored();
}
if (!isAllowSyncOutsideWeakSubjectivityPeriod()) {
validateWeakSubjectivityPeriod(client);
}
return SafeFuture.completedFuture(client);
}
return SafeFuture.completedFuture(client);
})
// Init other services
.thenRun(this::initAll)
Expand All @@ -431,6 +438,26 @@ protected SafeFuture<?> initialize() {
.thenCompose(__ -> timerService.start());
}

private boolean isUsingCustomInitialState() {
return beaconConfig.eth2NetworkConfig().getNetworkBoostrapConfig().isUsingCustomInitialState();
}

private boolean isAllowSyncOutsideWeakSubjectivityPeriod() {
return beaconConfig
.eth2NetworkConfig()
.getNetworkBoostrapConfig()
.isAllowSyncOutsideWeakSubjectivityPeriod();
}

private void validateWeakSubjectivityPeriod(final RecentChainData client) {
final AnchorPoint latestFinalizedAnchor = client.getStore().getLatestFinalized();
final UInt64 currentSlot = getCurrentSlot(client.getGenesisTime());
final WeakSubjectivityCalculator wsCalculator =
WeakSubjectivityCalculator.create(beaconConfig.weakSubjectivity());
wsInitializer.validateAnchorIsWithinWeakSubjectivityPeriod(
latestFinalizedAnchor, currentSlot, spec, wsCalculator);
}

public void initAll() {
initKeyValueStore();
initExecutionLayer();
Expand Down Expand Up @@ -1281,10 +1308,7 @@ protected void setupInitialState(final RecentChainData client) {
WeakSubjectivityPeriodCalculator to the WeakSubjectivityInitializer. Otherwise, we pass an Optional.empty().
*/
final Optional<WeakSubjectivityCalculator> maybeWsCalculator;
if (beaconConfig
.eth2NetworkConfig()
.getNetworkBoostrapConfig()
.isAllowSyncOutsideWeakSubjectivityPeriod()) {
if (isAllowSyncOutsideWeakSubjectivityPeriod()) {
maybeWsCalculator = Optional.empty();
} else {
maybeWsCalculator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ public SafeFuture<WeakSubjectivityConfig> finalizeAndStoreConfig(
});
}

public void validateAnchorIsWithinWeakSubjectivityPeriod(
final AnchorPoint initialAnchor,
final UInt64 currentSlot,
final Spec spec,
final WeakSubjectivityCalculator weakSubjectivityCalculator) {
final CheckpointState checkpointState =
CheckpointState.create(
spec,
initialAnchor.getCheckpoint(),
initialAnchor.getBlockSummary(),
initialAnchor.getState());
if (!weakSubjectivityCalculator.isWithinWeakSubjectivityPeriod(checkpointState, currentSlot)) {
throw new IllegalStateException(
"Cannot sync outside of weak subjectivity period. Consider re-syncing your node using --checkpoint-sync-url or use --ignore-weak-subjectivity-period-enabled to ignore this check.");
}
;
}

public void validateInitialAnchor(
final AnchorPoint initialAnchor,
final UInt64 currentSlot,
Expand All @@ -154,18 +172,9 @@ public void validateInitialAnchor(
}

maybeWsCalculator.ifPresent(
weakSubjectivityCalculator -> {
final CheckpointState checkpointState =
CheckpointState.create(
spec,
initialAnchor.getCheckpoint(),
initialAnchor.getBlockSummary(),
initialAnchor.getState());
if (!weakSubjectivityCalculator.isWithinWeakSubjectivityPeriod(
checkpointState, currentSlot)) {
throw new IllegalStateException("Cannot sync outside of weak subjectivity period.");
}
});
wsCalculator ->
validateAnchorIsWithinWeakSubjectivityPeriod(
initialAnchor, currentSlot, spec, wsCalculator));

if (initialAnchor.isGenesis()) {
// Skip extra validations for genesis state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void validateInitialAnchor_withSubjectivityCheck_outOfWeakSubjectivityPer
initializer.validateInitialAnchor(
anchor, currentSlot, spec, Optional.of(wsCalculator)))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Cannot sync outside of weak subjectivity period.");
.hasMessageContaining("Cannot sync outside of weak subjectivity period");
}

@Test
Expand Down

0 comments on commit b345751

Please sign in to comment.