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 2 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
13 changes: 6 additions & 7 deletions elements/lisk-dpos/src/delegates_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,11 @@ const _pickStandByDelegate = (
): number => {
const seedNumber = randomSeed.readBigUInt64BE();
const totalVoteWeight = _getTotalVoteWeight(delegateWeights);

// To pass voteWeight = 0, initialize threshold with negative value
// tslint:disable-next-line no-let
let threshold = seedNumber % totalVoteWeight;
let threshold =
totalVoteWeight === BigInt(0) ? BigInt(-1) : seedNumber % totalVoteWeight;
nazarhussain marked this conversation as resolved.
Show resolved Hide resolved
// tslint:disable-next-line no-let
for (let i = 0; i < delegateWeights.length; i += 1) {
const voteWeight = BigInt(delegateWeights[i].voteWeight);
Expand Down Expand Up @@ -378,12 +381,8 @@ 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 activeDelegateSlots = this.activeDelegates;
nazarhussain marked this conversation as resolved.
Show resolved Hide resolved
const activeDelegateAddresses = voteWeight.delegates
.slice(0, activeDelegateSlots)
.map(vw => vw.address);
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