Repository navigation
feat: implement should_apply_proposer_boost for gloas #9233
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
Changes from 12 commits
9bc6eb6
2d7f37c
57594ee
8aafc5e
09174a2
d2bb968
e9d11c0
28e0fbc
6b87bda
90677ef
545a190
148a1c5
cc95ae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -571,32 +571,41 @@ export class ForkChoice implements IForkChoice { | |
| computeDeltasMetrics?.newVoteValidators.set(newVoteValidators); | ||
|
|
||
| this.balances = newBalances; | ||
| /** | ||
| * The structure in line with deltas to propagate boost up the branch | ||
| * starting from the proposerIndex | ||
| */ | ||
| let proposerBoost: {root: RootHex; score: number} | null = null; | ||
| if (this.opts?.proposerBoost && this.proposerBoostRoot) { | ||
| const proposerBoostScore = | ||
| this.justifiedProposerBoostScore ?? | ||
| getCommitteeFraction(this.fcStore.justified.totalBalance, { | ||
| slotsPerEpoch: SLOTS_PER_EPOCH, | ||
| committeePercent: this.config.PROPOSER_SCORE_BOOST, | ||
| }); | ||
| proposerBoost = {root: this.proposerBoostRoot, score: proposerBoostScore}; | ||
| this.justifiedProposerBoostScore = proposerBoostScore; | ||
| } | ||
|
|
||
| const currentSlot = this.fcStore.currentSlot; | ||
| this.protoArray.applyScoreChanges({ | ||
| attestationDeltas, | ||
| proposerBoost, | ||
| const checkpoints = { | ||
| justifiedEpoch: this.fcStore.justified.checkpoint.epoch, | ||
| justifiedRoot: this.fcStore.justified.checkpoint.rootHex, | ||
| finalizedEpoch: this.fcStore.finalizedCheckpoint.epoch, | ||
| finalizedRoot: this.fcStore.finalizedCheckpoint.rootHex, | ||
| currentSlot, | ||
| }); | ||
| }; | ||
|
|
||
| const boostedBlock = | ||
| this.opts?.proposerBoost && this.proposerBoostRoot ? this.getBlockHexDefaultStatus(this.proposerBoostRoot) : null; | ||
|
|
||
| if (boostedBlock && isGloasBlock(boostedBlock)) { | ||
| // should_apply_proposer_boost judges the parent against the attestations known to the store, | ||
| // via is_head_weak (which also assumes equivocators' votes were already discounted before | ||
| // their balance is added back). Apply the attestation deltas first so the decision reads | ||
| // post-delta scores, then apply the boost in a second pass. | ||
| // https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/gloas/fork-choice.md#new-should_apply_proposer_boost | ||
| this.protoArray.applyScoreChanges({attestationDeltas, proposerBoost: null, ...checkpoints}); | ||
| const proposerBoost = this.shouldApplyProposerBoost() ? this.getProposerBoost() : null; | ||
| this.protoArray.applyScoreChanges({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is calling
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now
we can improve by calling 1) twice and call 2) in the end
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added TODO comment
ensi321 marked this conversation as resolved.
Resolved
|
||
| attestationDeltas: new Array<number>(this.protoArray.nodes.length).fill(0), | ||
| proposerBoost, | ||
| ...checkpoints, | ||
| }); | ||
| } else { | ||
| // Pre-gloas the boost is unconditional, so attestation deltas and boost deltas can propagate | ||
| // up the branch in a single pass | ||
| this.protoArray.applyScoreChanges({ | ||
| attestationDeltas, | ||
| proposerBoost: boostedBlock ? this.getProposerBoost() : null, | ||
| ...checkpoints, | ||
| }); | ||
| } | ||
|
|
||
| // findHead returns the ProtoNode representing the head | ||
| const head = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot); | ||
|
|
@@ -817,6 +826,8 @@ export class ForkChoice implements IForkChoice { | |
| targetRoot: toRootHex(targetRoot), | ||
| stateRoot: toRootHex(block.stateRoot), | ||
| timeliness: isTimely, | ||
| ptcTimeliness: this.isBlockPtcTimely(block, blockDelaySec), | ||
| proposerIndex: block.proposerIndex, | ||
|
|
||
| justifiedEpoch: stateJustifiedEpoch, | ||
| justifiedRoot: toRootHex(state.currentJustifiedCheckpoint.root), | ||
|
|
@@ -1584,7 +1595,8 @@ export class ForkChoice implements IForkChoice { | |
| // Only ever called on a block already in fork choice, so a miss is a broken invariant. | ||
| const node = this.protoArray.getNodeDefaultStatus(blockRoot); | ||
| if (node === undefined) { | ||
| // this is called for head so we should always have this in forkchoice, otherwise we have a serious error | ||
| // this is called for head or the boosted block's parent, both of which should always be | ||
| // in forkchoice, otherwise we have a serious error | ||
| throw new ForkChoiceError({code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK, root: blockRoot}); | ||
| } | ||
|
|
||
|
|
@@ -1604,7 +1616,7 @@ export class ForkChoice implements IForkChoice { | |
| // always 0. Return before fetching the state and walking the block's committees. | ||
| if (equivocatingIndices.size > 0) { | ||
| const state = this.fcStore.stateGetter({stateRoot: node.stateRoot}); | ||
| // Only ever called on the head, so the state is always cached. | ||
| // Only ever called on the head or the boosted block's parent, so the state is always cached. | ||
| // A miss is a broken invariant, not a recoverable state. | ||
| if (state === null) { | ||
| throw new ForkChoiceError({ | ||
|
|
@@ -1668,6 +1680,79 @@ export class ForkChoice implements IForkChoice { | |
| return this.fcStore.currentSlot === block.slot && isBeforeLateBlockCutoff; | ||
| } | ||
|
|
||
| /** | ||
| * Return true if the block arrived before the PTC (payload-timeliness committee) deadline, | ||
| * ie. block_timeliness[PTC_TIMELINESS_INDEX]. | ||
| * | ||
| * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/gloas/fork-choice.md#modified-record_block_timeliness | ||
| * | ||
| * Child class can overwrite this for testing purpose. | ||
| */ | ||
| protected isBlockPtcTimely(block: BeaconBlock, blockDelaySec: number): boolean { | ||
|
nflaig marked this conversation as resolved.
Resolved
|
||
| const ptcThresholdMs = this.config.getSlotComponentDurationMs(this.config.PAYLOAD_ATTESTATION_DUE_BPS); | ||
| return this.fcStore.currentSlot === block.slot && blockDelaySec * 1000 < ptcThresholdMs; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether proposer boost should be applied to `proposerBoostRoot`. | ||
| * | ||
| * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/gloas/fork-choice.md#new-should_apply_proposer_boost | ||
| * | ||
| * Pre-gloas blocks always receive the boost (unconditional, backward compatible). For gloas | ||
| * blocks the boost is withheld when the parent is a weak block from the previous slot and the | ||
| * proposer of that parent equivocated (published another PTC-timely block at the same slot). | ||
| */ | ||
| private shouldApplyProposerBoost(): boolean { | ||
| if (!this.proposerBoostRoot) { | ||
| return false; | ||
| } | ||
|
|
||
| const boostedBlock = this.getBlockHexDefaultStatus(this.proposerBoostRoot); | ||
| // Pre-gloas blocks always get boost | ||
| if (!boostedBlock || !isGloasBlock(boostedBlock)) { | ||
| return true; | ||
| } | ||
|
|
||
| const parentBlock = this.getBlockHexDefaultStatus(boostedBlock.parentRoot); | ||
| if (!parentBlock) { | ||
| return true; | ||
| } | ||
|
|
||
| // Apply proposer boost if parent is not from the previous slot | ||
| if (parentBlock.slot + 1 < boostedBlock.slot) { | ||
| return true; | ||
| } | ||
|
|
||
| // Apply proposer boost if parent is not weak | ||
| if (!this.isHeadWeak(parentBlock.blockRoot)) { | ||
| return true; | ||
| } | ||
|
|
||
| // Parent is weak and from the previous slot: apply boost only if there are no early | ||
| // equivocations, ie. no other PTC-timely block at the parent's slot from the same proposer. | ||
| return !this.protoArray.hasEquivocatingBlock(parentBlock.proposerIndex, parentBlock.slot, parentBlock.blockRoot); | ||
| } | ||
|
|
||
| /** | ||
| * The proposer boost to apply to `proposerBoostRoot`, propagated up the branch by | ||
| * applyScoreChanges() together with the deltas. | ||
| */ | ||
| private getProposerBoost(): {root: RootHex; score: number} | null { | ||
| if (!this.proposerBoostRoot) { | ||
| return null; | ||
| } | ||
|
|
||
| const proposerBoostScore = | ||
| this.justifiedProposerBoostScore ?? | ||
| getCommitteeFraction(this.fcStore.justified.totalBalance, { | ||
| slotsPerEpoch: SLOTS_PER_EPOCH, | ||
| committeePercent: this.config.PROPOSER_SCORE_BOOST, | ||
| }); | ||
| this.justifiedProposerBoostScore = proposerBoostScore; | ||
|
|
||
| return {root: this.proposerBoostRoot, score: proposerBoostScore}; | ||
| } | ||
|
|
||
| /** | ||
| * https://github.com/ethereum/consensus-specs/blob/v1.5.0/specs/phase0/fork-choice.md#is_proposing_on_time | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import {DataAvailabilityStatus} from "@lodestar/state-transition"; | ||
| import {Epoch, RootHex, Slot, UintNum64} from "@lodestar/types"; | ||
| import {Epoch, RootHex, Slot, UintNum64, ValidatorIndex} from "@lodestar/types"; | ||
|
|
||
| // RootHex is a root as a hex string | ||
| // Used for lightweight and easy comparison | ||
|
|
@@ -144,6 +144,13 @@ export type ProtoBlock = BlockExtraMeta & { | |
| // Indicate whether block arrives in a timely manner ie. before the 4 second mark | ||
| timeliness: boolean; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we may want to use a single
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a real benefit to this? Using bits impact readability, and we are only saving one boolean field per |
||
|
|
||
| // Indicate whether block arrives before the PTC deadline | ||
| // Spec: gloas/fork-choice.md#modified-record_block_timeliness (block_timeliness[PTC_TIMELINESS_INDEX]) | ||
| ptcTimeliness: boolean; | ||
|
|
||
| // The index of the block proposer. Used by should_apply_proposer_boost to detect proposer equivocations | ||
| proposerIndex: ValidatorIndex; | ||
|
|
||
| /** Payload status for this node (Gloas fork). Always FULL in pre-gloas */ | ||
| payloadStatus: PayloadStatus; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.