-
Notifications
You must be signed in to change notification settings - Fork 458
Add API for Random Module - Closes #6779 #6860
Add API for Random Module - Closes #6779 #6860
Conversation
- Add isSeedRevealValid util - randomByte util - bitwiseXOR
- isSeedRevealValid - getRandomBytes
return false; | ||
}; | ||
|
||
export const randomBytesUtil = ( |
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.
maybe getRandomSeed
as name?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
maybe getSeedRevealValidity
or checkSeedRevealValidity
as name?
const initRandomBuffer = Buffer.allocUnsafe(4); | ||
initRandomBuffer.writeInt32BE(height + numberOfSeeds, 0); |
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.
what do you think about using intToBuffer
from lisk-cryptography
here with signed
parameter true?
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.
true I forgot about this func in crypto lib, but I think we need unsigned
in this case
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.
there is write**U**Int32BE
for unsigned, since you didn't use it I thought you don't need unsigned.
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]; |
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.
What do you think about this? (maybe with better naming)
const isBufferLengthSame = bufferArray.every((elem, _, arr) => elem.length === arr[0].length);
if (!isBufferLengthSame) {
throw new Error('All input for XOR should be same size');
}
const outputSize = bufferArray[0].length;
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 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 seed = genesisDelegates.delegates[4].hashOnion.hashes[0]; | ||
const hashes = cryptography.hashOnion( | ||
Buffer.from(seed, 'hex'), | ||
genesisDelegates.delegates[0].hashOnion.distance, |
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.
Shouldn't we correspond the same index even if they are same value? genesisDelegates.delegates[4].hashOnion.distance
const initRandomBuffer = Buffer.allocUnsafe(4); | ||
initRandomBuffer.writeInt32BE(height + numberOfSeeds, 0); |
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.
same intToBuffer
comment for here
And also I think you should test failure edge cases of util functions but maybe you are saving them for another issue? |
- Rename util isSeedRevealValidUtil->getSeedRevealValidity - Rename util randomBytesUtil->getRandomSeed - Use intToBuffer - Use same delegate fixture for seed reveal validity
…lisk-sdk into 6779-add-api-rand-module * '6779-add-api-rand-module' of https://github.com/LiskHQ/lisk-sdk: 🔥 Remove constant RANDOM_SEED_BYTE_SIZE
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.
Don't we have protocol spec for random seed?
framework/src/modules/random/api.ts
Outdated
public async isSeedRevealValid( | ||
apiContext: ImmutableAPIContext, | ||
generatorAddress: Buffer, | ||
seedReveal: Buffer, |
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
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 comment
The 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 let
can solve the problem?
- Improve getSeedRevealValidity func
8419e1f
to
6bba550
Compare
d05cf36
to
b369b74
Compare
Update test to use protocol-specs in Issue #6780 |
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.
Great job Ishan 🎉
What was the problem?
This PR resolves #6779
How was it solved?
🌱 Add random module utils cd1b814
🌱 Add randomModule API 51d3bcb
✅ Add unit test for randomModule API c734169
How was it tested?
Run
npm run test:unit random/api.spec
underframework