feat(#140): expose voter stake in get_proposal_votes_history (+ fix witness vote-history ordering)

Closes #140 (closed).

What

GET /proposals/{proposal-id}/votes/history returned only (voter_name, approve, timestamp), so the Block Explorer's Proposals Votes History modal couldn't show voter power — the proposalVotesHistoryDialog.voterPower locale key was added across all 11 locales for block_explorer_ui#722 but had to be dropped during review (block_explorer_ui!916 (merged)) because the API didn't return stake. This closes that parity gap.

Each history record now carries voter_vests / direct_vests / proxied_vests / proxy — the same quadruple the sibling GET /proposals/votes already returns, so the FE can render both views with one component.

Query-only: no schema change, no new tables, processors, or indexes, as the issue specifies.

Two commits, reviewable independently

  • bb894f9 fix(witness) — unrelated correctness bug found while auditing sort keys for this change. Standalone, no dependency on #140 (closed). See below; happy to split into its own MR if preferred.
  • 11dc819 feat(#140) — the feature.

API semantics (please sanity-check these)

  • voter_vests is '0' when the voter currently has a governance proxy set, matching get_proposal_votes and hived's proposal totals (process_proposal_vote_stats_cache excludes proxied voters). direct_vests is never zeroed, so their real stake stays visible.
  • The implication is one-way: a proxied voter reports 0, but a voter who simply holds no VESTS also reports 0 with an empty proxy. Clients must read proxy to distinguish the two — which is why the field is included rather than optional.
  • The row is historical; the numbers are not. These are the voter's current stake, not their stake at the block of the vote. HAFBE stores no point-in-time vesting snapshot and get_witness_votes_history behaves identically. Documented in the OpenAPI descriptions. ⚠️ FE note: don't label this column "voting power at time of vote" — it isn't.
  • Naming diverges from the witness endpoint deliberately. Witness uses vests/account_vests, doesn't zero under proxy, and has no proxy field. Setting a proxy deletes witness votes, so that case is unreachable there; proposal votes survive a proxy and must be excluded from the proposal's total.

Stake sourcing

Chosen per voter, never per row, so a voter's approve and withdrawal rows always agree:

  1. account_vest_stats_cache — fast path, but only tracks accounts that currently vote or proxy. A voter who withdrew approval has no cache row.
  2. expired_voter_stats_view — resolves those, joined only for the page's cache-miss voters.

Pagination happens before enrichment, so the stake joins never see more than page-size rows. Measured on a 5M-block DB: 7.3 ms for an all-cache-hit page (fallback CTE reports rows=0, everything under it "never executed") vs 41.6 ms worst case with 100 misses — the same cost get_witness_votes_history already pays.

The witness fix (bb894f9)

get_witness_votes_history sorted on (source_op_block, voter_id) — not a total order. A Hive block is 3s and nothing stops an account voting and un-voting the same witness within one block, producing two rows identical on both keys. Tied rows have no defined order, so asc could return the un-vote before the vote it undid.

It also sorts twice (once in limited_set to pick the page, once after the UNION ALL to display it), both on the same incomplete key — so the two sorts could break an identical tie in opposite directions, making row order depend on page size.

Real case: cmtzco voted for arhag and un-voted in block 3067986. Reading arhag's 654 rows as fourteen 50-row pages vs one page gave 30 differing positions. To be precise: the multiset is preserved — no rows lost or duplicated, only reordered.

Fix: thread source_op (unique + monotonic within a block) through all four CTEs and add it as the final key to both ORDER BYs. This is the rule get_proposals, get_witnesses, get_witness_voters and get_account_proxies_power already apply — several with a "Tiebreaker: X for stable ordering" comment. Witness vote history was the omission.

I audited all 15 paginating sorts in the codebase; these two history endpoints were the only non-total ones.

Testing

  • 2 new mock patterns pin the cache-hit (initminer) and expired-view fallback (steem) branches.
  • tests/mocks/sql/verify.sql gains value-independent DB-level assertions that prove the fallback actually fired. The tavern patterns alone can't: a voter with 0 vests would make a working fallback and one broken back to COALESCE(..., 0) emit byte-identical JSON.
  • New tied_same_block_vote_unvote pattern pins the cmtzco/arhag ordering.
  • Zero existing witness fixtures changed — none of the 8 committed patterns contains a tied pair, which is exactly why the suite never caught this.
  • Verified against a live 5M-block DB: all stake branches, both tie-breaks, asc/desc symmetry, paged-vs-whole reconstruction (0 mismatches), and all 35 tied witnesses now chronological.

Edited by Mahdi Jaafar

Merge request reports

Loading
Loading