statechanges: Remove txHash and use tx TOID to reference transactions table#480
Open
aditya1702 wants to merge 35 commits intotxhash-remove-opsfrom
Open
statechanges: Remove txHash and use tx TOID to reference transactions table#480aditya1702 wants to merge 35 commits intotxhash-remove-opsfrom
statechanges: Remove txHash and use tx TOID to reference transactions table#480aditya1702 wants to merge 35 commits intotxhash-remove-opsfrom
Conversation
- Remove tx_hash column and its FK constraint to transactions(hash) - Add FK constraint on to_id referencing transactions(to_id) - Remove idx_state_changes_tx_hash index
The tx_hash is redundant since to_id can be used to look up transactions.
Transaction can be looked up via to_id, so txHash is redundant.
Updated calls in: - effects.go - token_transfer.go - contract_deploy.go - contracts/sac.go
- Remove txHashes slice and loop assignment - Update INSERT query to remove tx_hash column - Shift parameter indices accordingly - Update BatchCopy column list and row values - Update BatchGetByAccountAddress to use subquery for tx_hash filter
- BatchGetByTxHash -> BatchGetByToID (uses to_id instead of tx_hash) - BatchGetByTxHashes -> BatchGetByToIDs (uses to_ids array instead of tx_hashes) - Update queries to use to_id for filtering and partitioning
- Change StateChangeColumnsKey.TxHash to ToID (int64) - Rename stateChangesByTxHashLoader to stateChangesByToIDLoader - Update dataloader to use BatchGetByToID/BatchGetByToIDs - Update key extraction functions to use ToID
- Change loaderKey.TxHash to loaderKey.ToID (use obj.ToID instead of obj.Hash) - Call StateChangesByToIDLoader instead of StateChangesByTxHashLoader
- Remove TxHash from test state change structs - Update INSERT statements to remove tx_hash column - Update filter tests to check ToID instead of TxHash - Rename BatchGetByTxHash/TxHashes tests to BatchGetByToID/ToIDs - Remove TxHash assertions from effects and contract_deploy tests - Update buildStateChange helper function signature
- Update createTestStateChange helper in ingest_test.go - Remove TxHash from state change struct in statechange_resolvers_test.go
The sc3 state change previously had to_id=3 but only transactions with to_id=1 and to_id=2 exist. Changed to to_id=1 with state_change_order=2 to reference an existing transaction while avoiding PK conflict.
- Use valid to_id values that reference existing transactions - Remove tx_hash from transactions_test.go INSERT - Adjust test expectations to match updated test data
- Update PK from (to_id, state_change_order) to (to_id, operation_id, state_change_order) - Add partial index on operation_id for non-zero values - Enables proper FK semantics where to_id always references transaction to_id
- Add OperationID field to StateChangeCursor for 3-part cursor - Update GetCursor() method to include OperationID
- For operation changes: derive ToID from OperationID using TOID bitmask (&^ 0xFFF) - For fee changes: use transaction's to_id directly - This ensures FK constraint to transactions table is always valid
- Add operation_id to cursor columns in all queries - Update ordering to use (to_id, operation_id, state_change_order) - Update pagination predicates to use 3-column comparison - Update ON CONFLICT clause for 3-part PK - Update returned ID format to include operation_id
- Add scOpIDs parameter to all BatchGetByStateChangeIDs functions - Update tuple format from (to_id, state_change_order) to (to_id, operation_id, state_change_order) - Update state_change_id format to include operation_id
- Change format from to_id-state_change_order to to_id-operation_id-state_change_order - Return toIDs, opIDs, orders instead of toIDs, orders
- Update account_loaders.go to pass scOpIDs to BatchGetByStateChangeIDs - Update operation_loaders.go to pass scOpIDs to BatchGetByStateChangeIDs - Update transaction_loaders.go to pass scOpIDs to BatchGetByStateChangeIDs
- Update resolveStateChangeAccount/Operation/Transaction to accept operationID - Update all statechange resolvers to pass operationID to helper functions - Update cursor format from to_id:state_change_order to to_id:operation_id:state_change_order - Update parseStateChangeCursor to parse 3-part format
- Update statechanges_test.go: wantIDs format, verification queries, cursor tests - Update accounts_test.go: BatchGetByStateChangeIDs to use 3-part format - Update operations_test.go: BatchGetByStateChangeIDs to use 3-part format - Update transactions_test.go: BatchGetByStateChangeIDs to use 3-part format - Update test_utils.go: ToID derived from OperationID using TOID bitmask
statechanges: Remove txHash and use tx TOIDstatechanges: Remove txHash and use tx TOID to reference transactions table
There was a problem hiding this comment.
Pull request overview
This PR refactors the state_changes table schema to remove the redundant tx_hash column and instead use to_id (transaction TOID) to reference the transactions table. The change also updates the primary key to include operation_id as part of the composite key, resulting in better data normalization and referential integrity.
Changes:
- Remove
tx_hashcolumn fromstate_changestable and add foreign key constraint onto_idreferencingtransactions(to_id) - Update primary key from
(to_id, state_change_order)to(to_id, operation_id, state_change_order)to properly reflect data relationships - Update cursor format from
to_id:state_change_ordertoto_id:operation_id:state_change_orderacross GraphQL resolvers and tests - Refactor all database queries and dataloaders to use
to_idfor transaction lookups instead oftx_hash - Update StateChangeBuilder to remove
TxHashfield and properly setToIDfrom transaction ID
Reviewed changes
Copilot reviewed 39 out of 39 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/db/migrations/2025-06-10.4-create_indexer_table_state_changes.sql | Updates table schema to use to_id FK instead of tx_hash, changes PK to 3-column composite key |
| internal/indexer/types/types.go | Removes TxHash and TxID fields from StateChange struct, adds OperationID to StateChangeCursor |
| internal/indexer/processors/state_change_builder.go | Updates builder to set ToID from txID parameter, removes tx_hash handling |
| internal/data/statechanges.go | Refactors all queries to use to_id instead of tx_hash, updates cursor pagination to use 3-column comparison |
| internal/data/transactions.go | Updates BatchGetByStateChangeIDs to join on to_id instead of tx_hash |
| internal/data/operations.go | Updates BatchGetByStateChangeIDs to use 3-column composite key |
| internal/data/accounts.go | Updates BatchGetByStateChangeIDs to use 3-column composite key |
| internal/serve/graphql/resolvers/utils.go | Updates parseStateChangeCursor to parse 3 parts and include operation_id |
| internal/serve/graphql/resolvers/*.resolvers.go | Updates cursor encoding to format with 3 fields (to_id:operation_id:state_change_order) |
| internal/serve/graphql/dataloaders/*.go | Updates dataloaders to use ToID instead of TxHash, updates parseStateChangeIDs to handle 3 fields |
| Test files | Comprehensive test updates throughout to use new cursor format and validate operation_id handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR refactors the
state_changestable schema to remove the redundanttx_hashcolumn and instead useto_id(transaction TOID) to reference the transactions table. The change also updates the primary key to includeoperation_idas part of the composite key, resulting in better data normalization and referential integrity.tx_hashcolumn fromstate_changestable and add foreign key constraint onto_idreferencingtransactions(to_id)(to_id, state_change_order)to(to_id, operation_id, state_change_order)to properly reflect data relationshipsto_id:state_change_ordertoto_id:operation_id:state_change_orderacross GraphQL resolvers and teststo_idfor transaction lookups instead oftx_hashTxHashfield and properly setToIDfrom transaction IDKnown limitations
[TODO or N/A]
Issue that this PR addresses
Closes #483