@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
154 lines • 7.45 kB
JavaScript
;
// Copyright 2017-2025 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/>.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const apiRegistry_1 = require("../../apiRegistry");
const sanitizeNumbers_1 = require("../../sanitize/sanitizeNumbers");
const registries_1 = require("../../test-helpers/registries");
const mock_1 = require("../test-helpers/mock");
const activeEraMock_1 = require("../test-helpers/mock/data/activeEraMock");
const erasStakersOverviewKeys_1 = require("../test-helpers/mock/data/erasStakersOverviewKeys");
const validator14815152Entries_1 = require("../test-helpers/mock/data/validator14815152Entries");
const validators14815152Hex_1 = require("../test-helpers/mock/data/validators14815152Hex");
const fetchValidators14815152_json_1 = __importDefault(require("../test-helpers/responses/pallets/fetchValidators14815152.json"));
const PalletsStakingValidatorsService_1 = require("./PalletsStakingValidatorsService");
const validatorsAt = () => Promise.resolve().then(() => registries_1.polkadotRegistryV9370.createType('Vec<AccountId32>', validators14815152Hex_1.validators14815152Hex));
const validatorsEntriesAt = () => Promise.resolve().then(() => (0, validator14815152Entries_1.validatorsEntries)());
const erasStakersOverviewKeys = () => Promise.resolve().then(() => (0, erasStakersOverviewKeys_1.erasStakersOverview)());
const activeEra = () => Promise.resolve().then(() => (0, activeEraMock_1.activeEraMock)());
const mockHistoricApi = {
...mock_1.defaultMockApi,
query: {
session: {
validators: validatorsAt,
},
staking: {
validators: {
entries: validatorsEntriesAt,
},
},
},
};
// const mockApiNoStaking = {
// ...defaultMockApi,
// at: (_hash: Hash) => defaultMockApi,
// } as unknown as ApiPromise;
const mockApi = {
...mock_1.defaultMockApi,
at: (_hash) => mockHistoricApi,
};
const mockHistoricAHNextApi = {
...mock_1.defaultMockApi,
rpc: {
...mock_1.defaultMockApi.rpc,
state: {
...mock_1.defaultMockApi.rpc.state,
getRuntimeVersion: () => Promise.resolve().then(() => {
return {
specName: registries_1.assetHubKusamaRegistryV9430.createType('Text', 'statemine'),
specVersion: registries_1.assetHubKusamaRegistryV9430.createType('u32', 16),
transactionVersion: registries_1.assetHubKusamaRegistryV9430.createType('u32', 2),
implVersion: registries_1.assetHubKusamaRegistryV9430.createType('u32', 0),
implName: registries_1.assetHubKusamaRegistryV9430.createType('Text', 'parity-kusama'),
authoringVersion: registries_1.assetHubKusamaRegistryV9430.createType('u32', 0),
};
}),
},
},
query: {
staking: {
validators: {
entries: validatorsEntriesAt,
},
activeEra,
erasStakersOverview: {
keys: erasStakersOverviewKeys,
},
},
},
};
const mockAHNextApi = {
...mockHistoricAHNextApi,
at: (_hash) => mockHistoricAHNextApi,
};
const mockRCNextApi = {
...mock_1.defaultMockApi,
query: {
session: {
validators: validatorsAt,
},
staking: null,
},
at: (_hash) => ({
...mock_1.defaultMockApi,
query: {
session: {
validators: validatorsAt,
},
staking: null,
},
}),
};
/**
* Mock PalletStakingProgressService instance.
*/
describe('PalletsStakingValidatorsService', () => {
describe('derivePalletStakingValidators before AHM', () => {
it('Works for block 14815152', async () => {
const palletsStakingValidatorsService = new PalletsStakingValidatorsService_1.PalletsStakingValidatorsService('mock');
jest.spyOn(apiRegistry_1.ApiPromiseRegistry, 'getApi').mockImplementation(() => mockApi);
expect((0, sanitizeNumbers_1.sanitizeNumbers)(
// The inputted blockHash does not change the tests since the mocked data is all based
// on block 14815152
await palletsStakingValidatorsService.derivePalletStakingValidators(mock_1.blockHash789629))).toStrictEqual(fetchValidators14815152_json_1.default);
});
});
describe('derivePalletStakingValidators after AHM', () => {
it('it throws if historicApi does not have staking', async () => {
apiRegistry_1.ApiPromiseRegistry.assetHubInfo = {
isAssetHub: false,
isAssetHubMigrated: true,
};
const palletsStakingValidatorsService = new PalletsStakingValidatorsService_1.PalletsStakingValidatorsService('mock');
jest.spyOn(apiRegistry_1.ApiPromiseRegistry, 'getApi').mockImplementation(() => mockRCNextApi);
await expect(palletsStakingValidatorsService.derivePalletStakingValidators(mock_1.blockHash789629)).rejects.toThrow('Staking pallet not found for queried runtime');
});
it('it correctly computes the response when connected to AH post AHM', async () => {
apiRegistry_1.ApiPromiseRegistry.assetHubInfo = {
isAssetHub: false,
isAssetHubMigrated: false,
};
const palletsStakingValidatorsServicePreAHM = new PalletsStakingValidatorsService_1.PalletsStakingValidatorsService('mock');
jest.spyOn(apiRegistry_1.ApiPromiseRegistry, 'getApi').mockImplementation(() => mockAHNextApi);
const preAHMResponse = await palletsStakingValidatorsServicePreAHM.derivePalletStakingValidators(mock_1.blockHash789629);
apiRegistry_1.ApiPromiseRegistry.assetHubInfo = {
isAssetHub: true,
isAssetHubMigrated: true,
};
const palletsStakingValidatorsService = new PalletsStakingValidatorsService_1.PalletsStakingValidatorsService('statemine');
const postAHMResponse = await palletsStakingValidatorsService.derivePalletStakingValidators(mock_1.blockHash789629);
for (const [index, validator] of postAHMResponse.validators.entries()) {
expect(validator).toEqual(preAHMResponse.validators[index]);
}
expect(postAHMResponse.at).toEqual(preAHMResponse.at);
expect(postAHMResponse.validatorsToBeChilled).toEqual(preAHMResponse.validatorsToBeChilled);
});
});
});
//# sourceMappingURL=PalletsStakingValidatorsService.spec.js.map