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

feat: only extract roots of early gossip messages #7208

Open
wants to merge 2 commits into
base: unstable
Choose a base branch
from
Open
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
37 changes: 27 additions & 10 deletions packages/beacon-node/src/network/processor/extractSlotRootFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,40 @@ import {ExtractSlotRootFns} from "./types.js";
*/
export function createExtractBlockSlotRootFns(): ExtractSlotRootFns {
return {
[GossipType.beacon_attestation]: (data: Uint8Array): SlotRootHex | null => {
[GossipType.beacon_attestation]: (data: Uint8Array, extractRoot: boolean): SlotOptionalRoot | null => {
const slot = getSlotFromAttestationSerialized(data);
const root = getBlockRootFromAttestationSerialized(data);

if (slot === null || root === null) {
if (slot === null) {
return null;
}
return {slot, root};

if (extractRoot) {
const root = getBlockRootFromAttestationSerialized(data);
if (root === null) {
return null;
}

return {slot, root};
}

return {slot};
},
[GossipType.beacon_aggregate_and_proof]: (data: Uint8Array): SlotRootHex | null => {
[GossipType.beacon_aggregate_and_proof]: (data: Uint8Array, extractRoot: boolean): SlotOptionalRoot | null => {
const slot = getSlotFromSignedAggregateAndProofSerialized(data);
const root = getBlockRootFromSignedAggregateAndProofSerialized(data);

if (slot === null || root === null) {
if (slot === null) {
return null;
}
return {slot, root};

if (extractRoot) {
const root = getBlockRootFromSignedAggregateAndProofSerialized(data);

if (root === null) {
return null;
}

return {slot, root};
}

return {slot};
},
[GossipType.beacon_block]: (data: Uint8Array): SlotOptionalRoot | null => {
const slot = getSlotFromSignedBeaconBlockSerialized(data);
Expand Down
10 changes: 9 additions & 1 deletion packages/beacon-node/src/network/processor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class NetworkProcessor {
private readonly awaitingGossipsubMessagesByRootBySlot: MapDef<Slot, MapDef<RootHex, Set<PendingGossipsubMessage>>>;
private unknownBlockGossipsubMessagesCount = 0;
private isProcessingCurrentSlotBlock = false;
private receivedGossipBlock = false;
private unknownRootsBySlot = new MapDef<Slot, Set<RootHex>>(() => new Set());

constructor(
Expand Down Expand Up @@ -247,7 +248,10 @@ export class NetworkProcessor {
const extractBlockSlotRootFn = this.extractBlockSlotRootFns[topicType];
// check block root of Attestation and SignedAggregateAndProof messages
if (extractBlockSlotRootFn) {
const slotRoot = extractBlockSlotRootFn(message.msg.data);
// only extract roots for early gossip messages
// see https://github.com/ChainSafe/lodestar/issues/7205
const extractRoot = !this.receivedGossipBlock;
const slotRoot = extractBlockSlotRootFn(message.msg.data, extractRoot);
// if slotRoot is null, it means the msg.data is invalid
// in that case message will be rejected when deserializing data in later phase (gossipValidatorFn)
if (slotRoot) {
Expand Down Expand Up @@ -319,6 +323,9 @@ export class NetworkProcessor {
executionOptimistic: boolean;
}): Promise<void> {
this.isProcessingCurrentSlotBlock = false;
if (slot === this.chain.clock.currentSlot) {
this.receivedGossipBlock = true;
}
const byRootGossipsubMessages = this.awaitingGossipsubMessagesByRootBySlot.getOrDefault(slot);
const waitingGossipsubMessages = byRootGossipsubMessages.getOrDefault(rootHex);
if (waitingGossipsubMessages.size === 0) {
Expand Down Expand Up @@ -346,6 +353,7 @@ export class NetworkProcessor {

private onClockSlot(clockSlot: Slot): void {
this.isProcessingCurrentSlotBlock = false;
this.receivedGossipBlock = false;
const nowSec = Date.now() / 1000;
for (const [slot, gossipMessagesByRoot] of this.awaitingGossipsubMessagesByRootBySlot.entries()) {
if (slot < clockSlot) {
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/processor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export type PendingGossipsubMessage = {
};

export type ExtractSlotRootFns = {
[K in GossipType]?: (data: Uint8Array) => SlotOptionalRoot | null;
[K in GossipType]?: (data: Uint8Array, extractRoot: boolean) => SlotOptionalRoot | null;
};
Loading