-
Notifications
You must be signed in to change notification settings - Fork 458
Add API for Random Module - Closes #6779 #6860
Changes from 5 commits
cd1b814
51d3bcb
c734169
c7a5b5b
7863630
46a3206
57838e4
4ccb5ea
de2a46e
6bba550
b369b74
189a847
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,74 @@ | |
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
import * as cryptography from '@liskhq/lisk-cryptography'; | ||
import { SEED_REVEAL_HASH_SIZE } from './constants'; | ||
import { ValidatorSeedReveal } from './types'; | ||
|
||
export const isSeedRevealValidUtil = ( | ||
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. maybe |
||
generatorAddress: Buffer, | ||
seedReveal: Buffer, | ||
validatorsReveal: ValidatorSeedReveal[], | ||
) => { | ||
const largestSeedHeight = Math.max( | ||
...validatorsReveal | ||
.filter(sr => sr.generatorAddress.equals(generatorAddress)) | ||
.map(s => s.height), | ||
); | ||
|
||
const lastSeed = validatorsReveal.find( | ||
(seedObject: ValidatorSeedReveal) => | ||
seedObject.height === largestSeedHeight && | ||
seedObject.generatorAddress.equals(generatorAddress), | ||
); | ||
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. instead of looping multiple times, i think simple one for loop with outside |
||
|
||
if ( | ||
!lastSeed || | ||
lastSeed.seedReveal.equals(cryptography.hash(seedReveal).slice(0, SEED_REVEAL_HASH_SIZE)) | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export const randomBytesUtil = ( | ||
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. maybe |
||
height: number, | ||
numberOfSeeds: number, | ||
validatorsReveal: ValidatorSeedReveal[], | ||
) => { | ||
const initRandomBuffer = Buffer.allocUnsafe(4); | ||
initRandomBuffer.writeInt32BE(height + numberOfSeeds, 0); | ||
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. what do you think about using 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. true I forgot about this func in crypto lib, but I think we need 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. there is |
||
let randomSeed = cryptography.hash(initRandomBuffer).slice(0, 16); | ||
const currentSeeds = validatorsReveal.filter( | ||
v => height <= v.height && v.height <= height + numberOfSeeds, | ||
); | ||
for (const seedObject of currentSeeds) { | ||
if (seedObject.valid) { | ||
randomSeed = bitwiseXOR([randomSeed, seedObject.seedReveal]); | ||
} | ||
} | ||
|
||
return randomSeed; | ||
}; | ||
|
||
export const bitwiseXOR = (bufferArray: Buffer[]): Buffer => { | ||
if (bufferArray.length === 1) { | ||
return bufferArray[0]; | ||
} | ||
|
||
const bufferSizes = new Set(bufferArray.map(buffer => buffer.length)); | ||
if (bufferSizes.size > 1) { | ||
throw new Error('All input for XOR should be same size'); | ||
} | ||
const outputSize = [...bufferSizes][0]; | ||
Comment on lines
+75
to
+79
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. What do you think about this? (maybe with better naming)
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. I think we can improve this later, I copied this function from other place, it better to resolve it in a separate issue. Also, we can move to common utils if we find more usage of this func |
||
const result = Buffer.alloc(outputSize, 0); | ||
|
||
for (let i = 0; i < outputSize; i += 1) { | ||
// eslint-disable-next-line no-bitwise | ||
result[i] = bufferArray.map(b => b[i]).reduce((a, b) => a ^ b, 0); | ||
} | ||
|
||
return result; | ||
}; |
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.
i think i missed this on LIP as well, but this should be
blockAssets: BlockAssets
, so that dpos module or any other module don't need to know how to decode the asset