difftreelog
Tests: fix flakyness
in: master
3 files changed
tests/src/app-promotion.test.tsdiffbeforeafterboth37let nominal: bigint;37let nominal: bigint;38const palletAddress = calculatePalleteAddress('appstake');38const palletAddress = calculatePalleteAddress('appstake');39let accounts: IKeyringPair[] = [];39let accounts: IKeyringPair[] = [];40const LOCKING_PERIOD = 20n; // 20 blocks of relay41const UNLOCKING_PERIOD = 10n; // 20 blocks of parachain42const rewardAvailableInBlock = (stakedInBlock: bigint) => (stakedInBlock - stakedInBlock % LOCKING_PERIOD) + (LOCKING_PERIOD * 2n);404341before(async function () {44before(async function () {42 await usingPlaygrounds(async (helper, privateKeyWrapper) => {45 await usingPlaygrounds(async (helper, privateKeyWrapper) => {66 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});69 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});67 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected;70 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected;68 71 69 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(100n * nominal);70 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);72 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);71 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);73 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);72 // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo? 74 // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo? 73 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased 75 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased 747675 77 76 await helper.staking.stake(staker, 200n * nominal);78 await helper.staking.stake(staker, 200n * nominal);77 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(300n * nominal);78 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal);79 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal);79 expect((await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map((x) => x[1])).to.be.deep.equal([100n * nominal, 200n * nominal]);80 expect((await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map((x) => x[1])).to.be.deep.equal([100n * nominal, 200n * nominal]);80 });81 });83 it('should allow to create maximum 10 stakes for account', async () => {84 it('should allow to create maximum 10 stakes for account', async () => {84 await usingPlaygrounds(async (helper) => {85 await usingPlaygrounds(async (helper) => {85 const [staker] = await helper.arrange.createAccounts([2000n], alice);86 const [staker] = await helper.arrange.createAccounts([2000n], alice);86 console.log(staker.address);87 for (let i = 0; i < 10; i++) {87 for (let i = 0; i < 10; i++) {88 await helper.staking.stake(staker, 100n * nominal);88 await helper.staking.stake(staker, 100n * nominal);89 }89 }147 });147 });148148149 it('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async () => {149 it('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async () => {150 // TODO Flaky test151 await usingPlaygrounds(async (helper) => {150 await usingPlaygrounds(async (helper) => {152 const staker = accounts.pop()!;151 const staker = accounts.pop()!;153 await helper.staking.stake(staker, 100n * nominal);152 await helper.staking.stake(staker, 100n * nominal);154 await helper.staking.unstake(staker);153 await helper.staking.unstake(staker);154 const unstakedInBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}))[0][0];155155156 // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n156 // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n157 await waitForRelayBlock(helper.api!, 20);157 await helper.wait.forParachainBlockNumber(unstakedInBlock);158 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});158 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});159 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);159 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);160160181 181 182 // Can unstake multiple stakes182 // Can unstake multiple stakes183 await helper.staking.unstake(staker);183 await helper.staking.unstake(staker);184 const unstakingBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}))[0][0];184 pendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});185 pendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});185 unstakedPerBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address})).map(stake => stake[1]);186 unstakedPerBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address})).map(stake => stake[1]);186 stakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(stake => stake[1]);187 stakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(stake => stake[1]);189 expect(unstakedPerBlock).to.be.deep.equal([600n * nominal]);190 expect(unstakedPerBlock).to.be.deep.equal([600n * nominal]);190191191 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n});192 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n});192 await waitForRelayBlock(helper.api!, 20);193 await helper.wait.forParachainBlockNumber(unstakingBlock);193 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});194 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});194 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);195 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);195 });196 });202 // unstake has no effect if no stakes at all203 // unstake has no effect if no stakes at all203 await helper.staking.unstake(staker);204 await helper.staking.unstake(staker);204 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);205 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);205 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(0n);206 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper206 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper207207208 // TODO stake() unstake() waitUnstaked() unstake();208 // TODO stake() unstake() waitUnstaked() unstake();213 await helper.staking.unstake(staker);213 await helper.staking.unstake(staker);214214215 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);215 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);216 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(100n * nominal);217 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);216 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);218 });217 });219 });218 });247 });246 });248 });247 });249250 // TODO for different accounts in one block is possible251});248});252249253describe('Admin adress', () => {250describe('Admin adress', () => {636 });633 });637634638 it('should credit 0.05% for staking period', async () => { 635 it('should credit 0.05% for staking period', async () => { 639 // TODO flaky test640 await usingPlaygrounds(async helper => {636 await usingPlaygrounds(async helper => {641 const staker = accounts.pop()!;637 const staker = accounts.pop()!;642 638 643 await helper.staking.stake(staker, 100n * nominal);639 await helper.staking.stake(staker, 100n * nominal);644 await helper.staking.stake(staker, 200n * nominal);640 await helper.staking.stake(staker, 200n * nominal);641642 // wair rewards are available:643 const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[1][0];645 await waitForRelayBlock(helper.api!, 30);644 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock));645646 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));646 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));647647651 });651 });652652653 it('shoud be paid for more than one period if payments was missed', async () => {653 it('shoud be paid for more than one period if payments was missed', async () => {654 // TODO flaky test655 await usingPlaygrounds(async (helper) => {654 await usingPlaygrounds(async (helper) => {656 const staker = accounts.pop()!;655 const staker = accounts.pop()!;657656658 await helper.staking.stake(staker, 100n * nominal);657 await helper.staking.stake(staker, 100n * nominal);658 // wait for two rewards are available:659 await helper.staking.stake(staker, 200n * nominal);659 const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[0][0];660661 await waitForRelayBlock(helper.api!, 55);660 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock) + LOCKING_PERIOD);661662 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));662 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));663 const stakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});663 const stakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});664 expect(stakedPerBlock[0][1]).to.be.equal(calculateIncome(100n * nominal, 10n, 2));664 const frozenBalanceShouldBe = calculateIncome(100n * nominal, 10n, 2);665 expect(stakedPerBlock[1][1]).to.be.equal(calculateIncome(200n * nominal, 10n, 2));665 expect(stakedPerBlock[0][1]).to.be.equal(frozenBalanceShouldBe);666666667 const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address);667 const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address);668 const frozenBalanceShouldBe = calculateIncome(300n * nominal, 10n, 2);669668670 expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe});669 expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe});671 });670 });672 });671 });673 672 674 it('should not be credited for unstaked (reserved) balance', async () => {673 it('should not be credited for unstaked (reserved) balance', async () => {675 await usingPlaygrounds(async helper => {674 await usingPlaygrounds(async helper => {676 // staker unstakes before rewards has been initialized675 // staker unstakes before rewards has been payed677 const staker = accounts.pop()!;676 const staker = accounts.pop()!;678 await helper.staking.stake(staker, 100n * nominal);677 await helper.staking.stake(staker, 100n * nominal);679 await waitForRelayBlock(helper.api!, 40);678 await waitForRelayBlock(helper.api!, 40);709 });708 });710 });709 });710711 it('can be paid 1000 rewards in a time', async () => {712 await usingPlaygrounds(async (helper) => {713 expect.fail('Test not implemented');714 });715 });711716712 it.skip('can handle 40.000 rewards', async () => {717 it.skip('can handle 40.000 rewards', async () => {713 await usingPlaygrounds(async (helper) => {718 await usingPlaygrounds(async (helper) => {tests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/unique.dev.ts
+++ b/tests/src/util/playgrounds/unique.dev.ts
@@ -17,7 +17,6 @@
};
}
-
export class SilentConsole {
// TODO: Remove, this is temporary: Filter unneeded API output
// (Jaco promised it will be removed in the next version)
@@ -60,10 +59,12 @@
* Arrange methods for tests
*/
arrange: ArrangeGroup;
+ wait: WaitGroup;
constructor(logger: { log: (msg: any, level: any) => void, level: any }) {
super(logger);
this.arrange = new ArrangeGroup(this);
+ this.wait = new WaitGroup(this);
}
async connect(wsEndpoint: string, _listeners?: any): Promise<void> {
@@ -152,7 +153,7 @@
for (let index = 0; index < 5; index++) {
accountsCreated = await checkBalances();
if(accountsCreated) break;
- await this.waitNewBlocks(1);
+
}
if (!accountsCreated) throw Error('Accounts generation failed');
@@ -162,11 +163,10 @@
};
// TODO combine this method and createAccounts into one
- createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {
- let transactions: any = [];
- const accounts: IKeyringPair[] = [];
-
+ createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {
const createAsManyAsCan = async () => {
+ let transactions: any = [];
+ const accounts: IKeyringPair[] = [];
let nonce = await this.helper.chain.getNonce(donor.address);
const tokenNominal = this.helper.balance.getOneTokenNominal();
for (let i = 0; i < accountsToCreate; i++) {
@@ -226,13 +226,21 @@
const block2date = await findCreationDate(block2);
if(block2date! - block1date! < 9000) return true;
};
+}
+
+class WaitGroup {
+ helper: UniqueHelper;
+ constructor(helper: UniqueHelper) {
+ this.helper = helper;
+ }
+
/**
* Wait for specified bnumber of blocks
* @param blocksCount number of blocks to wait
* @returns
*/
- async waitNewBlocks(blocksCount = 1): Promise<void> {
+ async newBlocks(blocksCount = 1): Promise<void> {
// eslint-disable-next-line no-async-promise-executor
const promise = new Promise<void>(async (resolve) => {
const unsubscribe = await this.helper.api!.rpc.chain.subscribeNewHeads(() => {
@@ -246,4 +254,26 @@
});
return promise;
}
+
+ async forParachainBlockNumber(blockNumber: bigint) {
+ return new Promise<void>(async (resolve) => {
+ const unsubscribe = await this.helper.api!.rpc.chain.subscribeNewHeads(async (data: any) => {
+ if (data.number.toNumber() >= blockNumber) {
+ unsubscribe();
+ resolve();
+ }
+ });
+ });
+ }
+
+ async forRelayBlockNumber(blockNumber: bigint) {
+ return new Promise<void>(async (resolve) => {
+ const unsubscribe = await this.helper.api!.query.parachainSystem.validationData(async (data: any) => {
+ if (data.value.relayParentNumber.toNumber() >= blockNumber) {
+ unsubscribe();
+ resolve();
+ }
+ });
+ });
+ }
}
tests/src/util/playgrounds/unique.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -1980,16 +1980,16 @@
* @param signer keyring of signer
* @param amountToUnstake amount of tokens to unstake
* @param label extra label for log
- * @returns
+ * @returns block number where balances will be unlocked
*/
- async unstake(signer: TSigner, label?: string): Promise<boolean> {
+ async unstake(signer: TSigner, label?: string): Promise<number> {
if(typeof label === 'undefined') label = `${signer.address}`;
const unstakeResult = await this.helper.executeExtrinsic(
signer, 'api.tx.appPromotion.unstake',
[], true,
);
- // TODO extract info from unstakeResult
- return true;
+ // TODO extract block number fron events
+ return 1;
}
async getTotalStaked(address?: ICrossAccountId): Promise<bigint> {
@@ -1997,10 +1997,6 @@
return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked')).toBigInt();
}
- async getTotalStakingLocked(address: ICrossAccountId): Promise<bigint> {
- return (await this.helper.callRpc('api.rpc.appPromotion.totalStakingLocked', [address])).toBigInt();
- }
-
async getTotalStakedPerBlock(address: ICrossAccountId): Promise<bigint[][]> {
return (await this.helper.callRpc('api.rpc.appPromotion.totalStakedPerBlock', [address])).map(([block, amount]: any[]) => [block.toBigInt(), amount.toBigInt()]);
}
@@ -2447,4 +2443,4 @@
async burn(signer: TSigner, amount=1n) {
return await this.collection.burnToken(signer, this.tokenId, amount);
}
-}
\ No newline at end of file
+}