Skip to content

Commit

Permalink
fix: handle PreDeneb for SeenGossipBlockInput
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jan 13, 2024
1 parent d373fc0 commit a7fe6c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
17 changes: 13 additions & 4 deletions packages/beacon-node/src/chain/seenCache/seenGossipBlockInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {toHexString} from "@chainsafe/ssz";
import {deneb, RootHex, ssz, allForks} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {pruneSetToMax} from "@lodestar/utils";
import {BLOBSIDECAR_FIXED_SIZE} from "@lodestar/params";
import {BLOBSIDECAR_FIXED_SIZE, ForkSeq} from "@lodestar/params";

import {
BlockInput,
Expand All @@ -29,9 +29,11 @@ type BlockInputCacheType = {
const MAX_GOSSIPINPUT_CACHE = 5;

/**
* SeenGossipBlockInput tracks and caches the live blobs and blocks on the network to solve data availability
* for the blockInput. If no block has been seen yet for some already seen blobs, it responds will null, but
* on the first block or the consequent blobs it responds with blobs promise till all blobs become available.
* For predeneb, SeenGossipBlockInput only tracks and caches block so that we don't need to download known block
* roots. From deneb, it serves same purpose plus tracks and caches the live blobs and blocks on the network to
* solve data availability for the blockInput. If no block has been seen yet for some already seen blobs, it
* responds will null, but on the first block or the consequent blobs it responds with blobs promise till all blobs
* become available.
*
* One can start processing block on blobs promise blockInput response and can await on the promise before
* fully importing the block. The blobs promise is gets resolved as soon as all blobs corresponding to that
Expand Down Expand Up @@ -87,9 +89,16 @@ export class SeenGossipBlockInput {
if (!this.blockInputCache.has(blockHex)) {
this.blockInputCache.set(blockHex, blockCache);
}

const {block: signedBlock, blockBytes, blobsCache, availabilityPromise, resolveAvailability} = blockCache;

if (signedBlock !== undefined) {
if (config.getForkSeq(signedBlock.message.slot) < ForkSeq.deneb) {
return {
blockInput: getBlockInput.preDeneb(config, signedBlock, BlockSource.gossip, blockBytes ?? null),
blockInputMeta: {pending: null, haveBlobs: 0, expectedBlobs: 0},
};
}
// block is available, check if all blobs have shown up
const {slot, body} = signedBlock.message;
const {blobKzgCommitments} = body as deneb.BeaconBlockBody;
Expand Down
32 changes: 9 additions & 23 deletions packages/beacon-node/src/network/processor/gossipHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ import {PeerAction} from "../peers/index.js";
import {validateLightClientFinalityUpdate} from "../../chain/validation/lightClientFinalityUpdate.js";
import {validateLightClientOptimisticUpdate} from "../../chain/validation/lightClientOptimisticUpdate.js";
import {validateGossipBlobSidecar} from "../../chain/validation/blobSidecar.js";
import {
BlockInput,
BlockSource,
getBlockInput,
GossipedInputType,
BlobSidecarValidation,
} from "../../chain/blocks/types.js";
import {BlockInput, GossipedInputType, BlobSidecarValidation} from "../../chain/blocks/types.js";
import {sszDeserialize} from "../gossip/topic.js";
import {INetworkCore} from "../core/index.js";
import {INetwork} from "../interface.js";
Expand Down Expand Up @@ -123,29 +117,21 @@ function getDefaultHandlers(modules: ValidatorFnsModules, options: GossipHandler
const delaySec = chain.clock.secFromSlot(slot, seenTimestampSec);
const recvToVal = Date.now() / 1000 - seenTimestampSec;

let blockInput;
let blockInputMeta;

// always set block to seen cache for all forks so that we don't need to download it
const blockInputRes = chain.seenGossipBlockInput.getGossipBlockInput(config, {
type: GossipedInputType.block,
signedBlock,
blockBytes,
});
if (config.getForkSeq(signedBlock.message.slot) >= ForkSeq.deneb) {
blockInput = blockInputRes.blockInput;
blockInputMeta = blockInputRes.blockInputMeta;

// blockInput can't be returned null, improve by enforcing via return types
if (blockInput === null) {
throw Error(
`Invalid null blockInput returned by getGossipBlockInput for type=${GossipedInputType.block} blockHex=${blockHex} slot=${slot}`
);
}
} else {
blockInput = getBlockInput.preDeneb(config, signedBlock, BlockSource.gossip, blockBytes);
blockInputMeta = {};
const blockInput = blockInputRes.blockInput;
// blockInput can't be returned null, improve by enforcing via return types
if (blockInput === null) {
throw Error(
`Invalid null blockInput returned by getGossipBlockInput for type=${GossipedInputType.block} blockHex=${blockHex} slot=${slot}`
);
}
const blockInputMeta =
config.getForkSeq(signedBlock.message.slot) >= ForkSeq.deneb ? blockInputRes.blockInputMeta : {};

metrics?.gossipBlock.receivedToGossipValidate.observe(recvToVal);
logger.verbose("Received gossip block", {
Expand Down

0 comments on commit a7fe6c9

Please sign in to comment.