-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43447c4
commit baf8f08
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/brain/src/modules/cron/removeExitedValidators/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { BeaconchainApi, ValidatorApi, Web3SignerApi } from "../../apiClients/index.js"; | ||
import { ValidatorStatus } from "../../apiClients/types"; | ||
import { BrainDataBase } from "../../db/index.js"; | ||
import logger from "../../logger/index.js"; | ||
|
||
/** | ||
* Remove the validators with status "exited_slashed" or "withdrawal_done" | ||
*/ | ||
export async function removeExitedValidators( | ||
brainDb: BrainDataBase, | ||
signerApi: Web3SignerApi, | ||
validatorApi: ValidatorApi, | ||
beaconchainApi: BeaconchainApi | ||
): Promise<void> { | ||
try { | ||
// get validators from db | ||
const validators = brainDb.getData(); | ||
|
||
// get validators status from beaconchain | ||
const validatorsStatus = await beaconchainApi.postStateValidators({ | ||
stateId: "finalized", | ||
body: { | ||
ids: Object.keys(validators), | ||
statuses: [ValidatorStatus.EXITED_SLASHED, ValidatorStatus.WITHDRAWAL_DONE] | ||
} | ||
}); | ||
|
||
// filter validators with status "exited_slashed" or "withdrawal_done" | ||
const validatorsToRemove = validatorsStatus.data | ||
.filter( | ||
(validator) => | ||
validator.status === ValidatorStatus.EXITED_SLASHED || validator.status === ValidatorStatus.WITHDRAWAL_DONE | ||
) | ||
.map((validator) => validator.index); | ||
|
||
if (validatorsToRemove.length === 0) { | ||
logger.info("No validators with status 'exited_slashed' or 'withdrawal_done' to remove"); | ||
return; | ||
} | ||
|
||
logger.info( | ||
`Removing validators with status "exited_slashed" or "withdrawal_done": ${validatorsToRemove.join(", ")}` | ||
); | ||
|
||
// remove validators from the validator API | ||
await validatorApi.deleteRemoteKeys({ pubkeys: validatorsToRemove }); | ||
// remove validators from the signer API | ||
await signerApi.deleteRemoteKeys({ pubkeys: validatorsToRemove }); | ||
// remove validators from the DB | ||
brainDb.deleteValidators(validatorsToRemove); | ||
} catch (e) { | ||
logger.error(`Error removing exited validators: ${e.message}`, e); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/brain/src/modules/cron/removeExitedValidators/logPrefix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const logPrefix = "[CRON - removeExitedValidators]: "; |