Skip to content

Conversation

@pgherveou
Copy link
Contributor

@pgherveou pgherveou commented Jan 30, 2026

Summary

Fixes a UNIQUE constraint violation when processing blocks after a re-org:

UNIQUE constraint failed: transaction_hashes.transaction_hash

Problem

When a blockchain re-org occurs:

  1. Block A contains transaction TX1 → stored in transaction_hashes
  2. Server restarts (clearing the in-memory block_number_to_hashes map)
  3. Re-org happens, Block B (different hash) now contains the same TX1
  4. INSERT fails because TX1 already exists with old block_hash

When a blockchain re-org occurs and the same transaction is included in a
different block, the INSERT into transaction_hashes would fail with a
UNIQUE constraint violation if the server had restarted (losing the
in-memory block_number_to_hashes map used for pruning).

This change uses INSERT OR REPLACE to update the block_hash when a
transaction is re-included in a different block after a re-org, matching
the pattern already used for insert_block_mapping.

Fixes: UNIQUE constraint failed: transaction_hashes.transaction_hash
@pgherveou
Copy link
Contributor Author

/cmd prdoc --audience runtime_dev --bump patch

@pgherveou pgherveou added the T7-smart_contracts This PR/Issue is related to smart contracts. label Jan 30, 2026
@paritytech-workflow-stopper
Copy link

All GitHub workflows were cancelled due to failure one of the required jobs.
Failed workflow url: https://github.com/paritytech/polkadot-sdk/actions/runs/21521585069
Failed job name: check-try-runtime

@pgherveou pgherveou enabled auto-merge January 30, 2026 15:54
// Transaction should now point to block B
let (found_hash, _) = provider.find_transaction(&tx_hash).await.unwrap();
assert_eq!(found_hash, block_b.hash);

Copy link
Contributor

@0xRVE 0xRVE Jan 30, 2026

Choose a reason for hiding this comment

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

Suggested change
assert_eq!(count(&provider.pool, "eth_to_substrate_blocks", None).await, 1);

Or it is ok to have block A still sitting in the table?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point we can check in another PR, if we do clean up forked blocks

@pgherveou pgherveou requested a review from 0xRVE January 30, 2026 21:14
@pgherveou pgherveou added this pull request to the merge queue Jan 30, 2026
Merged via the queue into master with commit 684c79c Jan 30, 2026
267 of 275 checks passed
@pgherveou pgherveou deleted the fix/transaction-hash-reorg-conflict branch January 30, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T7-smart_contracts This PR/Issue is related to smart contracts.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants