Skip to content

Conversation

@j-rafique
Copy link
Contributor

@j-rafique j-rafique commented Dec 8, 2025

Performance Improvements

  • Action module: new secondary indices written on SetAction (x/action/v1/keeper/action.go:321).
    • Adds indices: state/creator/type/blockHeight/supernode (x/action/v1/keeper/action.go:22).
    • Impact: common list queries move from “scan all actions + filter” to “scan index keys + fetch matching actions”, cutting CPU +
      IAVL iteration time as action count grows.
  • ListActions now uses indices when filters exist (x/action/v1/keeper/query_list_actions.go:35).
    • State filter uses Action/state//… index (x/action/v1/keeper/query_list_actions.go:37).
    • Type-only filter uses Action/type//… index (x/action/v1/keeper/query_list_actions.go:62).
  • ListActionsByBlockHeight uses Action/block//… index (x/action/v1/keeper/query_list_actions_by_block_height.go:35).
    • Impact: avoids scanning every action to find those at one height; makes this query scale with “actions at that height”, not “all
      actions”.
  • ListActionsBySuperNode uses Action/supernode//… index (x/action/v1/keeper/query_list_actions_by_sn.go:30).
    • Impact: replaces “unmarshal every action + slices.Contains” with direct lookup by supernode association.
  • QueryActionByMetadata narrows scan to “actions of a type” via the type index (x/action/v1/keeper/query_action_by_metadata.go:41).
    • Impact: still O(#actions of that type) due to metadata decoding, but no longer O(#all actions).
  • Supernode module: direct account→validator index for lookups (x/supernode/v1/types/keys.go:22, x/supernode/v1/keeper/
    supernode.go:127).
    • Impact: GetSuperNodeByAccount becomes O(1) KV get + primary fetch instead of iterating all supernodes.
  • Supernode ranking: state-at-height lookup switched to binary search (x/supernode/v1/keeper/
    query_get_top_super_nodes_for_block.go:180).
    • Impact: for S supernodes each with H state records, this changes per-request state resolution from O(S·H) to O(S·logH).

Bug Fixes / Correctness Improvements

  • Bounded prefix iterators for actions to avoid accidentally iterating past the prefix range (x/action/v1/keeper/action.go:459, x/
    action/v1/keeper/action.go:496).
    • Impact: prevents unintended full-store scans and reduces query/migration latency.
  • Index maintenance includes cleanup when indexed fields change (state/creator/type/blockHeight/supernodes) (x/action/v1/keeper/
    action.go:342 onward).
  • “Stale index entry” behavior is handled safely by skipping missing primaries in query paths (e.g. x/action/v1/keeper/
    query_list_actions.go:43, x/action/v1/keeper/query_list_actions_by_sn.go:37).
  • Supernode account index uniqueness enforced (prevents silent overwrite) (x/supernode/v1/keeper/supernode.go:60).
    • This directly addresses the PR comment about “secondary index overwrite”.
  • Supernode key prefix split (sn_ vs sna_) to prevent sn_ iterators from seeing index entries (x/supernode/v1/types/keys.go:24).
  • SetSuperNodeActive/Stopped/IsSuperNodeActive stop re-parsing sdk.ValAddress via bech32 round-trips (less error-prone) (x/supernode/
    v1/keeper/supernode.go:186, x/supernode/v1/keeper/supernode.go:256, x/supernode/v1/keeper/supernode.go:303).

Upgrade Handler (v1.8.8) Placeholder

  • New custom handler v1.8.8 registered in upgrade list (app/upgrades/upgrades.go:33, app/upgrades/upgrades.go:76) and wired keepers
    into upgrade params (app/app.go:324, app/upgrades/params/params.go:9).
  • Handler runs normal module migrations first, then backfills indices by replaying SetAction / SetSuperNode over existing state (app/
    upgrades/v1_8_8/upgrade.go:33).
  • No StoreUpgrades needed because it’s only new keys within existing stores, not new module stores (app/upgrades/v1_8_8/upgrade.go:25).

@j-rafique j-rafique marked this pull request as draft December 8, 2025 20:04
@roomote
Copy link

roomote bot commented Dec 8, 2025

Rooviewer Clock   Follow along on Roo Cloud

I've reviewed the changes and identified a few issues that need addressing. The updates improve query capabilities but introduce a couple of potential edge cases in index management and request validation.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

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 introduces secondary indices to optimize query performance by eliminating full-table scans in both Action and Supernode modules.

Key Changes:

  • Added secondary indices for actions (by creator, type, block height, supernode) and supernodes (by account address)
  • New ListActionsByCreator query endpoint with pagination support
  • Optimized existing query methods to leverage indices for O(1) or O(log n) lookups instead of O(n) scans
  • Enhanced CLI with autocli commands for all list query operations

Reviewed changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
x/supernode/v1/types/keys.go Adds SuperNodeByAccountKey prefix for secondary index mapping account→validator address
x/supernode/v1/types/expected_keepers.go Adds GetSuperNodeByAccount interface method for efficient account-based lookups
x/supernode/v1/mocks/expected_keepers_mock.go Generated mock for new GetSuperNodeByAccount method
x/supernode/v1/keeper/supernode.go Implements index maintenance in SetSuperNode, adds GetSuperNodeByAccount method, fixes pagination to use FilteredPaginate, updates SetSuperNodeActive default case handling
x/supernode/v1/keeper/query_get_super_node_by_super_node_address.go Optimizes query from O(n) full scan to O(1) index lookup using GetSuperNodeByAccount
x/supernode/v1/keeper/msg_server_stop_supernode.go Formatting fix (indentation correction)
x/action/v1/types/query.pb.go Generated protobuf code for new ListActionsByCreator RPC and types
x/action/v1/types/query.pb.gw.go Generated gRPC gateway handlers for ListActionsByCreator endpoint
x/action/v1/types/action.pb.go Import reordering (formatting change)
x/action/v1/keeper/action.go Adds index prefixes and implements comprehensive index maintenance for state, creator, type, block height, and supernode fields in SetAction
x/action/v1/keeper/query_list_actions.go Refactors to use state/type indices when filters are provided, avoiding full scans
x/action/v1/keeper/query_list_actions_by_creator.go New query handler using creator index for efficient lookups
x/action/v1/keeper/query_list_actions_by_creator_test.go Comprehensive unit tests for the new query endpoint
x/action/v1/keeper/query_list_actions_by_sn.go Optimizes to use supernode index instead of full scan
x/action/v1/keeper/query_list_actions_by_block_height.go Optimizes to use block height index
x/action/v1/keeper/query_list_expired_actions.go Refactors to delegate to ListActions with state filter, leveraging state index
x/action/v1/keeper/query_action_by_metadata.go Updates to use type index as initial filter before metadata matching
x/action/v1/module/autocli.go Adds CLI commands for all list query operations with appropriate flags
proto/lumera/action/v1/query.proto Adds ListActionsByCreator RPC definition and request/response types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

mateeullahmalik added a commit that referenced this pull request Dec 16, 2025
@j-rafique j-rafique marked this pull request as ready for review December 17, 2025 15:31
@roomote
Copy link

roomote bot commented Dec 17, 2025

Rooviewer Clock   See task on Roo Cloud

I've reviewed the changes and all previously identified issues have been addressed in the latest commits.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@a-ok123 a-ok123 self-requested a review December 17, 2025 22:41
@mateeullahmalik mateeullahmalik merged commit c8d3cdb into master Dec 18, 2025
8 checks passed
@mateeullahmalik mateeullahmalik deleted the feat/query-indices branch December 19, 2025 11:06
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.

4 participants