@@ -9,6 +9,8 @@ type ProposalVpValues = Map<string, { cb: number; vpValueByStrategy: number[] }>
99
1010type Datum = {
1111 id : string ;
12+ voter : string ;
13+ space : string ;
1214 vpState : string ;
1315 vpByStrategy : number [ ] ;
1416 vpValueByStrategy : number [ ] ;
@@ -22,6 +24,8 @@ const PROPOSALS_BATCH_SIZE = 50000;
2224const datumSchema = z
2325 . object ( {
2426 id : z . string ( ) ,
27+ voter : z . string ( ) ,
28+ space : z . string ( ) ,
2529 vpState : z . string ( ) ,
2630 vpValueByStrategy : z . array ( z . number ( ) . finite ( ) ) ,
2731 vpByStrategy : z . array ( z . number ( ) . finite ( ) )
@@ -31,17 +35,26 @@ const datumSchema = z
3135 } ) ;
3236
3337async function getPendingVotes ( ) : Promise <
34- { id : string ; proposal : string ; vpState : string ; vpByStrategy : number [ ] } [ ]
38+ {
39+ id : string ;
40+ voter : string ;
41+ space : string ;
42+ proposal : string ;
43+ vpState : string ;
44+ vpByStrategy : number [ ] ;
45+ } [ ]
3546> {
3647 const query = `
37- SELECT id, proposal, vp_state, vp_by_strategy
48+ SELECT id, voter, space, proposal, vp_state, vp_by_strategy
3849 FROM votes
3950 WHERE cb = ?
4051 LIMIT ?` ;
4152 const results = await db . queryAsync ( query , [ CB . PENDING_COMPUTE , DEFAULT_BATCH_SIZE ] ) ;
4253
4354 return results . map ( ( r : any ) => ( {
4455 id : r . id ,
56+ voter : r . voter ,
57+ space : r . space ,
4558 proposal : r . proposal ,
4659 vpState : r . vp_state ,
4760 vpByStrategy : JSON . parse ( r . vp_by_strategy )
@@ -88,6 +101,7 @@ async function refreshVotesVpValues(data: Datum[]) {
88101 const ids : string [ ] = [ ] ;
89102 const vpValues : Map < string , number > = new Map ( ) ;
90103 const cbValues : Map < string , number > = new Map ( ) ;
104+ const leaderboardPairs : Set < string > = new Set ( ) ;
91105
92106 for ( const datum of data ) {
93107 if ( datum . proposalCb === CB . INELIGIBLE ) {
@@ -110,6 +124,8 @@ async function refreshVotesVpValues(data: Datum[]) {
110124 validatedDatum . id ,
111125 validatedDatum . vpState === 'final' ? CB . FINAL : CB . PENDING_FINAL
112126 ) ;
127+
128+ leaderboardPairs . add ( `${ validatedDatum . voter } :${ validatedDatum . space } ` ) ;
113129 } catch ( e ) {
114130 capture ( e ) ;
115131 ids . push ( datum . id ) ;
@@ -132,8 +148,30 @@ async function refreshVotesVpValues(data: Datum[]) {
132148 cbParams . push ( id , cbValues . get ( id ) ! ) ;
133149 }
134150
135- const query = `UPDATE votes SET vp_value = CASE ${ vpCases } END, cb = CASE ${ cbCases } END WHERE id IN (${ placeholders } )` ;
136- await db . queryAsync ( query , [ ...vpParams , ...cbParams , ...ids ] ) ;
151+ const queries : string [ ] = [
152+ `UPDATE votes SET vp_value = CASE ${ vpCases } END, cb = CASE ${ cbCases } END WHERE id IN (${ placeholders } ) AND cb = ?`
153+ ] ;
154+ const params : ( number | string ) [ ] = [ ...vpParams , ...cbParams , ...ids , CB . PENDING_COMPUTE ] ;
155+
156+ // Refresh leaderboard vp_value using SUM from votes table (idempotent)
157+ if ( leaderboardPairs . size ) {
158+ const pairPlaceholders = Array . from ( leaderboardPairs )
159+ . map ( ( ) => '(?, ?)' )
160+ . join ( ', ' ) ;
161+ queries . push (
162+ `UPDATE leaderboard l
163+ SET vp_value = COALESCE((
164+ SELECT SUM(v.vp_value) FROM votes v WHERE v.voter = l.user AND v.space = l.space
165+ ), 0)
166+ WHERE (l.user, l.space) IN (${ pairPlaceholders } )`
167+ ) ;
168+ for ( const pair of leaderboardPairs ) {
169+ const [ voter , space ] = pair . split ( ':' ) ;
170+ params . push ( voter , space ) ;
171+ }
172+ }
173+
174+ await db . queryAsync ( queries . join ( ';' ) , params ) ;
137175}
138176
139177async function processBatch ( proposalVpValues : ProposalVpValues ) : Promise < number > {
@@ -146,6 +184,8 @@ async function processBatch(proposalVpValues: ProposalVpValues): Promise<number>
146184 const proposal = proposalVpValues . get ( v . proposal ) ! ;
147185 return {
148186 id : v . id ,
187+ voter : v . voter ,
188+ space : v . space ,
149189 vpState : v . vpState ,
150190 vpByStrategy : v . vpByStrategy ,
151191 vpValueByStrategy : proposal . vpValueByStrategy ,
0 commit comments