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

Add API for Random Module - Closes #6779 #6860

Merged

Conversation

ishantiw
Copy link
Contributor

What was the problem?

This PR resolves #6779

How was it solved?

🌱 Add random module utils cd1b814

  • Add isSeedRevealValid util
  • randomByte util
  • bitwiseXOR

🌱 Add randomModule API 51d3bcb

  • isSeedRevealValid
  • getRandomBytes

✅ Add unit test for randomModule API c734169

How was it tested?

Run npm run test:unit random/api.spec under framework

- Add isSeedRevealValid util
- randomByte util
- bitwiseXOR
- isSeedRevealValid
- getRandomBytes
@ishantiw ishantiw requested a review from shuse2 October 22, 2021 00:38
@ishantiw ishantiw self-assigned this Oct 22, 2021
@ishantiw ishantiw requested a review from mehmetegemen October 22, 2021 07:47
return false;
};

export const randomBytesUtil = (
Copy link
Contributor

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 = (
Copy link
Contributor

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?

Comment on lines 51 to 52
const initRandomBuffer = Buffer.allocUnsafe(4);
initRandomBuffer.writeInt32BE(height + numberOfSeeds, 0);
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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.

Comment on lines +71 to +75
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];
Copy link
Contributor

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;

Copy link
Contributor Author

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,
Copy link
Contributor

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

Comment on lines 174 to 175
const initRandomBuffer = Buffer.allocUnsafe(4);
initRandomBuffer.writeInt32BE(height + numberOfSeeds, 0);
Copy link
Contributor

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

@mehmetegemen
Copy link
Contributor

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
Copy link
Collaborator

@shuse2 shuse2 left a 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?

public async isSeedRevealValid(
apiContext: ImmutableAPIContext,
generatorAddress: Buffer,
seedReveal: Buffer,
Copy link
Collaborator

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

Comment on lines 25 to 35
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),
);
Copy link
Collaborator

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
@ishantiw ishantiw force-pushed the 6779-add-api-rand-module branch from 8419e1f to 6bba550 Compare October 22, 2021 16:08
framework/src/modules/random/api.ts Outdated Show resolved Hide resolved
@ishantiw ishantiw force-pushed the 6779-add-api-rand-module branch from d05cf36 to b369b74 Compare October 22, 2021 16:24
@ishantiw
Copy link
Contributor Author

Update test to use protocol-specs in Issue #6780

Copy link
Contributor

@mehmetegemen mehmetegemen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job Ishan 🎉

@shuse2 shuse2 merged commit 6b16cc9 into feature/6554-improve-framework-architecture Oct 25, 2021
@shuse2 shuse2 deleted the 6779-add-api-rand-module branch October 25, 2021 11:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants