From b369b74a3bfbeb65c064cf7619fb315ee3d1491a Mon Sep 17 00:00:00 2001 From: Ishan Date: Fri, 22 Oct 2021 18:15:55 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20test=20when=20block=20asset?= =?UTF-8?q?=20is=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/src/modules/random/api.ts | 6 +++++- framework/test/unit/modules/random/api.spec.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/framework/src/modules/random/api.ts b/framework/src/modules/random/api.ts index 93761231d6f..bd1ce1b435e 100644 --- a/framework/src/modules/random/api.ts +++ b/framework/src/modules/random/api.ts @@ -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, - blockAssets?.getAsset(this.moduleID) ?? Buffer.alloc(0), + asset, ); return getSeedRevealValidity(generatorAddress, seedReveal, validatorReveals); diff --git a/framework/test/unit/modules/random/api.spec.ts b/framework/test/unit/modules/random/api.spec.ts index 48b065ba82e..53d5af87601 100644 --- a/framework/test/unit/modules/random/api.spec.ts +++ b/framework/test/unit/modules/random/api.spec.ts @@ -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');