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

Commit

Permalink
Merge pull request #5126 from LiskHQ/5123-dpos-standby-delegates
Browse files Browse the repository at this point in the history
Wrong selection for top 101 delegates as active delegates - Closes #5123
  • Loading branch information
shuse2 authored Apr 7, 2020
2 parents b4c4952 + 0c47cf4 commit 9038ab4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
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

0 comments on commit 9038ab4

Please sign in to comment.