Skip to content

feat: user time from the worker code rather DB#56

Merged
robert-zaremba merged 1 commit intomasterfrom
robert/use-worker-time
Aug 8, 2025
Merged

feat: user time from the worker code rather DB#56
robert-zaremba merged 1 commit intomasterfrom
robert/use-worker-time

Conversation

@robert-zaremba
Copy link
Contributor

@robert-zaremba robert-zaremba commented Aug 8, 2025

Description

As discussed:

  • time is managed by the worker code
  • time is timestamp in MS

Summary by Sourcery

Use worker-provided millisecond timestamps instead of database-generated unixepoch for transaction status updates and block processing.

Enhancements:

  • Use JavaScript Date.now() to set updated_at in minting status updates (minted, failed, reorg, finalized).
  • Change processed_at column in btc_blocks schema to integer without default to store millisecond timestamps.

Signed-off-by: Robert Zaremba <robert@zaremba.ch>
Copilot AI review requested due to automatic review settings August 8, 2025 10:19
@robert-zaremba robert-zaremba requested a review from a team as a code owner August 8, 2025 10:19
@robert-zaremba robert-zaremba requested a review from sczembor August 8, 2025 10:19
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 8, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This 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 logic

classDiagram
    class Indexer {
        +updateMintedStatus(processedTxIds)
        +updateFailedStatus(processedTxIds)
        +handleReorg(pendingTxs, latestHeight)
        +selectFinalizedNbtcTxs(pendingTxs, latestHeight)
    }
    class D1PreparedStatement
    Indexer --> D1PreparedStatement : prepares and binds
Loading

File-Level Changes

Change Details Files
Move timestamp generation from SQLite to worker code
  • Introduce const now = +new Date() before update statements
  • Replace unixepoch('subsec') with ${now} in minted status update
  • Replace unixepoch('subsec') with ${now} in failed status update
  • Replace unixepoch('subsec') with ${now} in reorg status update
  • Replace unixepoch('subsec') with ${now} in finalized status update
packages/btcindexer/src/btcindexer.ts
Update migration to use millisecond timestamps
  • Change processed_at type from REAL DEFAULT unixepoch to INTEGER NOT NULL
  • Add comment indicating timestamp_ms and remove default expression
packages/btcindexer/db/migrations/0001_initial_schema.sql

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @robert-zaremba - I've reviewed your changes - here's some feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@sczembor sczembor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-approve

@robert-zaremba robert-zaremba merged commit 03045bc into master Aug 8, 2025
12 checks passed
@robert-zaremba robert-zaremba deleted the robert/use-worker-time branch August 8, 2025 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants