Skip to content

Commit

Permalink
wait one third before publishing lc updates in on onSyncAggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
dadepo committed Oct 23, 2022
1 parent 16dcb0c commit 003a233
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 27 additions & 10 deletions packages/beacon-node/src/chain/lightClient/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {altair, phase0, Root, RootHex, Slot, ssz, SyncPeriod} from "@lodestar/types";
import {IChainForkConfig} from "@lodestar/config";
import {CachedBeaconStateAltair, computeSyncPeriodAtEpoch, computeSyncPeriodAtSlot} from "@lodestar/state-transition";
import {ILogger, MapDef, pruneSetToMax} from "@lodestar/utils";
import {
CachedBeaconStateAltair,
computeSyncPeriodAtEpoch,
computeSyncPeriodAtSlot,
computeTimeAtSlot,
} from "@lodestar/state-transition";
import {ILogger, MapDef, pruneSetToMax, sleep} from "@lodestar/utils";
import {BitArray, CompositeViewDU, toHexString} from "@chainsafe/ssz";
import {MIN_SYNC_COMMITTEE_PARTICIPANTS, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
import {IBeaconDb} from "../../db/index.js";
Expand Down Expand Up @@ -224,7 +229,13 @@ export class LightClientServer {
const signedBlockRoot = block.parentRoot;
const syncPeriod = computeSyncPeriodAtSlot(block.slot);

this.onSyncAggregate(syncPeriod, block.body.syncAggregate, block.slot, signedBlockRoot).catch((e) => {
this.onSyncAggregate(
syncPeriod,
block.body.syncAggregate,
block.slot,
signedBlockRoot,
postState.genesisTime
).catch((e) => {
this.logger.error("Error onSyncAggregate", {}, e);
this.metrics?.lightclientServer.onSyncAggregate.inc({event: "error"});
});
Expand Down Expand Up @@ -445,7 +456,8 @@ export class LightClientServer {
syncPeriod: SyncPeriod,
syncAggregate: altair.SyncAggregate,
signatureSlot: Slot,
signedBlockRoot: Root
signedBlockRoot: Root,
genesisTime: number
): Promise<void> {
this.metrics?.lightclientServer.onSyncAggregate.inc({event: "processed"});

Expand Down Expand Up @@ -482,11 +494,6 @@ export class LightClientServer {
return;
}

// Emit update
// - At the earliest: 6 second after the slot start
// - After a new update has INCREMENT_THRESHOLD == 32 bits more than the previous emitted threshold
this.emitter.emit(ChainEvent.lightClientOptimisticUpdate, headerUpdate);

// Persist latest best update for getLatestHeadUpdate()
// TODO: Once SyncAggregate are constructed from P2P too, count bits to decide "best"
if (!this.latestHeadUpdate || attestedData.attestedHeader.slot > this.latestHeadUpdate.attestedHeader.slot) {
Expand All @@ -510,11 +517,21 @@ export class LightClientServer {
finalityBranch: attestedData.finalityBranch,
signatureSlot,
};
this.emitter.emit(ChainEvent.lightClientFinalityUpdate, this.finalized);
this.metrics?.lightclientServer.onSyncAggregate.inc({event: "update_latest_finalized_update"});
}
}

// Emit update
// messages SHOULD be broadcast after one-third of slot has transpired
// https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/p2p-interface.md#sync-committee
const minPubTime = computeTimeAtSlot(this.config, signatureSlot, genesisTime) + this.config.SECONDS_PER_SLOT / 3;
const waitTime = minPubTime - Date.now() / 1000;
await sleep(waitTime);

this.emitter.emit(ChainEvent.lightClientOptimisticUpdate, headerUpdate);
if (this.finalized) {
this.emitter.emit(ChainEvent.lightClientFinalityUpdate, this.finalized);
}
// Check if this update is better, otherwise ignore
await this.maybeStoreNewBestPartialUpdate(syncPeriod, syncAggregate, signatureSlot, attestedData);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/beacon-node/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ export class Network implements INetwork {

private onLightClientFinalityUpdate = async (finalityUpdate: altair.LightClientFinalityUpdate): Promise<void> => {
try {
// messages SHOULD be broadcasted after one-third of slot has transpired
await this.clock.waitForSlot(finalityUpdate.signatureSlot + 1 / 3);
return await this.gossip.publishLightClientFinalityUpdate(finalityUpdate);
} catch (e) {
this.logger.debug("Error on BeaconGossipHandler.onLightclientFinalityUpdate", {}, e as Error);
Expand All @@ -373,8 +371,6 @@ export class Network implements INetwork {
optimisticUpdate: altair.LightClientOptimisticUpdate
): Promise<void> => {
try {
// messages SHOULD be broadcasted after one-third of slot has transpired
await this.clock.waitForSlot(optimisticUpdate.signatureSlot + 1 / 3);
return await this.gossip.publishLightClientOptimisticUpdate(optimisticUpdate);
} catch (e) {
this.logger.debug("Error on BeaconGossipHandler.onLightclientOptimisticUpdate", {}, e as Error);
Expand Down

0 comments on commit 003a233

Please sign in to comment.