Skip to content

Commit

Permalink
chore: cleanups
Browse files Browse the repository at this point in the history
simplify ballotCounter creating ballots
resolve tally promise earlier
typos
  • Loading branch information
Chris-Hibbert committed Jun 20, 2021
1 parent 358ab77 commit c297a97
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/governance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@agoric/ertp": "^0.11.2",
"@agoric/eventual-send": "^0.13.16",
"@agoric/marshal": "^0.4.13",
"@agoric/nat": "^4.1.0",
"@agoric/store": "^0.4.15",
"@agoric/promise-kit": "^0.2.13",
"@agoric/zoe": "^0.16.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/governance/src/ballotBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const buildEqualWeightBallot = (
method,
question,
positions,
maxChoices = 0n,
maxChoices = 0,
) => {
const choose = chosenPositions => {
assert(
Expand Down Expand Up @@ -57,7 +57,7 @@ const buildEqualWeightBallot = (
};

/** @type {BuildBallot} */
const buildBallot = (method, question, positions, maxChoices = 0n) => {
const buildBallot = (method, question, positions, maxChoices = 0) => {
assert.typeof(question, 'string');

switch (method) {
Expand Down
20 changes: 6 additions & 14 deletions packages/governance/src/binaryBallotCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ import { ChoiceMethod, buildBallot } from './ballotBuilder';

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

const makeBinaryBallot = (question, positionAName, positionBName) => {
const positions = [];
assert.typeof(question, 'string');
assert.typeof(positionAName, 'string');
assert.typeof(positionBName, 'string');
positions.push(positionAName, positionBName);

return buildBallot(ChoiceMethod.CHOOSE_N, question, positions, 1n);
};

const makeQuorumCounter = quorumThreshold => {
const check = stats => {
const votes = stats.results.reduce(
Expand All @@ -42,14 +32,17 @@ const makeBinaryBallotCounter = (
positions.length === 2,
X`Binary ballots must have exactly two positions. had ${positions.length}: ${positions}`,
);
const [aName, bName] = positions;
assert.typeof(positions[0], 'string');
assert.typeof(positions[1], 'string');
assert.typeof(question, 'string');
if (tieOutcome) {
assert(
positions.includes(tieOutcome),
X`The default outcome on a tie must be one of the positions, not ${tieOutcome}`,
);
}
const template = makeBinaryBallot(question, aName, bName);

const template = buildBallot(ChoiceMethod.CHOOSE_N, question, positions, 1);
const ballotDetails = template.getDetails();

assert(
Expand Down Expand Up @@ -107,6 +100,7 @@ const makeBinaryBallotCounter = (
{ position: positions[1], total: tally[positions[1]] },
],
};
tallyPromise.resolve(stats);

if (!makeQuorumCounter(threshold).check(stats)) {
outcomePromise.reject('No quorum');
Expand All @@ -120,8 +114,6 @@ const makeBinaryBallotCounter = (
} else {
outcomePromise.resolve(tieOutcome);
}

tallyPromise.resolve(stats);
};

const sharedFacet = {
Expand Down
2 changes: 2 additions & 0 deletions packages/governance/src/paramManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assertIsRatio } from '@agoric/zoe/src/contractSupport';
import { AmountMath, looksLikeBrand } from '@agoric/ertp';
import { Far } from '@agoric/marshal';
import { assertKeywordName } from '@agoric/zoe/src/cleanProposal';
import { Nat } from '@agoric/nat';

/**
* @type {{
Expand Down Expand Up @@ -58,6 +59,7 @@ const assertType = (type, value, name) => {
break;
case ParamType.NAT:
assert.typeof(value, 'bigint');
Nat(value);
break;
case ParamType.RATIO:
assertIsRatio(value);
Expand Down
8 changes: 4 additions & 4 deletions packages/governance/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
*/

/**
* @typedef {Object} QurorumCounter
* @typedef {Object} QuorumCounter
* @property {(VoteStatistics) => boolean} check
*/

Expand All @@ -74,7 +74,7 @@
* @param {ChoiceMethod} method
* @param {string} question
* @param {string[]} positions
* @param {bigint} maxChoices
* @param {number} maxChoices
* @returns {Ballot}
*/

Expand Down Expand Up @@ -110,8 +110,8 @@
/**
* @typedef {Object} CompleteOrderedBallot
* @property {string} question
* @property {string[]} ordered - ordered list of position from most prefered to
* least prefered
* @property {string[]} ordered - ordered list of position from most preferred to
* least preferred
*/

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/governance/test/test-param-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ test('params one BigInt', async t => {
},
'value should be a bigint',
);
t.throws(
() => updateBigint(-1000n),
{
message: '-1000 is negative',
},
'NAT params must be positive',
);
});

test('params one ratio', async t => {
Expand Down
2 changes: 1 addition & 1 deletion packages/governance/test/unitTests/test-ballotCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ test('binary ballot too many', async t => {
() =>
E(voterFacet).submitVote(aliceSeat, aliceTemplate.choose(alicePositions)),
{
message: 'only "[1n]" position(s) allowed',
message: 'only 1 position(s) allowed',
},
);
});
Expand Down

0 comments on commit c297a97

Please sign in to comment.