|
| 1 | +//! Focused tests for voting snapshot stability across repeated reads. |
| 2 | +//! |
| 3 | +//! The community-consensus "snapshot" is computed (not persisted) by |
| 4 | +//! [`crate::markets::MarketAnalytics::calculate_community_consensus`] and consumed |
| 5 | +//! by [`crate::markets::MarketUtils::determine_winning_outcomes`]. These tests pin |
| 6 | +//! the two critical determinism invariants: |
| 7 | +//! |
| 8 | +//! 1. Repeated reads of the consensus yield an identical value for the same vote |
| 9 | +//! census (stable snapshot). |
| 10 | +//! 2. Tie-breaking does not depend on Soroban `Map` iteration order; ties resolve |
| 11 | +//! against the market's canonical, application-ordered `outcomes` list. |
| 12 | +
|
| 13 | +#![cfg(test)] |
| 14 | + |
| 15 | +use soroban_sdk::testutils::Address as _; |
| 16 | +use soroban_sdk::{vec, Address, Env, Map, String, Vec}; |
| 17 | + |
| 18 | +use crate::markets::{MarketAnalytics, MarketUtils}; |
| 19 | +use crate::types::{Market, MarketState, OracleConfig, OracleProvider}; |
| 20 | + |
| 21 | +/// Builds a Market with the given outcomes and no votes. |
| 22 | +fn make_market(env: &Env, outcomes: Vec<String>) -> Market { |
| 23 | + Market::new( |
| 24 | + env, |
| 25 | + Address::generate(env), |
| 26 | + String::from_str(env, "Will test outcomes be deterministically selected?"), |
| 27 | + outcomes, |
| 28 | + env.ledger().timestamp() + 86400, |
| 29 | + OracleConfig::new( |
| 30 | + OracleProvider::pyth(), |
| 31 | + Address::from_str( |
| 32 | + env, |
| 33 | + "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF", |
| 34 | + ), |
| 35 | + String::from_str(env, "TEST/USD"), |
| 36 | + 2_500_000, |
| 37 | + String::from_str(env, "gt"), |
| 38 | + ), |
| 39 | + None, |
| 40 | + 86400, |
| 41 | + MarketState::Active, |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +fn outcomes(env: &Env, names: &[&str]) -> Vec<String> { |
| 46 | + let mut out = Vec::new(env); |
| 47 | + for name in names { |
| 48 | + out.push_back(String::from_str(env, name)); |
| 49 | + } |
| 50 | + out |
| 51 | +} |
| 52 | + |
| 53 | +/// Casts one vote of `stake` for each `(outcome, stake)` entry under a fresh |
| 54 | +/// address. Outcomes are the canonical winners candidates; votes are keyed by |
| 55 | +/// generated addresses (the contract logic keys votes by Address). |
| 56 | +fn cast_votes(env: &Env, market: &mut Market, entries: &[(&str, i128)]) { |
| 57 | + for (outcome, stake) in entries { |
| 58 | + let address = Address::generate(env); |
| 59 | + market |
| 60 | + .votes |
| 61 | + .set(address.clone(), String::from_str(env, outcome)); |
| 62 | + market.stakes.set(address, (*stake).into()); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// ── Repeated-read stability ───────────────────────────────────── |
| 67 | + |
| 68 | +#[test] |
| 69 | +fn test_consensus_is_stable_across_repeated_reads() { |
| 70 | + let env = Env::default(); |
| 71 | + let mut market = make_market(&env, outcomes(&env, &["a", "b", "c"])); |
| 72 | + cast_votes( |
| 73 | + &env, |
| 74 | + &mut market, |
| 75 | + &[("a", 100), ("a", 100), ("b", 100), ("c", 100)], |
| 76 | + ); |
| 77 | + |
| 78 | + let first = MarketAnalytics::calculate_community_consensus(&market); |
| 79 | + for _ in 0..10 { |
| 80 | + let repeated = MarketAnalytics::calculate_community_consensus(&market); |
| 81 | + assert_eq!(repeated.outcome, first.outcome, "consensus outcome drifted"); |
| 82 | + assert_eq!(repeated.votes, first.votes, "consensus vote count drifted"); |
| 83 | + assert_eq!( |
| 84 | + repeated.total_votes, first.total_votes, |
| 85 | + "consensus total drifted" |
| 86 | + ); |
| 87 | + assert_eq!( |
| 88 | + repeated.percentage, first.percentage, |
| 89 | + "consensus percentage drifted" |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // Clear majority: "a" wins with 2/4 votes = 50%. |
| 94 | + assert_eq!(first.outcome, String::from_str(&env, "a")); |
| 95 | + assert_eq!(first.votes, 2); |
| 96 | + assert_eq!(first.total_votes, 4); |
| 97 | + assert_eq!(first.percentage, 50); |
| 98 | +} |
| 99 | + |
| 100 | +// ── Tie-breaking determinism ──────────────────────────────────── |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn test_tie_breaks_deterministically_by_canonical_outcome_order() { |
| 104 | + let env = Env::default(); |
| 105 | + // Exact three-way structure: "a" and "b" tie for the lead (2 each), "c" has 1. |
| 106 | + let mut market = make_market(&env, outcomes(&env, &["a", "b", "c"])); |
| 107 | + cast_votes( |
| 108 | + &env, |
| 109 | + &mut market, |
| 110 | + &[("a", 100), ("a", 100), ("b", 100), ("b", 100), ("c", 100)], |
| 111 | + ); |
| 112 | + |
| 113 | + // "a" is first-listed among the tied leaders, so it must win the tie. |
| 114 | + let consensus = MarketAnalytics::calculate_community_consensus(&market); |
| 115 | + assert_eq!(consensus.outcome, String::from_str(&env, "a")); |
| 116 | + assert_eq!(consensus.votes, 2); |
| 117 | + assert_eq!(consensus.total_votes, 5); |
| 118 | + assert_eq!(consensus.percentage, 40); |
| 119 | + |
| 120 | + // Repeated reads never flip the chosen outcome. |
| 121 | + for _ in 0..10 { |
| 122 | + let repeated = MarketAnalytics::calculate_community_consensus(&market); |
| 123 | + assert_eq!(repeated.outcome, String::from_str(&env, "a")); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#[test] |
| 128 | +fn test_tie_break_is_independent_of_vote_insertion_order() { |
| 129 | + let env = Env::default(); |
| 130 | + |
| 131 | + // Same final census, different insertion order into the underlying Map. |
| 132 | + let insert_orderings: [&[(&str, i128)]; 2] = [ |
| 133 | + &[("a", 100), ("b", 100), ("a", 100), ("b", 100)], |
| 134 | + &[("b", 100), ("a", 100), ("b", 100), ("a", 100)], |
| 135 | + ]; |
| 136 | + |
| 137 | + let mut outcomes_seen: Vec<String> = Vec::new(&env); |
| 138 | + for entries in insert_orderings.iter() { |
| 139 | + let mut market = make_market(&env, outcomes(&env, &["a", "b"])); |
| 140 | + cast_votes(&env, &mut market, entries); |
| 141 | + let consensus = MarketAnalytics::calculate_community_consensus(&market); |
| 142 | + outcomes_seen.push_back(consensus.outcome.clone()); |
| 143 | + } |
| 144 | + |
| 145 | + // Both insertion orders must resolve to the same deterministic winner ("a"). |
| 146 | + assert_eq!(outcomes_seen.len(), 2); |
| 147 | + assert_eq!(outcomes_seen.get(0).unwrap(), String::from_str(&env, "a")); |
| 148 | + assert_eq!(outcomes_seen.get(1).unwrap(), String::from_str(&env, "a")); |
| 149 | +} |
| 150 | + |
| 151 | +// ── Consistency between consensus and winning-outcome selection ─ |
| 152 | + |
| 153 | +#[test] |
| 154 | +fn test_consensus_and_winning_outcomes_agree_on_ties() { |
| 155 | + let env = Env::default(); |
| 156 | + let mut market = make_market(&env, outcomes(&env, &["a", "b", "c"])); |
| 157 | + cast_votes( |
| 158 | + &env, |
| 159 | + &mut market, |
| 160 | + &[("a", 100), ("b", 100), ("a", 50), ("b", 50)], |
| 161 | + ); |
| 162 | + |
| 163 | + let consensus = MarketAnalytics::calculate_community_consensus(&market); |
| 164 | + let oracle = String::from_str(&env, "b"); |
| 165 | + |
| 166 | + // With an exact vote tie between "a" and "b", the leader is "a" (canonical |
| 167 | + // order), but stakes are tied (150 each). The stake-based tie-break yields |
| 168 | + // BOTH as winners. Regardless of exact selection, repeated resolution must be |
| 169 | + // identical. |
| 170 | + let first = MarketUtils::determine_winning_outcomes(&env, &market, &oracle, &consensus, 0); |
| 171 | + assert_eq!(first.len(), 2, "vote-tied + stake-tied outcomes both win"); |
| 172 | + for _ in 0..10 { |
| 173 | + let repeated = |
| 174 | + MarketUtils::determine_winning_outcomes(&env, &market, &oracle, &consensus, 0); |
| 175 | + assert_eq!(repeated.len(), first.len(), "winning outcome count drifted"); |
| 176 | + for i in 0..first.len() { |
| 177 | + assert_eq!( |
| 178 | + repeated.get(i).unwrap(), |
| 179 | + first.get(i).unwrap(), |
| 180 | + "winning outcome set/order drifted" |
| 181 | + ); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// ── Boundary cases ────────────────────────────────────────────── |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn test_no_votes_yields_empty_deterministic_consensus() { |
| 190 | + let env = Env::default(); |
| 191 | + let market = make_market(&env, outcomes(&env, &["yes", "no"])); |
| 192 | + |
| 193 | + let first = MarketAnalytics::calculate_community_consensus(&market); |
| 194 | + assert_eq!(first.total_votes, 0); |
| 195 | + assert_eq!(first.votes, 0); |
| 196 | + assert_eq!(first.percentage, 0); |
| 197 | + // No votes => no consensus outcome. |
| 198 | + assert!(first.outcome.is_empty()); |
| 199 | + |
| 200 | + let second = MarketAnalytics::calculate_community_consensus(&market); |
| 201 | + assert_eq!(first.outcome, second.outcome); |
| 202 | + assert_eq!(first.votes, second.votes); |
| 203 | +} |
| 204 | + |
| 205 | +#[test] |
| 206 | +fn test_single_outcome_unanimous_vote() { |
| 207 | + let env = Env::default(); |
| 208 | + let mut market = make_market(&env, outcomes(&env, &["only"])); |
| 209 | + cast_votes( |
| 210 | + &env, |
| 211 | + &mut market, |
| 212 | + &[("only", 100), ("only", 100), ("only", 100)], |
| 213 | + ); |
| 214 | + |
| 215 | + let consensus = MarketAnalytics::calculate_community_consensus(&market); |
| 216 | + assert_eq!(consensus.outcome, String::from_str(&env, "only")); |
| 217 | + assert_eq!(consensus.votes, 3); |
| 218 | + assert_eq!(consensus.total_votes, 3); |
| 219 | + assert_eq!(consensus.percentage, 100); |
| 220 | + |
| 221 | + // Regression: unanimous vote is stable across reads. |
| 222 | + let oracle = String::from_str(&env, "only"); |
| 223 | + let first = MarketUtils::determine_winning_outcomes(&env, &market, &oracle, &consensus, 0); |
| 224 | + let second = MarketUtils::determine_winning_outcomes(&env, &market, &oracle, &consensus, 0); |
| 225 | + assert_eq!(first.len(), second.len()); |
| 226 | + assert_eq!(first.len(), 1); |
| 227 | + assert_eq!(first.get(0).unwrap(), String::from_str(&env, "only")); |
| 228 | + assert_eq!(second.get(0).unwrap(), String::from_str(&env, "only")); |
| 229 | +} |
| 230 | + |
| 231 | +// ── Regression: clear majority behavior is unchanged ──────────── |
| 232 | + |
| 233 | +#[test] |
| 234 | +fn test_clear_majority_still_selects_leader() { |
| 235 | + let env = Env::default(); |
| 236 | + let mut market = make_market(&env, outcomes(&env, &["a", "b"])); |
| 237 | + cast_votes( |
| 238 | + &env, |
| 239 | + &mut market, |
| 240 | + &[("a", 100), ("a", 100), ("a", 100), ("b", 100)], |
| 241 | + ); |
| 242 | + |
| 243 | + let consensus = MarketAnalytics::calculate_community_consensus(&market); |
| 244 | + assert_eq!(consensus.outcome, String::from_str(&env, "a")); |
| 245 | + assert_eq!(consensus.votes, 3); |
| 246 | + assert_eq!(consensus.total_votes, 4); |
| 247 | + assert_eq!(consensus.percentage, 75); |
| 248 | +} |
0 commit comments