perf(voting): bound reads and intent batches and remove measured query multiplication #47

Open
opened 2026-10-14 03:11:48 +00:00 by PlasticDigits · 0 comments
Owner

Scope and source

Priority: P2. Gap coverage: GAP-046. Derived from GAP_1791945837.md, reviewed code commit f8dbe93df953a78b17cb3304b717dcf84c4cd517. Current e476a68da09645c2308cbf47b90211fcaffcb7af adds only the report, so implementation findings remain applicable. Report evidence is local reproduction/source inspection unless explicitly proposed; no production exploit or current live-readiness claim.

Coverage and sequencing: gap backlog tracker. Workstream 15. Dependencies: 02, 04, 05, 08.

Current codebase

Lists fetch all proposals with bodies then tally per proposal; detail embeds all comments/analysis plus separate endpoints. Intents use fetch_all. Freeze calls balance functions for every active registration synchronously. Transfer legs repeat registration/balance/checkpoint queries; membership checks are linear and BSC repeatedly normalizes the registered set. No capacity baseline exists.

Why this implementation is needed

Large electorates/threads can exhaust memory or request latency, while query multiplication increases RPC/DB cost. Optimizations must preserve fixed accounting and atomic freeze visibility.

Relevant files

Add stable cursor pagination/maximum limits and lean list projections with grouped tallies. Separate bounded comments/analysis reads and fair indexed intent selection. Normalize membership into a HashSet once per batch, batch checkpoint persistence only after 02 correctness, and use EXPLAIN/benchmarks for indexes. Measure real-data-shaped freeze duration; if needed use a durable bounded freeze job with no partially visible open snapshot and explicit retry states. Keep integer arithmetic, revision approval and dual-chain state checks intact.

Constraints and guardrails

Preserve standalone offchain advisory voting, no new smart contracts or archive-node dependency, BSC as a core electorate, raw 18-decimal integer balances and separate Terra/BSC address identities. Keep Legal fail-closed transactional UI and the proven DEX Terra/Bridge EVM connectors; no browser BSC balance RPC, seed/key prompt or embedded admin/model credential. API must retain a restricted role separate from the ledger writer. Freeze at actual opening, never at draft creation or a client-supplied height. Agents own design, implementation, review and evidence under repository guidance; do not shift approvals/tests to the human or invent PASS evidence.

Acceptance criteria

Every public list/thread query has documented bounded size/order; pagination neither loses nor duplicates rows with concurrent inserts. Query counts stop scaling per proposal for tallies. Intent processing is fair and parser membership is reused. Agreed measured p95/soak budgets are met; opening remains all-or-nothing and immutable across optimized paths.

Test plan: supported paths, failure paths and concurrency

Empty/small/large datasets, max/default/invalid limits, equal timestamps and cursor boundary inserts/deletes, both-chain tallies and large threads. Fair queue under failures, normalization variants and repeated same-block events. Before/after EXPLAIN/query counts, concurrency load/soak and freeze at increasing electorate sizes; job crash/retry/cancel if introduced. Compare optimized balances/tallies to reference implementation.

Test plan: attack, hack and abuse vectors

Unbounded limit/cursor injection, expensive-search/large-thread floods, huge electorate freeze contention, skewed address normalization and batch replays. Verify SQL binding, bounded memory/DB occupancy, no starvation and no partial freeze or changed power under load.

Verification and completion criteria

Attach reproducible datasets, SQL/query plans and before/after latency/query-count/memory evidence with chosen budgets. Run accounting and approval regressions; no speculative index or throughput claim without measurements.

Record commit and pinned toolchain, commands/results and sanitized artifacts. Relevant Rust changes require cargo test --workspace --lib plus isolated Postgres integration (explicit guarded LEDGER_TEST_DATABASE_URL); frontend changes require npm test and production build plus relevant browser cases. Run secret scanning for new fixtures/config. Exercise fresh/upgrade migrations when schema/grants change; report skipped/live-only checks explicitly.

