-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
/pallets/staking/validators
endpoint (#1045)
* Added the `validators` endpoint - Controller, service, response type - Returns all validators addresses and their corresponding status (active / waiting) * Validators Info per block - Added the `at` query param - Modified the Service accordingly * yarn lint * Changes `array` to `Set` (James Super Tip) - Changed the data structure used to save the validators in the active set from an `array` to a `Set` (based on James suggestion) - Changed also the used functions accordingly. - Merged in one loop the check of which validator is in the active set and the population of the returned array. * Lint & Update docs - yarn lint - Added validators endpoint to the docs - Corrected other parts of the docs mentioned by the swagger as errors. * Added in latest e2e tests the validators endpoint * Changes from Tarik s comments * Adjustments in the docstring * Update src/services/pallets/PalletsStakingValidatorsService.ts Co-authored-by: Tarik Gul <47201679+TarikGul@users.noreply.github.com> * yarn lint:fix Co-authored-by: Tarik Gul <47201679+TarikGul@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
src/controllers/pallets/PalletsStakingValidatorsController.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 @@ | ||
// Copyright 2017-2022 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { ApiPromise } from '@polkadot/api'; | ||
import { RequestHandler } from 'express'; | ||
|
||
import { PalletsStakingValidatorsService } from '../../services'; | ||
import AbstractController from '../AbstractController'; | ||
|
||
export default class PalletsStakingValidatorsController extends AbstractController<PalletsStakingValidatorsService> { | ||
constructor(api: ApiPromise) { | ||
super( | ||
api, | ||
'/pallets/staking/validators', | ||
new PalletsStakingValidatorsService(api) | ||
); | ||
this.initRoutes(); | ||
} | ||
|
||
protected initRoutes(): void { | ||
this.safeMountAsyncGetHandlers([['', this.getPalletStakingValidators]]); | ||
} | ||
|
||
/** | ||
* Get the progress of the staking pallet system. | ||
* | ||
* @param _req Express Request | ||
* @param res Express Response | ||
*/ | ||
private getPalletStakingValidators: RequestHandler = async ( | ||
{ query: { at } }, | ||
res | ||
): Promise<void> => { | ||
const hash = await this.getHashFromAt(at); | ||
|
||
PalletsStakingValidatorsController.sanitizedSend( | ||
res, | ||
await this.service.derivePalletStakingValidators(hash) | ||
); | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2017-2022 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { BlockHash } from '@polkadot/types/interfaces'; | ||
import { IPalletStakingValidator, IValidator } from 'src/types/responses'; | ||
|
||
import { AbstractService } from '../AbstractService'; | ||
|
||
export class PalletsStakingValidatorsService extends AbstractService { | ||
/** | ||
* Fetch all validators addresses and their status at a given block. | ||
* The status of the validator can be either: | ||
* - `active` (validator is part of the active set) or | ||
* - `waiting` (validator did not get into the active set this era) | ||
* | ||
* @param hash `BlockHash` to make call at | ||
*/ | ||
async derivePalletStakingValidators( | ||
hash: BlockHash | ||
): Promise<IPalletStakingValidator> { | ||
const { api } = this; | ||
const historicApi = await api.at(hash); | ||
|
||
const validatorSession = await historicApi.query.session.validators(); | ||
const validatorsActiveSet = new Set<string>(); | ||
for (const address of validatorSession) { | ||
validatorsActiveSet.add(address.toString()); | ||
} | ||
|
||
// Populating the returned array with the Validator address and its | ||
// status. If the address is found in the `validatorsActiveSet` then | ||
// status is `active` otherwise is set to `waiting` | ||
const validators: IValidator[] = []; | ||
const validatorsEntries = | ||
await historicApi.query.staking.validators.entries(); | ||
validatorsEntries.map(([key]) => { | ||
const address = key.args.map((k) => k.toHuman())[0]; | ||
const status: string = validatorsActiveSet.has(address) | ||
? 'active' | ||
: 'waiting'; | ||
validators.push({ address, status }); | ||
}); | ||
|
||
return { | ||
validators, | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2017-2022 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
export interface IValidator { | ||
/** | ||
* Address of the validator. | ||
*/ | ||
address: string; | ||
/** | ||
* The status of the validator which can be either `active` or `waiting`. | ||
* `Active` means that the validator is part of the active set and | ||
* `waiting` means that the validator did not get into the active set this era. | ||
*/ | ||
status: string; | ||
} | ||
|
||
export interface IPalletStakingValidator { | ||
validators: IValidator[]; | ||
} |
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