Skip to content

Commit

Permalink
refactor: move submitVote() to a facet for the voter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Jun 15, 2021
1 parent 1e25870 commit 1c9ea98
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
13 changes: 9 additions & 4 deletions packages/governance/src/binaryBallotCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Far } from '@agoric/marshal';
import { E } from '@agoric/eventual-send';
import { ChoiceMethod, buildBallot } from './ballotBuilder';

const TIE_VOTE = "It's a tie!";

const makeWeightedBallot = (ballot, weight) => ({ ballot, weight });

const makeBinaryBallot = (question, positionAName, positionBName) => {
Expand Down Expand Up @@ -83,7 +85,7 @@ const makeBinaryBallotCounter = (question, aName, bName) => {
} else if (tally[positionB] > tally[positionA]) {
outcomePromise.resolve(positionB);
} else {
outcomePromise.resolve("It's a tie!");
outcomePromise.resolve(TIE_VOTE);
}

tallyPromise.resolve(stats);
Expand All @@ -95,10 +97,13 @@ const makeBinaryBallotCounter = (question, aName, bName) => {
getQuestionPositions,
};

const voterFacet = Far('voterFacet', {
submitVote: recordBallot,
});

const creatorFacet = Far('adminFacet', {
closeVoting: () => (isOpen = false),
countVotes,
submitVote: recordBallot,
...sharedFacet,
});

Expand All @@ -107,7 +112,7 @@ const makeBinaryBallotCounter = (question, aName, bName) => {
getStats: () => tallyPromise.promise,
...sharedFacet,
});
return { publicFacet, creatorFacet };
return { publicFacet, creatorFacet, voterFacet };
};

