Skip to content

Commit

Permalink
fix: adjust historyDepth to a consts (#1115)
Browse files Browse the repository at this point in the history
* fix: adjust historyDepth to a consts

* maintain historical integrity
  • Loading branch information
TarikGul committed Oct 27, 2022
1 parent d0defe0 commit bc3c2a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/services/accounts/AccountsStakingPayoutsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ import { AccountsStakingPayoutsService } from './AccountsStakingPayoutsService';
*/
const era = polkadotRegistryV9122.createType('EraIndex', 532);

const historyDepthAt = (): Promise<u32> =>
Promise.resolve().then(() => {
return polkadotRegistryV9122.createType('u32', 84);
});
const historyDepthAt = polkadotRegistryV9122.createType('u32', 84);

const erasRewardPointsAt = (
_eraIndex: EraIndex
Expand Down Expand Up @@ -125,12 +122,16 @@ const deriveEraExposure = (_eraIndex: EraIndex): Promise<DeriveEraExposure> =>

const mockHistoricApi = {
registry: polkadotRegistryV9122,
consts: {
staking: {
historyDepth: historyDepthAt,
},
},
query: {
staking: {
ledger: ledgerAt,
erasRewardPoints: erasRewardPointsAt,
erasValidatorReward: erasValidatorRewardAt,
historyDepth: historyDepthAt,
erasValidatorPrefs: erasValidatorPrefsAt,
bonded: bondedAt,
},
Expand Down
18 changes: 14 additions & 4 deletions src/services/accounts/AccountsStakingPayoutsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,20 @@ export class AccountsStakingPayoutsService extends AbstractService {
const { api } = this;
const historicApi = await api.at(hash);

const [{ number }, historyDepth] = await Promise.all([
api.rpc.chain.getHeader(hash),
historicApi.query.staking.historyDepth<u32>(),
]);
const { number } = await api.rpc.chain.getHeader(hash);

/**
* Given https://github.com/polkadot-js/api/issues/5232,
* polkadot-js, and substrate treats historyDepth as a consts. In order
* to maintain historical integrity we need to make a check to cover both the
* storage query and the consts.
*/
let historyDepth: u32;
if (historicApi.consts.staking.historyDepth) {
historyDepth = historicApi.consts.staking.historyDepth;
} else {
historyDepth = await historicApi.query.staking.historyDepth<u32>();
}

// Information is kept for eras in `[current_era - history_depth; current_era]`
if (depth > historyDepth.toNumber()) {
Expand Down

0 comments on commit bc3c2a5

Please sign in to comment.