This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
Update validators from DPoS module - Closes #6876 #6878
Merged
shuse2
merged 5 commits into
feature/6554-improve-framework-architecture
from
6876-dpos_update_validator
Nov 8, 2021
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
20b061e
:seedling: Migrate _updateValidators to new DPoS module
shuse2 aff4b44
Merge branch 'feature/6554-improve-framework-architecture' of https:/…
shuse2 0007a94
:recyle: Fix mock on test
shuse2 4ce2b70
Merge branch 'feature/6554-improve-framework-architecture' of https:/…
shuse2 23448b9
:white_check_mark: Update test mock
shuse2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
import { verifyData } from '@liskhq/lisk-cryptography'; | ||
import { hash, verifyData } from '@liskhq/lisk-cryptography'; | ||
import { NotFoundError } from '@liskhq/lisk-chain'; | ||
import { UnlockingObject, VoterData } from './types'; | ||
import { | ||
|
@@ -24,6 +24,7 @@ import { | |
} from './constants'; | ||
import { SubStore } from '../../node/state_machine/types'; | ||
import { voterStoreSchema } from './schemas'; | ||
import { Validator } from '../../node/consensus/types'; | ||
|
||
export const sortUnlocking = (unlocks: UnlockingObject[]): void => { | ||
unlocks.sort((a, b) => { | ||
|
@@ -147,3 +148,96 @@ export const getVoterOrDefault = async (voterStore: SubStore, address: Buffer) = | |
return voterData; | ||
} | ||
}; | ||
|
||
export interface DelegateWeight { | ||
readonly delegateAddress: Buffer; | ||
readonly delegateWeight: bigint; | ||
} | ||
|
||
export const pickStandByDelegate = ( | ||
delegateWeights: ReadonlyArray<DelegateWeight>, | ||
randomSeed: Buffer, | ||
): number => { | ||
const seedNumber = randomSeed.readBigUInt64BE(); | ||
const totalVoteWeight = delegateWeights.reduce( | ||
(prev, current) => prev + BigInt(current.delegateWeight), | ||
BigInt(0), | ||
); | ||
|
||
let threshold = seedNumber % totalVoteWeight; | ||
for (let i = 0; i < delegateWeights.length; i += 1) { | ||
const voteWeight = BigInt(delegateWeights[i].delegateWeight); | ||
if (voteWeight > threshold) { | ||
return i; | ||
} | ||
threshold -= voteWeight; | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
export const shuffleDelegateList = ( | ||
previousRoundSeed1: Buffer, | ||
addresses: ReadonlyArray<Buffer>, | ||
): Buffer[] => { | ||
const delegateList = [...addresses].map(delegate => ({ | ||
address: delegate, | ||
})) as { address: Buffer; roundHash: Buffer }[]; | ||
|
||
for (const delegate of delegateList) { | ||
const seedSource = Buffer.concat([previousRoundSeed1, delegate.address]); | ||
delegate.roundHash = hash(seedSource); | ||
} | ||
|
||
delegateList.sort((delegate1, delegate2) => { | ||
const diff = delegate1.roundHash.compare(delegate2.roundHash); | ||
if (diff !== 0) { | ||
return diff; | ||
} | ||
|
||
return delegate1.address.compare(delegate2.address); | ||
}); | ||
|
||
return delegateList.map(delegate => delegate.address); | ||
}; | ||
|
||
export const selectStandbyDelegates = ( | ||
delegateWeights: DelegateWeight[], | ||
randomSeed1: Buffer, | ||
randomSeed2?: Buffer, | ||
): Buffer[] => { | ||
const numberOfCandidates = 1 + (randomSeed2 !== undefined ? 1 : 0); | ||
// if delegate weights is smaller than number selecting, select all | ||
if (delegateWeights.length <= numberOfCandidates) { | ||
return delegateWeights.map(c => c.delegateAddress); | ||
} | ||
const result: Buffer[] = []; | ||
const index = pickStandByDelegate(delegateWeights, randomSeed1); | ||
const [selected] = delegateWeights.splice(index, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the below is |
||
result.push(selected.delegateAddress); | ||
// if seed2 is missing, return only 1 | ||
if (!randomSeed2) { | ||
return result; | ||
} | ||
const secondIndex = pickStandByDelegate(delegateWeights, randomSeed2); | ||
const [secondStandby] = delegateWeights.splice(secondIndex, 1); | ||
result.push(secondStandby.delegateAddress); | ||
|
||
return result; | ||
}; | ||
|
||
export const validtorsEqual = (v1: Validator[], v2: Validator[]): boolean => { | ||
if (v1.length !== v2.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < v1.length; i += 1) { | ||
if (!v1[i].address.equals(v2[i].address)) { | ||
return false; | ||
} | ||
if (v1[i].bftWeight !== v2[i].bftWeight) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo