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

Commit

Permalink
✅ Add test when block asset is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Oct 22, 2021
1 parent 6bba550 commit b369b74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion framework/src/modules/random/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ export class RandomAPI extends BaseAPI {
EMPTY_KEY,
seedRevealSchema,
);
const asset = blockAssets.getAsset(this.moduleID);
if (!asset) {
throw new Error('Block asset is missing.');
}

const { seedReveal } = codec.decode<BlockHeaderAssetRandomModule>(
blockHeaderAssetRandomModule,
blockAssets?.getAsset(this.moduleID) ?? Buffer.alloc(0),
asset,
);

return getSeedRevealValidity(generatorAddress, seedReveal, validatorReveals);
Expand Down
15 changes: 15 additions & 0 deletions framework/test/unit/modules/random/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ describe('RandomModuleAPI', () => {
);
});

it('should throw error when asset is undefined', async () => {
// Arrange
const address = Buffer.from(genesisDelegates.delegates[0].address, 'hex');

const blockAsset: BlockAsset = {
moduleID: randomAPI['moduleID'],
data: undefined as any,
};

// Act & Assert
await expect(
randomAPI.isSeedRevealValid(context, address, new BlockAssets([blockAsset])),
).rejects.toThrow('Block asset is missing.');
});

it('should return true for a valid seed reveal', async () => {
// Arrange
const address = Buffer.from(genesisDelegates.delegates[0].address, 'hex');
Expand Down

0 comments on commit b369b74

Please sign in to comment.