Skip to content

Commit

Permalink
fix: invalid validator active set (#1256)
Browse files Browse the repository at this point in the history
* fix: invalid validator active set

* allow storageKey typefactory to take in a string and number

* add v9370 metadata polkadot registry

* fix spacing

* add validator address mock data for testing

* mock response

* Create mock validator entry data

* create test
  • Loading branch information
TarikGul committed Apr 3, 2023
1 parent 6bfb577 commit a13269b
Show file tree
Hide file tree
Showing 9 changed files with 4,796 additions and 5 deletions.
75 changes: 75 additions & 0 deletions src/services/pallets/PalletsStakingValidatorsService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2017-2023 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 type { ApiPromise } from '@polkadot/api';
import type { ApiDecoration } from '@polkadot/api/types';
import type { Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { polkadotRegistryV9370 } from '../../test-helpers/registries';
import { blockHash789629, defaultMockApi } from '../test-helpers/mock';
import { validatorsEntries } from '../test-helpers/mock/data/validator14815152Entries';
import { validators14815152Hex } from '../test-helpers/mock/data/validators14815152Hex';
import fetchValidators14815152 from '../test-helpers/responses/pallets/fetchValidators14815152.json';
import { PalletsStakingValidatorsService } from './PalletsStakingValidatorsService';

const validatorsAt = () =>
Promise.resolve().then(() =>
polkadotRegistryV9370.createType('Vec<AccountId32>', validators14815152Hex)
);

const validatorsEntriesAt = () =>
Promise.resolve().then(() => validatorsEntries());

const mockHistoricApi = {
query: {
session: {
validators: validatorsAt,
},
staking: {
validators: {
entries: validatorsEntriesAt,
},
},
},
} as unknown as ApiDecoration<'promise'>;

const mockApi = {
...defaultMockApi,
at: (_hash: Hash) => mockHistoricApi,
} as unknown as ApiPromise;
/**
* Mock PalletStakingProgressService instance.
*/
const palletsStakingValidatorsService = new PalletsStakingValidatorsService(
mockApi
);

describe('PalletsStakingValidatorsService', () => {
describe('derivePalletStakingValidators', () => {
it('Works for block 14815152', async () => {
expect(
sanitizeNumbers(
// The inputted blockHash does not change the tests since the mocked data is all based
// on block 14815152
await palletsStakingValidatorsService.derivePalletStakingValidators(
blockHash789629
)
)
).toStrictEqual(fetchValidators14815152);
});
});
});
19 changes: 15 additions & 4 deletions src/services/pallets/PalletsStakingValidatorsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,25 @@ export class PalletsStakingValidatorsService extends AbstractService {
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';
const address = key.args.map((k) => k.toString())[0];
let status: 'active' | 'waiting';
if (validatorsActiveSet.has(address)) {
status = 'active';
validatorsActiveSet.delete(address);
} else {
status = 'waiting';
}
validators.push({ address, status });
});

if (validatorsActiveSet.size > 0) {
validatorsActiveSet.forEach((address) =>
validators.push({ address, status: 'active' })
);
}

return {
validators,
};
Expand Down
38 changes: 38 additions & 0 deletions src/services/test-helpers/mock/data/validator14815152Entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2017-2023 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 { polkadotMetadataRpcV9370 } from '../../../../test-helpers/metadata/polkadotV9370Metadata';
import {
createApiWithAugmentations,
TypeFactory,
} from '../../../../test-helpers/typeFactory';
import { validatorsAddresses } from './validatorsAddresses';

const typeFactoryApiV9370 = createApiWithAugmentations(
polkadotMetadataRpcV9370
);
const factory = new TypeFactory(typeFactoryApiV9370);

export const validatorsEntries = () => {
return validatorsAddresses.map((addr) => {
const storage = factory.storageKey(
addr,
'AccountId32',
typeFactoryApiV9370.query.staking.validators
);
return [storage];
});
};
18 changes: 18 additions & 0 deletions src/services/test-helpers/mock/data/validators14815152Hex.ts

Large diffs are not rendered by default.

Loading

0 comments on commit a13269b

Please sign in to comment.