-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid validator active set (#1256)
* 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
Showing
9 changed files
with
4,796 additions
and
5 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/services/pallets/PalletsStakingValidatorsService.spec.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,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); | ||
}); | ||
}); | ||
}); |
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
38 changes: 38 additions & 0 deletions
38
src/services/test-helpers/mock/data/validator14815152Entries.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,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
18
src/services/test-helpers/mock/data/validators14815152Hex.ts
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.