Browse Source

readability improved

Gleb Urvanov 5 years ago
parent
commit
20980de49e
2 changed files with 35 additions and 7 deletions
  1. 30 6
      tests/src/tests/electingCouncilTest.ts
  2. 5 1
      tests/src/utils/apiWrapper.ts

+ 30 - 6
tests/src/tests/electingCouncilTest.ts

@@ -2,7 +2,6 @@ import { membershipTest } from './membershipCreationTest';
 import { KeyringPair } from '@polkadot/keyring/types';
 import { ApiWrapper } from '../utils/apiWrapper';
 import { WsProvider, Keyring } from '@polkadot/api';
-import { stringToU8a } from '@polkadot/util';
 import { initConfig } from '../utils/config';
 import BN = require('bn.js');
 import { registerJoystreamTypes, Seat } from '@joystream/types';
@@ -35,9 +34,9 @@ describe('Council integration tests', () => {
   membershipTest(m2KeyPairs);
 
   it('Electing a council test', async () => {
+    // Setup goes here because M keypairs are generated after before() function
     sudo = keyring.addFromUri(sudoUri);
     let now = await apiWrapper.getBestBlock();
-    await apiWrapper.sudoStartAnnouncingPerion(sudo, now.addn(100));
     const applyForCouncilFee: BN = apiWrapper.estimateApplyForCouncilFee(greaterStake);
     const voteForCouncilFee: BN = apiWrapper.estimateVoteForCouncilFee(sudo.address, sudo.address, greaterStake);
     const salt: string[] = new Array();
@@ -45,14 +44,19 @@ describe('Council integration tests', () => {
       salt.push(''.concat(uuid().replace(/-/g, '')));
     });
     const revealVoteFee: BN = apiWrapper.estimateRevealVoteFee(sudo.address, salt[0]);
+
+    // Topping the balances
     await apiWrapper.transferBalanceToAccounts(sudo, m2KeyPairs, applyForCouncilFee.add(greaterStake));
     await apiWrapper.transferBalanceToAccounts(
       sudo,
       m1KeyPairs,
       voteForCouncilFee.add(revealVoteFee).add(greaterStake)
     );
+
+    // First K members stake more
+    await apiWrapper.sudoStartAnnouncingPerion(sudo, now.addn(100));
     await apiWrapper.batchApplyForCouncilElection(m2KeyPairs.slice(0, K), greaterStake);
-    m2KeyPairs.forEach(keyPair =>
+    m2KeyPairs.slice(0, K).forEach(keyPair =>
       apiWrapper.getCouncilElectionStake(keyPair.address).then(stake => {
         assert(
           stake.eq(greaterStake),
@@ -60,7 +64,19 @@ describe('Council integration tests', () => {
         );
       })
     );
+
+    // Last members stake less
     await apiWrapper.batchApplyForCouncilElection(m2KeyPairs.slice(K), lesserStake);
+    m2KeyPairs.slice(K).forEach(keyPair =>
+      apiWrapper.getCouncilElectionStake(keyPair.address).then(stake => {
+        assert(
+          stake.eq(lesserStake),
+          `${keyPair.address} not applied correctrly for council election with stake ${stake} versus expected ${lesserStake}`
+        );
+      })
+    );
+
+    // Voting
     await apiWrapper.sudoStartVotingPerion(sudo, now.addn(100));
     await apiWrapper.batchVoteForCouncilMember(
       m1KeyPairs.slice(0, K),
@@ -69,14 +85,20 @@ describe('Council integration tests', () => {
       lesserStake
     );
     await apiWrapper.batchVoteForCouncilMember(m1KeyPairs.slice(K), m2KeyPairs.slice(K), salt.slice(K), greaterStake);
+
+    // Revealing
     await apiWrapper.sudoStartRevealingPerion(sudo, now.addn(100));
     await apiWrapper.batchRevealVote(m1KeyPairs.slice(0, K), m2KeyPairs.slice(0, K), salt.slice(0, K));
     await apiWrapper.batchRevealVote(m1KeyPairs.slice(K), m2KeyPairs.slice(K), salt.slice(K));
     now = await apiWrapper.getBestBlock();
-    await apiWrapper.sudoStartRevealingPerion(sudo, now.addn(2));
-    // TODO get duration from chain
-    await Utils.wait(6000);
+
+    // Resolving election
+    // 3 is to ensure the revealing block is in future
+    await apiWrapper.sudoStartRevealingPerion(sudo, now.addn(3));
+    await Utils.wait(apiWrapper.getBlockDuration().muln(2.5).toNumber());
     const seats: Seat[] = await apiWrapper.getCouncil();
+
+    // Preparing collections to increase assertion readability
     const m2addresses: string[] = m2KeyPairs.map(keyPair => keyPair.address);
     const m1addresses: string[] = m1KeyPairs.map(keyPair => keyPair.address);
     const members: string[] = seats.map(seat => seat.member.toString());
@@ -84,6 +106,8 @@ describe('Council integration tests', () => {
       (array, seat) => array.concat(seat.backers.map(baker => baker.member.toString())),
       new Array()
     );
+
+    // Assertions
     m2addresses.forEach(address => assert(members.includes(address), `Account ${address} is not in the council`));
     m1addresses.forEach(address => assert(bakers.includes(address), `Account ${address} is not in the voters`));
     seats.forEach(seat =>

+ 5 - 1
tests/src/utils/apiWrapper.ts

@@ -1,5 +1,5 @@
 import { ApiPromise, WsProvider } from '@polkadot/api';
-import { Option, Vec } from '@polkadot/types';
+import { Option, Vec, UInt } from '@polkadot/types';
 import { Codec } from '@polkadot/types/types';
 import { KeyringPair } from '@polkadot/keyring/types';
 import { UserInfo, PaidMembershipTerms } from '@joystream/types/lib/members';
@@ -185,4 +185,8 @@ export class ApiWrapper {
       return JSON.parse(seats.toString());
     });
   }
+
+  public getBlockDuration(): BN {
+    return this.api.createType('Moment', this.api.consts.babe.expectedBlockTime).toBn();
+  }
 }