|
1 | 1 | import { type PutBlock } from "./api/put-blocks"; |
2 | | -import { type BlockQueueRecord } from "@gonative-cc/lib/nbtc"; |
| 2 | +import { type BlockQueueRecord, kvBlocksKey } from "@gonative-cc/lib/nbtc"; |
3 | 3 |
|
| 4 | +/// Enqueue new blocks to the indexer processing queue. |
4 | 5 | export async function handleIngestBlocks( |
5 | 6 | blocks: PutBlock[], |
6 | | - blockStore: KVNamespace, |
| 7 | + btcBlocksStore: KVNamespace, |
7 | 8 | blockQueue: Queue, |
8 | 9 | ): Promise<void> { |
9 | 10 | if (blocks.length === 0) { |
10 | 11 | throw new Error("Empty block batch"); |
11 | 12 | } |
12 | | - |
13 | | - const blockMetas = blocks.map((block) => { |
14 | | - const blockHash = block.block.getId(); |
15 | | - const kvKey = `b:${block.network}:${blockHash}`; |
16 | | - return { block, blockHash, kvKey }; |
17 | | - }); |
18 | | - |
19 | | - // Batch KV puts |
| 13 | + const timestamp_ms = Date.now(); |
| 14 | + const batch: MessageSendRequest<BlockQueueRecord>[] = []; |
20 | 15 | await Promise.all( |
21 | | - blockMetas.map((meta) => blockStore.put(meta.kvKey, meta.block.block.toBuffer())), |
| 16 | + blocks.map((b) => { |
| 17 | + const hash = b.block.getId(); |
| 18 | + batch.push({ |
| 19 | + body: { |
| 20 | + hash, |
| 21 | + timestamp_ms, |
| 22 | + height: b.height, |
| 23 | + network: b.network, |
| 24 | + }, |
| 25 | + }); |
| 26 | + const kvKey = kvBlocksKey(b.network, b.block.getId()); |
| 27 | + return btcBlocksStore.put(kvKey, b.block.toBuffer()); |
| 28 | + }), |
22 | 29 | ); |
23 | 30 |
|
24 | | - const messages: BlockQueueRecord[] = []; |
25 | | - for (const meta of blockMetas) { |
26 | | - messages.push({ |
27 | | - hash: meta.blockHash, |
28 | | - height: meta.block.height, |
29 | | - network: meta.block.network, |
30 | | - kv_key: meta.kvKey, |
31 | | - }); |
32 | | - } |
33 | | - |
34 | | - // Enqueue parsing requests |
35 | | - if (messages.length > 0) { |
36 | | - await blockQueue.sendBatch(messages.map((body: BlockQueueRecord) => ({ body }))); |
| 31 | + if (batch.length > 0) { |
| 32 | + await blockQueue.sendBatch(batch); |
37 | 33 | } |
38 | 34 | } |
0 commit comments