Skip to content

Commit

Permalink
pick: fix: more robust governance bouncer test (#4912) (#4922)
Browse files Browse the repository at this point in the history
fix: more robust governance bouncer test (#4912)

* fix: more robust governance test

* chore: lint

* fix: simpler tryUntilSuccess

* chore: lint
  • Loading branch information
kylezs authored May 31, 2024
1 parent 53276be commit 801ff35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bouncer/shared/multiple_members_governance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import Keyring from '../polkadot/keyring';
import { getChainflipApi, observeEvent } from '../shared/utils';
import { getChainflipApi, observeEvent, tryUntilSuccess } from '../shared/utils';
import { snowWhite, submitGovernanceExtrinsic } from '../shared/cf_governance';

async function getGovernanceMembers(): Promise<string[]> {
Expand Down Expand Up @@ -30,7 +30,7 @@ async function addAliceToGovernance() {
await using chainflip = await getChainflipApi();
await observeEvent('governance:Executed', chainflip);

assert((await getGovernanceMembers()).length === 2, 'Governance should now have 2 members');
await tryUntilSuccess(async () => (await getGovernanceMembers()).length === 2, 3000, 10);

console.log('Added Alice to governance!');
}
Expand Down
16 changes: 16 additions & 0 deletions bouncer/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,19 @@ export function calculateFeeWithBps(fineAmount: bigint, bps: number): bigint {
const divisor = BigInt(10000 / bps);
return fineAmount / divisor + (fineAmount % divisor > divisor / 2n ? 1n : 0n);
}

// Throws error if unsuccessful.
export async function tryUntilSuccess(
closure: () => Promise<boolean>,
pollTime: number,
maxAttempts: number,
logTag?: string,
) {
for (let i = 0; i < maxAttempts; i++) {
if (await closure()) {
return;
}
await sleep(pollTime);
}
throw new Error('tryUntilSuccess failed: ' + logTag);
}

0 comments on commit 801ff35

Please sign in to comment.