<!-- voting-gap-1791945837:15 --> ## Scope and source **Priority:** P2. **Gap coverage:** GAP-046. Derived from [GAP_1791945837.md](https://git.cl8y.com/code/voting/src/branch/main/gaps/GAP_1791945837.md), reviewed code commit `f8dbe93df953a78b17cb3304b717dcf84c4cd517`. Current `e476a68da09645c2308cbf47b90211fcaffcb7af` adds only the report, so implementation findings remain applicable. Report evidence is local reproduction/source inspection unless explicitly proposed; no production exploit or current live-readiness claim. Coverage and sequencing: [gap backlog tracker](https://git.cl8y.com/code/voting/issues/35). Workstream **15**. Dependencies: [02](https://git.cl8y.com/code/voting/issues/37), [04](https://git.cl8y.com/code/voting/issues/39), [05](https://git.cl8y.com/code/voting/issues/40), [08](https://git.cl8y.com/code/voting/issues/43). ## Current codebase Lists fetch all proposals with bodies then tally per proposal; detail embeds all comments/analysis plus separate endpoints. Intents use fetch_all. Freeze calls balance functions for every active registration synchronously. Transfer legs repeat registration/balance/checkpoint queries; membership checks are linear and BSC repeatedly normalizes the registered set. No capacity baseline exists. ## Why this implementation is needed Large electorates/threads can exhaust memory or request latency, while query multiplication increases RPC/DB cost. Optimizations must preserve fixed accounting and atomic freeze visibility. ## Relevant files - [`operator-voting/src/api.rs`](https://git.cl8y.com/code/voting/src/branch/main/operator-voting/src/api.rs) - [`operator-voting/src/db.rs`](https://git.cl8y.com/code/voting/src/branch/main/operator-voting/src/db.rs) - [`operator-voting/src/balance_query.rs`](https://git.cl8y.com/code/voting/src/branch/main/operator-voting/src/balance_query.rs) - [`ledger/src/db.rs`](https://git.cl8y.com/code/voting/src/branch/main/ledger/src/db.rs) - [`ledger/src/register.rs`](https://git.cl8y.com/code/voting/src/branch/main/ledger/src/register.rs) - [`ledger/src/parser/cw20.rs`](https://git.cl8y.com/code/voting/src/branch/main/ledger/src/parser/cw20.rs) - [`ledger/src/parser/bep20.rs`](https://git.cl8y.com/code/voting/src/branch/main/ledger/src/parser/bep20.rs) - [`frontend/src/services/operatorVoting.ts`](https://git.cl8y.com/code/voting/src/branch/main/frontend/src/services/operatorVoting.ts) - [`frontend/src/pages/VoteListPage.tsx`](https://git.cl8y.com/code/voting/src/branch/main/frontend/src/pages/VoteListPage.tsx) - [`docs/OPERATOR_VOTING.md`](https://git.cl8y.com/code/voting/src/branch/main/docs/OPERATOR_VOTING.md) ## Recommended solution Add stable cursor pagination/maximum limits and lean list projections with grouped tallies. Separate bounded comments/analysis reads and fair indexed intent selection. Normalize membership into a HashSet once per batch, batch checkpoint persistence only after 02 correctness, and use EXPLAIN/benchmarks for indexes. Measure real-data-shaped freeze duration; if needed use a durable bounded freeze job with no partially visible open snapshot and explicit retry states. Keep integer arithmetic, revision approval and dual-chain state checks intact. ## Constraints and guardrails Preserve standalone offchain advisory voting, no new smart contracts or archive-node dependency, BSC as a core electorate, raw 18-decimal integer balances and separate Terra/BSC address identities. Keep Legal fail-closed transactional UI and the proven DEX Terra/Bridge EVM connectors; no browser BSC balance RPC, seed/key prompt or embedded admin/model credential. API must retain a restricted role separate from the ledger writer. Freeze at actual opening, never at draft creation or a client-supplied height. Agents own design, implementation, review and evidence under repository guidance; do not shift approvals/tests to the human or invent PASS evidence. ## Acceptance criteria Every public list/thread query has documented bounded size/order; pagination neither loses nor duplicates rows with concurrent inserts. Query counts stop scaling per proposal for tallies. Intent processing is fair and parser membership is reused. Agreed measured p95/soak budgets are met; opening remains all-or-nothing and immutable across optimized paths. ## Test plan: supported paths, failure paths and concurrency Empty/small/large datasets, max/default/invalid limits, equal timestamps and cursor boundary inserts/deletes, both-chain tallies and large threads. Fair queue under failures, normalization variants and repeated same-block events. Before/after EXPLAIN/query counts, concurrency load/soak and freeze at increasing electorate sizes; job crash/retry/cancel if introduced. Compare optimized balances/tallies to reference implementation. ## Test plan: attack, hack and abuse vectors Unbounded limit/cursor injection, expensive-search/large-thread floods, huge electorate freeze contention, skewed address normalization and batch replays. Verify SQL binding, bounded memory/DB occupancy, no starvation and no partial freeze or changed power under load. ## Verification and completion criteria Attach reproducible datasets, SQL/query plans and before/after latency/query-count/memory evidence with chosen budgets. Run accounting and approval regressions; no speculative index or throughput claim without measurements. Record commit and pinned toolchain, commands/results and sanitized artifacts. Relevant Rust changes require `cargo test --workspace --lib` plus isolated Postgres integration (explicit guarded LEDGER_TEST_DATABASE_URL); frontend changes require `npm test` and production build plus relevant browser cases. Run secret scanning for new fixtures/config. Exercise fresh/upgrade migrations when schema/grants change; report skipped/live-only checks explicitly.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
code/voting#47
No description provided.