const start = zcf => {
Expand All @@ -118,4 +123,4 @@ const start = zcf => {
harden(start);
harden(makeBinaryBallotCounter);

export { makeBinaryBallotCounter, start };
export { makeBinaryBallotCounter, start, TIE_VOTE };
51 changes: 24 additions & 27 deletions packages/governance/test/unitTests/test-ballotCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const makeThreshold = seats => {
};

test('binary ballot', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -35,15 +35,15 @@ test('binary ballot', async t => {
const alicePositions = aliceTemplate.getPositions();
t.deepEqual(alicePositions.length, 2);
t.deepEqual(alicePositions[0], FISH);
creatorFacet.submitVote(aliceSeat, aliceTemplate.choose(alicePositions[0]));
voterFacet.submitVote(aliceSeat, aliceTemplate.choose(alicePositions[0]));
creatorFacet.closeVoting();
creatorFacet.countVotes(makeThreshold(1n));
const outcome = await E(publicFacet).getOutcome();
t.deepEqual(outcome, FISH);
});

test('binary spoiled', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -54,7 +54,7 @@ test('binary spoiled', async t => {
const alicePositions = aliceTemplate.getPositions();
t.deepEqual(alicePositions.length, 2);
t.deepEqual(alicePositions[0], FISH);
creatorFacet.submitVote(aliceSeat, {
voterFacet.submitVote(aliceSeat, {
question: QUESTION,
chosen: ['no'],
});
Expand All @@ -67,7 +67,7 @@ test('binary spoiled', async t => {
});

test('binary tied', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -77,16 +77,16 @@ test('binary tied', async t => {
const bobSeat = makeHandle('Seat');

const positions = aliceTemplate.getPositions();
creatorFacet.submitVote(aliceSeat, aliceTemplate.choose(positions[0]));
creatorFacet.submitVote(bobSeat, aliceTemplate.choose(positions[1]));
voterFacet.submitVote(aliceSeat, aliceTemplate.choose(positions[0]));
voterFacet.submitVote(bobSeat, aliceTemplate.choose(positions[1]));
creatorFacet.closeVoting();
creatorFacet.countVotes(makeThreshold(2n));
const outcome = await E(publicFacet).getOutcome();
t.deepEqual(outcome, "It's a tie!");
});

test('binary bad vote', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -95,7 +95,7 @@ test('binary bad vote', async t => {
const aliceSeat = makeHandle('Seat');

t.throws(
() => creatorFacet.submitVote(aliceSeat, aliceTemplate.choose('worms')),
() => voterFacet.submitVote(aliceSeat, aliceTemplate.choose('worms')),
{
message: 'Not a valid position: "worms"',
},
Expand All @@ -116,7 +116,7 @@ test('binary no votes', async t => {
});

test('binary still open', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -127,14 +127,14 @@ test('binary still open', async t => {
const alicePositions = aliceTemplate.getPositions();
t.deepEqual(alicePositions.length, 2);
t.deepEqual(alicePositions[0], 'Fish');
creatorFacet.submitVote(aliceSeat, aliceTemplate.choose(alicePositions[0]));
voterFacet.submitVote(aliceSeat, aliceTemplate.choose(alicePositions[0]));
await t.throwsAsync(() => creatorFacet.countVotes(makeThreshold(1n)), {
message: `can't count votes while the election is open`,
});
});

test('binary weights', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -145,7 +145,7 @@ test('binary weights', async t => {
const alicePositions = aliceTemplate.getPositions();
t.deepEqual(alicePositions.length, 2);
t.deepEqual(alicePositions[0], 'Fish');
creatorFacet.submitVote(
voterFacet.submitVote(
aliceSeat,
aliceTemplate.choose(alicePositions[0]),
37n,
Expand All @@ -157,7 +157,7 @@ test('binary weights', async t => {
});

test('binary contested', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -169,8 +169,8 @@ test('binary contested', async t => {
const positions = template.getPositions();
t.deepEqual(positions.length, 2);

creatorFacet.submitVote(aliceSeat, template.choose(positions[0]), 23n);
creatorFacet.submitVote(bobSeat, template.choose(positions[1]), 47n);
voterFacet.submitVote(aliceSeat, template.choose(positions[0]), 23n);
voterFacet.submitVote(bobSeat, template.choose(positions[1]), 47n);
creatorFacet.closeVoting();
creatorFacet.countVotes(makeThreshold(3n));

Expand All @@ -179,7 +179,7 @@ test('binary contested', async t => {
});

test('binary revote', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -191,9 +191,9 @@ test('binary revote', async t => {
const positions = template.getPositions();
t.deepEqual(positions.length, 2);

creatorFacet.submitVote(aliceSeat, template.choose(positions[0]), 23n);
creatorFacet.submitVote(bobSeat, template.choose(positions[1]), 47n);
creatorFacet.submitVote(bobSeat, template.choose(positions[1]), 15n);
voterFacet.submitVote(aliceSeat, template.choose(positions[0]), 23n);
voterFacet.submitVote(bobSeat, template.choose(positions[1]), 47n);
voterFacet.submitVote(bobSeat, template.choose(positions[1]), 15n);
creatorFacet.closeVoting();
creatorFacet.countVotes(makeThreshold(5n));

Expand All @@ -202,7 +202,7 @@ test('binary revote', async t => {
});

test('binary ballot too many', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -213,18 +213,15 @@ test('binary ballot too many', async t => {
const alicePositions = aliceTemplate.getPositions();
t.throws(
() =>
creatorFacet.submitVote(
aliceSeat,
aliceTemplate.choose(...alicePositions),
),
voterFacet.submitVote(aliceSeat, aliceTemplate.choose(...alicePositions)),
{
message: 'only 1 position(s) allowed',
},
);
});

test('binary no quorum', async t => {
const { publicFacet, creatorFacet } = makeBinaryBallotCounter(
const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter(
QUESTION,
FISH,
BAIT,
Expand All @@ -233,7 +230,7 @@ test('binary no quorum', async t => {
const aliceSeat = makeHandle('Seat');

const positions = aliceTemplate.getPositions();
creatorFacet.submitVote(aliceSeat, aliceTemplate.choose(positions[0]));
voterFacet.submitVote(aliceSeat, aliceTemplate.choose(positions[0]));
creatorFacet.closeVoting();
creatorFacet.countVotes(makeThreshold(2n));
await E(publicFacet)
Expand Down

0 comments on commit 1c9ea98

Please sign in to comment.