@@ -10,21 +10,12 @@ import { fetchSnapshotEpoch } from '~~/packages/nimiq-validator-trustscore/src/f
1010import { tables , useDrizzle } from './drizzle'
1111import { handleValidatorLogo } from './logo'
1212import { defaultValidatorJSON } from './schemas'
13- import { PayoutType } from './types'
1413import { getUnlistedActiveValidatorAddresses , isKnownValidatorProfile } from './validator-listing'
14+ import { stripInternalValidatorFields } from './validator-public'
1515
1616export const getStoredValidatorsId = ( ) => useDrizzle ( ) . select ( { id : tables . validators . id } ) . from ( tables . validators ) . execute ( ) . then ( r => r . map ( v => v . id ) )
1717export const getStoredValidatorsAddress = ( ) => useDrizzle ( ) . select ( { address : tables . validators . address } ) . from ( tables . validators ) . execute ( ) . then ( r => r . map ( v => v . address ) )
18-
19- function isMissingIsListedColumnError ( error : unknown ) {
20- const message = String ( error )
21- return message . includes ( 'is_listed' )
22- && ( message . includes ( 'no such column' ) || message . includes ( 'has no column named' ) )
23- }
24-
25- let hasWarnedMissingIsListedColumn = false
26-
27- const validatorFieldsWithoutListState = {
18+ const validatorFieldsWithListState = {
2819 id : tables . validators . id ,
2920 name : tables . validators . name ,
3021 address : tables . validators . address ,
@@ -38,55 +29,21 @@ const validatorFieldsWithoutListState = {
3829 accentColor : tables . validators . accentColor ,
3930 website : tables . validators . website ,
4031 contact : tables . validators . contact ,
41- }
42- const validatorFieldsWithListState = {
43- ...validatorFieldsWithoutListState ,
4432 isListed : tables . validators . isListed ,
4533}
4634
47- async function selectValidatorsWithOptionalListState ( filters : SQLWrapper [ ] = [ ] ) {
48- try {
49- const query = useDrizzle ( ) . select ( validatorFieldsWithListState ) . from ( tables . validators )
50- return filters . length > 0
51- ? await query . where ( and ( ...filters ) ) . execute ( )
52- : await query . execute ( )
53- }
54- catch ( error ) {
55- if ( ! isMissingIsListedColumnError ( error ) )
56- throw error
57-
58- if ( ! hasWarnedMissingIsListedColumn ) {
59- hasWarnedMissingIsListedColumn = true
60- consola . warn ( '`validators.is_listed` column is missing, reading validators without list state until migration is applied' )
61- }
62-
63- const query = useDrizzle ( ) . select ( validatorFieldsWithoutListState ) . from ( tables . validators )
64- const rows = filters . length > 0
65- ? await query . where ( and ( ...filters ) ) . execute ( )
66- : await query . execute ( )
67- return rows . map ( row => ( { ...row , isListed : null as boolean | null } ) )
68- }
35+ async function selectValidatorsWithListState ( filters : SQLWrapper [ ] = [ ] ) {
36+ const query = useDrizzle ( ) . select ( validatorFieldsWithListState ) . from ( tables . validators )
37+ return filters . length > 0
38+ ? await query . where ( and ( ...filters ) ) . execute ( )
39+ : await query . execute ( )
6940}
7041
7142export async function getStoredValidatorsListState ( ) {
72- try {
73- return await useDrizzle ( )
74- . select ( { address : tables . validators . address , isListed : tables . validators . isListed } )
75- . from ( tables . validators )
76- . execute ( )
77- }
78- catch ( error ) {
79- if ( ! isMissingIsListedColumnError ( error ) )
80- throw error
81-
82- if ( ! hasWarnedMissingIsListedColumn ) {
83- hasWarnedMissingIsListedColumn = true
84- consola . warn ( '`validators.is_listed` column is missing, using backwards-compatible fallback until migration is applied' )
85- }
86-
87- const addresses = await getStoredValidatorsAddress ( )
88- return addresses . map ( address => ( { address, isListed : null } ) )
89- }
43+ return useDrizzle ( )
44+ . select ( { address : tables . validators . address , isListed : tables . validators . isListed } )
45+ . from ( tables . validators )
46+ . execute ( )
9047}
9148
9249const validators = new Map < string , number > ( )
@@ -140,57 +97,27 @@ export async function storeValidator(address: string, rest: ValidatorJSON = defa
14097 consola . info ( `${ upsert ? 'Updating' : 'Storing' } validator ${ address } ` )
14198
14299 const brandingParameters = await handleValidatorLogo ( address , rest )
143- const valuesWithoutListState = { ...rest , ...brandingParameters }
144- const valuesWithListState = { ...valuesWithoutListState , isListed }
100+ const values = { ...rest , ...brandingParameters , isListed }
145101
146102 try {
147103 if ( validatorId ) {
148104 await useDrizzle ( )
149105 . update ( tables . validators )
150- . set ( valuesWithListState )
106+ . set ( values )
151107 . where ( eq ( tables . validators . id , validatorId ) )
152108 . execute ( )
153109 }
154110 else {
155111 validatorId = await useDrizzle ( )
156112 . insert ( tables . validators )
157- . values ( { ...valuesWithListState , address } )
113+ . values ( { ...values , address } )
158114 . returning ( )
159115 . get ( )
160116 . then ( r => r . id )
161117 }
162118 }
163- catch ( e ) {
164- if ( ! isMissingIsListedColumnError ( e ) ) {
165- consola . error ( `There was an error while writing ${ address } into the database` , e )
166- }
167- else {
168- if ( ! hasWarnedMissingIsListedColumn ) {
169- hasWarnedMissingIsListedColumn = true
170- consola . warn ( '`validators.is_listed` column is missing, storing validators without list state until migration is applied' )
171- }
172-
173- try {
174- if ( validatorId ) {
175- await useDrizzle ( )
176- . update ( tables . validators )
177- . set ( valuesWithoutListState )
178- . where ( eq ( tables . validators . id , validatorId ) )
179- . execute ( )
180- }
181- else {
182- validatorId = await useDrizzle ( )
183- . insert ( tables . validators )
184- . values ( { ...valuesWithoutListState , address } )
185- . returning ( )
186- . get ( )
187- . then ( r => r . id )
188- }
189- }
190- catch ( fallbackError ) {
191- consola . error ( `There was an error while writing ${ address } into the database` , fallbackError )
192- }
193- }
119+ catch ( error ) {
120+ consola . error ( `There was an error while writing ${ address } into the database` , error )
194121 }
195122
196123 validators . set ( address , validatorId ! )
@@ -199,39 +126,11 @@ export async function storeValidator(address: string, rest: ValidatorJSON = defa
199126
200127export async function markValidatorsAsUnlisted ( addresses : string [ ] ) {
201128 await Promise . all ( addresses . map ( async ( address ) => {
202- const brandingParameters = await handleValidatorLogo ( address , defaultValidatorJSON )
203- const valuesWithoutListState = {
204- name : 'Unknown validator' ,
205- description : null ,
206- fee : null ,
207- payoutType : PayoutType . None ,
208- payoutSchedule : '' ,
209- isMaintainedByNimiq : false ,
210- website : null ,
211- contact : null ,
212- ...brandingParameters ,
213- }
214-
215- try {
216- await useDrizzle ( )
217- . update ( tables . validators )
218- . set ( {
219- ...valuesWithoutListState ,
220- isListed : false ,
221- } )
222- . where ( eq ( tables . validators . address , address ) )
223- . execute ( )
224- }
225- catch ( error ) {
226- if ( ! isMissingIsListedColumnError ( error ) )
227- throw error
228-
229- await useDrizzle ( )
230- . update ( tables . validators )
231- . set ( valuesWithoutListState )
232- . where ( eq ( tables . validators . address , address ) )
233- . execute ( )
234- }
129+ await useDrizzle ( )
130+ . update ( tables . validators )
131+ . set ( { isListed : false } )
132+ . where ( eq ( tables . validators . address , address ) )
133+ . execute ( )
235134 } ) )
236135}
237136
@@ -251,7 +150,7 @@ export async function fetchValidators(_event: H3Event, params: FetchValidatorsOp
251150 filters . push ( eq ( tables . validators . payoutType , payoutType ) )
252151
253152 try {
254- const dbValidators = await selectValidatorsWithOptionalListState ( filters )
153+ const dbValidators = await selectValidatorsWithListState ( filters )
255154
256155 const visibleValidators = onlyKnown ? dbValidators . filter ( isKnownValidatorProfile ) : dbValidators
257156 const validatorIds = visibleValidators . map ( v => v . id )
@@ -320,7 +219,8 @@ export async function fetchValidators(_event: H3Event, params: FetchValidatorsOp
320219 const activityByValidatorId = new Map ( activityRows . map ( row => [ row . validatorId , row ] ) )
321220
322221 const validators = visibleValidators . map ( ( validator ) => {
323- const { logo, contact, hasDefaultLogo, ...rest } = validator
222+ const { logo, hasDefaultLogo } = validator
223+ const rest = stripInternalValidatorFields ( validator )
324224 const scoreRow = scoresByValidatorId . get ( validator . id )
325225 const activityRow = activityByValidatorId . get ( validator . id )
326226
@@ -378,13 +278,13 @@ export const cachedFetchValidators = defineCachedFunction((_event: H3Event, para
378278} )
379279
380280export interface FetchValidatorOptions { address : string , range : Range }
381- export type FetchedValidatorDetails = Validator & { activity : Activity [ ] , scores : Score [ ] , score ?: Score }
281+ export type FetchedValidatorDetails = Omit < Validator , 'isListed' > & { activity : Activity [ ] , scores : Score [ ] , score ?: Score }
382282
383283export async function fetchValidator ( _event : H3Event , params : FetchValidatorOptions ) : Result < FetchedValidatorDetails > {
384284 const { address, range : { fromEpoch, toEpoch } } = params
385285
386286 try {
387- const validator = ( await selectValidatorsWithOptionalListState ( [ eq ( tables . validators . address , address ) ] ) ) . at ( 0 )
287+ const validator = ( await selectValidatorsWithListState ( [ eq ( tables . validators . address , address ) ] ) ) . at ( 0 )
388288
389289 if ( ! validator )
390290 return [ false , `Validator with address ${ address } not found` , undefined ]
@@ -421,7 +321,8 @@ export async function fetchValidator(_event: H3Event, params: FetchValidatorOpti
421321 ) )
422322 . execute ( )
423323
424- return [ true , undefined , { ...validator , scores, activity, score } ]
324+ const publicValidator = stripInternalValidatorFields ( validator )
325+ return [ true , undefined , { ...publicValidator , scores, activity, score } ]
425326 }
426327 catch ( error ) {
427328 consola . error ( `Error fetching validator ${ address } : ${ error } ` )
0 commit comments