Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Speedup inflation pallet #1017

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .docker/Dockerfile-chain-dev
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .docker/Dockerfile-unique
Original file line number Diff line number Diff line change
Expand Up @@ -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/{}"' \;

Expand Down
2 changes: 1 addition & 1 deletion .docker/docker-compose.gov.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion js-packages/tests/creditFeesToTreasury.seqtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function skipInflationBlock(api: ApiPromise): Promise<void> {
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 - (blockInterval / 5)) {
unsubscribe();
resolve();
} else {
Expand Down
6 changes: 3 additions & 3 deletions js-packages/tests/eth/util/playgrounds/unique.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } {
Expand Down Expand Up @@ -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() {
Expand Down
30 changes: 25 additions & 5 deletions js-packages/tests/inflation.seqtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
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;

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;
});
});

Expand All @@ -36,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();
Expand All @@ -55,4 +57,22 @@ 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();
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 + 1);

const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);
expect(Number(treasuryBalanceAfter)).to.be.eqls(Number(treasuryBalanceBefore + blockInflation));
});
});
1 change: 1 addition & 0 deletions pallets/inflation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ std = [
'sp-std/std',
]
try-runtime = ["frame-support/try-runtime"]
fast-inflation = []

[dependencies]
parity-scale-codec = { workspace = true }
Expand Down
8 changes: 7 additions & 1 deletion runtime/common/config/pallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ impl pallet_balances_adapter::Config for Runtime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<Self>;
}

// every time per how many blocks inflation is applied
#[cfg(feature = "fast-inflation")]
parameter_types! {
pub const InflationBlockInterval: BlockNumber = 100; // 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
Expand Down
1 change: 1 addition & 0 deletions runtime/opal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ lookahead = []
preimage = []
refungible = []
session-test-timings = []
fast-inflation = []

################################################################################
# local dependencies
Expand Down
1 change: 1 addition & 0 deletions runtime/quartz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ governance = []
preimage = []
refungible = []
session-test-timings = []
fast-inflation = []

################################################################################
# local dependencies
Expand Down
1 change: 1 addition & 0 deletions runtime/unique/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ governance = []
preimage = []
refungible = []
session-test-timings = []
fast-inflation = []

################################################################################
# local dependencies
Expand Down
Loading