-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathindex.ts
280 lines (253 loc) Β· 10.6 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
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {routes, ServerApi, isSignedBlockContents, isSignedBlindedBlockContents} from "@lodestar/api";
import {computeTimeAtSlot} from "@lodestar/state-transition";
import {SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
import {sleep} from "@lodestar/utils";
import {allForks, deneb} from "@lodestar/types";
import {
BlockSource,
getBlockInput,
ImportBlockOpts,
BlockInput,
blobSidecarsToBlobsSidecar,
} from "../../../../chain/blocks/types.js";
import {promiseAllMaybeAsync} from "../../../../util/promises.js";
import {isOptimisticBlock} from "../../../../util/forkChoice.js";
import {BlockError, BlockErrorCode} from "../../../../chain/errors/index.js";
import {OpSource} from "../../../../metrics/validatorMonitor.js";
import {NetworkEvent} from "../../../../network/index.js";
import {ApiModules} from "../../types.js";
import {resolveBlockId, toBeaconHeaderResponse} from "./utils.js";
/**
* 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">): ServerApi<routes.beacon.block.Api> {
return {
async getBlockHeaders(filters) {
// 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;
const result: routes.beacon.BlockHeaderResponse[] = [];
if (filters.parentRoot) {
const parentRoot = filters.parentRoot;
const finalizedBlock = await db.blockArchive.getByParentRoot(fromHexString(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(fromHexString(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;
}
}
}
})
);
return {
executionOptimistic,
data: result.filter(
(item) =>
// skip if no slot filter
!(filters.slot !== undefined && filters.slot !== 0) || item.header.message.slot === filters.slot
),
};
}
const headSlot = chain.forkChoice.getHead().slot;
if (!filters.parentRoot && filters.slot === undefined) {
filters.slot = headSlot;
}
if (filters.slot !== undefined) {
// future slot
if (filters.slot > headSlot) {
return {executionOptimistic: false, data: []};
}
const canonicalBlock = await chain.getCanonicalBlockAtSlot(filters.slot);
// skip slot
if (!canonicalBlock) {
return {executionOptimistic: false, data: []};
}
const canonicalRoot = config
.getForkTypes(canonicalBlock.block.message.slot)
.BeaconBlock.hashTreeRoot(canonicalBlock.block.message);
result.push(toBeaconHeaderResponse(config, canonicalBlock.block, true));
// fork blocks
// TODO: What is this logic?
await Promise.all(
chain.forkChoice.getBlockSummariesAtSlot(filters.slot).map(async (summary) => {
if (isOptimisticBlock(summary)) {
executionOptimistic = true;
}
if (summary.blockRoot !== toHexString(canonicalRoot)) {
const block = await db.block.get(fromHexString(summary.blockRoot));
if (block) {
result.push(toBeaconHeaderResponse(config, block));
}
}
})
);
}
return {
executionOptimistic,
data: result,
};
},
async getBlockHeader(blockId) {
const {block, executionOptimistic} = await resolveBlockId(chain, blockId);
return {
executionOptimistic,
data: toBeaconHeaderResponse(config, block, true),
};
},
async getBlock(blockId) {
const {block} = await resolveBlockId(chain, blockId);
return {
data: block,
};
},
async getBlockV2(blockId) {
const {block, executionOptimistic} = await resolveBlockId(chain, blockId);
return {
executionOptimistic,
data: block,
version: config.getForkName(block.message.slot),
};
},
async getBlockAttestations(blockId) {
const {block, executionOptimistic} = await resolveBlockId(chain, blockId);
return {
executionOptimistic,
data: Array.from(block.message.body.attestations),
};
},
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 {
executionOptimistic: isOptimisticBlock(head),
data: {root: fromHexString(head.blockRoot)},
};
}
if (slot < head.slot && head.slot <= slot + SLOTS_PER_HISTORICAL_ROOT) {
const state = chain.getHeadState();
return {
executionOptimistic: isOptimisticBlock(head),
data: {root: state.blockRoots.get(slot % SLOTS_PER_HISTORICAL_ROOT)},
};
}
} else if (blockId === "head") {
const head = chain.forkChoice.getHead();
return {
executionOptimistic: isOptimisticBlock(head),
data: {root: fromHexString(head.blockRoot)},
};
}
// Slow path
const {block, executionOptimistic} = await resolveBlockId(chain, blockId);
return {
executionOptimistic,
data: {root: config.getForkTypes(block.message.slot).BeaconBlock.hashTreeRoot(block.message)},
};
},
async publishBlindedBlock(signedBlindedBlockOrContents) {
const executionBuilder = chain.executionBuilder;
if (!executionBuilder) throw Error("exeutionBuilder required to publish SignedBlindedBeaconBlock");
// Mechanism for blobs & blocks on builder is not yet finalized
if (isSignedBlindedBlockContents(signedBlindedBlockOrContents)) {
throw Error("exeutionBuilder not yet implemented for deneb+ forks");
} else {
const signedBlockOrContents = await executionBuilder.submitBlindedBlock(signedBlindedBlockOrContents);
// 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
return this.publishBlock(signedBlockOrContents, {ignoreIfKnown: true});
}
},
async publishBlock(signedBlockOrContents, opts: ImportBlockOpts = {}) {
const seenTimestampSec = Date.now() / 1000;
let blockForImport: BlockInput, signedBlock: allForks.SignedBeaconBlock, signedBlobs: deneb.SignedBlobSidecars;
if (isSignedBlockContents(signedBlockOrContents)) {
({signedBlock, signedBlobSidecars: signedBlobs} = signedBlockOrContents);
blockForImport = getBlockInput.postDeneb(
config,
signedBlock,
BlockSource.api,
// The blobsSidecar will be replaced in the followup PRs with just blobs
blobSidecarsToBlobsSidecar(
config,
signedBlock,
signedBlobs.map((sblob) => sblob.message)
),
null
);
} else {
signedBlock = signedBlockOrContents;
signedBlobs = [];
// TODO: Once API supports submitting data as SSZ, replace null with blockBytes
blockForImport = getBlockInput.preDeneb(config, signedBlock, BlockSource.api, null);
}
// 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);
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.
() => 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;
}),
...signedBlobs.map((signedBlob) => () => network.publishBlobSidecar(signedBlob)),
];
await promiseAllMaybeAsync(publishPromises);
},
async getBlobSidecars(blockId) {
const {block, executionOptimistic} = await resolveBlockId(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=${toHexString(blockRoot)}`);
}
return {
executionOptimistic,
data: blobSidecars,
};
},
};
}