Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CREATE TABLE btc_blocks (
height INTEGER PRIMARY KEY,
hash TEXT NOT NULL UNIQUE,
processed_at REAL DEFAULT (unixepoch('subsec')),
processed_at INTEGER NOT NULL, -- timestamp_ms
status TEXT NOT NULL DEFAULT 'new' CHECK (status IN ('new', 'scanned')) -- 'new' | 'scanned'
) STRICT;

Expand Down
11 changes: 7 additions & 4 deletions packages/btcindexer/src/btcindexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,12 @@ export class Indexer implements Storage {
});
}
}
const now = +new Date();
const setMintedStmt = this.d1.prepare(
"UPDATE nbtc_minting SET status = 'minted', updated_at = unixepoch('subsec') WHERE tx_id = ?",
`UPDATE nbtc_minting SET status = 'minted', updated_at = ${now} WHERE tx_id = ?`,
);
const setFailedStmt = this.d1.prepare(
"UPDATE nbtc_minting SET status = 'failed', updated_at = unixepoch('subsec') WHERE tx_id = ?",
`UPDATE nbtc_minting SET status = 'failed', updated_at = ${now} WHERE tx_id = ?`,
);
const updates = processedTxIds.map((p) =>
p.success ? setMintedStmt.bind(p.tx_id) : setFailedStmt.bind(p.tx_id),
Expand Down Expand Up @@ -351,9 +352,10 @@ export class Indexer implements Storage {
): Promise<{ reorgUpdates: D1PreparedStatement[]; reorgedTxIds: string[] }> {
const reorgUpdates: D1PreparedStatement[] = [];
const reorgedTxIds: string[] = [];
const now = +new Date();
const reorgCheckStmt = this.d1.prepare("SELECT hash FROM btc_blocks WHERE height = ?");
const reorgStmt = this.d1.prepare(
"UPDATE nbtc_minting SET status = 'reorg', updated_at = unixepoch('subsec') WHERE tx_id = ?",
`UPDATE nbtc_minting SET status = 'reorg', updated_at = ${now} WHERE tx_id = ?`,
);

for (const tx of pendingTxs) {
Expand All @@ -376,8 +378,9 @@ export class Indexer implements Storage {

selectFinalizedNbtcTxs(pendingTxs: PendingTx[], latestHeight: number): D1PreparedStatement[] {
const updates: D1PreparedStatement[] = [];
const now = +new Date();
const finalizeStmt = this.d1.prepare(
"UPDATE nbtc_minting SET status = 'finalized', updated_at = unixepoch('subsec') WHERE tx_id = ?",
`UPDATE nbtc_minting SET status = 'finalized', updated_at = ${now} WHERE tx_id = ?`,
);

for (const tx of pendingTxs) {
Expand Down
Loading