Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion packages/api/src/beacon/routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export enum EventType {
lightClientFinalityUpdate = "light_client_finality_update",
/** Payload attributes for block proposal */
payloadAttributes = "payload_attributes",
/** The node has received a valid blobSidecar (from P2P or API) */
/** The node has received a valid BlobSidecar (from P2P or API) */
blobSidecar = "blob_sidecar",
}

Expand Down
38 changes: 33 additions & 5 deletions packages/beacon-node/src/api/impl/beacon/blocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {routes} from "@lodestar/api";
import {ApiError, ApplicationMethods} from "@lodestar/api/server";
import {ForkPostBellatrix, SLOTS_PER_HISTORICAL_ROOT, isForkPostBellatrix, isForkPostElectra} from "@lodestar/params";
import {
ForkName,
ForkPostBellatrix,
SLOTS_PER_HISTORICAL_ROOT,
isForkPostBellatrix,
isForkPostElectra,
} from "@lodestar/params";
import {
computeEpochAtSlot,
computeTimeAtSlot,
Expand All @@ -16,11 +22,12 @@ import {
deneb,
isSignedBlockContents,
} from "@lodestar/types";
import {fromHex, sleep, toRootHex} from "@lodestar/utils";
import {fromHex, sleep, toHex, toRootHex} from "@lodestar/utils";
import {
BlobsSource,
BlockInput,
BlockInputDataBlobs,
BlockInputType,
BlockSource,
ImportBlockOpts,
getBlockInput,
Expand All @@ -31,7 +38,7 @@ import {BlockError, BlockErrorCode, BlockGossipError} from "../../../../chain/er
import {validateGossipBlock} from "../../../../chain/validation/block.js";
import {OpSource} from "../../../../chain/validatorMonitor.js";
import {NetworkEvent} from "../../../../network/index.js";
import {computeBlobSidecars} from "../../../../util/blobs.js";
import {computeBlobSidecars, kzgCommitmentToVersionedHash} from "../../../../util/blobs.js";
import {isOptimisticBlock} from "../../../../util/forkChoice.js";
import {promiseAllMaybeAsync} from "../../../../util/promises.js";
import {ApiModules} from "../../types.js";
Expand Down Expand Up @@ -200,8 +207,6 @@ export function getBeaconBlockApi({
await sleep(msToBlockSlot);
}

chain.emitter.emit(routes.events.EventType.blockGossip, {slot, block: blockRoot});
Copy link
Member Author

Choose a reason for hiding this comment

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

moved event down to emit it after we publish to the network


// TODO: Validate block
const delaySec =
seenTimestampSec - (chain.genesisTime + blockForImport.block.message.slot * config.SECONDS_PER_SLOT);
Expand Down Expand Up @@ -235,6 +240,29 @@ export function getBeaconBlockApi({
}),
];
await promiseAllMaybeAsync(publishPromises);

if (chain.emitter.listenerCount(routes.events.EventType.blockGossip)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

we should probably add this check to a bunch more events, I don't see why we would emit them if there is no listener

chain.emitter.emit(routes.events.EventType.blockGossip, {slot, block: blockRoot});
}

if (
chain.emitter.listenerCount(routes.events.EventType.blobSidecar) &&
blockForImport.type === BlockInputType.availableData &&
(blockForImport.blockData.fork === ForkName.deneb || blockForImport.blockData.fork === ForkName.electra)
) {
const {blobs} = blockForImport.blockData;

for (const blobSidecar of blobs) {
const {index, kzgCommitment} = blobSidecar;
chain.emitter.emit(routes.events.EventType.blobSidecar, {
blockRoot,
slot,
index,
kzgCommitment: toHex(kzgCommitment),
versionedHash: toHex(kzgCommitmentToVersionedHash(kzgCommitment)),
});
}
}
};

const publishBlindedBlock: ApplicationMethods<routes.beacon.block.Endpoints>["publishBlindedBlock"] = async (
Expand Down
16 changes: 0 additions & 16 deletions packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,6 @@ export async function importBlock(
this.emitter.emit(routes.events.EventType.proposerSlashing, proposerSlashing);
}
}
if (
blockInput.type === BlockInputType.availableData &&
this.emitter.listenerCount(routes.events.EventType.blobSidecar)
) {
const {blobs} = blockInput.blockData;
for (const blobSidecar of blobs) {
const {index, kzgCommitment} = blobSidecar;
this.emitter.emit(routes.events.EventType.blobSidecar, {
blockRoot: blockRootHex,
slot: blockSlot,
index,
kzgCommitment: toHex(kzgCommitment),
versionedHash: toHex(kzgCommitmentToVersionedHash(kzgCommitment)),
});
}
}
});
}

