Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update eraElectionStatus for runtime upgrade v30 #485

Merged
merged 11 commits into from
Mar 29, 2021
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,8 @@ components:
status:
type: object
description: >-
[Deprecated](Works for polkadot runtimes before v0.8.30).

Era election status: either `Close: null` or `Open: <BlockNumber>`.
A status of `Close` indicates that the submission window for solutions
from off-chain Phragmen is not open. A status of `Open` indicates that the
Expand Down
25 changes: 18 additions & 7 deletions src/services/pallets/PalletsStakingProgressService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ export class PalletsStakingProgressService extends AbstractService {
const [
validatorCount,
forceEra,
eraElectionStatus,
validators,
{ number },
] = await Promise.all([
api.query.staking.validatorCount.at(hash),
api.query.staking.forceEra.at(hash),
api.query.staking.eraElectionStatus.at(hash),
api.query.session.validators.at(hash),
api.rpc.chain.getHeader(hash),
]);

let eraElectionStatus;
/**
* Polkadot runtimes v0.8.30 and above do not support eraElectionStatus, so we check
* to see if eraElectionStatus is mounted to the api, and if were running on a
* runtime less than v0.8.30 it will return a successful result. If it doesn't
* we do nothing and let `eraElectionStatus` stay undefined.
*/
if (api.query.staking.eraElectionStatus) {
eraElectionStatus = await api.query.staking.eraElectionStatus.at(hash);
}

const {
eraLength,
eraProgress,
Expand Down Expand Up @@ -88,7 +97,7 @@ export class PalletsStakingProgressService extends AbstractService {
if (electionLookAhead.eq(new BN(0))) {
// no offchain solutions accepted
toggle = null;
} else if ((eraElectionStatus as { isClose?: boolean }).isClose) {
} else if ((eraElectionStatus as { isClose?: boolean })?.isClose) {
// election window is yet to open
toggle = nextCurrentEra.sub(electionLookAhead);
} else {
Expand All @@ -99,10 +108,12 @@ export class PalletsStakingProgressService extends AbstractService {
return {
...baseResponse,
nextActiveEraEstimate: nextActiveEra.toString(10),
electionStatus: {
status: eraElectionStatus.toJSON(),
toggleEstimate: toggle?.toString(10) ?? null,
},
electionStatus: eraElectionStatus
? {
status: eraElectionStatus.toJSON(),
toggleEstimate: toggle?.toString(10) ?? null,
}
: 'Deprecated, see docs',
idealValidatorCount: validatorCount.toString(10),
validatorSet: validators.map((accountId) => accountId.toString()),
};
Expand Down
10 changes: 6 additions & 4 deletions src/types/responses/PalletStakingProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export interface IPalletStakingProgress {
nextActiveEraEstimate?: AnyJson;
nextSessionEstimate: string | null;
unappliedSlashes: AnyJson[] | null;
electionStatus?: {
status: AnyJson;
toggleEstimate: string | null;
};
electionStatus?:
| {
status: AnyJson;
toggleEstimate: string | null;
}
| string;
Comment on lines +14 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dq: What do the |s do here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here we have a Union Type, the first | denotes that the type has not yet been decided but it will guaranteed be one of the following below. And all following |'s are there to allow electionStatus to fulfill multiple types.

validatorSet?: string[] | null;
}