-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
index.ts
502 lines (454 loc) Β· 19.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import {routes} from "@lodestar/api";
import {ApplicationMethods} from "@lodestar/api/server";
import {
computeEpochAtSlot,
computeTimeAtSlot,
reconstructFullBlockOrContents,
signedBeaconBlockToBlinded,
} from "@lodestar/state-transition";
import {ForkExecution, SLOTS_PER_HISTORICAL_ROOT, isForkExecution} from "@lodestar/params";
import {sleep, fromHex, toHex} from "@lodestar/utils";
import {
deneb,
isSignedBlockContents,
ProducedBlockSource,
SignedBeaconBlock,
SignedBeaconBlockOrContents,
SignedBlindedBeaconBlock,
} from "@lodestar/types";
import {
BlockSource,
getBlockInput,
ImportBlockOpts,
BlockInput,
BlobsSource,
BlockInputDataBlobs,
} from "../../../../chain/blocks/types.js";
import {promiseAllMaybeAsync} from "../../../../util/promises.js";
import {isOptimisticBlock} from "../../../../util/forkChoice.js";
import {computeBlobSidecars} from "../../../../util/blobs.js";
import {BlockError, BlockErrorCode, BlockGossipError} from "../../../../chain/errors/index.js";
import {OpSource} from "../../../../metrics/validatorMonitor.js";
import {NetworkEvent} from "../../../../network/index.js";
import {ApiModules} from "../../types.js";
import {validateGossipBlock} from "../../../../chain/validation/block.js";
import {verifyBlocksInEpoch} from "../../../../chain/blocks/verifyBlock.js";
import {BeaconChain} from "../../../../chain/chain.js";
import {getBlockResponse, toBeaconHeaderResponse} from "./utils.js";
type PublishBlockOpts = ImportBlockOpts;
/**
* Validator clock may be advanced from beacon's clock. If the validator requests a resource in a
* future slot, wait some time instead of rejecting the request because it's in the future
*/
const MAX_API_CLOCK_DISPARITY_MS = 1000;
/**
* PeerID of identity keypair to signal self for score reporting
*/
const IDENTITY_PEER_ID = ""; // TODO: Compute identity keypair
export function getBeaconBlockApi({
chain,
config,
metrics,
network,
db,
}: Pick<
ApiModules,
"chain" | "config" | "metrics" | "network" | "db"
>): ApplicationMethods<routes.beacon.block.Endpoints> {
const publishBlock: ApplicationMethods<routes.beacon.block.Endpoints>["publishBlockV2"] = async (
{signedBlockOrContents, broadcastValidation},
context,
opts: PublishBlockOpts = {}
) => {
const seenTimestampSec = Date.now() / 1000;
let blockForImport: BlockInput, signedBlock: SignedBeaconBlock, blobSidecars: deneb.BlobSidecars;
if (isSignedBlockContents(signedBlockOrContents)) {
({signedBlock} = signedBlockOrContents);
blobSidecars = computeBlobSidecars(config, signedBlock, signedBlockOrContents);
const blockData = {
fork: config.getForkName(signedBlock.message.slot),
blobs: blobSidecars,
blobsSource: BlobsSource.api,
blobsBytes: blobSidecars.map(() => null),
} as BlockInputDataBlobs;
blockForImport = getBlockInput.availableData(
config,
signedBlock,
BlockSource.api,
// don't bundle any bytes for block and blobs
null,
blockData
);
} else {
signedBlock = signedBlockOrContents;
blobSidecars = [];
blockForImport = getBlockInput.preData(config, signedBlock, BlockSource.api, context?.sszBytes ?? null);
}
// check what validations have been requested before broadcasting and publishing the block
// TODO: add validation time to metrics
broadcastValidation = broadcastValidation ?? routes.beacon.BroadcastValidation.gossip;
// if block is locally produced, full or blinded, it already is 'consensus' validated as it went through
// state transition to produce the stateRoot
const slot = signedBlock.message.slot;
const fork = config.getForkName(slot);
const blockRoot = toHex(chain.config.getForkTypes(slot).BeaconBlock.hashTreeRoot(signedBlock.message));
// bodyRoot should be the same to produced block
const bodyRoot = toHex(chain.config.getForkTypes(slot).BeaconBlockBody.hashTreeRoot(signedBlock.message.body));
const blockLocallyProduced =
chain.producedBlockRoot.has(blockRoot) || chain.producedBlindedBlockRoot.has(blockRoot);
const valLogMeta = {slot, blockRoot, bodyRoot, broadcastValidation, blockLocallyProduced};
switch (broadcastValidation) {
case routes.beacon.BroadcastValidation.gossip: {
if (!blockLocallyProduced) {
try {
await validateGossipBlock(config, chain, signedBlock, fork);
} catch (error) {
if (error instanceof BlockGossipError && error.type.code === BlockErrorCode.ALREADY_KNOWN) {
chain.logger.debug("Ignoring known block during publishing", valLogMeta);
// Blocks might already be published by another node as part of a fallback setup or DVT cluster
// and can reach our node by gossip before the api. The error can be ignored and should not result in a 500 response.
return;
}
chain.logger.error("Gossip validations failed while publishing the block", valLogMeta, error as Error);
chain.persistInvalidSszValue(
chain.config.getForkTypes(slot).SignedBeaconBlock,
signedBlock,
"api_reject_gossip_failure"
);
throw error;
}
}
chain.logger.debug("Gossip checks validated while publishing the block", valLogMeta);
break;
}
case routes.beacon.BroadcastValidation.consensusAndEquivocation:
case routes.beacon.BroadcastValidation.consensus: {
// check if this beacon node produced the block else run validations
if (!blockLocallyProduced) {
const parentBlock = chain.forkChoice.getBlock(signedBlock.message.parentRoot);
if (parentBlock === null) {
network.events.emit(NetworkEvent.unknownBlockParent, {
blockInput: blockForImport,
peer: IDENTITY_PEER_ID,
});
chain.persistInvalidSszValue(
chain.config.getForkTypes(slot).SignedBeaconBlock,
signedBlock,
"api_reject_parent_unknown"
);
throw new BlockError(signedBlock, {
code: BlockErrorCode.PARENT_UNKNOWN,
parentRoot: toHex(signedBlock.message.parentRoot),
});
}
try {
await verifyBlocksInEpoch.call(chain as BeaconChain, parentBlock, [blockForImport], {
...opts,
verifyOnly: true,
skipVerifyBlockSignatures: true,
skipVerifyExecutionPayload: true,
seenTimestampSec,
});
} catch (error) {
chain.logger.error("Consensus checks failed while publishing the block", valLogMeta, error as Error);
chain.persistInvalidSszValue(
chain.config.getForkTypes(slot).SignedBeaconBlock,
signedBlock,
"api_reject_consensus_failure"
);
throw error;
}
}
chain.logger.debug("Consensus validated while publishing block", valLogMeta);
if (broadcastValidation === routes.beacon.BroadcastValidation.consensusAndEquivocation) {
const message = `Equivocation checks not yet implemented for broadcastValidation=${broadcastValidation}`;
if (chain.opts.broadcastValidationStrictness === "error") {
throw Error(message);
} else {
chain.logger.warn(message, valLogMeta);
}
}
break;
}
case routes.beacon.BroadcastValidation.none: {
chain.logger.debug("Skipping broadcast validation", valLogMeta);
break;
}
default: {
// error or log warning we do not support this validation
const message = `Broadcast validation of ${broadcastValidation} type not implemented yet`;
if (chain.opts.broadcastValidationStrictness === "error") {
throw Error(message);
} else {
chain.logger.warn(message, valLogMeta);
}
}
}
// Simple implementation of a pending block queue. Keeping the block here recycles the API logic, and keeps the
// REST request promise without any extra infrastructure.
const msToBlockSlot =
computeTimeAtSlot(config, blockForImport.block.message.slot, chain.genesisTime) * 1000 - Date.now();
if (msToBlockSlot <= MAX_API_CLOCK_DISPARITY_MS && msToBlockSlot > 0) {
// If block is a bit early, hold it in a promise. Equivalent to a pending queue.
await sleep(msToBlockSlot);
}
// TODO: Validate block
metrics?.registerBeaconBlock(OpSource.api, seenTimestampSec, blockForImport.block.message);
chain.logger.info("Publishing block", valLogMeta);
const publishPromises = [
// Send the block, regardless of whether or not it is valid. The API
// specification is very clear that this is the desired behaviour.
//
// i) Publish blobs and block before importing so that network can see them asap
// ii) publish blobs first because
// a) by the times nodes see block, they might decide to pull blobs
// b) they might require more hops to reach recipients in peerDAS kind of setup where
// blobs might need to hop between nodes because of partial subnet subscription
...blobSidecars.map((blobSidecar) => () => network.publishBlobSidecar(blobSidecar)),
() => network.publishBeaconBlock(signedBlock) as Promise<unknown>,
() =>
// there is no rush to persist block since we published it to gossip anyway
chain.processBlock(blockForImport, {...opts, eagerPersistBlock: false}).catch((e) => {
if (e instanceof BlockError && e.type.code === BlockErrorCode.PARENT_UNKNOWN) {
network.events.emit(NetworkEvent.unknownBlockParent, {
blockInput: blockForImport,
peer: IDENTITY_PEER_ID,
});
}
throw e;
}),
];
await promiseAllMaybeAsync(publishPromises);
};
const publishBlindedBlock: ApplicationMethods<routes.beacon.block.Endpoints>["publishBlindedBlock"] = async (
{signedBlindedBlock},
context,
opts: PublishBlockOpts = {}
) => {
const slot = signedBlindedBlock.message.slot;
const blockRoot = toHex(
chain.config
.getExecutionForkTypes(signedBlindedBlock.message.slot)
.BlindedBeaconBlock.hashTreeRoot(signedBlindedBlock.message)
);
// Either the payload/blobs are cached from i) engine locally or ii) they are from the builder
//
// executionPayload can be null or a real payload in locally produced so check for presence of root
const executionPayload = chain.producedBlockRoot.get(blockRoot);
if (executionPayload !== undefined) {
const source = ProducedBlockSource.engine;
chain.logger.debug("Reconstructing signedBlockOrContents", {slot, blockRoot, source});
const contents = executionPayload
? chain.producedContentsCache.get(toHex(executionPayload.blockHash)) ?? null
: null;
const signedBlockOrContents = reconstructFullBlockOrContents(signedBlindedBlock, {executionPayload, contents});
chain.logger.info("Publishing assembled block", {slot, blockRoot, source});
return publishBlock({signedBlockOrContents}, {...context, sszBytes: null}, opts);
} else {
const source = ProducedBlockSource.builder;
chain.logger.debug("Reconstructing signedBlockOrContents", {slot, blockRoot, source});
const signedBlockOrContents = await reconstructBuilderBlockOrContents(chain, signedBlindedBlock);
// the full block is published by relay and it's possible that the block is already known to us
// by gossip
//
// see: https://github.com/ChainSafe/lodestar/issues/5404
chain.logger.info("Publishing assembled block", {slot, blockRoot, source});
return publishBlock({signedBlockOrContents}, {...context, sszBytes: null}, {...opts, ignoreIfKnown: true});
}
};
return {
async getBlockHeaders({slot, parentRoot}) {
// TODO - SLOW CODE: This code seems like it could be improved
// If one block in the response contains an optimistic block, mark the entire response as optimistic
let executionOptimistic = false;
// If one block in the response is non finalized, mark the entire response as unfinalized
let finalized = true;
const result: routes.beacon.BlockHeaderResponse[] = [];
if (parentRoot) {
const finalizedBlock = await db.blockArchive.getByParentRoot(fromHex(parentRoot));
if (finalizedBlock) {
result.push(toBeaconHeaderResponse(config, finalizedBlock, true));
}
const nonFinalizedBlocks = chain.forkChoice.getBlockSummariesByParentRoot(parentRoot);
await Promise.all(
nonFinalizedBlocks.map(async (summary) => {
const block = await db.block.get(fromHex(summary.blockRoot));
if (block) {
const canonical = chain.forkChoice.getCanonicalBlockAtSlot(block.message.slot);
if (canonical) {
result.push(toBeaconHeaderResponse(config, block, canonical.blockRoot === summary.blockRoot));
if (isOptimisticBlock(canonical)) {
executionOptimistic = true;
}
// Block from hot db which only contains unfinalized blocks
finalized = false;
}
}
})
);
return {
data: result.filter(
(item) =>
// skip if no slot filter
!(slot !== undefined && slot !== 0) || item.header.message.slot === slot
),
meta: {executionOptimistic, finalized},
};
}
const headSlot = chain.forkChoice.getHead().slot;
if (!parentRoot && slot === undefined) {
slot = headSlot;
}
if (slot !== undefined) {
// future slot
if (slot > headSlot) {
return {data: [], meta: {executionOptimistic: false, finalized: false}};
}
const canonicalBlock = await chain.getCanonicalBlockAtSlot(slot);
// skip slot
if (!canonicalBlock) {
return {data: [], meta: {executionOptimistic: false, finalized: false}};
}
const canonicalRoot = config
.getForkTypes(canonicalBlock.block.message.slot)
.BeaconBlock.hashTreeRoot(canonicalBlock.block.message);
result.push(toBeaconHeaderResponse(config, canonicalBlock.block, true));
if (!canonicalBlock.finalized) {
finalized = false;
}
// fork blocks
// TODO: What is this logic?
await Promise.all(
chain.forkChoice.getBlockSummariesAtSlot(slot).map(async (summary) => {
if (isOptimisticBlock(summary)) {
executionOptimistic = true;
}
finalized = false;
if (summary.blockRoot !== toHex(canonicalRoot)) {
const block = await db.block.get(fromHex(summary.blockRoot));
if (block) {
result.push(toBeaconHeaderResponse(config, block));
}
}
})
);
}
return {
data: result,
meta: {executionOptimistic, finalized},
};
},
async getBlockHeader({blockId}) {
const {block, executionOptimistic, finalized} = await getBlockResponse(chain, blockId);
return {
data: toBeaconHeaderResponse(config, block, true),
meta: {executionOptimistic, finalized},
};
},
async getBlockV2({blockId}) {
const {block, executionOptimistic, finalized} = await getBlockResponse(chain, blockId);
return {
data: block,
meta: {
executionOptimistic,
finalized,
version: config.getForkName(block.message.slot),
},
};
},
async getBlindedBlock({blockId}) {
const {block, executionOptimistic, finalized} = await getBlockResponse(chain, blockId);
const fork = config.getForkName(block.message.slot);
return {
data: isForkExecution(fork)
? signedBeaconBlockToBlinded(config, block as SignedBeaconBlock<ForkExecution>)
: block,
meta: {
executionOptimistic,
finalized,
version: fork,
},
};
},
async getBlockAttestations({blockId}) {
const {block, executionOptimistic, finalized} = await getBlockResponse(chain, blockId);
return {
data: Array.from(block.message.body.attestations),
meta: {executionOptimistic, finalized},
};
},
async getBlockRoot({blockId}) {
// Fast path: From head state already available in memory get historical blockRoot
const slot = typeof blockId === "string" ? parseInt(blockId) : blockId;
if (!Number.isNaN(slot)) {
const head = chain.forkChoice.getHead();
if (slot === head.slot) {
return {
data: {root: fromHex(head.blockRoot)},
meta: {executionOptimistic: isOptimisticBlock(head), finalized: false},
};
}
if (slot < head.slot && head.slot <= slot + SLOTS_PER_HISTORICAL_ROOT) {
const state = chain.getHeadState();
return {
data: {root: state.blockRoots.get(slot % SLOTS_PER_HISTORICAL_ROOT)},
meta: {
executionOptimistic: isOptimisticBlock(head),
finalized: computeEpochAtSlot(slot) <= chain.forkChoice.getFinalizedCheckpoint().epoch,
},
};
}
} else if (blockId === "head") {
const head = chain.forkChoice.getHead();
return {
data: {root: fromHex(head.blockRoot)},
meta: {executionOptimistic: isOptimisticBlock(head), finalized: false},
};
}
// Slow path
const {block, executionOptimistic, finalized} = await getBlockResponse(chain, blockId);
return {
data: {root: config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message)},
meta: {executionOptimistic, finalized},
};
},
publishBlock,
publishBlindedBlock,
async publishBlindedBlockV2(args, context, opts) {
await publishBlindedBlock(args, context, opts);
},
async publishBlockV2(args, context, opts) {
await publishBlock(args, context, opts);
},
async getBlobSidecars({blockId, indices}) {
const {block, executionOptimistic, finalized} = await getBlockResponse(chain, blockId);
const blockRoot = config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message);
let {blobSidecars} = (await db.blobSidecars.get(blockRoot)) ?? {};
if (!blobSidecars) {
({blobSidecars} = (await db.blobSidecarsArchive.get(block.message.slot)) ?? {});
}
if (!blobSidecars) {
throw Error(`blobSidecars not found in db for slot=${block.message.slot} root=${toHex(blockRoot)}`);
}
return {
data: indices ? blobSidecars.filter(({index}) => indices.includes(index)) : blobSidecars,
meta: {
executionOptimistic,
finalized,
version: config.getForkName(block.message.slot),
},
};
},
};
}
async function reconstructBuilderBlockOrContents(
chain: ApiModules["chain"],
signedBlindedBlock: SignedBlindedBeaconBlock
): Promise<SignedBeaconBlockOrContents> {
const executionBuilder = chain.executionBuilder;
if (!executionBuilder) {
throw Error("executionBuilder required to publish SignedBlindedBeaconBlock");
}
const signedBlockOrContents = await executionBuilder.submitBlindedBlock(signedBlindedBlock);
return signedBlockOrContents;
}