Expand Down
25 changes: 19 additions & 6 deletions packages/beacon-node/src/network/processor/gossipHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ssz,
sszTypesFor,
} from "@lodestar/types";
import {LogLevel, Logger, prettyBytes, toRootHex} from "@lodestar/utils";
import {LogLevel, Logger, prettyBytes, toHex, toRootHex} from "@lodestar/utils";
import {
BlobSidecarValidation,
BlockInput,
Expand Down Expand Up @@ -53,6 +53,7 @@ import {validateLightClientFinalityUpdate} from "../../chain/validation/lightCli
import {validateLightClientOptimisticUpdate} from "../../chain/validation/lightClientOptimisticUpdate.js";
import {OpSource} from "../../chain/validatorMonitor.js";
import {Metrics} from "../../metrics/index.js";
import {kzgCommitmentToVersionedHash} from "../../util/blobs.js";
import {INetworkCore} from "../core/index.js";
import {NetworkEvent, NetworkEventBus} from "../events.js";
import {
Expand Down Expand Up @@ -168,7 +169,9 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand

logger.debug("Validated gossip block", {...logCtx, recvToValidation, validationTime});

chain.emitter.emit(routes.events.EventType.blockGossip, {slot, block: blockRootHex});
if (chain.emitter.listenerCount(routes.events.EventType.blockGossip)) {
chain.emitter.emit(routes.events.EventType.blockGossip, {slot, block: blockRootHex});
}

return blockInput;
} catch (e) {
Expand Down Expand Up @@ -197,8 +200,8 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand
const blobBlockHeader = blobSidecar.signedBlockHeader.message;
const slot = blobBlockHeader.slot;
const fork = config.getForkName(slot);
const blockRoot = ssz.phase0.BeaconBlockHeader.hashTreeRoot(blobBlockHeader);
const blockHex = prettyBytes(blockRoot);
const blockRootHex = toRootHex(ssz.phase0.BeaconBlockHeader.hashTreeRoot(blobBlockHeader));
const blockShortHex = prettyBytes(blockRootHex);

const delaySec = chain.clock.secFromSlot(slot, seenTimestampSec);
const recvToValLatency = Date.now() / 1000 - seenTimestampSec;
Expand All @@ -220,9 +223,19 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand
metrics?.gossipBlob.recvToValidation.observe(recvToValidation);
metrics?.gossipBlob.validationTime.observe(validationTime);

if (chain.emitter.listenerCount(routes.events.EventType.blobSidecar)) {
chain.emitter.emit(routes.events.EventType.blobSidecar, {
blockRoot: blockRootHex,
slot,
index: blobSidecar.index,
kzgCommitment: toHex(blobSidecar.kzgCommitment),
versionedHash: toHex(kzgCommitmentToVersionedHash(blobSidecar.kzgCommitment)),
});
}

logger.debug("Received gossip blob", {
slot: slot,
root: blockHex,
root: blockShortHex,
currentSlot: chain.clock.currentSlot,
peerId: peerIdStr,
delaySec,
Expand All @@ -238,7 +251,7 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand
if (e instanceof BlobSidecarGossipError) {
// Don't trigger this yet if full block and blobs haven't arrived yet
if (e.type.code === BlobSidecarErrorCode.PARENT_UNKNOWN && blockInput.block !== null) {
logger.debug("Gossip blob has error", {slot, root: blockHex, code: e.type.code});
logger.debug("Gossip blob has error", {slot, root: blockShortHex, code: e.type.code});
events.emit(NetworkEvent.unknownBlockParent, {blockInput, peer: peerIdStr});
}

Expand Down
Loading