@@ -10,9 +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'
14+ import { getUnlistedActiveValidatorAddresses , isKnownValidatorProfile } from './validator-listing'
1315
1416export const getStoredValidatorsId = ( ) => useDrizzle ( ) . select ( { id : tables . validators . id } ) . from ( tables . validators ) . execute ( ) . then ( r => r . map ( v => v . id ) )
1517export const getStoredValidatorsAddress = ( ) => useDrizzle ( ) . select ( { address : tables . validators . address } ) . from ( tables . validators ) . execute ( ) . then ( r => r . map ( v => v . address ) )
18+ export const getStoredValidatorsListState = ( ) => useDrizzle ( ) . select ( { address : tables . validators . address , isListed : tables . validators . isListed } ) . from ( tables . validators ) . execute ( )
1619
1720const validators = new Map < string , number > ( )
1821
@@ -22,6 +25,12 @@ interface StoreValidatorOptions {
2225 * @default false
2326 */
2427 upsert ?: boolean
28+
29+ /**
30+ * Controls if the validator should appear in `only-known=true`.
31+ * @default false
32+ */
33+ isListed ?: boolean
2534}
2635
2736export async function storeValidator ( address : string , rest : ValidatorJSON = defaultValidatorJSON , options : StoreValidatorOptions = { } ) : Promise < number | undefined > {
@@ -34,7 +43,7 @@ export async function storeValidator(address: string, rest: ValidatorJSON = defa
3443 return
3544 }
3645
37- const { upsert = false } = options
46+ const { upsert = false , isListed = false } = options
3847
3948 // If the validator is cached and upsert is not true, return it
4049 if ( ! upsert && validators . has ( address ) ) {
@@ -63,14 +72,14 @@ export async function storeValidator(address: string, rest: ValidatorJSON = defa
6372 if ( validatorId ) {
6473 await useDrizzle ( )
6574 . update ( tables . validators )
66- . set ( { ...rest , ...brandingParameters } )
75+ . set ( { ...rest , ...brandingParameters , isListed } )
6776 . where ( eq ( tables . validators . id , validatorId ) )
6877 . execute ( )
6978 }
7079 else {
7180 validatorId = await useDrizzle ( )
7281 . insert ( tables . validators )
73- . values ( { ...rest , address, ...brandingParameters } )
82+ . values ( { ...rest , address, ...brandingParameters , isListed } )
7483 . returning ( )
7584 . get ( )
7685 . then ( r => r . id )
@@ -84,6 +93,28 @@ export async function storeValidator(address: string, rest: ValidatorJSON = defa
8493 return validatorId
8594}
8695
96+ export async function markValidatorsAsUnlisted ( addresses : string [ ] ) {
97+ await Promise . all ( addresses . map ( async ( address ) => {
98+ const brandingParameters = await handleValidatorLogo ( address , defaultValidatorJSON )
99+ await useDrizzle ( )
100+ . update ( tables . validators )
101+ . set ( {
102+ name : 'Unknown validator' ,
103+ description : null ,
104+ fee : null ,
105+ payoutType : PayoutType . None ,
106+ payoutSchedule : '' ,
107+ isMaintainedByNimiq : false ,
108+ website : null ,
109+ contact : null ,
110+ isListed : false ,
111+ ...brandingParameters ,
112+ } )
113+ . where ( eq ( tables . validators . address , address ) )
114+ . execute ( )
115+ } ) )
116+ }
117+
87118export type FetchValidatorsOptions = MainQuerySchema & { epochNumber : number }
88119
89120export async function fetchValidators ( _event : H3Event , params : FetchValidatorsOptions ) : Result < FetchedValidator [ ] > {
@@ -98,16 +129,15 @@ export async function fetchValidators(_event: H3Event, params: FetchValidatorsOp
98129 const filters : SQLWrapper [ ] = [ ]
99130 if ( payoutType )
100131 filters . push ( eq ( tables . validators . payoutType , payoutType ) )
101- if ( onlyKnown )
102- filters . push ( sql `lower(${ tables . validators . name } ) NOT LIKE lower('%Unknown validator%')` )
103132
104133 try {
105134 const validatorsQuery = useDrizzle ( ) . select ( ) . from ( tables . validators )
106135 const dbValidators = filters . length > 0
107136 ? await validatorsQuery . where ( and ( ...filters ) ) . execute ( )
108137 : await validatorsQuery . execute ( )
109138
110- const validatorIds = dbValidators . map ( v => v . id )
139+ const visibleValidators = onlyKnown ? dbValidators . filter ( isKnownValidatorProfile ) : dbValidators
140+ const validatorIds = visibleValidators . map ( v => v . id )
111141 if ( validatorIds . length === 0 )
112142 return [ true , undefined , [ ] ]
113143
@@ -172,7 +202,7 @@ export async function fetchValidators(_event: H3Event, params: FetchValidatorsOp
172202 const scoresByValidatorId = new Map ( scoresRows . map ( row => [ row . validatorId , row ] ) )
173203 const activityByValidatorId = new Map ( activityRows . map ( row => [ row . validatorId , row ] ) )
174204
175- const validators = dbValidators . map ( ( validator ) => {
205+ const validators = visibleValidators . map ( ( validator ) => {
176206 const { logo, contact, hasDefaultLogo, ...rest } = validator
177207 const scoreRow = scoresByValidatorId . get ( validator . id )
178208 const activityRow = activityByValidatorId . get ( validator . id )
@@ -301,17 +331,20 @@ export async function categorizeValidatorsSnapshotEpoch(): Result<SnapshotEpochV
301331 if ( ! epochOk )
302332 return [ false , error , undefined ]
303333
304- const dbAddresses = await getStoredValidatorsAddress ( )
334+ const storedValidators = await getStoredValidatorsListState ( )
335+ const dbAddresses = storedValidators . map ( v => v . address )
305336 const electedValidators = epoch . validators . filter ( v => v . elected ) as ElectedValidator [ ]
306337 const unelectedValidators = epoch . validators . filter ( v => ! v . elected ) as UnelectedValidator [ ]
307338 const untrackedValidators = electedValidators . filter ( v => ! dbAddresses . includes ( v . address ) ) as ( ElectedValidator & UnelectedValidator ) [ ]
308339 const deletedValidators = dbAddresses . filter ( dbAddress => ! epoch . validators . map ( v => v . address ) . includes ( dbAddress ) )
340+ const unlistedActiveValidators = getUnlistedActiveValidatorAddresses ( epoch . validators , storedValidators )
309341
310342 return [ true , undefined , {
311343 epochNumber : epoch . epochNumber ,
312344 electedValidators,
313345 unelectedValidators,
314346 untrackedValidators,
315347 deletedValidators,
348+ unlistedActiveValidators,
316349 } ]
317350}
0 commit comments