Skip to content

Commit f313297

Browse files
btcindexer: fix ingestor integration and types (#186)
* fix(block-ingestor): integration issues and overall style * update & unifiy CF binding names Signed-off-by: Robert Zaremba <robert@zaremba.ch>
1 parent 06f2325 commit f313297

File tree

16 files changed

+17332
-15422
lines changed

16 files changed

+17332
-15422
lines changed

packages/block-ingestor/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010

1111
try {
1212
const blocks = PutBlocksReq.decode(await request.arrayBuffer());
13-
await handleIngestBlocks(blocks, env.BlockStore, env.BlockQueue);
13+
await handleIngestBlocks(blocks, env.BtcBlocks, env.BlockQueue);
1414
return new Response("Blocks ingested successfully", { status: 200 });
1515
} catch (e) {
1616
console.error("Failed to ingest blocks", e);
Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
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";
33

4+
/// Enqueue new blocks to the indexer processing queue.
45
export async function handleIngestBlocks(
56
blocks: PutBlock[],
6-
blockStore: KVNamespace,
7+
btcBlocksStore: KVNamespace,
78
blockQueue: Queue,
89
): Promise<void> {
910
if (blocks.length === 0) {
1011
throw new Error("Empty block batch");
1112
}
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>[] = [];
2015
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+
}),
2229
);
2330

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);
3733
}
3834
}

0 commit comments

Comments
 (0)