Skip to content

Commit

Permalink
Fix nns neuron sorting (#4804)
Browse files Browse the repository at this point in the history
# Motivation

Fix inconsistency in nns neuron sorting when there are neurons with the
same stake and dissolve delay.

# Changes

- Additionally compare neurons by createdTimestamp.

# Tests

- Tested manually.
- Unit tests extended.

# Todos

- [x] Add entry to changelog (if necessary).
  • Loading branch information
mstrasinskis authored Apr 30, 2024
1 parent 167660e commit abf9d48
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ proposal is successful, the changes it released will be moved from this file to

#### Fixed

* Inconsistency in similar NNS neuron sorting.
* Adjust Metrics block visibility for certain screen widths.

#### Security
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/lib/utils/neuron.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ export const formatMaturity = (value?: bigint): string =>

// Used to sort neurons by:
// * decreasing stake, or when stake is equal by
// * decreasing dissolve delay
// * decreasing dissolve delay, or when dissolve delay is equal by
// * decreasing created timestamp
const compareNeurons = (a: NeuronInfo, b: NeuronInfo): number => {
const stakeA = neuronStake(a);
const stakeB = neuronStake(b);
Expand All @@ -317,6 +318,14 @@ const compareNeurons = (a: NeuronInfo, b: NeuronInfo): number => {
if (dissolveDelayA < dissolveDelayB) {
return 1;
}
const createdTimestampA = a.createdTimestampSeconds;
const createdTimestampB = b.createdTimestampSeconds;
if (createdTimestampA > createdTimestampB) {
return -1;
}
if (createdTimestampA < createdTimestampB) {
return 1;
}
return 0;
};

Expand Down
30 changes: 30 additions & 0 deletions frontend/src/tests/lib/utils/neuron.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,36 @@ describe("neuron-utils", () => {
neuron1,
]);
});

it("should sort neurons by createdTimestamp when stake and dissolve delay are equal", () => {
const neuronA = {
...mockNeuron,
dissolveDelaySeconds: 200_000_000n,
createdTimestampSeconds: 1n,
};
const neuronB = {
...mockNeuron,
dissolveDelaySeconds: 100_000_000n,
createdTimestampSeconds: 3n,
};
const neuronC = {
...mockNeuron,
dissolveDelaySeconds: 100_000_000n,
createdTimestampSeconds: 2n,
};
expect(sortNeuronsByStake([])).toEqual([]);
expect(sortNeuronsByStake([neuronA])).toEqual([neuronA]);
expect(sortNeuronsByStake([neuronB, neuronC, neuronA])).toEqual([
neuronA,
neuronB,
neuronC,
]);
expect(sortNeuronsByStake([neuronA, neuronB, neuronC])).toEqual([
neuronA,
neuronB,
neuronC,
]);
});
});

describe("isNeuronControllable", () => {
Expand Down

0 comments on commit abf9d48

Please sign in to comment.