From bd1788c4ac4fda9779c3fe631044b26d7c9c2259 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Tue, 24 Sep 2024 18:43:14 +0200 Subject: [PATCH 01/13] Use ballots from proposal to evaluate voted state --- packages/nns/src/utils/neurons.utils.ts | 29 ++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.ts b/packages/nns/src/utils/neurons.utils.ts index c9a21677d..8d63a0635 100644 --- a/packages/nns/src/utils/neurons.utils.ts +++ b/packages/nns/src/utils/neurons.utils.ts @@ -1,25 +1,24 @@ import type { Vote } from "../enums/governance.enums"; +import type { NeuronId } from "../types/common"; import type { Ballot, - BallotInfo, NeuronInfo, - ProposalId, ProposalInfo, } from "../types/governance_converters"; const voteForProposal = ({ - recentBallots, - proposalId, + ballots, + neuronId, }: { - recentBallots: BallotInfo[]; - proposalId: ProposalId | undefined; + ballots: Ballot[]; + neuronId: NeuronId | undefined; }): Vote | undefined => { - if (!proposalId) { + if (!neuronId) { return undefined; } - const ballot: BallotInfo | undefined = recentBallots.find( - ({ proposalId: id }: BallotInfo) => id === proposalId, + const ballot: Ballot | undefined = ballots.find( + ({ neuronId: id }) => id === neuronId, ); return ballot?.vote; }; @@ -72,11 +71,11 @@ export const votableNeurons = ({ neurons: NeuronInfo[]; proposal: ProposalInfo; }): NeuronInfo[] => { - const { id: proposalId } = proposal; + const { ballots } = proposal; return neurons.filter( - ({ recentBallots, neuronId }: NeuronInfo) => - voteForProposal({ recentBallots, proposalId }) === undefined && + ({ neuronId }: NeuronInfo) => + voteForProposal({ ballots, neuronId }) === undefined && ineligibleNeurons({ neurons, proposal }).find( ({ neuronId: ineligibleNeuronId }: NeuronInfo) => ineligibleNeuronId === neuronId, @@ -93,12 +92,12 @@ export const votableNeurons = ({ */ export const votedNeurons = ({ neurons, - proposal: { id: proposalId }, + proposal: { ballots }, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }): NeuronInfo[] => neurons.filter( - ({ recentBallots }: NeuronInfo) => - voteForProposal({ recentBallots, proposalId }) !== undefined, + ({ neuronId }: NeuronInfo) => + voteForProposal({ ballots, neuronId }) !== undefined, ); From 894b72167a6d6595da792c45849bd8e1e5f8989d Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:44:42 +0000 Subject: [PATCH 02/13] =?UTF-8?q?=F0=9F=A4=96=20Documentation=20auto-updat?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/nns/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/nns/README.md b/packages/nns/README.md index 7504e0407..5469d7230 100644 --- a/packages/nns/README.md +++ b/packages/nns/README.md @@ -96,7 +96,7 @@ Parameters: - `params.neurons`: The neurons to filter. - `params.proposal`: The proposal to match against the selected neurons. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L39) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L38) #### :gear: votableNeurons @@ -111,22 +111,22 @@ Parameters: - `params.neurons`: The neurons to filter. - `params.proposal`: The proposal to match against the selected neurons. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L68) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L67) #### :gear: votedNeurons Filter the neurons that have voted for a proposal. -| Function | Type | -| -------------- | ------------------------------------------------------------------------------------------------------------------ | -| `votedNeurons` | `({ neurons, proposal: { id: proposalId }, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }) => NeuronInfo[]` | +| Function | Type | +| -------------- | ----------------------------------------------------------------------------------------------------------- | +| `votedNeurons` | `({ neurons, proposal: { ballots }, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }) => NeuronInfo[]` | Parameters: - `params.neurons`: The neurons to filter. - `params.proposal`: The proposal for which some neurons might have already voted. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L94) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L93) ### :factory: GenesisTokenCanister From 213d201f7170c2bb2fb862bef44be47e4c02f452 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Wed, 25 Sep 2024 17:25:09 +0200 Subject: [PATCH 03/13] Fix votableNeurons logic --- packages/nns/src/utils/neurons.utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.ts b/packages/nns/src/utils/neurons.utils.ts index 8d63a0635..c7075c530 100644 --- a/packages/nns/src/utils/neurons.utils.ts +++ b/packages/nns/src/utils/neurons.utils.ts @@ -1,4 +1,4 @@ -import type { Vote } from "../enums/governance.enums"; +import { Vote } from "../enums/governance.enums"; import type { NeuronId } from "../types/common"; import type { Ballot, @@ -75,7 +75,7 @@ export const votableNeurons = ({ return neurons.filter( ({ neuronId }: NeuronInfo) => - voteForProposal({ ballots, neuronId }) === undefined && + voteForProposal({ ballots, neuronId }) === Vote.Unspecified && ineligibleNeurons({ neurons, proposal }).find( ({ neuronId: ineligibleNeuronId }: NeuronInfo) => ineligibleNeuronId === neuronId, From e92b3cb2e0d4caac21a71294195e83ea89bf2354 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Tue, 1 Oct 2024 17:08:48 +0200 Subject: [PATCH 04/13] Fix votedNeurons --- packages/nns/src/utils/neurons.utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nns/src/utils/neurons.utils.ts b/packages/nns/src/utils/neurons.utils.ts index c7075c530..d449b08e0 100644 --- a/packages/nns/src/utils/neurons.utils.ts +++ b/packages/nns/src/utils/neurons.utils.ts @@ -99,5 +99,5 @@ export const votedNeurons = ({ }): NeuronInfo[] => neurons.filter( ({ neuronId }: NeuronInfo) => - voteForProposal({ ballots, neuronId }) !== undefined, + voteForProposal({ ballots, neuronId }) !== Vote.Unspecified, ); From 4079a1beef5889cb7e38aaff556a81e7a57e5c2a Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Tue, 1 Oct 2024 17:23:14 +0200 Subject: [PATCH 05/13] Adjust tests to rely on the proposal ballots --- packages/nns/src/utils/neurons.utils.spec.ts | 101 ++++++++++++------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.spec.ts b/packages/nns/src/utils/neurons.utils.spec.ts index b46df30d5..343720143 100644 --- a/packages/nns/src/utils/neurons.utils.spec.ts +++ b/packages/nns/src/utils/neurons.utils.spec.ts @@ -47,6 +47,18 @@ describe("neurons-utils", () => { recentBallots: [], votingPower: BigInt(1), } as unknown as NeuronInfo, + { + createdTimestampSeconds: proposalTimestampSeconds - BigInt(2), + neuronId: proposalNeuronId + 1n, + recentBallots: [], + votingPower: BigInt(1), + } as unknown as NeuronInfo, + { + createdTimestampSeconds: proposalTimestampSeconds - BigInt(3), + neuronId: proposalNeuronId + 2n, + recentBallots: [], + votingPower: BigInt(1), + } as unknown as NeuronInfo, ]; it("should has an ineligible neuron because created after proposal", () => { @@ -104,16 +116,17 @@ describe("neurons-utils", () => { it("should have votable neurons because not yet voted", () => { const votable = votableNeurons({ - proposal, + proposal: { + ...proposal, + ballots: [{ + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }], + }, neurons: [ { ...eligibleNeuronsDate[0], - recentBallots: [ - { - proposalId: BigInt(4), - vote: Vote.No, - }, - ], }, ], }); @@ -122,11 +135,17 @@ describe("neurons-utils", () => { it("should have votable neurons because never voted", () => { const votable = votableNeurons({ - proposal, + proposal: { + ...proposal, + ballots: [{ + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }], + }, neurons: [ { ...eligibleNeuronsDate[0], - recentBallots: [], }, ], }); @@ -135,40 +154,44 @@ describe("neurons-utils", () => { it("should have votable neurons regardless of voting power", () => { const votable = votableNeurons({ - proposal, - neurons: [ - { - ...eligibleNeuronsDate[0], - recentBallots: [], - votingPower: BigInt(0), - }, - { - ...eligibleNeuronsDate[0], - recentBallots: [], - votingPower: BigInt(1), - }, - { - ...eligibleNeuronsDate[0], - recentBallots: [], - votingPower: BigInt(0), - }, - ], + proposal: { + ...proposal, + ballots: [ + { + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsDate[1].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsDate[2].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + ], + }, + neurons: eligibleNeuronsDate, }); expect(votable.length).toEqual(3); }); it("should not have voted neurons because votable", () => { const voted = votedNeurons({ - proposal, + proposal: { + ...proposal, + ballots: [{ + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }], + }, neurons: [ { ...eligibleNeuronsDate[0], - recentBallots: [ - { - proposalId: BigInt(4), - vote: Vote.No, - }, - ], }, ], }); @@ -177,11 +200,17 @@ describe("neurons-utils", () => { it("should not have voted neurons because never voted", () => { const voted = votedNeurons({ - proposal, + proposal: { + ...proposal, + ballots: [{ + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }], + }, neurons: [ { ...eligibleNeuronsDate[0], - recentBallots: [], }, ], }); From fd8c2b538701bb02c06d2a0602ce2a847a7d908e Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:24:18 +0000 Subject: [PATCH 06/13] Updating formatting --- packages/nns/src/utils/neurons.utils.spec.ts | 48 ++++++++++++-------- packages/nns/src/utils/neurons.utils.ts | 2 +- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.spec.ts b/packages/nns/src/utils/neurons.utils.spec.ts index 343720143..885e6e77a 100644 --- a/packages/nns/src/utils/neurons.utils.spec.ts +++ b/packages/nns/src/utils/neurons.utils.spec.ts @@ -118,11 +118,13 @@ describe("neurons-utils", () => { const votable = votableNeurons({ proposal: { ...proposal, - ballots: [{ - neuronId: eligibleNeuronsDate[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }], + ballots: [ + { + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + ], }, neurons: [ { @@ -137,11 +139,13 @@ describe("neurons-utils", () => { const votable = votableNeurons({ proposal: { ...proposal, - ballots: [{ - neuronId: eligibleNeuronsDate[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }], + ballots: [ + { + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + ], }, neurons: [ { @@ -183,11 +187,13 @@ describe("neurons-utils", () => { const voted = votedNeurons({ proposal: { ...proposal, - ballots: [{ - neuronId: eligibleNeuronsDate[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }], + ballots: [ + { + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + ], }, neurons: [ { @@ -202,11 +208,13 @@ describe("neurons-utils", () => { const voted = votedNeurons({ proposal: { ...proposal, - ballots: [{ - neuronId: eligibleNeuronsDate[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }], + ballots: [ + { + neuronId: eligibleNeuronsDate[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + ], }, neurons: [ { diff --git a/packages/nns/src/utils/neurons.utils.ts b/packages/nns/src/utils/neurons.utils.ts index d449b08e0..da5603817 100644 --- a/packages/nns/src/utils/neurons.utils.ts +++ b/packages/nns/src/utils/neurons.utils.ts @@ -1,4 +1,4 @@ -import { Vote } from "../enums/governance.enums"; +import { Vote } from "../enums/governance.enums"; import type { NeuronId } from "../types/common"; import type { Ballot, From 2d79551fdb1bd7c6f3678b6daa34f62885995654 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Tue, 1 Oct 2024 17:36:59 +0200 Subject: [PATCH 07/13] Update test --- packages/nns/src/utils/neurons.utils.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.spec.ts b/packages/nns/src/utils/neurons.utils.spec.ts index 343720143..f29ae4f54 100644 --- a/packages/nns/src/utils/neurons.utils.spec.ts +++ b/packages/nns/src/utils/neurons.utils.spec.ts @@ -160,7 +160,7 @@ describe("neurons-utils", () => { { neuronId: eligibleNeuronsDate[0].neuronId, vote: Vote.Unspecified, - votingPower: BigInt(1), + votingPower: BigInt(0), }, { neuronId: eligibleNeuronsDate[1].neuronId, @@ -170,7 +170,7 @@ describe("neurons-utils", () => { { neuronId: eligibleNeuronsDate[2].neuronId, vote: Vote.Unspecified, - votingPower: BigInt(1), + votingPower: BigInt(0), }, ], }, From 48aab7ce4199f828f61aeec6af9e82b8051dd17f Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Tue, 1 Oct 2024 20:10:41 +0200 Subject: [PATCH 08/13] Update tests --- packages/nns/src/utils/neurons.utils.spec.ts | 49 +++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.spec.ts b/packages/nns/src/utils/neurons.utils.spec.ts index 5538527ca..3fd400f2a 100644 --- a/packages/nns/src/utils/neurons.utils.spec.ts +++ b/packages/nns/src/utils/neurons.utils.spec.ts @@ -40,7 +40,7 @@ describe("neurons-utils", () => { } as unknown as NeuronInfo, ]; - const eligibleNeuronsDate: NeuronInfo[] = [ + const eligibleNeuronsData: NeuronInfo[] = [ { createdTimestampSeconds: proposalTimestampSeconds - BigInt(1), neuronId: proposalNeuronId, @@ -101,7 +101,7 @@ describe("neurons-utils", () => { proposal, neurons: [ { - ...eligibleNeuronsDate[0], + ...eligibleNeuronsData[0], recentBallots: [ { proposalId, @@ -120,15 +120,25 @@ describe("neurons-utils", () => { ...proposal, ballots: [ { - neuronId: eligibleNeuronsDate[0].neuronId, + neuronId: eligibleNeuronsData[0].neuronId, vote: Vote.Unspecified, votingPower: BigInt(1), }, + { + neuronId: eligibleNeuronsData[0].neuronId, + vote: Vote.Yes, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsData[0].neuronId, + vote: Vote.No, + votingPower: BigInt(1), + }, ], }, neurons: [ { - ...eligibleNeuronsDate[0], + ...eligibleNeuronsData[0], }, ], }); @@ -141,15 +151,20 @@ describe("neurons-utils", () => { ...proposal, ballots: [ { - neuronId: eligibleNeuronsDate[0].neuronId, + neuronId: eligibleNeuronsData[0].neuronId, vote: Vote.Unspecified, votingPower: BigInt(1), }, + { + neuronId: eligibleNeuronsData[1].neuronId, + vote: Vote.No, + votingPower: BigInt(1), + }, ], }, neurons: [ { - ...eligibleNeuronsDate[0], + ...eligibleNeuronsData[0], }, ], }); @@ -162,25 +177,25 @@ describe("neurons-utils", () => { ...proposal, ballots: [ { - neuronId: eligibleNeuronsDate[0].neuronId, + neuronId: eligibleNeuronsData[0].neuronId, vote: Vote.Unspecified, votingPower: BigInt(0), }, { - neuronId: eligibleNeuronsDate[1].neuronId, - vote: Vote.Unspecified, + neuronId: eligibleNeuronsData[1].neuronId, + vote: Vote.Yes, votingPower: BigInt(1), }, { - neuronId: eligibleNeuronsDate[2].neuronId, + neuronId: eligibleNeuronsData[2].neuronId, vote: Vote.Unspecified, votingPower: BigInt(0), }, ], }, - neurons: eligibleNeuronsDate, + neurons: eligibleNeuronsData, }); - expect(votable.length).toEqual(3); + expect(votable.length).toEqual(2); }); it("should not have voted neurons because votable", () => { @@ -189,7 +204,7 @@ describe("neurons-utils", () => { ...proposal, ballots: [ { - neuronId: eligibleNeuronsDate[0].neuronId, + neuronId: eligibleNeuronsData[0].neuronId, vote: Vote.Unspecified, votingPower: BigInt(1), }, @@ -197,7 +212,7 @@ describe("neurons-utils", () => { }, neurons: [ { - ...eligibleNeuronsDate[0], + ...eligibleNeuronsData[0], }, ], }); @@ -210,7 +225,7 @@ describe("neurons-utils", () => { ...proposal, ballots: [ { - neuronId: eligibleNeuronsDate[0].neuronId, + neuronId: eligibleNeuronsData[0].neuronId, vote: Vote.Unspecified, votingPower: BigInt(1), }, @@ -218,7 +233,7 @@ describe("neurons-utils", () => { }, neurons: [ { - ...eligibleNeuronsDate[0], + ...eligibleNeuronsData[0], }, ], }); @@ -230,7 +245,7 @@ describe("neurons-utils", () => { proposal, neurons: [ { - ...eligibleNeuronsDate[0], + ...eligibleNeuronsData[0], recentBallots: [ { proposalId, From c46bd7645810a783dfc363d5178fc26a89af4c87 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Tue, 1 Oct 2024 20:12:40 +0200 Subject: [PATCH 09/13] refactor: voting functions --- packages/nns/src/utils/neurons.utils.ts | 39 +++++++++---------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.ts b/packages/nns/src/utils/neurons.utils.ts index da5603817..9bd94ad64 100644 --- a/packages/nns/src/utils/neurons.utils.ts +++ b/packages/nns/src/utils/neurons.utils.ts @@ -6,22 +6,15 @@ import type { ProposalInfo, } from "../types/governance_converters"; -const voteForProposal = ({ - ballots, - neuronId, +const getNeuronVoteForProposal = ({ + proposal: { ballots }, + neuron: {neuronId}, }: { - ballots: Ballot[]; - neuronId: NeuronId | undefined; -}): Vote | undefined => { - if (!neuronId) { - return undefined; - } - - const ballot: Ballot | undefined = ballots.find( + proposal: ProposalInfo; + neuron: NeuronInfo; +}): Vote | undefined => ballots.find( ({ neuronId: id }) => id === neuronId, - ); - return ballot?.vote; -}; + )?.vote; /** * Filter the neurons that are ineligible to vote to a proposal. @@ -70,18 +63,14 @@ export const votableNeurons = ({ }: { neurons: NeuronInfo[]; proposal: ProposalInfo; -}): NeuronInfo[] => { - const { ballots } = proposal; - - return neurons.filter( - ({ neuronId }: NeuronInfo) => - voteForProposal({ ballots, neuronId }) === Vote.Unspecified && +}): NeuronInfo[] => neurons.filter( + (neuron: NeuronInfo) => + getNeuronVoteForProposal({ proposal, neuron }) === Vote.Unspecified && ineligibleNeurons({ neurons, proposal }).find( ({ neuronId: ineligibleNeuronId }: NeuronInfo) => - ineligibleNeuronId === neuronId, + ineligibleNeuronId === neuron.neuronId, ) === undefined, ); -}; /** * Filter the neurons that have voted for a proposal. @@ -92,12 +81,12 @@ export const votableNeurons = ({ */ export const votedNeurons = ({ neurons, - proposal: { ballots }, + proposal, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }): NeuronInfo[] => neurons.filter( - ({ neuronId }: NeuronInfo) => - voteForProposal({ ballots, neuronId }) !== Vote.Unspecified, + (neuron: NeuronInfo) => + getNeuronVoteForProposal({ proposal, neuron }) !== Vote.Unspecified, ); From 110f8df4580d362c3998104eac5419b17c1f1673 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:13:32 +0000 Subject: [PATCH 10/13] Updating formatting --- packages/nns/src/utils/neurons.utils.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.ts b/packages/nns/src/utils/neurons.utils.ts index 9bd94ad64..0530afd78 100644 --- a/packages/nns/src/utils/neurons.utils.ts +++ b/packages/nns/src/utils/neurons.utils.ts @@ -1,5 +1,4 @@ import { Vote } from "../enums/governance.enums"; -import type { NeuronId } from "../types/common"; import type { Ballot, NeuronInfo, @@ -8,13 +7,12 @@ import type { const getNeuronVoteForProposal = ({ proposal: { ballots }, - neuron: {neuronId}, + neuron: { neuronId }, }: { proposal: ProposalInfo; neuron: NeuronInfo; -}): Vote | undefined => ballots.find( - ({ neuronId: id }) => id === neuronId, - )?.vote; +}): Vote | undefined => + ballots.find(({ neuronId: id }) => id === neuronId)?.vote; /** * Filter the neurons that are ineligible to vote to a proposal. @@ -63,7 +61,8 @@ export const votableNeurons = ({ }: { neurons: NeuronInfo[]; proposal: ProposalInfo; -}): NeuronInfo[] => neurons.filter( +}): NeuronInfo[] => + neurons.filter( (neuron: NeuronInfo) => getNeuronVoteForProposal({ proposal, neuron }) === Vote.Unspecified && ineligibleNeurons({ neurons, proposal }).find( From 10eabefab735813070cb16cfb9ae4c7116f2e2b5 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:14:48 +0000 Subject: [PATCH 11/13] =?UTF-8?q?=F0=9F=A4=96=20Documentation=20auto-updat?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/nns/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/nns/README.md b/packages/nns/README.md index 5469d7230..6f36f471e 100644 --- a/packages/nns/README.md +++ b/packages/nns/README.md @@ -96,7 +96,7 @@ Parameters: - `params.neurons`: The neurons to filter. - `params.proposal`: The proposal to match against the selected neurons. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L38) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L29) #### :gear: votableNeurons @@ -111,22 +111,22 @@ Parameters: - `params.neurons`: The neurons to filter. - `params.proposal`: The proposal to match against the selected neurons. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L67) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L58) #### :gear: votedNeurons Filter the neurons that have voted for a proposal. -| Function | Type | -| -------------- | ----------------------------------------------------------------------------------------------------------- | -| `votedNeurons` | `({ neurons, proposal: { ballots }, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }) => NeuronInfo[]` | +| Function | Type | +| -------------- | ---------------------------------------------------------------------------------------------- | +| `votedNeurons` | `({ neurons, proposal, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }) => NeuronInfo[]` | Parameters: - `params.neurons`: The neurons to filter. - `params.proposal`: The proposal for which some neurons might have already voted. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L93) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L81) ### :factory: GenesisTokenCanister From fe1eb82ba70670364f0ccaa0019e64ea2aaf69d3 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Wed, 2 Oct 2024 08:58:59 +0200 Subject: [PATCH 12/13] Update tests --- packages/nns/src/utils/neurons.utils.spec.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.spec.ts b/packages/nns/src/utils/neurons.utils.spec.ts index 3fd400f2a..a9625f099 100644 --- a/packages/nns/src/utils/neurons.utils.spec.ts +++ b/packages/nns/src/utils/neurons.utils.spec.ts @@ -136,11 +136,7 @@ describe("neurons-utils", () => { }, ], }, - neurons: [ - { - ...eligibleNeuronsData[0], - }, - ], + neurons: eligibleNeuronsData, }); expect(votable.length).toEqual(1); }); @@ -162,11 +158,7 @@ describe("neurons-utils", () => { }, ], }, - neurons: [ - { - ...eligibleNeuronsData[0], - }, - ], + neurons: eligibleNeuronsData, }); expect(votable.length).toEqual(1); }); From 787b90b509d0b9bd12b0bd14f1bdb77ca07f2017 Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Wed, 2 Oct 2024 09:21:41 +0200 Subject: [PATCH 13/13] Cleanup tests - Separate describe blocks - Remove duplications --- packages/nns/src/utils/neurons.utils.spec.ts | 273 ++++++++----------- 1 file changed, 113 insertions(+), 160 deletions(-) diff --git a/packages/nns/src/utils/neurons.utils.spec.ts b/packages/nns/src/utils/neurons.utils.spec.ts index a9625f099..eba6d7bc7 100644 --- a/packages/nns/src/utils/neurons.utils.spec.ts +++ b/packages/nns/src/utils/neurons.utils.spec.ts @@ -61,192 +61,145 @@ describe("neurons-utils", () => { } as unknown as NeuronInfo, ]; - it("should has an ineligible neuron because created after proposal", () => { - const ineligible = ineligibleNeurons({ - proposal, - neurons: ineligibleNeuronsDate, + describe("ineligibleNeurons", () => { + it("should has an ineligible neuron because created after proposal", () => { + const ineligible = ineligibleNeurons({ + proposal, + neurons: ineligibleNeuronsDate, + }); + expect(ineligible.length).toEqual(1); }); - expect(ineligible.length).toEqual(1); - }); - it("should has an ineligible neuron because dissolve too short", () => { - const ineligible = ineligibleNeurons({ - proposal, - neurons: ineligibleNeuronsTooShort, + it("should has an ineligible neuron because dissolve too short", () => { + const ineligible = ineligibleNeurons({ + proposal, + neurons: ineligibleNeuronsTooShort, + }); + expect(ineligible.length).toEqual(1); }); - expect(ineligible.length).toEqual(1); - }); - it("should has not ineligible neuron because empty", () => { - const ineligible = ineligibleNeurons({ proposal, neurons: [] }); - expect(ineligible.length).toEqual(0); - }); - - it("should not have votable neurons because ineligible", () => { - let votable = votableNeurons({ - proposal, - neurons: ineligibleNeuronsDate, + it("should has not ineligible neuron because empty", () => { + const ineligible = ineligibleNeurons({ proposal, neurons: [] }); + expect(ineligible.length).toEqual(0); }); - expect(votable.length).toEqual(0); - - votable = votableNeurons({ - proposal, - neurons: ineligibleNeuronsTooShort, - }); - expect(votable.length).toEqual(0); }); - it("should not have votable neurons because already voted", () => { - const votable = votableNeurons({ - proposal, - neurons: [ - { - ...eligibleNeuronsData[0], - recentBallots: [ - { - proposalId, - vote: Vote.No, - }, - ], - }, - ], + describe("votableNeurons", () => { + it("should not have votable neurons because ineligible", () => { + let votable = votableNeurons({ + proposal, + neurons: ineligibleNeuronsDate, + }); + expect(votable.length).toEqual(0); + + votable = votableNeurons({ + proposal, + neurons: ineligibleNeuronsTooShort, + }); + expect(votable.length).toEqual(0); }); - expect(votable.length).toEqual(0); - }); - it("should have votable neurons because not yet voted", () => { - const votable = votableNeurons({ - proposal: { - ...proposal, - ballots: [ - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }, - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.Yes, - votingPower: BigInt(1), - }, - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.No, - votingPower: BigInt(1), - }, - ], - }, - neurons: eligibleNeuronsData, - }); - expect(votable.length).toEqual(1); - }); - - it("should have votable neurons because never voted", () => { - const votable = votableNeurons({ - proposal: { - ...proposal, - ballots: [ - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }, - { - neuronId: eligibleNeuronsData[1].neuronId, - vote: Vote.No, - votingPower: BigInt(1), - }, - ], - }, - neurons: eligibleNeuronsData, - }); - expect(votable.length).toEqual(1); - }); - - it("should have votable neurons regardless of voting power", () => { - const votable = votableNeurons({ - proposal: { - ...proposal, - ballots: [ - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(0), - }, - { - neuronId: eligibleNeuronsData[1].neuronId, - vote: Vote.Yes, - votingPower: BigInt(1), - }, + it("should not have votable neurons because already voted", () => { + const votable = votableNeurons({ + proposal, + neurons: [ { - neuronId: eligibleNeuronsData[2].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(0), + ...eligibleNeuronsData[0], + recentBallots: [ + { + proposalId, + vote: Vote.No, + }, + ], }, ], - }, - neurons: eligibleNeuronsData, + }); + expect(votable.length).toEqual(0); }); - expect(votable.length).toEqual(2); - }); - it("should not have voted neurons because votable", () => { - const voted = votedNeurons({ - proposal: { - ...proposal, - ballots: [ - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }, - ], - }, - neurons: [ - { - ...eligibleNeuronsData[0], + it("should have votable neurons because not yet voted", () => { + const votable = votableNeurons({ + proposal: { + ...proposal, + ballots: [ + { + neuronId: eligibleNeuronsData[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsData[1].neuronId, + vote: Vote.Yes, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsData[2].neuronId, + vote: Vote.No, + votingPower: BigInt(1), + }, + ], }, - ], + neurons: eligibleNeuronsData, + }); + expect(votable.length).toEqual(1); + expect(votable).toEqual([eligibleNeuronsData[0]]); }); - expect(voted.length).toEqual(0); - }); - it("should not have voted neurons because never voted", () => { - const voted = votedNeurons({ - proposal: { - ...proposal, - ballots: [ - { - neuronId: eligibleNeuronsData[0].neuronId, - vote: Vote.Unspecified, - votingPower: BigInt(1), - }, - ], - }, - neurons: [ - { - ...eligibleNeuronsData[0], + it("should have votable neurons regardless of voting power", () => { + const votable = votableNeurons({ + proposal: { + ...proposal, + ballots: [ + { + neuronId: eligibleNeuronsData[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(0), + }, + { + neuronId: eligibleNeuronsData[1].neuronId, + vote: Vote.Yes, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsData[2].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(2), + }, + ], }, - ], + neurons: eligibleNeuronsData, + }); + expect(votable.length).toEqual(2); + expect(votable).toEqual([eligibleNeuronsData[0], eligibleNeuronsData[2]]); }); - expect(voted.length).toEqual(0); }); - it("should have voted neurons because has voted", () => { - const voted = votedNeurons({ - proposal, - neurons: [ - { - ...eligibleNeuronsData[0], - recentBallots: [ + describe("votedNeurons", () => { + it("should have only voted neurons", () => { + const voted = votedNeurons({ + proposal: { + ...proposal, + ballots: [ + { + neuronId: eligibleNeuronsData[0].neuronId, + vote: Vote.Unspecified, + votingPower: BigInt(1), + }, + { + neuronId: eligibleNeuronsData[1].neuronId, + vote: Vote.Yes, + votingPower: BigInt(1), + }, { - proposalId, + neuronId: eligibleNeuronsData[2].neuronId, vote: Vote.No, + votingPower: BigInt(1), }, ], }, - ], + neurons: eligibleNeuronsData, + }); + expect(voted).toEqual([eligibleNeuronsData[1], eligibleNeuronsData[2]]); }); - expect(voted.length).toEqual(1); }); });