chore: remove dead sorting logic (#2016)
* chore: remove dead sorting logic * style: lint * style: lint
This commit is contained in:
parent
d5045b8ec4
commit
cdff4886b2
@ -33,10 +33,7 @@ import type {
|
|||||||
InMemoryCacheConfig,
|
InMemoryCacheConfig,
|
||||||
Reference,
|
Reference,
|
||||||
} from '@apollo/client';
|
} from '@apollo/client';
|
||||||
import sortBy from 'lodash/sortBy';
|
|
||||||
import uniqBy from 'lodash/uniqBy';
|
|
||||||
|
|
||||||
import { deterministicShuffle } from './lib/deterministic-shuffle';
|
|
||||||
import { addDecimal } from '@vegaprotocol/react-helpers';
|
import { addDecimal } from '@vegaprotocol/react-helpers';
|
||||||
|
|
||||||
const formatUintToNumber = (amount: string, decimals = 18) =>
|
const formatUintToNumber = (amount: string, decimals = 18) =>
|
||||||
@ -51,35 +48,8 @@ const createReadField = (fieldName: string) => ({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create seed in memory. Validator list order will remain the same
|
|
||||||
// until the page is refreshed.
|
|
||||||
const VALIDATOR_RANDOMISER_SEED = (
|
|
||||||
Math.floor(Math.random() * 1000) + 1
|
|
||||||
).toString();
|
|
||||||
|
|
||||||
const cache: InMemoryCacheConfig = {
|
const cache: InMemoryCacheConfig = {
|
||||||
typePolicies: {
|
typePolicies: {
|
||||||
Query: {
|
|
||||||
fields: {
|
|
||||||
nodes: {
|
|
||||||
// Merge function to make the validator list random but remain consistent
|
|
||||||
// as the user navigates around the site. If the user refreshes the list
|
|
||||||
// will be randomised.
|
|
||||||
merge: (existing = [], incoming) => {
|
|
||||||
// uniqBy will take the first of any matches
|
|
||||||
const uniq = uniqBy([...incoming, ...existing], 'id');
|
|
||||||
// sort result so that the input is consistent
|
|
||||||
const sorted = sortBy(uniq, 'id');
|
|
||||||
// randomise based on seed string
|
|
||||||
const random = deterministicShuffle(
|
|
||||||
VALIDATOR_RANDOMISER_SEED,
|
|
||||||
sorted
|
|
||||||
);
|
|
||||||
return random;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Account: {
|
Account: {
|
||||||
keyFields: false,
|
keyFields: false,
|
||||||
fields: {
|
fields: {
|
||||||
|
@ -1,111 +0,0 @@
|
|||||||
import {
|
|
||||||
stringTo32BitHash,
|
|
||||||
createRandomGenerator,
|
|
||||||
deterministicShuffle,
|
|
||||||
} from './deterministic-shuffle';
|
|
||||||
|
|
||||||
it('Converts a string to a hash as expected', () => {
|
|
||||||
expect(stringTo32BitHash('test')).toEqual(1706);
|
|
||||||
expect(stringTo32BitHash('0x0ddba11')).toEqual(31040);
|
|
||||||
expect(stringTo32BitHash('Rhosllannerchrugog')).toEqual(27853302);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Random generator is deterministic by seed: matching output', () => {
|
|
||||||
const genSeedOne = createRandomGenerator(1);
|
|
||||||
const anotherGenSeedOne = createRandomGenerator(1);
|
|
||||||
|
|
||||||
expect(genSeedOne()).toEqual(anotherGenSeedOne());
|
|
||||||
expect(genSeedOne()).toEqual(anotherGenSeedOne());
|
|
||||||
expect(genSeedOne()).toEqual(anotherGenSeedOne());
|
|
||||||
|
|
||||||
// Throw a result away so they are out of step
|
|
||||||
genSeedOne();
|
|
||||||
|
|
||||||
expect(genSeedOne()).not.toEqual(anotherGenSeedOne());
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Random generator is deterministic by seed: non-matching output', () => {
|
|
||||||
const genSeedOne = createRandomGenerator(1);
|
|
||||||
const genSeedTwo = createRandomGenerator(2);
|
|
||||||
|
|
||||||
expect(genSeedOne()).not.toEqual(genSeedTwo());
|
|
||||||
expect(genSeedOne()).not.toEqual(genSeedTwo());
|
|
||||||
expect(genSeedOne()).not.toEqual(genSeedTwo());
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Random generator is deterministic by seed: switching seed overrides original seed and produces deterministic output', () => {
|
|
||||||
const genSeedOne = createRandomGenerator(1);
|
|
||||||
const genSeedTwo = createRandomGenerator(2);
|
|
||||||
|
|
||||||
const firstTwoSeed = genSeedTwo();
|
|
||||||
expect(genSeedOne()).not.toEqual(firstTwoSeed);
|
|
||||||
|
|
||||||
const secondTwoSeed = genSeedTwo();
|
|
||||||
expect(genSeedOne()).not.toEqual(secondTwoSeed);
|
|
||||||
|
|
||||||
expect(genSeedOne(2)).toEqual(firstTwoSeed);
|
|
||||||
expect(genSeedOne()).toEqual(secondTwoSeed);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('deterministicShuffle shuffles deterministically: strings', () => {
|
|
||||||
const defaultInputStrings = ['one', 'two', 'three', 'four', 'five'];
|
|
||||||
const testSeedOne = deterministicShuffle('test', defaultInputStrings);
|
|
||||||
const testSeedTwo = deterministicShuffle('test', defaultInputStrings);
|
|
||||||
const testSeedThree = deterministicShuffle('test', defaultInputStrings);
|
|
||||||
|
|
||||||
expect(testSeedOne).toEqual(['three', 'four', 'one', 'two', 'five']);
|
|
||||||
expect(testSeedTwo).not.toEqual(testSeedOne);
|
|
||||||
expect(testSeedThree).not.toEqual(testSeedOne);
|
|
||||||
|
|
||||||
const altSeedOne = deterministicShuffle(
|
|
||||||
'anything-except-test',
|
|
||||||
defaultInputStrings
|
|
||||||
);
|
|
||||||
expect(altSeedOne).not.toEqual(testSeedOne);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('deterministicShuffle shuffles deterministically: numbers', () => {
|
|
||||||
const defaultInputNumbers = [1, 2, 3, 4, 5];
|
|
||||||
const testSeedOne = deterministicShuffle('test', defaultInputNumbers);
|
|
||||||
const testSeedTwo = deterministicShuffle('test', defaultInputNumbers);
|
|
||||||
const testSeedThree = deterministicShuffle('test', defaultInputNumbers);
|
|
||||||
|
|
||||||
expect(testSeedOne).toEqual([3, 4, 1, 2, 5]);
|
|
||||||
expect(testSeedTwo).not.toEqual(testSeedOne);
|
|
||||||
expect(testSeedThree).not.toEqual(testSeedOne);
|
|
||||||
|
|
||||||
const altSeedOne = deterministicShuffle(
|
|
||||||
'anything-except-test',
|
|
||||||
defaultInputNumbers
|
|
||||||
);
|
|
||||||
expect(altSeedOne).not.toEqual(testSeedOne);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('deterministicShuffle shuffles deterministically: objects', () => {
|
|
||||||
const defaultInputObjects = [
|
|
||||||
{ test: 1 },
|
|
||||||
{ test: 2 },
|
|
||||||
{ test: 3 },
|
|
||||||
{ test: 4 },
|
|
||||||
{ test: 5 },
|
|
||||||
];
|
|
||||||
const testSeedOne = deterministicShuffle('test', defaultInputObjects);
|
|
||||||
const testSeedTwo = deterministicShuffle('test', defaultInputObjects);
|
|
||||||
const testSeedThree = deterministicShuffle('test', defaultInputObjects);
|
|
||||||
|
|
||||||
expect(testSeedOne).toEqual([
|
|
||||||
{ test: 3 },
|
|
||||||
{ test: 4 },
|
|
||||||
{ test: 1 },
|
|
||||||
{ test: 2 },
|
|
||||||
{ test: 5 },
|
|
||||||
]);
|
|
||||||
expect(testSeedTwo).not.toEqual(testSeedOne);
|
|
||||||
expect(testSeedThree).not.toEqual(testSeedOne);
|
|
||||||
|
|
||||||
const altSeedOne = deterministicShuffle(
|
|
||||||
'anything-except-test',
|
|
||||||
defaultInputObjects
|
|
||||||
);
|
|
||||||
expect(altSeedOne).not.toEqual(testSeedOne);
|
|
||||||
});
|
|
@ -1,36 +0,0 @@
|
|||||||
// creates a random number generator function.
|
|
||||||
export function createRandomGenerator(seed: number) {
|
|
||||||
const a = 5486230734; // some big numbers
|
|
||||||
const b = 6908969830;
|
|
||||||
const m = 9853205067;
|
|
||||||
let x = seed;
|
|
||||||
// returns a random value 0 <= num < 1
|
|
||||||
return function (seed = x) {
|
|
||||||
// seed is optional. If supplied sets a new seed
|
|
||||||
x = (seed * a + b) % m;
|
|
||||||
return x / m;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// function creates a 32bit hash of a string
|
|
||||||
export function stringTo32BitHash(str: string) {
|
|
||||||
let v = 0;
|
|
||||||
for (let i = 0; i < str.length; i += 1) {
|
|
||||||
v += str.charCodeAt(i) << i % 24;
|
|
||||||
}
|
|
||||||
return v % 0xffffffff;
|
|
||||||
}
|
|
||||||
|
|
||||||
// shuffle array using the str as a key.
|
|
||||||
export function deterministicShuffle(
|
|
||||||
str: string,
|
|
||||||
arr: Array<string | number | object>
|
|
||||||
) {
|
|
||||||
const rArr = [];
|
|
||||||
const random = createRandomGenerator(stringTo32BitHash(str));
|
|
||||||
while (arr.length > 1) {
|
|
||||||
rArr.push(arr.splice(Math.floor(random() * arr.length), 1)[0]);
|
|
||||||
}
|
|
||||||
rArr.push(arr[0]);
|
|
||||||
return rArr;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user