code wiki / _hdl_build / nx_connect_polls_lib.nx

nx_connect_polls_lib.nx source

↩ module page · 53 lines · 2153 B

1// nx_connect_polls_lib.nx -- the poll/ballot engine, extracted as a shared library so the verifying gate 2// (nx_connect_polls) and the LIVE app (nx_connect_serve /community) bind ONE implementation. The live tally 3// was previously a single stored choice with the counts recomputed inline -- structurally revote-replaces, 4// but a second expression of the rule rather than the verified engine. 5// 6// THE LAW: one voter, one ballot. A revote REPLACES the voter's existing ballot in place, so a tally can 7// never inflate however many times someone votes. Counts are derived by scanning ballots, and the ballot 8// store holds the voter only so a re-vote can find it -- reported counts are aggregate, never per-person. 9// Symbols moved VERBATIM (no renames): renaming is what de-grounded compare rows on the earlier splits. 10// license_tier: ORIGINAL 11const PL_MAXV: i64 = 64 12 13func pl_vote(bv: *i64, bc: *i64, nb: *i64, meta: *i64, voter: i64, choice: i64) -> i64 { 14 // meta: [0]=closed [1]=nopts [2]=member_lo [3]=member_hi (membership = data-driven id range) 15 if meta[0]==1 { return 0-2 } 16 if voter<meta[2] { return 0-1 } 17 if voter>meta[3] { return 0-1 } 18 if choice<0 { return 0-3 } 19 if choice>=meta[1] { return 0-3 } 20 var i: i64=0 21 while i<nb[0] { 22 if bv[i]==voter { bc[i]=choice; return 2 } 23 i=i+1 24 } 25 let k: i64=nb[0] 26 bv[k]=voter; bc[k]=choice 27 nb[0]=k+1 28 return 1 29} 30func pl_count(bc: *i64, nb: *i64, choice: i64) -> i64 { 31 var c: i64=0 32 var i: i64=0 33 while i<nb[0] { if bc[i]==choice { c=c+1 } i=i+1 } 34 return c 35} 36// NEG-CONTROL incumbent tally: APPEND-only (revote double-counts) 37func pl_vote_append(bv: *i64, bc: *i64, nb: *i64, voter: i64, choice: i64) -> i64 { 38 let k: i64=nb[0] 39 bv[k]=voter; bc[k]=choice 40 nb[0]=k+1 41 return 1 42} 43// quiz score for a voter: revealed ONLY after close; -1 before close 44func pl_quiz_score(bv: *i64, bc: *i64, nb: *i64, meta: *i64, correct: i64, voter: i64) -> i64 { 45 if meta[0]==0 { return 0-1 } 46 var i: i64=0 47 while i<nb[0] { 48 if bv[i]==voter { if bc[i]==correct { return 1 } return 0 } 49 i=i+1 50 } 51 return 0 52} 53