From 83c9d635120704c16d3e7f8df5a958fb03a48196 Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Wed, 15 Nov 2023 12:45:40 +0000 Subject: [PATCH 1/6] feat: speedup inflation pallet --- .../tests/creditFeesToTreasury.seqtest.ts | 2 +- js-packages/tests/inflation.seqtest.ts | 21 +++++++++++++++++++ pallets/inflation/src/tests.rs | 2 +- runtime/common/config/pallets/mod.rs | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/js-packages/tests/creditFeesToTreasury.seqtest.ts b/js-packages/tests/creditFeesToTreasury.seqtest.ts index 483cd6c07c..b13b174de8 100644 --- a/js-packages/tests/creditFeesToTreasury.seqtest.ts +++ b/js-packages/tests/creditFeesToTreasury.seqtest.ts @@ -33,7 +33,7 @@ function skipInflationBlock(api: ApiPromise): Promise { const blockInterval = inflationBlockInterval.toNumber(); const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => { const currentBlock = head.number.toNumber(); - if(currentBlock % blockInterval < blockInterval - 10) { + if(currentBlock % blockInterval < blockInterval - 2) { unsubscribe(); resolve(); } else { diff --git a/js-packages/tests/inflation.seqtest.ts b/js-packages/tests/inflation.seqtest.ts index 1dae08096f..4af110b55c 100644 --- a/js-packages/tests/inflation.seqtest.ts +++ b/js-packages/tests/inflation.seqtest.ts @@ -17,6 +17,8 @@ import type {IKeyringPair} from '@polkadot/types/types'; import {expect, itSub, usingPlaygrounds} from './util/index.js'; +const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z'; + // todo:playgrounds requires sudo, look into on the later stage describe('integration test: Inflation', () => { let superuser: IKeyringPair; @@ -55,4 +57,23 @@ describe('integration test: Inflation', () => { expect(Math.abs(Number(expectedInflation))).to.be.lessThanOrEqual(tolerance); }); + + itSub('Inflation happens after inflation block interval', async ({helper}) => { + const api = helper.getApi(); + const blockInterval = await api.consts.inflation.inflationBlockInterval.toNumber(); + + const relayBlock = (await api.query.parachainSystem.lastRelayChainBlockNumber()).toNumber(); + await helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [helper.constructApiCall('api.tx.inflation.startInflation', [relayBlock])]); + const blockInflation = (await helper.callRpc('api.query.inflation.blockInflation', []) as any).toBigInt(); + const startBlock = (relayBlock + blockInterval) - (relayBlock % blockInterval) + 1; + + await helper.wait.forRelayBlockNumber(startBlock); + + const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY); + + await helper.wait.forRelayBlockNumber(startBlock + blockInterval); + + const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY); + expect(Number(treasuryBalanceAfter)).to.be.eqls(Number(treasuryBalanceBefore + blockInflation)); + }); }); diff --git a/pallets/inflation/src/tests.rs b/pallets/inflation/src/tests.rs index b3cde26614..5323e68298 100644 --- a/pallets/inflation/src/tests.rs +++ b/pallets/inflation/src/tests.rs @@ -106,7 +106,7 @@ impl frame_system::Config for Test { parameter_types! { pub TreasuryAccountId: u64 = 1234; - pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied + pub const InflationBlockInterval: u32 = 10; // every time per how many blocks inflation is applied pub static MockBlockNumberProvider: u32 = 0; } diff --git a/runtime/common/config/pallets/mod.rs b/runtime/common/config/pallets/mod.rs index 9bab7683a3..784fc29799 100644 --- a/runtime/common/config/pallets/mod.rs +++ b/runtime/common/config/pallets/mod.rs @@ -102,7 +102,7 @@ impl pallet_balances_adapter::Config for Runtime { } parameter_types! { - pub const InflationBlockInterval: BlockNumber = 100; // every time per how many blocks inflation is applied + pub const InflationBlockInterval: BlockNumber = 10; // every time per how many blocks inflation is applied } /// Pallet-inflation needs block number in on_initialize, where there is no `validation_data` exists yet From 8b49ab609aee57637797c8619128949d1afd15ad Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Wed, 15 Nov 2023 12:45:40 +0000 Subject: [PATCH 2/6] feat: add fast-inflation flag --- js-packages/tests/creditFeesToTreasury.seqtest.ts | 2 +- pallets/inflation/Cargo.toml | 1 + runtime/common/config/pallets/mod.rs | 8 +++++++- runtime/opal/Cargo.toml | 1 + runtime/quartz/Cargo.toml | 1 + runtime/unique/Cargo.toml | 1 + 6 files changed, 12 insertions(+), 2 deletions(-) diff --git a/js-packages/tests/creditFeesToTreasury.seqtest.ts b/js-packages/tests/creditFeesToTreasury.seqtest.ts index b13b174de8..5a8a26fd42 100644 --- a/js-packages/tests/creditFeesToTreasury.seqtest.ts +++ b/js-packages/tests/creditFeesToTreasury.seqtest.ts @@ -33,7 +33,7 @@ function skipInflationBlock(api: ApiPromise): Promise { const blockInterval = inflationBlockInterval.toNumber(); const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => { const currentBlock = head.number.toNumber(); - if(currentBlock % blockInterval < blockInterval - 2) { + if(currentBlock % blockInterval < blockInterval - (blockInterval / 5)) { unsubscribe(); resolve(); } else { diff --git a/pallets/inflation/Cargo.toml b/pallets/inflation/Cargo.toml index 95716c6eba..9d149b1975 100644 --- a/pallets/inflation/Cargo.toml +++ b/pallets/inflation/Cargo.toml @@ -27,6 +27,7 @@ std = [ 'sp-std/std', ] try-runtime = ["frame-support/try-runtime"] +fast-inflation = [] [dependencies] parity-scale-codec = { workspace = true } diff --git a/runtime/common/config/pallets/mod.rs b/runtime/common/config/pallets/mod.rs index 784fc29799..3a4633c7ed 100644 --- a/runtime/common/config/pallets/mod.rs +++ b/runtime/common/config/pallets/mod.rs @@ -101,8 +101,14 @@ impl pallet_balances_adapter::Config for Runtime { type WeightInfo = pallet_balances::weights::SubstrateWeight; } +// every time per how many blocks inflation is applied +#[cfg(feature = "fast-inflation")] parameter_types! { - pub const InflationBlockInterval: BlockNumber = 10; // every time per how many blocks inflation is applied + pub const InflationBlockInterval: BlockNumber = 10; +} +#[cfg(not(feature = "fast-inflation"))] +parameter_types! { + pub const InflationBlockInterval: BlockNumber = 100; } /// Pallet-inflation needs block number in on_initialize, where there is no `validation_data` exists yet diff --git a/runtime/opal/Cargo.toml b/runtime/opal/Cargo.toml index 70ae0d3b54..c960119b86 100644 --- a/runtime/opal/Cargo.toml +++ b/runtime/opal/Cargo.toml @@ -232,6 +232,7 @@ lookahead = [] preimage = [] refungible = [] session-test-timings = [] +fast-inflation = [] ################################################################################ # local dependencies diff --git a/runtime/quartz/Cargo.toml b/runtime/quartz/Cargo.toml index 60b55375d0..b5a3136219 100644 --- a/runtime/quartz/Cargo.toml +++ b/runtime/quartz/Cargo.toml @@ -221,6 +221,7 @@ governance = [] preimage = [] refungible = [] session-test-timings = [] +fast-inflation = [] ################################################################################ # local dependencies diff --git a/runtime/unique/Cargo.toml b/runtime/unique/Cargo.toml index 7ad2e7fb31..cf900df14d 100644 --- a/runtime/unique/Cargo.toml +++ b/runtime/unique/Cargo.toml @@ -224,6 +224,7 @@ governance = [] preimage = [] refungible = [] session-test-timings = [] +fast-inflation = [] ################################################################################ # local dependencies From 43d9e123e7450857c082fe32f672f428c8d86100 Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Wed, 15 Nov 2023 12:45:40 +0000 Subject: [PATCH 3/6] fix: code review --- js-packages/tests/inflation.seqtest.ts | 13 ++++++------- pallets/inflation/src/tests.rs | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/js-packages/tests/inflation.seqtest.ts b/js-packages/tests/inflation.seqtest.ts index 4af110b55c..d685fd16c4 100644 --- a/js-packages/tests/inflation.seqtest.ts +++ b/js-packages/tests/inflation.seqtest.ts @@ -24,8 +24,12 @@ describe('integration test: Inflation', () => { let superuser: IKeyringPair; before(async () => { - await usingPlaygrounds(async (_, privateKey) => { + await usingPlaygrounds(async (helper, privateKey) => { superuser = await privateKey('//Alice'); + const api = helper.getApi(); + + const relayBlock = (await api.query.parachainSystem.lastRelayChainBlockNumber()).toNumber(); + await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [helper.constructApiCall('api.tx.inflation.startInflation', [relayBlock])])).to.not.be.rejected; }); }); @@ -38,10 +42,6 @@ describe('integration test: Inflation', () => { // Make sure superuser can't start inflation without explicit sudo await expect(helper.executeExtrinsic(superuser, 'api.tx.inflation.startInflation', [1])).to.be.rejectedWith(/BadOrigin/); - // Start inflation on relay block 1 (Alice is sudo) - const tx = helper.constructApiCall('api.tx.inflation.startInflation', [1]); - await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [tx])).to.not.be.rejected; - const blockInterval = (helper.getApi().consts.inflation.inflationBlockInterval as any).toBigInt(); const totalIssuanceStart = ((await helper.callRpc('api.query.inflation.startingYearTotalIssuance', [])) as any).toBigInt(); const blockInflation = (await helper.callRpc('api.query.inflation.blockInflation', []) as any).toBigInt(); @@ -63,7 +63,6 @@ describe('integration test: Inflation', () => { const blockInterval = await api.consts.inflation.inflationBlockInterval.toNumber(); const relayBlock = (await api.query.parachainSystem.lastRelayChainBlockNumber()).toNumber(); - await helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [helper.constructApiCall('api.tx.inflation.startInflation', [relayBlock])]); const blockInflation = (await helper.callRpc('api.query.inflation.blockInflation', []) as any).toBigInt(); const startBlock = (relayBlock + blockInterval) - (relayBlock % blockInterval) + 1; @@ -71,7 +70,7 @@ describe('integration test: Inflation', () => { const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY); - await helper.wait.forRelayBlockNumber(startBlock + blockInterval); + await helper.wait.forRelayBlockNumber(startBlock + blockInterval + 1); const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY); expect(Number(treasuryBalanceAfter)).to.be.eqls(Number(treasuryBalanceBefore + blockInflation)); diff --git a/pallets/inflation/src/tests.rs b/pallets/inflation/src/tests.rs index 5323e68298..b3cde26614 100644 --- a/pallets/inflation/src/tests.rs +++ b/pallets/inflation/src/tests.rs @@ -106,7 +106,7 @@ impl frame_system::Config for Test { parameter_types! { pub TreasuryAccountId: u64 = 1234; - pub const InflationBlockInterval: u32 = 10; // every time per how many blocks inflation is applied + pub const InflationBlockInterval: u32 = 100; // every time per how many blocks inflation is applied pub static MockBlockNumberProvider: u32 = 0; } From 3976407f1d5d7bb55167a874a1c4b669934c4678 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 15 Nov 2023 12:45:40 +0000 Subject: [PATCH 4/6] ci: enable fast-inflation for integration tests --- .docker/Dockerfile-chain-dev | 4 ++-- .docker/Dockerfile-unique | 2 +- .docker/docker-compose.gov.j2 | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.docker/Dockerfile-chain-dev b/.docker/Dockerfile-chain-dev index e18b7e0330..a19bf8c885 100644 --- a/.docker/Dockerfile-chain-dev +++ b/.docker/Dockerfile-chain-dev @@ -21,7 +21,7 @@ COPY . /dev_chain WORKDIR /dev_chain -RUN cargo build --profile integration-tests --features=${NETWORK}-runtime +RUN cargo build --profile integration-tests --features=${NETWORK}-runtime,fast-inflation RUN echo "$NETWORK" -CMD cargo run --profile integration-tests --features=${NETWORK}-runtime -- --dev -linfo --rpc-cors=all --unsafe-rpc-external +CMD cargo run --profile integration-tests --features=${NETWORK}-runtime,fast-inflation -- --dev -linfo --rpc-cors=all --unsafe-rpc-external diff --git a/.docker/Dockerfile-unique b/.docker/Dockerfile-unique index 4853de2514..06aded347e 100644 --- a/.docker/Dockerfile-unique +++ b/.docker/Dockerfile-unique @@ -47,7 +47,7 @@ RUN --mount=type=cache,target=/cargo-home/registry \ --mount=type=cache,target=/unique_parachain/unique-chain/target \ cd unique-chain && \ echo "Using runtime features '$RUNTIME_FEATURES'" && \ - CARGO_INCREMENTAL=0 cargo build --profile integration-tests --features="$RUNTIME_FEATURES" --locked && \ + CARGO_INCREMENTAL=0 cargo build --profile integration-tests --features=fast-inflation,"$RUNTIME_FEATURES" --locked && \ mv ./target/integration-tests/unique-collator /unique_parachain/unique-chain/ && \ cd target/integration-tests/wbuild && find . -name "*.wasm" -exec sh -c 'mkdir -p "../../../wasm/$(dirname {})"; cp {} "../../../wasm/{}"' \; diff --git a/.docker/docker-compose.gov.j2 b/.docker/docker-compose.gov.j2 index d08c0ef902..b411da8672 100644 --- a/.docker/docker-compose.gov.j2 +++ b/.docker/docker-compose.gov.j2 @@ -21,4 +21,4 @@ services: options: max-size: "1m" max-file: "3" - command: cargo run --profile integration-tests --features={{ NETWORK }}-runtime,gov-test-timings -- --dev -linfo --rpc-cors=all --unsafe-rpc-external + command: cargo run --profile integration-tests --features={{ NETWORK }}-runtime,gov-test-timings,fast-inflation -- --dev -linfo --rpc-cors=all --unsafe-rpc-external From d9ca20aed46c3667168135af16c77529933c8d5a Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Wed, 15 Nov 2023 18:46:42 +0000 Subject: [PATCH 5/6] fix: web3 issue in tests --- js-packages/tests/eth/util/playgrounds/unique.dev.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js-packages/tests/eth/util/playgrounds/unique.dev.ts b/js-packages/tests/eth/util/playgrounds/unique.dev.ts index 3b1f015699..cf248ff7ec 100644 --- a/js-packages/tests/eth/util/playgrounds/unique.dev.ts +++ b/js-packages/tests/eth/util/playgrounds/unique.dev.ts @@ -590,8 +590,8 @@ export class EthUniqueHelper extends DevUniqueHelper { connectWeb3(wsEndpoint: string) { if(this.web3 !== null) return; - this.web3Provider = new (web3 as any).providers.WebsocketProvider(wsEndpoint); - this.web3 = new (web3 as any)(this.web3Provider); + this.web3Provider = new web3.default.providers.WebsocketProvider(wsEndpoint); + this.web3 = new web3.default(this.web3Provider); } override async disconnect() { From cc9eeb2f3b2c037d19a241207dc4bd64716d1e45 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 16 Nov 2023 12:13:18 +0100 Subject: [PATCH 6/6] test: remove web3 as any --- js-packages/tests/eth/util/playgrounds/unique.dev.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js-packages/tests/eth/util/playgrounds/unique.dev.ts b/js-packages/tests/eth/util/playgrounds/unique.dev.ts index cf248ff7ec..0041fedaee 100644 --- a/js-packages/tests/eth/util/playgrounds/unique.dev.ts +++ b/js-packages/tests/eth/util/playgrounds/unique.dev.ts @@ -460,7 +460,7 @@ class EthAddressGroup extends EthGroupBase { fromCollectionId(collectionId: number): string { if(collectionId >= 0xffffffff || collectionId < 0) throw new Error('collectionId overflow'); - return (web3 as any).utils.toChecksumAddress(`0x17c4e6453cc49aaaaeaca894e6d9683e${collectionId.toString(16).padStart(8, '0')}`); + return web3.default.utils.toChecksumAddress(`0x17c4e6453cc49aaaaeaca894e6d9683e${collectionId.toString(16).padStart(8, '0')}`); } extractTokenId(address: string): { collectionId: number, tokenId: number } {