From 1c9ea98e7928e9b60d54525f5fd6f73cc41ac58e Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Tue, 15 Jun 2021 13:26:03 -0700 Subject: [PATCH] refactor: move submitVote() to a facet for the voter. --- .../governance/src/binaryBallotCounter.js | 13 +++-- .../test/unitTests/test-ballotCount.js | 51 +++++++++---------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/packages/governance/src/binaryBallotCounter.js b/packages/governance/src/binaryBallotCounter.js index ff45e7ab9272..a9d0007da243 100644 --- a/packages/governance/src/binaryBallotCounter.js +++ b/packages/governance/src/binaryBallotCounter.js @@ -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) => { @@ -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); @@ -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, }); @@ -107,7 +112,7 @@ const makeBinaryBallotCounter = (question, aName, bName) => { getStats: () => tallyPromise.promise, ...sharedFacet, }); - return { publicFacet, creatorFacet }; + return { publicFacet, creatorFacet, voterFacet }; }; const start = zcf => { @@ -118,4 +123,4 @@ const start = zcf => { harden(start); harden(makeBinaryBallotCounter); -export { makeBinaryBallotCounter, start }; +export { makeBinaryBallotCounter, start, TIE_VOTE }; diff --git a/packages/governance/test/unitTests/test-ballotCount.js b/packages/governance/test/unitTests/test-ballotCount.js index 2e97be41e723..d7e901d0c408 100644 --- a/packages/governance/test/unitTests/test-ballotCount.js +++ b/packages/governance/test/unitTests/test-ballotCount.js @@ -24,7 +24,7 @@ const makeThreshold = seats => { }; test('binary ballot', async t => { - const { publicFacet, creatorFacet } = makeBinaryBallotCounter( + const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter( QUESTION, FISH, BAIT, @@ -35,7 +35,7 @@ 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(); @@ -43,7 +43,7 @@ test('binary ballot', async t => { }); test('binary spoiled', async t => { - const { publicFacet, creatorFacet } = makeBinaryBallotCounter( + const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter( QUESTION, FISH, BAIT, @@ -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'], }); @@ -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, @@ -77,8 +77,8 @@ 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(); @@ -86,7 +86,7 @@ test('binary tied', async t => { }); test('binary bad vote', async t => { - const { publicFacet, creatorFacet } = makeBinaryBallotCounter( + const { publicFacet, voterFacet } = makeBinaryBallotCounter( QUESTION, FISH, BAIT, @@ -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"', }, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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)); @@ -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, @@ -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)); @@ -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, @@ -213,10 +213,7 @@ 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', }, @@ -224,7 +221,7 @@ test('binary ballot too many', async t => { }); test('binary no quorum', async t => { - const { publicFacet, creatorFacet } = makeBinaryBallotCounter( + const { publicFacet, creatorFacet, voterFacet } = makeBinaryBallotCounter( QUESTION, FISH, BAIT, @@ -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)