Skip to content

fix(db): make token transfer insert idempotent#49

Merged
satyakwok merged 1 commit into
mainfrom
fix/token-transfer-insert-idempotent
May 26, 2026
Merged

fix(db): make token transfer insert idempotent#49
satyakwok merged 1 commit into
mainfrom
fix/token-transfer-insert-idempotent

Conversation

@satyakwok
Copy link
Copy Markdown
Member

@satyakwok satyakwok commented May 26, 2026

Summary

What changed and why. 1-3 sentences.

Scope

  • Contract change (new file or logic edit) — needs slither pass + audit consideration
  • Test-only change
  • Deploy script / CI / docs only
  • Repo tooling (Makefile, lefthook, etc.)

Checks

  • forge build clean
  • forge test green (including fuzz + invariant)
  • forge fmt --check clean
  • slither --no-fail-pedantic reviewed (no new high-severity findings)
  • Storage layout diff reviewed if any contract field added/removed (run make storage and inspect docs/storage/*.json)

Linked issue

Closes #

Deploy impact

  • No on-chain change (test/CI/docs only)
  • Requires v-bump + redeploy when shipped
  • Backwards-compatible (extends, doesn't reorder storage)
  • Breaking — needs migration plan in RELEASES.md

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced reliability of token transfer data synchronization to prevent duplicate entry conflicts during retry operations.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 26, 2026

Warning

Review limit reached

@satyakwok, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 55 minutes and 44 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: bd9a3ed2-e638-4e85-bc53-85d8b4f6daee

📥 Commits

Reviewing files that changed from the base of the PR and between 4579837 and 67df636.

📒 Files selected for processing (1)
  • crates/db/src/token_transfers.rs
📝 Walkthrough

Walkthrough

The insert function for a single decoded token transfer is modified to include an ON CONFLICT (tx_hash, log_index) DO NOTHING clause in the SQL statement, making it idempotent at the database layer. The associated documentation comment is updated to reflect the retry-safety guarantee. The change mirrors the existing behavior of the batch insert function and uses the same (tx_hash, log_index) conflict key.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Sentriscloud/indexer-rs#47: Establishes the unique index on (tx_hash, log_index) and makes batch inserts idempotent with ON CONFLICT handling; this PR applies the same idempotency pattern to single inserts.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is an unfilled template with no actual summary, scope selection, checks completion, linked issues, or deploy impact information provided by the author. Fill in the Summary section explaining what changed and why, select appropriate Scope checkbox, mark completed Checks, link the related issue, and specify Deploy impact.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(db): make token transfer insert idempotent' clearly and concisely summarizes the main change—making the token transfer insert operation idempotent at the database layer.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/token-transfer-insert-idempotent

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/db/src/token_transfers.rs`:
- Around line 7-8: Add a regression test that verifies the single-row insert is
retry-safe by calling the insert function in crates/db/src/token_transfers.rs
twice with the same (tx_hash, log_index) and asserting the second call returns
Ok(()) and that the table still has only one row for that key; specifically
create a test that inserts a decoded transfer via the same insert function twice
and then queries the token_transfers table (or uses the existing fetch helper)
to assert row count is 1, since crates/sync/src/block_writer.rs:41-66 depends on
this conflict-handling behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8c743f2e-1268-440f-8584-9229695ae9e1

📥 Commits

Reviewing files that changed from the base of the PR and between e603197 and 4579837.

📒 Files selected for processing (1)
  • crates/db/src/token_transfers.rs

Comment on lines +7 to +8
/// Insert a single decoded transfer. Retry-safe via `(tx_hash, log_index)`
/// ON CONFLICT DO NOTHING — matches the batch insert path.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Add a duplicate-insert regression test.

This change makes the single-row path rely on DB conflict handling for retry safety. Please add coverage that calls insert twice with the same (tx_hash, log_index) and asserts the second call still returns Ok(()) without creating a second row; crates/sync/src/block_writer.rs:41-66 depends on that behavior during retries.

Also applies to: 16-17

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/db/src/token_transfers.rs` around lines 7 - 8, Add a regression test
that verifies the single-row insert is retry-safe by calling the insert function
in crates/db/src/token_transfers.rs twice with the same (tx_hash, log_index) and
asserting the second call returns Ok(()) and that the table still has only one
row for that key; specifically create a test that inserts a decoded transfer via
the same insert function twice and then queries the token_transfers table (or
uses the existing fetch helper) to assert row count is 1, since
crates/sync/src/block_writer.rs:41-66 depends on this conflict-handling
behavior.

@satyakwok satyakwok force-pushed the fix/token-transfer-insert-idempotent branch from 4579837 to 67df636 Compare May 26, 2026 06:28
@satyakwok satyakwok merged commit 07af3d6 into main May 26, 2026
8 checks passed
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.

1 participant