Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the zero finalized light client header upgrade #5139

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/beacon-node/src/chain/lightClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,18 +602,18 @@ export class LightClientServer {
isFinalized = true;
finalityBranch = attestedData.finalityBranch;
finalizedHeader = finalizedHeaderAttested;
// Fork of LightClientUpdate is based off on attested header's fork
const attestedFork = this.config.getForkName(attestedHeader.beacon.slot);
if (this.config.getForkName(finalizedHeader.beacon.slot) !== attestedFork) {
finalizedHeader = upgradeLightClientHeader(this.config, attestedFork, finalizedHeader);
}
} else {
isFinalized = false;
finalityBranch = this.zero.finalityBranch;
// No need to upgrade finalizedHeader because its anyway set to zero of highest fork
finalizedHeader = this.zero.finalizedHeader;
}

// Fork of LightClientUpdate is based off on attested header's fork
const attestedFork = this.config.getForkName(attestedHeader.beacon.slot);
if (this.config.getForkName(finalizedHeader.beacon.slot) !== attestedFork) {
finalizedHeader = upgradeLightClientHeader(this.config, attestedFork, finalizedHeader);
}

const newUpdate = {
attestedHeader,
nextSyncCommittee: nextSyncCommittee,
Expand Down
19 changes: 14 additions & 5 deletions packages/light-client/src/spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ export function upgradeLightClientHeader(
targetFork: ForkName,
header: altair.LightClientHeader
): allForks.LightClientHeader {
const upgradedHeader = header;

const headerFork = config.getForkName(header.beacon.slot);
switch (headerFork) {
case ForkName.phase0:
throw Error(`Invalid target fork=${headerFork} for LightClientHeader`);
if (ForkSeq[headerFork] >= ForkSeq[targetFork]) {
throw Error(`Invalid upgrade request from headerFork=${headerFork} to targetFork=${targetFork}`);
}

// We are modifying the same header object, may be we could create a copy, but its
// not required as of now
const upgradedHeader = header as allForks.LightClientHeader;
const startUpgradeFromFork = Object.values(ForkName)[ForkSeq[headerFork] + 1];

switch (startUpgradeFromFork) {
default:
throw Error(
`Invalid startUpgradeFromFork=${startUpgradeFromFork} for headerFork=${headerFork} in upgradeLightClientHeader to targetFork=${targetFork}`
);

case ForkName.altair:
case ForkName.bellatrix:
Expand Down