Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
10 changes: 9 additions & 1 deletion packages/beacon-node/src/chain/errors/proposerPreferences.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Slot, ValidatorIndex} from "@lodestar/types";
import {RootHex, Slot, ValidatorIndex} from "@lodestar/types";
import {GossipActionError} from "./gossipValidation.js";

export enum ProposerPreferencesErrorCode {
INVALID_EPOCH = "PROPOSER_PREFERENCES_ERROR_INVALID_EPOCH",
PROPOSAL_SLOT_PASSED = "PROPOSER_PREFERENCES_ERROR_PROPOSAL_SLOT_PASSED",
UNKNOWN_DEPENDENT_ROOT = "PROPOSER_PREFERENCES_ERROR_UNKNOWN_DEPENDENT_ROOT",
INVALID_PROPOSER = "PROPOSER_PREFERENCES_ERROR_INVALID_PROPOSER",
ALREADY_KNOWN = "PROPOSER_PREFERENCES_ERROR_ALREADY_KNOWN",
INVALID_SIGNATURE = "PROPOSER_PREFERENCES_ERROR_INVALID_SIGNATURE",
Expand All @@ -20,15 +21,22 @@ export type ProposerPreferencesErrorType =
proposalSlot: Slot;
currentSlot: Slot;
}
| {
code: ProposerPreferencesErrorCode.UNKNOWN_DEPENDENT_ROOT;
proposalSlot: Slot;
dependentRoot: RootHex;
}
| {
code: ProposerPreferencesErrorCode.INVALID_PROPOSER;
proposalSlot: Slot;
validatorIndex: ValidatorIndex;
dependentRoot: RootHex;
}
| {
code: ProposerPreferencesErrorCode.ALREADY_KNOWN;
proposalSlot: Slot;
validatorIndex: ValidatorIndex;
dependentRoot: RootHex;
}
| {
code: ProposerPreferencesErrorCode.INVALID_SIGNATURE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import {Slot, ValidatorIndex} from "@lodestar/types";
import {RootHex, Slot, ValidatorIndex} from "@lodestar/types";
import {MapDef} from "@lodestar/utils";

/**
* Tracks signed proposer preferences we've already seen per (proposal_slot, validator_index).
* Tracks signed proposer preferences we've already seen per (dependent_root, proposal_slot, validator_index).
*/
export class SeenProposerPreferences {
private readonly validatorIndexesBySlot = new MapDef<Slot, Set<ValidatorIndex>>(() => new Set<ValidatorIndex>());
private readonly validatorBySlotByDependentRoot = new MapDef<RootHex, Map<Slot, ValidatorIndex>>(
() => new Map<Slot, ValidatorIndex>()
);

isKnown(proposalSlot: Slot, validatorIndex: ValidatorIndex): boolean {
return this.validatorIndexesBySlot.get(proposalSlot)?.has(validatorIndex) === true;
isKnown(dependentRoot: RootHex, proposalSlot: Slot, validatorIndex: ValidatorIndex): boolean {
return this.validatorBySlotByDependentRoot.get(dependentRoot)?.get(proposalSlot) === validatorIndex;
}

add(proposalSlot: Slot, validatorIndex: ValidatorIndex): void {
this.validatorIndexesBySlot.getOrDefault(proposalSlot).add(validatorIndex);
add(dependentRoot: RootHex, proposalSlot: Slot, validatorIndex: ValidatorIndex): void {
this.validatorBySlotByDependentRoot.getOrDefault(dependentRoot).set(proposalSlot, validatorIndex);
}

/**
* Entries are only load-bearing while `proposal_slot > state.slot`. Once the slot has passed the
* `[IGNORE] proposal_slot > state.slot` gossip rule takes over, so drop them on each slot tick.
* Entries are only load-bearing while `proposal_slot > current_slot`. Once the slot has
* passed the `[IGNORE] proposal_slot > current_slot` gossip rule takes over, so drop them
* on each slot tick.
*/
prune(currentSlot: Slot): void {
for (const slot of this.validatorIndexesBySlot.keys()) {
if (slot < currentSlot) {
this.validatorIndexesBySlot.delete(slot);
for (const [dependentRoot, slotMap] of this.validatorBySlotByDependentRoot.entries()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not storing by slot as key of the 1st map? that would help pruning easier

for (const slot of slotMap.keys()) {
if (slot < currentSlot) {
slotMap.delete(slot);
}
}
if (slotMap.size === 0) {
this.validatorBySlotByDependentRoot.delete(dependentRoot);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ async function validateExecutionPayloadBid(
});
}

// [IGNORE] the `SignedProposerPreferences` where `preferences.proposal_slot`
// is equal to `bid.slot` has been seen.
// TODO GLOAS: Implement this along with proposer preference
// [IGNORE] A `SignedProposerPreferences` matching `bid.slot` and the bid's branch has been
// seen — i.e. `proposal_slot == bid.slot` AND `dependent_root ==
// get_proposer_dependent_root(parent_state, compute_epoch_at_slot(bid.slot))`,
// where `parent_state` is the post-state of `bid.parent_block_root`.
// This is the message referenced as `proposer_preferences` in the following REJECT rules.
// TODO GLOAS: Implement once a ProposerPreferencesPool exists.

// [REJECT] `bid.builder_index` is a valid/active builder index -- i.e.
// `is_active_builder(state, bid.builder_index)` returns `True`.
Expand All @@ -71,11 +74,11 @@ async function validateExecutionPayloadBid(
});
}

// [REJECT] `bid.fee_recipient` matches the `fee_recipient` from the proposer's
// `SignedProposerPreferences` associated with `bid.slot`.
// [REJECT] `bid.gas_limit` matches the `gas_limit` from the proposer's
// `SignedProposerPreferences` associated with `bid.slot`.
// TODO GLOAS: Implement this along with proposer preference
// [REJECT] `bid.fee_recipient == proposer_preferences.fee_recipient`.
// [REJECT] `bid.gas_limit == proposer_preferences.gas_limit`.
// Both compared against the matching `proposer_preferences` defined above (same branch
// via dependent_root, same proposal_slot).
// TODO GLOAS: Implement once a ProposerPreferencesPool exists.

// [REJECT] The length of KZG commitments is less than or equal to the limitation defined in the
// consensus layer -- i.e. validate that
Expand Down
55 changes: 37 additions & 18 deletions packages/beacon-node/src/chain/validation/proposerPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import {
computeEpochAtSlot,
createSingleSignatureSetFromComponents,
getProposerPreferencesSigningRoot,
isStatePostGloas,
} from "@lodestar/state-transition";
import {gloas} from "@lodestar/types";
import {ValidatorIndex, gloas} from "@lodestar/types";
import {toRootHex} from "@lodestar/utils";
import {GossipAction, ProposerPreferencesError, ProposerPreferencesErrorCode} from "../errors/index.js";
import {IBeaconChain} from "../index.js";
import {RegenCaller} from "../regen/index.js";

/**
* Validates a gossiped `SignedProposerPreferences` per
Expand All @@ -19,7 +18,8 @@ export async function validateGossipProposerPreferences(
signedProposerPreferences: gloas.SignedProposerPreferences
): Promise<void> {
const preferences = signedProposerPreferences.message;
const {proposalSlot, validatorIndex} = preferences;
const {proposalSlot, validatorIndex, dependentRoot} = preferences;
const dependentRootHex = toRootHex(dependentRoot);
const proposalEpoch = computeEpochAtSlot(proposalSlot);

// [IGNORE] `preferences.proposal_slot` is in the current or next epoch.
Expand All @@ -42,32 +42,51 @@ export async function validateGossipProposerPreferences(
});
}

const state = await chain.getHeadStateAtCurrentEpoch(RegenCaller.validateGossipProposerPreferences);
if (!isStatePostGloas(state)) {
throw new Error(`Expected gloas+ state for proposer preferences validation, got fork=${state.forkName}`);
// [IGNORE] The block with root `dependent_root` has been seen by the node.
// Resolve the proposer lookahead for the message's branch via head state (fast path) or
// the previous-root checkpoint state (populated by `processSlotsToNearestCheckpoint` for
// any imported branch crossing into `proposalEpoch - 1`). The head-state path also handles
// narrow timing windows where the checkpoint state isn't yet populated.
const headState = chain.getHeadState();
let proposers: ValidatorIndex[] | null = null;
if (headState.epoch === proposalEpoch && headState.currentDecisionRoot === dependentRootHex) {
proposers = headState.currentProposers;
} else if (headState.epoch === proposalEpoch - 1 && headState.nextDecisionRoot === dependentRootHex) {
proposers = headState.nextProposers;
} else {
// Sync lookup only to not trigger disk reload from gossip input.
const checkpointState = chain.regen.getCheckpointStateSync({epoch: proposalEpoch - 1, rootHex: dependentRootHex});
if (checkpointState !== null) {
// State is at `proposalEpoch - 1`, so proposers for `proposalSlot` (next epoch from
// the state's perspective) live in `nextProposers`.
proposers = checkpointState.nextProposers;
}
}
if (proposers === null) {
throw new ProposerPreferencesError(GossipAction.IGNORE, {
code: ProposerPreferencesErrorCode.UNKNOWN_DEPENDENT_ROOT,
proposalSlot,
dependentRoot: dependentRootHex,
});
}

// [REJECT] `preferences.validator_index` is present at the correct slot in the current or next
// epoch's portion of `state.proposer_lookahead` — i.e. `is_valid_proposal_slot(state, preferences)`
// returns True.
const epochOffset = proposalEpoch - state.epoch;
const proposers = epochOffset === 0 ? state.currentProposers : state.nextProposers;
const expectedProposer = proposers[proposalSlot % SLOTS_PER_EPOCH];
if (epochOffset < 0 || epochOffset > 1 || expectedProposer !== validatorIndex) {
// [REJECT] `is_valid_proposal_slot(state, preferences)` returns True.
if (proposers[proposalSlot % SLOTS_PER_EPOCH] !== validatorIndex) {
throw new ProposerPreferencesError(GossipAction.REJECT, {
code: ProposerPreferencesErrorCode.INVALID_PROPOSER,
proposalSlot,
validatorIndex,
dependentRoot: dependentRootHex,
});
}

// [IGNORE] The `signed_proposer_preferences` is the first valid message received from the validator
// with index `preferences.validator_index` and the given slot `preferences.proposal_slot`.
if (chain.seenProposerPreferences.isKnown(proposalSlot, validatorIndex)) {
// [IGNORE] First valid message for (dependent_root, proposal_slot, validator_index).
if (chain.seenProposerPreferences.isKnown(dependentRootHex, proposalSlot, validatorIndex)) {
throw new ProposerPreferencesError(GossipAction.IGNORE, {
code: ProposerPreferencesErrorCode.ALREADY_KNOWN,
proposalSlot,
validatorIndex,
dependentRoot: dependentRootHex,
});
}

Expand All @@ -87,5 +106,5 @@ export async function validateGossipProposerPreferences(
}

// Valid
chain.seenProposerPreferences.add(proposalSlot, validatorIndex);
chain.seenProposerPreferences.add(dependentRootHex, proposalSlot, validatorIndex);
}
2 changes: 0 additions & 2 deletions packages/beacon-node/test/spec/utils/specTestIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export const defaultSkipOpts: SkipOpts = {
// TODO-GLOAS: re-enable after gloas light client is implemented
/^gloas\/light_client\/.*/,
/^gloas\/ssz_static\/LightClient(Bootstrap|FinalityUpdate|Header|OptimisticUpdate|Update)\/.*/,
// TODO-GLOAS: re-enable after alpha.7 is released and https://github.com/ChainSafe/lodestar/pull/9303 is merged
/^gloas\/ssz_static\/(Signed)?ProposerPreferences\/.*/,
],
skippedTests: [
// TODO-GLOAS: re-enable after gloas light client is implemented
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/gloas/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const IndexedPayloadAttestation = new ContainerType(

export const ProposerPreferences = new ContainerType(
{
dependentRoot: Root,
proposalSlot: Slot,
validatorIndex: ValidatorIndex,
feeRecipient: ExecutionAddress,
Expand Down
2 changes: 1 addition & 1 deletion spec-tests-version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ethereumConsensusSpecsTests": {
"specVersion": "v1.7.0-alpha.6",
"specVersion": "v1.7.0-alpha.7",
"specTestsRepoUrl": "https://github.com/ethereum/consensus-specs",
"outputDirBase": "spec-tests",
"testsToDownload": [
Expand Down
Loading