From fa934d3cd9a42b0d27302af40571a275def1b458 Mon Sep 17 00:00:00 2001 From: has5aan Date: Mon, 24 Oct 2022 13:44:24 +0200 Subject: [PATCH] :seedling: Adds skipVerify flag to dryRunTransaction endpoint --- framework/src/engine/endpoint/txpool.ts | 22 ++++++----- framework/src/engine/generator/schemas.ts | 5 +++ .../test/unit/engine/endpoint/txpool.spec.ts | 39 ++++++++++++++++++- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/framework/src/engine/endpoint/txpool.ts b/framework/src/engine/endpoint/txpool.ts index befd3a9ab6b..86f1aeba030 100644 --- a/framework/src/engine/endpoint/txpool.ts +++ b/framework/src/engine/endpoint/txpool.ts @@ -115,16 +115,18 @@ export class TxpoolEndpoint { const transaction = Transaction.fromBytes(Buffer.from(req.transaction, 'hex')); const header = this._chain.lastBlock.header.toObject(); - const { result } = await this._abi.verifyTransaction({ - contextID: Buffer.alloc(0), - transaction: transaction.toObject(), - header, - }); - if (result === TransactionVerifyResult.INVALID) { - return { - success: false, - events: [], - }; + if (!req.skipVerify) { + const { result } = await this._abi.verifyTransaction({ + contextID: Buffer.alloc(0), + transaction: transaction.toObject(), + header, + }); + if (result === TransactionVerifyResult.INVALID) { + return { + success: false, + events: [], + }; + } } const stateStore = new StateStore(this._blockchainDB); diff --git a/framework/src/engine/generator/schemas.ts b/framework/src/engine/generator/schemas.ts index 1b7f11e9571..eeeca65c753 100644 --- a/framework/src/engine/generator/schemas.ts +++ b/framework/src/engine/generator/schemas.ts @@ -252,6 +252,7 @@ export const previouslyGeneratedInfoSchema = { export interface DryRunTransactionRequest { transaction: string; + skipVerify: boolean; } export interface DryRunTransactionResponse { @@ -269,6 +270,10 @@ export const dryRunTransactionRequestSchema = { type: 'string', format: 'hex', }, + skipVerify: { + type: 'boolean', + default: false, + }, }, }; diff --git a/framework/test/unit/engine/endpoint/txpool.spec.ts b/framework/test/unit/engine/endpoint/txpool.spec.ts index 1cde782ccbb..8383b6a5903 100644 --- a/framework/test/unit/engine/endpoint/txpool.spec.ts +++ b/framework/test/unit/engine/endpoint/txpool.spec.ts @@ -212,7 +212,7 @@ describe('generator endpoint', () => { }, chainID, }), - ).rejects.toThrow(LiskValidationError); + ).rejects.toThrow("must have required property 'transaction'"); }); it('should reject with error when transaction bytes is invalid', async () => { @@ -226,6 +226,19 @@ describe('generator endpoint', () => { }), ).rejects.toThrow(); }); + + it('should reject with error when skipVerify is not boolean', async () => { + await expect( + endpoint.dryRunTransaction({ + logger, + params: { + transaction: 'xxxx', + skipVerify: 'test', + }, + chainID, + }), + ).rejects.toThrow("'.skipVerify' should be of type 'boolean'"); + }); }); describe('when verify transaction fails', () => { @@ -290,6 +303,30 @@ describe('generator endpoint', () => { }); }); + it('should not verify transaction when skipVerify', async () => { + (abi.verifyTransaction as jest.Mock).mockResolvedValue({ + result: TransactionVerifyResult.OK, + }); + + (abi.executeTransaction as jest.Mock).mockResolvedValue({ + result: TransactionExecutionResult.OK, + events, + }); + + await expect( + endpoint.dryRunTransaction({ + logger, + params: { + transaction: tx.getBytes().toString('hex'), + skipVerify: true, + }, + chainID, + }), + ).toResolve(); + + expect(abi.verifyTransaction).toBeCalledTimes(0); + }); + describe('when both verification is success & execution returns OK', () => { it('should return true with corresponding events', async () => { (abi.executeTransaction as jest.Mock).mockResolvedValue({