Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Wrong selection for top 101 delegates as active delegates - Closes #5123 #5126

Merged
merged 5 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions elements/lisk-dpos/src/delegates_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ const _pickStandByDelegate = (
): number => {
const seedNumber = randomSeed.readBigUInt64BE();
const totalVoteWeight = _getTotalVoteWeight(delegateWeights);

// tslint:disable-next-line no-let
let threshold = seedNumber % totalVoteWeight;
// tslint:disable-next-line no-let
Expand Down Expand Up @@ -378,22 +379,26 @@ export class DelegatesList {
throw new Error(`Corresponding vote weight for round ${round} not found`);
}
// Expect that voteWeight is stored in order of voteWeight and address
const hasStandbySlot =
voteWeight.delegates.length >
this.activeDelegates + this.standbyDelegates;
const activeDelegateSlots = hasStandbySlot
? this.activeDelegates
: this.activeDelegates + this.standbyDelegates;
const hasStandbySlot = voteWeight.delegates.length > this.activeDelegates;
const activeDelegateAddresses = voteWeight.delegates
.slice(0, activeDelegateSlots)
.slice(0, this.activeDelegates)
.map(vw => vw.address);
const standbyDelegateAddresses = [];
const standbyDelegateVoteWeights = hasStandbySlot
? voteWeight.delegates.slice(activeDelegateSlots)
? voteWeight.delegates.slice(this.activeDelegates)
: [];

// Only choose standby delegate if it exists
if (standbyDelegateVoteWeights.length !== 0) {
// If standby delegates are less or equal to what required
// Then don't choose based on random seed and consider those as standby
if (
standbyDelegateVoteWeights.length > 0 &&
standbyDelegateVoteWeights.length <= this.standbyDelegates
) {
for (const delegate of standbyDelegateVoteWeights) {
standbyDelegateAddresses.push(delegate.address);
}
// If standby delegates are more than what required then choose based on random seed
} else if (standbyDelegateVoteWeights.length > this.standbyDelegates) {
// tslint:disable-next-line no-let
for (let i = 0; i < this.standbyDelegates; i += 1) {
const standbyDelegateIndex = _pickStandByDelegate(
Expand Down
11 changes: 9 additions & 2 deletions elements/lisk-dpos/test/unit/vote_weight_snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,15 +774,22 @@ describe('Vote weight snapshot', () => {
const mockedForgersList = JSON.stringify([
{
round: 10,
delegates: [...forgers.map(d => d.address).slice(0, 102)],
delegates: [...forgers.map(d => d.address).slice(0, 100)],
standby: [...forgers.map(d => d.address).slice(101, 102)],
},
]);

const mockedVoteWeights = JSON.stringify([
{
round: 11,
delegates: [
...delegates.map(d => ({
...delegates.slice(0, 100).map(d => ({
address: d.address,
voteWeight: d.totalVotesReceived.toString(),
})),
],
standby: [
...delegates.slice(101, 102).map(d => ({
address: d.address,
voteWeight: d.totalVotesReceived.toString(),
})),
Expand Down