Skip to content

Commit

Permalink
ramdom list validators
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Jun 6, 2024
1 parent c63a35b commit 87ef363
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/components/ValidatorList/ValidatorTable/ValidatorTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { _ } from "src/lib/scripts";
import { tableThemes } from "src/constants/tableThemes";
import { sortDirections } from "src/constants/sortDirections";
import consts from "src/constants/consts";
import { formatPercentage, formatInteger, formatOrai } from "src/helpers/helper";
import { formatPercentage, formatInteger, formatOrai, groupAndShuffle } from "src/helpers/helper";
import { compareTwoValues } from "src/helpers/compare";
import Delegate from "src/components/common/Delegate";
import ThemedTable from "src/components/common/ThemedTable";
Expand Down Expand Up @@ -214,9 +214,9 @@ const ValidatorTable = memo(({ data = [] }) => {

const sortData = (data, extraSortField = sortFields.RANK) => {
if (!data) return [];

if (isFirstSort) {
return [...data].sort((a, b) => 0.5 - Math.random());
const groupSize = 10;
return groupAndShuffle(data, groupSize).flat();
}

if (canSort) {
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,25 @@ export const calculateTallyProposal = ({ totalVote, bonded, tally }) => {
vote_percentage: votePercentage,
};
};

export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}

export function groupAndShuffle(array, groupSize) {
const sortedArray = array
.map(e => {
return { ...e, votingPowerMixUpTime: e.voting_power * e.uptime };
})
.sort((a, b) => b.votingPowerMixUpTime - a.votingPowerMixUpTime);
const groups = [];
for (let i = 0; i < sortedArray.length; i += groupSize) {
groups.push(sortedArray.slice(i, i + groupSize));
}
const shuffledGroups = groups.map(group => shuffleArray(group));
return shuffledGroups;
}

0 comments on commit 87ef363

Please sign in to comment.