feat: user time from the worker code rather DB#56
Conversation
Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThis PR consolidates timestamp management within the worker by capturing the current time in milliseconds in code and embedding it into SQL updates, and adjusts the initial migration schema to store millisecond timestamps in an INTEGER column. Class diagram for Indexer timestamp update logicclassDiagram
class Indexer {
+updateMintedStatus(processedTxIds)
+updateFailedStatus(processedTxIds)
+handleReorg(pendingTxs, latestHeight)
+selectFinalizedNbtcTxs(pendingTxs, latestHeight)
}
class D1PreparedStatement
Indexer --> D1PreparedStatement : prepares and binds
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Pull Request Overview
This PR transitions time management from database-level functions to worker code, standardizing timestamp handling across the application. The change ensures timestamps are consistently stored as millisecond values generated by the application rather than database functions.
- Replace database
unixepoch('subsec')calls with JavaScript+new Date()for timestamp generation - Update database schema to expect integer millisecond timestamps instead of real subsecond values
- Maintain consistent timing across all status update operations (minted, failed, reorg, finalized)
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| btcindexer.ts | Replace SQL unixepoch functions with JavaScript timestamp generation for status updates |
| 0001_initial_schema.sql | Update btc_blocks table to use integer millisecond timestamps instead of database-generated subsecond values |
There was a problem hiding this comment.
Hey @robert-zaremba - I've reviewed your changes - here's some feedback:
- Instead of injecting
nowdirectly into the SQL string, use a bound parameter forupdated_atto avoid potential SQL injection and allow statement reuse. - The migration removes the default for processed_at and makes it NOT NULL—ensure all INSERT paths supply a timestamp_ms or restore a safe default.
- You’re recomputing
const now = +new Date()in each method; consider centralizing timestamp generation or passing it in to reduce repetition.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of injecting `now` directly into the SQL string, use a bound parameter for `updated_at` to avoid potential SQL injection and allow statement reuse.
- The migration removes the default for processed_at and makes it NOT NULL—ensure all INSERT paths supply a timestamp_ms or restore a safe default.
- You’re recomputing `const now = +new Date()` in each method; consider centralizing timestamp generation or passing it in to reduce repetition.
## Individual Comments
### Comment 1
<location> `packages/btcindexer/src/btcindexer.ts:286` </location>
<code_context>
+ 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) =>
</code_context>
<issue_to_address>
Directly interpolating timestamps into SQL can risk SQL injection if not controlled.
Although 'now' is not user input, using parameter binding here would improve consistency and reduce future risk if user-supplied values are introduced.
Suggested implementation:
```typescript
const setMintedStmt = this.d1.prepare(
`UPDATE nbtc_minting SET status = 'minted', updated_at = ? WHERE tx_id = ?`,
);
const setFailedStmt = this.d1.prepare(
`UPDATE nbtc_minting SET status = 'failed', updated_at = ? WHERE tx_id = ?`,
);
const updates = processedTxIds.map((p) =>
p.success ? setMintedStmt.bind(now, p.tx_id) : setFailedStmt.bind(now, p.tx_id),
```
```typescript
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 = ? WHERE tx_id = ?`,
);
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Description
As discussed:
Summary by Sourcery
Use worker-provided millisecond timestamps instead of database-generated unixepoch for transaction status updates and block processing.
Enhancements: