|
@@ -9,8 +9,8 @@ import { Fixture } from '../IFixture'
|
|
|
|
|
|
export class ElectCouncilFixture implements Fixture {
|
|
|
private api: Api
|
|
|
- private membersKeyPairs: KeyringPair[]
|
|
|
- private councilKeyPairs: KeyringPair[]
|
|
|
+ private voterKeyPairs: KeyringPair[]
|
|
|
+ private applicantKeyPairs: KeyringPair[]
|
|
|
private k: number
|
|
|
private sudo: KeyringPair
|
|
|
private greaterStake: BN
|
|
@@ -18,16 +18,16 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
|
|
|
public constructor(
|
|
|
api: Api,
|
|
|
- membersKeyPairs: KeyringPair[],
|
|
|
- councilKeyPairs: KeyringPair[],
|
|
|
+ voterKeyPairs: KeyringPair[],
|
|
|
+ applicantKeyPairs: KeyringPair[],
|
|
|
k: number,
|
|
|
sudo: KeyringPair,
|
|
|
greaterStake: BN,
|
|
|
lesserStake: BN
|
|
|
) {
|
|
|
this.api = api
|
|
|
- this.membersKeyPairs = membersKeyPairs
|
|
|
- this.councilKeyPairs = councilKeyPairs
|
|
|
+ this.voterKeyPairs = voterKeyPairs
|
|
|
+ this.applicantKeyPairs = applicantKeyPairs
|
|
|
this.k = k
|
|
|
this.sudo = sudo
|
|
|
this.greaterStake = greaterStake
|
|
@@ -35,6 +35,9 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
}
|
|
|
|
|
|
public async runner(expectFailure: boolean): Promise<void> {
|
|
|
+ // Assert no council exists
|
|
|
+ assert((await this.api.getCouncil()).length === 0)
|
|
|
+
|
|
|
let now = await this.api.getBestBlock()
|
|
|
const applyForCouncilFee: BN = this.api.estimateApplyForCouncilFee(this.greaterStake)
|
|
|
const voteForCouncilFee: BN = this.api.estimateVoteForCouncilFee(
|
|
@@ -43,23 +46,27 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
this.greaterStake
|
|
|
)
|
|
|
const salt: string[] = []
|
|
|
- this.membersKeyPairs.forEach(() => {
|
|
|
+ this.voterKeyPairs.forEach(() => {
|
|
|
salt.push(''.concat(uuid().replace(/-/g, '')))
|
|
|
})
|
|
|
const revealVoteFee: BN = this.api.estimateRevealVoteFee(this.sudo.address, salt[0])
|
|
|
|
|
|
// Topping the balances
|
|
|
- await this.api.transferBalanceToAccounts(this.sudo, this.councilKeyPairs, applyForCouncilFee.add(this.greaterStake))
|
|
|
await this.api.transferBalanceToAccounts(
|
|
|
this.sudo,
|
|
|
- this.membersKeyPairs,
|
|
|
+ this.applicantKeyPairs,
|
|
|
+ applyForCouncilFee.add(this.greaterStake)
|
|
|
+ )
|
|
|
+ await this.api.transferBalanceToAccounts(
|
|
|
+ this.sudo,
|
|
|
+ this.voterKeyPairs,
|
|
|
voteForCouncilFee.add(revealVoteFee).add(this.greaterStake)
|
|
|
)
|
|
|
|
|
|
// First K members stake more
|
|
|
await this.api.sudoStartAnnouncingPeriod(this.sudo, now.addn(100))
|
|
|
- await this.api.batchApplyForCouncilElection(this.councilKeyPairs.slice(0, this.k), this.greaterStake)
|
|
|
- this.councilKeyPairs.slice(0, this.k).forEach((keyPair) =>
|
|
|
+ await this.api.batchApplyForCouncilElection(this.applicantKeyPairs.slice(0, this.k), this.greaterStake)
|
|
|
+ this.applicantKeyPairs.slice(0, this.k).forEach((keyPair) =>
|
|
|
this.api.getCouncilElectionStake(keyPair.address).then((stake) => {
|
|
|
assert(
|
|
|
stake.eq(this.greaterStake),
|
|
@@ -69,8 +76,8 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
)
|
|
|
|
|
|
// Last members stake less
|
|
|
- await this.api.batchApplyForCouncilElection(this.councilKeyPairs.slice(this.k), this.lesserStake)
|
|
|
- this.councilKeyPairs.slice(this.k).forEach((keyPair) =>
|
|
|
+ await this.api.batchApplyForCouncilElection(this.applicantKeyPairs.slice(this.k), this.lesserStake)
|
|
|
+ this.applicantKeyPairs.slice(this.k).forEach((keyPair) =>
|
|
|
this.api.getCouncilElectionStake(keyPair.address).then((stake) => {
|
|
|
assert(
|
|
|
stake.eq(this.lesserStake),
|
|
@@ -82,14 +89,14 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
// Voting
|
|
|
await this.api.sudoStartVotingPeriod(this.sudo, now.addn(100))
|
|
|
await this.api.batchVoteForCouncilMember(
|
|
|
- this.membersKeyPairs.slice(0, this.k),
|
|
|
- this.councilKeyPairs.slice(0, this.k),
|
|
|
+ this.voterKeyPairs.slice(0, this.k),
|
|
|
+ this.applicantKeyPairs.slice(0, this.k),
|
|
|
salt.slice(0, this.k),
|
|
|
this.lesserStake
|
|
|
)
|
|
|
await this.api.batchVoteForCouncilMember(
|
|
|
- this.membersKeyPairs.slice(this.k),
|
|
|
- this.councilKeyPairs.slice(this.k),
|
|
|
+ this.voterKeyPairs.slice(this.k),
|
|
|
+ this.applicantKeyPairs.slice(this.k),
|
|
|
salt.slice(this.k),
|
|
|
this.greaterStake
|
|
|
)
|
|
@@ -97,13 +104,13 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
// Revealing
|
|
|
await this.api.sudoStartRevealingPeriod(this.sudo, now.addn(100))
|
|
|
await this.api.batchRevealVote(
|
|
|
- this.membersKeyPairs.slice(0, this.k),
|
|
|
- this.councilKeyPairs.slice(0, this.k),
|
|
|
+ this.voterKeyPairs.slice(0, this.k),
|
|
|
+ this.applicantKeyPairs.slice(0, this.k),
|
|
|
salt.slice(0, this.k)
|
|
|
)
|
|
|
await this.api.batchRevealVote(
|
|
|
- this.membersKeyPairs.slice(this.k),
|
|
|
- this.councilKeyPairs.slice(this.k),
|
|
|
+ this.voterKeyPairs.slice(this.k),
|
|
|
+ this.applicantKeyPairs.slice(this.k),
|
|
|
salt.slice(this.k)
|
|
|
)
|
|
|
now = await this.api.getBestBlock()
|
|
@@ -114,21 +121,14 @@ export class ElectCouncilFixture implements Fixture {
|
|
|
await Utils.wait(this.api.getBlockDuration().muln(2.5).toNumber())
|
|
|
const seats: Seat[] = await this.api.getCouncil()
|
|
|
|
|
|
- // Preparing collections to increase assertion readability
|
|
|
- const councilAddresses: string[] = this.councilKeyPairs.map((keyPair) => keyPair.address)
|
|
|
- const membersAddresses: string[] = this.membersKeyPairs.map((keyPair) => keyPair.address)
|
|
|
- const members: string[] = seats.map((seat) => seat.member.toString())
|
|
|
- const bakers: string[] = seats.map((seat) => seat.backers.map((baker) => baker.member.toString())).flat()
|
|
|
+ // Assert a council was created
|
|
|
+ assert(seats.length)
|
|
|
+
|
|
|
+ // const applicantAddresses: string[] = this.applicantKeyPairs.map((keyPair) => keyPair.address)
|
|
|
+ // const voterAddresses: string[] = this.voterKeyPairs.map((keyPair) => keyPair.address)
|
|
|
+ // const councilMembers: string[] = seats.map((seat) => seat.member.toString())
|
|
|
+ // const backers: string[] = seats.map((seat) => seat.backers.map((backer) => backer.member.toString())).flat()
|
|
|
|
|
|
- // Assertions
|
|
|
- councilAddresses.forEach((address) => assert(members.includes(address), `Account ${address} is not in the council`))
|
|
|
- membersAddresses.forEach((address) => assert(bakers.includes(address), `Account ${address} is not in the voters`))
|
|
|
- seats.forEach((seat) =>
|
|
|
- assert(
|
|
|
- Utils.getTotalStake(seat).eq(this.greaterStake.add(this.lesserStake)),
|
|
|
- `Member ${seat.member} has unexpected stake ${Utils.getTotalStake(seat)}`
|
|
|
- )
|
|
|
- )
|
|
|
if (expectFailure) {
|
|
|
throw new Error('Successful fixture run while expecting failure')
|
|
|
}
|