-
Notifications
You must be signed in to change notification settings - Fork 1.1k
core: Fix duplicate VID when multiple offchain triggers fire in the same block #6336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
core: Fix duplicate VID when multiple offchain triggers fire in the same block #6336
Conversation
When specVersion >= 1.3.0, VIDs are computed deterministically as (block_number << 32) + vid_seq. Each offchain trigger in handle_offchain_triggers creates a fresh EntityCache with vid_seq reset to RESERVED_VIDS (100). When multiple file data source triggers fire in the same block and write to the same entity table, they produce identical VIDs, causing PostgreSQL primary key violations: "duplicate key value violates unique constraint task_metadata_pkey" Fix: Pass the onchain EntityCache's final vid_seq into handle_offchain_triggers and accumulate it across loop iterations, so each offchain trigger continues the sequence from where the previous one left off.
Two tests demonstrate the bug where multiple offchain triggers (e.g. file data sources) in the same block each create a fresh EntityCache with vid_seq reset to RESERVED_VIDS (100), producing duplicate VIDs that violate the primary key constraint. - offchain_trigger_vid_collision_without_fix: proves VIDs collide - offchain_trigger_vid_no_collision_with_fix: proves threading vid_seq across triggers prevents collisions
20bba68 to
f24825a
Compare
No Problem. One thing I am not entirely sure about is that now everywhere an |
Good call. I looked into it a bit more and the blast radius is actually pretty narrow. onchain triggers share a single the manual read-before-consume / write-after-create pattern is definitely fragile. I think the simplest fix is adding how does that approach sound to you i could go ahead and implement it if you think it sounds good |
Each iteration of the offchain trigger loop in
handle_offchain_triggerscreates a freshBlockStatewithvid_seqreset toRESERVED_VIDS(100). When two or more offchain triggers (e.g. file/ipfs data sources) fire in the same block and write to the same entity table, they produce identical VIDs, causing a unique constraint violation.This threads
vid_seqfrom the onchainEntityCachethrough the offchain trigger loop so each trigger continues the sequence where the previous one left off. Also adds unit tests demonstrating both the collision and the fix.There are tests for illustrative purposes showing the bug and the fix
Fixes #6335