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: add blob_sidecar sse event #6044

Merged
merged 4 commits into from
Oct 20, 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
21 changes: 19 additions & 2 deletions packages/api/src/beacon/routes/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ContainerType} from "@chainsafe/ssz";
import {ContainerType, ValueOf} from "@chainsafe/ssz";
import {Epoch, phase0, capella, Slot, ssz, StringType, RootHex, altair, UintNum64, allForks} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {isForkExecution, ForkName} from "@lodestar/params";
Expand All @@ -7,6 +7,19 @@ import {RouteDef, TypeJson, WithVersion} from "../../utils/index.js";
import {HttpStatusCode} from "../../utils/client/httpStatusCode.js";
import {ApiClientResponse} from "../../interfaces.js";

const stringType = new StringType();
export const blobSidecarSSE = new ContainerType(
{
blockRoot: stringType,
index: ssz.BlobIndex,
slot: ssz.Slot,
kzgCommitment: stringType,
versionedHash: stringType,
},
{typeName: "BlobSidecarSSE", jsonCase: "eth2"}
);
type BlobSidecarSSE = ValueOf<typeof blobSidecarSSE>;

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

export enum EventType {
Expand Down Expand Up @@ -39,6 +52,8 @@ export enum EventType {
lightClientUpdate = "light_client_update",
/** Payload attributes for block proposal */
payloadAttributes = "payload_attributes",
/** The node has received a valid blobSidecar (from P2P or API) */
blobSidecar = "blob_sidecar",
}

export const eventTypes: {[K in EventType]: K} = {
Expand All @@ -54,6 +69,7 @@ export const eventTypes: {[K in EventType]: K} = {
[EventType.lightClientFinalityUpdate]: EventType.lightClientFinalityUpdate,
[EventType.lightClientUpdate]: EventType.lightClientUpdate,
[EventType.payloadAttributes]: EventType.payloadAttributes,
[EventType.blobSidecar]: EventType.blobSidecar,
};

export type EventData = {
Expand Down Expand Up @@ -95,6 +111,7 @@ export type EventData = {
[EventType.lightClientFinalityUpdate]: allForks.LightClientFinalityUpdate;
[EventType.lightClientUpdate]: allForks.LightClientUpdate;
[EventType.payloadAttributes]: {version: ForkName; data: allForks.SSEPayloadAttributes};
[EventType.blobSidecar]: BlobSidecarSSE;
};

export type BeaconEvent = {[K in EventType]: {type: K; message: EventData[K]}}[EventType];
Expand Down Expand Up @@ -130,7 +147,6 @@ export type ReqTypes = {
// The request is very simple: (topics) => {query: {topics}}, and the test will ensure compatibility server - client

export function getTypeByEvent(config: ChainForkConfig): {[K in EventType]: TypeJson<EventData[K]>} {
const stringType = new StringType();
const getLightClientTypeFromHeader = (data: allForks.LightClientHeader): allForks.AllForksLightClientSSZTypes => {
return config.getLightClientForkTypes(data.beacon.slot);
};
Expand Down Expand Up @@ -190,6 +206,7 @@ export function getTypeByEvent(config: ChainForkConfig): {[K in EventType]: Type
[EventType.payloadAttributes]: WithVersion((fork) =>
isForkExecution(fork) ? ssz.allForksExecution[fork].SSEPayloadAttributes : ssz.bellatrix.SSEPayloadAttributes
),
[EventType.blobSidecar]: blobSidecarSSE,

[EventType.lightClientOptimisticUpdate]: {
toJson: (data) =>
Expand Down
3 changes: 2 additions & 1 deletion packages/api/test/unit/beacon/testData/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ssz} from "@lodestar/types";
import {ForkName} from "@lodestar/params";
import {Api, EventData, EventType} from "../../../../src/beacon/routes/events.js";
import {Api, EventData, EventType, blobSidecarSSE} from "../../../../src/beacon/routes/events.js";
import {GenericServerTestCases} from "../../../utils/genericServerTest.js";

const abortController = new AbortController();
Expand Down Expand Up @@ -109,4 +109,5 @@ export const eventTestData: EventData = {
version: ForkName.bellatrix,
data: ssz.bellatrix.SSEPayloadAttributes.defaultValue(),
},
[EventType.blobSidecar]: blobSidecarSSE.defaultValue(),
};
22 changes: 19 additions & 3 deletions packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
computeStartSlotAtEpoch,
isStateValidatorsNodesPopulated,
RootCache,
kzgCommitmentToVersionedHash,
} from "@lodestar/state-transition";
import {routes} from "@lodestar/api";
import {ForkChoiceError, ForkChoiceErrorCode, EpochDifference, AncestorStatus} from "@lodestar/fork-choice";
Expand All @@ -18,7 +19,7 @@ import {isQueueErrorAborted} from "../../util/queue/index.js";
import {ChainEvent, ReorgEventData} from "../emitter.js";
import {REPROCESS_MIN_TIME_TO_NEXT_SLOT_SEC} from "../reprocess.js";
import type {BeaconChain} from "../chain.js";
import {FullyVerifiedBlock, ImportBlockOpts, AttestationImportOpt} from "./types.js";
import {FullyVerifiedBlock, ImportBlockOpts, AttestationImportOpt, BlockInputType} from "./types.js";
import {getCheckpointFromState} from "./utils/checkpoint.js";
import {writeBlockInputToDb} from "./writeBlockInputToDb.js";

Expand Down Expand Up @@ -89,13 +90,28 @@ export async function importBlock(

this.metrics?.importBlock.bySource.inc({source});
this.logger.verbose("Added block to forkchoice and state cache", {slot: block.message.slot, root: blockRootHex});

// We want to import block asap so call all event handler in the next event loop
setTimeout(() => {
const slot = block.message.slot;
this.emitter.emit(routes.events.EventType.block, {
block: toHexString(this.config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message)),
slot: block.message.slot,
block: blockRootHex,
slot,
executionOptimistic: blockSummary != null && isOptimisticBlock(blockSummary),
});

if (blockInput.type === BlockInputType.postDeneb) {
for (const blobSidecar of blockInput.blobs) {
const {index, kzgCommitment} = blobSidecar;
this.emitter.emit(routes.events.EventType.blobSidecar, {
blockRoot: blockRootHex,
slot,
index,
kzgCommitment: toHexString(kzgCommitment),
versionedHash: toHexString(kzgCommitmentToVersionedHash(kzgCommitment)),
});
}
}
}, 0);

// 3. Import attestations to fork choice
Expand Down