From 606388f7b0060f314e755734f3f18774a275c2ac Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Tue, 26 Jul 2022 13:45:13 +0200 Subject: [PATCH 01/18] :arrow_up: Bump lisk-db to support multiple snapshots --- framework/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/package.json b/framework/package.json index 07e45f40884..abb6a684198 100644 --- a/framework/package.json +++ b/framework/package.json @@ -45,7 +45,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27", + "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", "@liskhq/lisk-p2p": "^0.8.0-alpha.0", "@liskhq/lisk-transaction-pool": "^0.6.0-alpha.0", "@liskhq/lisk-transactions": "^6.0.0-alpha.0", From 5112e8125c5cdc434048fe03fa2156b44d038833 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Tue, 26 Jul 2022 13:51:39 +0200 Subject: [PATCH 02/18] :recycle: Update the event queue --- framework/src/state_machine/event_queue.ts | 9 +++++---- framework/src/state_machine/state_machine.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/framework/src/state_machine/event_queue.ts b/framework/src/state_machine/event_queue.ts index 2dbf2d819a7..91e4ddf0ef8 100644 --- a/framework/src/state_machine/event_queue.ts +++ b/framework/src/state_machine/event_queue.ts @@ -59,18 +59,19 @@ export class EventQueue { }); } - public createSnapshot(): void { + public createSnapshot(): number { this._snapshotIndex = this._events.length; + return this._snapshotIndex; } - public restoreSnapshot(): void { - const newEvents = this._events.splice(this._snapshotIndex); + public restoreSnapshot(snapshotID: number): void { + const newEvents = this._events.splice(snapshotID); const nonRevertableEvents = newEvents .filter(eventData => eventData.noRevert) .map((eventData, i) => ({ event: new Event({ ...eventData.event.toObject(), - index: this._snapshotIndex + i, + index: snapshotID + i, }), noRevert: false, })); diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index d836b680e2d..b480879b798 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -165,7 +165,7 @@ export class StateMachine { } const command = this._getCommand(ctx.transaction.moduleID, ctx.transaction.commandID); // Execute command - ctx.eventQueue.createSnapshot(); + const snapshotID = ctx.eventQueue.createSnapshot(); ctx.stateStore.createSnapshot(); const commandContext = ctx.createCommandExecuteContext(command.schema); try { @@ -177,7 +177,7 @@ export class StateMachine { [ctx.transaction.id], ); } catch (error) { - ctx.eventQueue.restoreSnapshot(); + ctx.eventQueue.restoreSnapshot(snapshotID); ctx.stateStore.restoreSnapshot(); ctx.eventQueue.add( ctx.transaction.moduleID, @@ -194,6 +194,8 @@ export class StateMachine { try { await mod.afterCommandExecute(transactionContext); } catch (error) { + ctx.eventQueue.restoreSnapshot(snapshotID); + ctx.stateStore.restoreSnapshot(); status = TransactionExecutionResult.INVALID; return status; } @@ -204,6 +206,8 @@ export class StateMachine { try { await mod.afterCommandExecute(transactionContext); } catch (error) { + ctx.eventQueue.restoreSnapshot(snapshotID); + ctx.stateStore.restoreSnapshot(); status = TransactionExecutionResult.INVALID; } } From 9b7a57f6f34d8741d8330c91a61928510b54ffc5 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Tue, 26 Jul 2022 13:53:30 +0200 Subject: [PATCH 03/18] :white_check_mark: Update tests --- .../unit/state_machine/event_queue.spec.ts | 8 ++-- .../unit/state_machine/state_machine.spec.ts | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/framework/test/unit/state_machine/event_queue.spec.ts b/framework/test/unit/state_machine/event_queue.spec.ts index 71fb11157aa..1f895462238 100644 --- a/framework/test/unit/state_machine/event_queue.spec.ts +++ b/framework/test/unit/state_machine/event_queue.spec.ts @@ -108,11 +108,11 @@ describe('EventQueue', () => { events.map(e => eventQueue.add(e.moduleID, e.typeID, e.data, e.topics)); expect(eventQueue.getEvents()).toHaveLength(events.length); - eventQueue.createSnapshot(); + const snapshotID = eventQueue.createSnapshot(); eventQueue.add(utils.intToBuffer(3, 4), Buffer.from([0, 0, 0, 1]), utils.getRandomBytes(100), [ utils.getRandomBytes(32), ]); - eventQueue.restoreSnapshot(); + eventQueue.restoreSnapshot(snapshotID); expect(eventQueue.getEvents()).toHaveLength(events.length); eventQueue.getEvents().forEach((e, i) => { @@ -128,7 +128,7 @@ describe('EventQueue', () => { events.map(e => eventQueue.add(e.moduleID, e.typeID, e.data, e.topics)); expect(eventQueue.getEvents()).toHaveLength(events.length); - eventQueue.createSnapshot(); + const snapshotID = eventQueue.createSnapshot(); eventQueue.add( utils.intToBuffer(3, 4), Buffer.from([0, 0, 0, 1]), @@ -150,7 +150,7 @@ describe('EventQueue', () => { [utils.getRandomBytes(32)], false, ); - eventQueue.restoreSnapshot(); + eventQueue.restoreSnapshot(snapshotID); expect(eventQueue.getEvents()).toHaveLength(events.length + 1); const queuedEvents = eventQueue.getEvents(); diff --git a/framework/test/unit/state_machine/state_machine.spec.ts b/framework/test/unit/state_machine/state_machine.spec.ts index c0787b5b3b7..e19a85ea564 100644 --- a/framework/test/unit/state_machine/state_machine.spec.ts +++ b/framework/test/unit/state_machine/state_machine.spec.ts @@ -205,6 +205,49 @@ describe('state_machine', () => { expect(events).toHaveLength(1); expect(events[0].toObject().topics[0]).toEqual(transaction.id); }); + + it('should rollback state if afterCommandExecute fails', () => { + const events = [ + { + moduleID: utils.intToBuffer(3, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], + }, + { + moduleID: utils.intToBuffer(4, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], + }, + { + moduleID: utils.intToBuffer(2, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32)], + }, + ]; + + events.map(e => eventQueue.add(e.moduleID, e.typeID, e.data, e.topics)); + + const snapshotID = eventQueue.createSnapshot(); + + eventQueue.add( + utils.intToBuffer(3, 4), + Buffer.from([0, 0, 0, 1]), + utils.getRandomBytes(100), + [utils.getRandomBytes(32)], + ); + + mod.afterCommandExecute.mockImplementation(() => { + throw new Error('error'); + }); + + eventQueue.restoreSnapshot(snapshotID); + + expect(snapshotID).toBe(events.length); + expect(eventQueue.getEvents()).toHaveLength(events.length); + }); }); describe('verifyAssets', () => { From da91733f19a376360cd2e9f05858dce071ecc59b Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Tue, 26 Jul 2022 23:07:22 +0200 Subject: [PATCH 04/18] :arrow_up: Bump lisk-db --- commander/package.json | 2 +- elements/lisk-chain/package.json | 2 +- elements/lisk-elements/package.json | 2 +- sdk/package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commander/package.json b/commander/package.json index 7c8031c0c4d..d4145835c35 100644 --- a/commander/package.json +++ b/commander/package.json @@ -94,7 +94,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27", + "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", "@liskhq/lisk-passphrase": "^4.0.0-alpha.0", "@liskhq/lisk-transactions": "^6.0.0-alpha.0", "@liskhq/lisk-utils": "^0.3.0-alpha.0", diff --git a/elements/lisk-chain/package.json b/elements/lisk-chain/package.json index 8d9d08ae19a..411b7dac05c 100644 --- a/elements/lisk-chain/package.json +++ b/elements/lisk-chain/package.json @@ -37,7 +37,7 @@ "dependencies": { "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27", + "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", "@liskhq/lisk-tree": "^0.3.0-alpha.0", "@liskhq/lisk-utils": "^0.3.0-alpha.0", "@liskhq/lisk-validator": "^0.7.0-alpha.0", diff --git a/elements/lisk-elements/package.json b/elements/lisk-elements/package.json index 40b4af1af6c..e2e298b2e6c 100644 --- a/elements/lisk-elements/package.json +++ b/elements/lisk-elements/package.json @@ -39,7 +39,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27", + "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", "@liskhq/lisk-p2p": "^0.8.0-alpha.0", "@liskhq/lisk-passphrase": "^4.0.0-alpha.0", "@liskhq/lisk-transaction-pool": "^0.6.0-alpha.0", diff --git a/sdk/package.json b/sdk/package.json index 58c51c5462e..c2a54e7475e 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -33,7 +33,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27", + "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", "@liskhq/lisk-p2p": "^0.8.0-alpha.0", "@liskhq/lisk-passphrase": "^4.0.0-alpha.0", "@liskhq/lisk-transaction-pool": "^0.6.0-alpha.0", From 31ebae6a3367aa6bbd5121b7e2c135089fbbad31 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Tue, 26 Jul 2022 23:11:42 +0200 Subject: [PATCH 05/18] :recycle: Refactor the event queue --- framework/src/state_machine/event_queue.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/framework/src/state_machine/event_queue.ts b/framework/src/state_machine/event_queue.ts index 91e4ddf0ef8..1ca7ca45d9e 100644 --- a/framework/src/state_machine/event_queue.ts +++ b/framework/src/state_machine/event_queue.ts @@ -21,7 +21,6 @@ interface RevertibleEvent { export class EventQueue { private readonly _events: RevertibleEvent[]; - private _snapshotIndex = -1; public constructor() { this._events = []; @@ -60,8 +59,7 @@ export class EventQueue { } public createSnapshot(): number { - this._snapshotIndex = this._events.length; - return this._snapshotIndex; + return this._events.length; } public restoreSnapshot(snapshotID: number): void { @@ -76,7 +74,6 @@ export class EventQueue { noRevert: false, })); this._events.push(...nonRevertableEvents); - this._snapshotIndex = -1; } public getEvents(): Event[] { From deb728db58d47dc4d8b234ac8fd1945df990e9f9 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Wed, 27 Jul 2022 00:12:39 +0200 Subject: [PATCH 06/18] :white_check_mark: Update tests --- .../unit/state_machine/state_machine.spec.ts | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/framework/test/unit/state_machine/state_machine.spec.ts b/framework/test/unit/state_machine/state_machine.spec.ts index e19a85ea564..592b5182f00 100644 --- a/framework/test/unit/state_machine/state_machine.spec.ts +++ b/framework/test/unit/state_machine/state_machine.spec.ts @@ -206,7 +206,7 @@ describe('state_machine', () => { expect(events[0].toObject().topics[0]).toEqual(transaction.id); }); - it('should rollback state if afterCommandExecute fails', () => { + it('should rollback state if afterCommandExecute fails', async () => { const events = [ { moduleID: utils.intToBuffer(3, 4), @@ -227,26 +227,33 @@ describe('state_machine', () => { topics: [utils.getRandomBytes(32)], }, ]; - - events.map(e => eventQueue.add(e.moduleID, e.typeID, e.data, e.topics)); + for (const e of events) { + eventQueue.add(e.moduleID, e.typeID, e.data, e.topics); + } const snapshotID = eventQueue.createSnapshot(); - eventQueue.add( - utils.intToBuffer(3, 4), - Buffer.from([0, 0, 0, 1]), - utils.getRandomBytes(100), - [utils.getRandomBytes(32)], - ); - - mod.afterCommandExecute.mockImplementation(() => { - throw new Error('error'); + const ctx = new TransactionContext({ + eventQueue, + logger, + stateStore, + header, + assets, + networkIdentifier, + transaction, + currentValidators: [], + impliesMaxPrevote: true, + maxHeightCertified: 0, + certificateThreshold: BigInt(0), }); + await stateMachine.executeTransaction(ctx); + expect(systemMod.afterCommandExecute).toHaveBeenCalled(); + expect(mod.afterCommandExecute).toHaveBeenCalled(); - eventQueue.restoreSnapshot(snapshotID); + ctx.eventQueue.restoreSnapshot(snapshotID); expect(snapshotID).toBe(events.length); - expect(eventQueue.getEvents()).toHaveLength(events.length); + expect(ctx.eventQueue.getEvents()).toHaveLength(events.length); }); }); From 2808523b0a61aac38bca5c9dce96b24591724dd8 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Thu, 28 Jul 2022 14:24:45 +0200 Subject: [PATCH 07/18] :arrow_up: Bump lisk-db --- commander/package.json | 2 +- elements/lisk-chain/package.json | 2 +- elements/lisk-elements/package.json | 2 +- framework/package.json | 2 +- sdk/package.json | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/commander/package.json b/commander/package.json index d4145835c35..87873728557 100644 --- a/commander/package.json +++ b/commander/package.json @@ -94,7 +94,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", + "@liskhq/lisk-db": "0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f", "@liskhq/lisk-passphrase": "^4.0.0-alpha.0", "@liskhq/lisk-transactions": "^6.0.0-alpha.0", "@liskhq/lisk-utils": "^0.3.0-alpha.0", diff --git a/elements/lisk-chain/package.json b/elements/lisk-chain/package.json index 411b7dac05c..6f67a3821c4 100644 --- a/elements/lisk-chain/package.json +++ b/elements/lisk-chain/package.json @@ -37,7 +37,7 @@ "dependencies": { "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", + "@liskhq/lisk-db": "0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f", "@liskhq/lisk-tree": "^0.3.0-alpha.0", "@liskhq/lisk-utils": "^0.3.0-alpha.0", "@liskhq/lisk-validator": "^0.7.0-alpha.0", diff --git a/elements/lisk-elements/package.json b/elements/lisk-elements/package.json index e2e298b2e6c..26846ca77d3 100644 --- a/elements/lisk-elements/package.json +++ b/elements/lisk-elements/package.json @@ -39,7 +39,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", + "@liskhq/lisk-db": "0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f", "@liskhq/lisk-p2p": "^0.8.0-alpha.0", "@liskhq/lisk-passphrase": "^4.0.0-alpha.0", "@liskhq/lisk-transaction-pool": "^0.6.0-alpha.0", diff --git a/framework/package.json b/framework/package.json index abb6a684198..2547577a896 100644 --- a/framework/package.json +++ b/framework/package.json @@ -45,7 +45,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", + "@liskhq/lisk-db": "0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f", "@liskhq/lisk-p2p": "^0.8.0-alpha.0", "@liskhq/lisk-transaction-pool": "^0.6.0-alpha.0", "@liskhq/lisk-transactions": "^6.0.0-alpha.0", diff --git a/sdk/package.json b/sdk/package.json index c2a54e7475e..caec3c62618 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -33,7 +33,7 @@ "@liskhq/lisk-chain": "^0.4.0-alpha.0", "@liskhq/lisk-codec": "^0.3.0-alpha.0", "@liskhq/lisk-cryptography": "^4.0.0-alpha.0", - "@liskhq/lisk-db": "^0.3.0-debug.20-5058ddc140c457740c275198deebe158b896f2d7", + "@liskhq/lisk-db": "0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f", "@liskhq/lisk-p2p": "^0.8.0-alpha.0", "@liskhq/lisk-passphrase": "^4.0.0-alpha.0", "@liskhq/lisk-transaction-pool": "^0.6.0-alpha.0", From aeae0abd40a08c8457f1b469b8d552ccd2f47aca Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Thu, 28 Jul 2022 14:27:04 +0200 Subject: [PATCH 08/18] :recycle: Update the snapshot functionality --- .../src/state_machine/prefixed_state_read_writer.ts | 12 ++++++------ framework/src/state_machine/state_machine.ts | 12 ++++-------- framework/src/testing/in_memory_prefixed_state.ts | 4 +++- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/framework/src/state_machine/prefixed_state_read_writer.ts b/framework/src/state_machine/prefixed_state_read_writer.ts index d147edf493e..0c7d655757a 100644 --- a/framework/src/state_machine/prefixed_state_read_writer.ts +++ b/framework/src/state_machine/prefixed_state_read_writer.ts @@ -21,8 +21,8 @@ export interface StateDBReadWriter { set(key: Buffer, value: Buffer): Promise; del(key: Buffer): Promise; range(options?: IterateOptions): Promise<{ key: Buffer; value: Buffer }[]>; - snapshot(): void; - restoreSnapshot(): void; + snapshot(): number; + restoreSnapshot(snapshotID: number): void; } interface KeyValue { @@ -117,12 +117,12 @@ export class PrefixedStateReadWriter { })); } - public createSnapshot(): void { - this._readWriter.snapshot(); + public createSnapshot(): number { + return this._readWriter.snapshot(); } - public restoreSnapshot(): void { - this._readWriter.restoreSnapshot(); + public restoreSnapshot(snapshotID: number): void { + this._readWriter.restoreSnapshot(snapshotID); } private _getKey(key: Buffer): Buffer { diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index b480879b798..1a147711780 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -165,8 +165,8 @@ export class StateMachine { } const command = this._getCommand(ctx.transaction.moduleID, ctx.transaction.commandID); // Execute command - const snapshotID = ctx.eventQueue.createSnapshot(); - ctx.stateStore.createSnapshot(); + const eventQueueSnapshotID = ctx.eventQueue.createSnapshot(); + const stateStoreSnapshotID = ctx.stateStore.createSnapshot(); const commandContext = ctx.createCommandExecuteContext(command.schema); try { await command.execute(commandContext); @@ -177,8 +177,8 @@ export class StateMachine { [ctx.transaction.id], ); } catch (error) { - ctx.eventQueue.restoreSnapshot(snapshotID); - ctx.stateStore.restoreSnapshot(); + ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); + ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); ctx.eventQueue.add( ctx.transaction.moduleID, EVENT_STANDARD_TYPE_ID, @@ -194,8 +194,6 @@ export class StateMachine { try { await mod.afterCommandExecute(transactionContext); } catch (error) { - ctx.eventQueue.restoreSnapshot(snapshotID); - ctx.stateStore.restoreSnapshot(); status = TransactionExecutionResult.INVALID; return status; } @@ -206,8 +204,6 @@ export class StateMachine { try { await mod.afterCommandExecute(transactionContext); } catch (error) { - ctx.eventQueue.restoreSnapshot(snapshotID); - ctx.stateStore.restoreSnapshot(); status = TransactionExecutionResult.INVALID; } } diff --git a/framework/src/testing/in_memory_prefixed_state.ts b/framework/src/testing/in_memory_prefixed_state.ts index d0e5d498bc2..13f740d872c 100644 --- a/framework/src/testing/in_memory_prefixed_state.ts +++ b/framework/src/testing/in_memory_prefixed_state.ts @@ -61,7 +61,9 @@ export class InMemoryPrefixedStateDB { } // eslint-disable-next-line @typescript-eslint/no-empty-function - public snapshot(): void {} + public snapshot(): number { + return 0; + } // eslint-disable-next-line @typescript-eslint/no-empty-function public restoreSnapshot(): void {} From 1419d29b2134e3fee1e2a2911cbd2932a186d40f Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Thu, 28 Jul 2022 15:01:16 +0200 Subject: [PATCH 09/18] :recycle: Update the snapshot functionality --- framework/src/state_machine/state_machine.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index 1a147711780..92c5d048a40 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -143,12 +143,16 @@ export class StateMachine { public async executeTransaction(ctx: TransactionContext): Promise { let status = TransactionExecutionResult.OK; const transactionContext = ctx.createTransactionExecuteContext(); + const eventQueueSnapshotID = ctx.eventQueue.createSnapshot(); + const stateStoreSnapshotID = ctx.stateStore.createSnapshot(); for (const mod of this._systemModules) { if (mod.beforeCommandExecute) { try { await mod.beforeCommandExecute(transactionContext); } catch (error) { status = TransactionExecutionResult.INVALID; + ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); + ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); return status; } } @@ -159,14 +163,14 @@ export class StateMachine { await mod.beforeCommandExecute(transactionContext); } catch (error) { status = TransactionExecutionResult.INVALID; + ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); + ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); return status; } } } const command = this._getCommand(ctx.transaction.moduleID, ctx.transaction.commandID); // Execute command - const eventQueueSnapshotID = ctx.eventQueue.createSnapshot(); - const stateStoreSnapshotID = ctx.stateStore.createSnapshot(); const commandContext = ctx.createCommandExecuteContext(command.schema); try { await command.execute(commandContext); @@ -177,8 +181,6 @@ export class StateMachine { [ctx.transaction.id], ); } catch (error) { - ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); - ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); ctx.eventQueue.add( ctx.transaction.moduleID, EVENT_STANDARD_TYPE_ID, @@ -195,6 +197,8 @@ export class StateMachine { await mod.afterCommandExecute(transactionContext); } catch (error) { status = TransactionExecutionResult.INVALID; + ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); + ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); return status; } } @@ -205,6 +209,8 @@ export class StateMachine { await mod.afterCommandExecute(transactionContext); } catch (error) { status = TransactionExecutionResult.INVALID; + ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); + ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); } } } From 0ef0ee2017eb553a93664fbb507518a948611fb4 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Thu, 28 Jul 2022 16:33:06 +0200 Subject: [PATCH 10/18] :recycle: Update the snapshot functionality --- framework/src/state_machine/state_machine.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index 92c5d048a40..b19964217dd 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -150,9 +150,9 @@ export class StateMachine { try { await mod.beforeCommandExecute(transactionContext); } catch (error) { - status = TransactionExecutionResult.INVALID; ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); + status = TransactionExecutionResult.INVALID; return status; } } @@ -171,6 +171,8 @@ export class StateMachine { } const command = this._getCommand(ctx.transaction.moduleID, ctx.transaction.commandID); // Execute command + const commandEventQueueSnapshotID = ctx.eventQueue.createSnapshot(); + const commandStateStoreSnapshotID = ctx.stateStore.createSnapshot(); const commandContext = ctx.createCommandExecuteContext(command.schema); try { await command.execute(commandContext); @@ -181,6 +183,8 @@ export class StateMachine { [ctx.transaction.id], ); } catch (error) { + ctx.eventQueue.restoreSnapshot(commandEventQueueSnapshotID); + ctx.stateStore.restoreSnapshot(commandStateStoreSnapshotID); ctx.eventQueue.add( ctx.transaction.moduleID, EVENT_STANDARD_TYPE_ID, @@ -196,9 +200,9 @@ export class StateMachine { try { await mod.afterCommandExecute(transactionContext); } catch (error) { - status = TransactionExecutionResult.INVALID; ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); + status = TransactionExecutionResult.INVALID; return status; } } @@ -208,9 +212,9 @@ export class StateMachine { try { await mod.afterCommandExecute(transactionContext); } catch (error) { - status = TransactionExecutionResult.INVALID; ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); + status = TransactionExecutionResult.INVALID; } } } From 3a15eb5a3ef252d4b16c8311017750f3e5157778 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Thu, 28 Jul 2022 16:34:24 +0200 Subject: [PATCH 11/18] :arrow_up: Bump lisk-db --- yarn.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yarn.lock b/yarn.lock index 6c536f85e7b..06f09dca462 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2500,10 +2500,10 @@ dependencies: "@types/node" "11.11.2" -"@liskhq/lisk-db@^0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27": - version "0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27" - resolved "https://npm.lisk.com/@liskhq%2flisk-db/-/lisk-db-0.3.0-debug.20-b44082d07ab158736d00f7fd1706ca72ca2c9d27.tgz#596e6e645ea2e5f4763661f776f4b82086316705" - integrity sha512-5tXeUCOjwYDVJGCdXj3gKBFC+EhIZschM111EWyAzHjIdQU6FQ10pEwNRadoW/b9ixDzCOhBKBhklwNSykADYQ== +"@liskhq/lisk-db@0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f": + version "0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f" + resolved "https://npm.lisk.com/@liskhq%2flisk-db/-/lisk-db-0.3.0-debug.20-b876469836935925369a887fe03e50a67c26c38f.tgz#e2fc899d796c22422027361a0666ef7564f4a2ae" + integrity sha512-1I35XKfjUTZAcARrcTUSnYjFMliDEPVOFro6Jo+KsrBEMjgpoUz2qZQqEfuFp1aVTsmkGjYw66jWfGHKZOAb1g== dependencies: "@mapbox/node-pre-gyp" "^1.0.9" "@types/node" "^16" From 54acad3ba0fce01e8808a053c2164b35bd9c9f1b Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 10:29:36 +0200 Subject: [PATCH 12/18] :white_check_mark: Update tests --- .../test/unit/state_machine/custom_modules.ts | 1 + .../unit/state_machine/state_machine.spec.ts | 105 ++++++++++++++---- 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/framework/test/unit/state_machine/custom_modules.ts b/framework/test/unit/state_machine/custom_modules.ts index 7fb6b235130..a903db4fac1 100644 --- a/framework/test/unit/state_machine/custom_modules.ts +++ b/framework/test/unit/state_machine/custom_modules.ts @@ -72,6 +72,7 @@ export class CustomModule1 extends BaseModule { public verifyAssets = jest.fn(); public beforeTransactionsExecute = jest.fn(); + public beforeCommandExecute = jest.fn(); public afterCommandExecute = jest.fn(); public metadata(): ModuleMetadata { throw new Error('Method not implemented.'); diff --git a/framework/test/unit/state_machine/state_machine.spec.ts b/framework/test/unit/state_machine/state_machine.spec.ts index 592b5182f00..68e68de7321 100644 --- a/framework/test/unit/state_machine/state_machine.spec.ts +++ b/framework/test/unit/state_machine/state_machine.spec.ts @@ -144,6 +144,27 @@ describe('state_machine', () => { }); describe('executeTransaction', () => { + const testEvents = [ + { + moduleID: utils.intToBuffer(3, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], + }, + { + moduleID: utils.intToBuffer(4, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], + }, + { + moduleID: utils.intToBuffer(2, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32)], + }, + ]; + it('should call all registered transaction execute hooks', async () => { const ctx = new TransactionContext({ eventQueue, @@ -206,28 +227,52 @@ describe('state_machine', () => { expect(events[0].toObject().topics[0]).toEqual(transaction.id); }); + it('should rollback state if beforeCommandExecute fails', async () => { + for (const e of testEvents) { + eventQueue.add(e.moduleID, e.typeID, e.data, e.topics); + } + + const snapshotID = eventQueue.createSnapshot(); + + const ctx = new TransactionContext({ + eventQueue, + logger, + stateStore, + header, + assets, + networkIdentifier, + transaction, + currentValidators: [], + impliesMaxPrevote: true, + maxHeightCertified: 0, + certificateThreshold: BigInt(0), + }); + await stateMachine.executeTransaction(ctx); + + eventQueue.add( + utils.intToBuffer(3, 4), + Buffer.from([0, 0, 0, 1]), + utils.getRandomBytes(100), + [utils.getRandomBytes(32)], + ); + + systemMod.beforeCommandExecute.mockImplementation(() => { + throw new Error('beforeCommandExecute failed'); + }); + mod.beforeCommandExecute.mockImplementation(() => { + throw new Error('beforeCommandExecute failed'); + }); + expect(systemMod.beforeCommandExecute).toHaveBeenCalled(); + expect(mod.beforeCommandExecute).toHaveBeenCalled(); + + ctx.eventQueue.restoreSnapshot(snapshotID); + + expect(snapshotID).toBe(testEvents.length); + expect(ctx.eventQueue.getEvents()).toHaveLength(testEvents.length); + }); + it('should rollback state if afterCommandExecute fails', async () => { - const events = [ - { - moduleID: utils.intToBuffer(3, 4), - typeID: Buffer.from([0, 0, 0, 0]), - data: utils.getRandomBytes(20), - topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], - }, - { - moduleID: utils.intToBuffer(4, 4), - typeID: Buffer.from([0, 0, 0, 0]), - data: utils.getRandomBytes(20), - topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], - }, - { - moduleID: utils.intToBuffer(2, 4), - typeID: Buffer.from([0, 0, 0, 0]), - data: utils.getRandomBytes(20), - topics: [utils.getRandomBytes(32)], - }, - ]; - for (const e of events) { + for (const e of testEvents) { eventQueue.add(e.moduleID, e.typeID, e.data, e.topics); } @@ -247,13 +292,27 @@ describe('state_machine', () => { certificateThreshold: BigInt(0), }); await stateMachine.executeTransaction(ctx); + + eventQueue.add( + utils.intToBuffer(3, 4), + Buffer.from([0, 0, 0, 1]), + utils.getRandomBytes(100), + [utils.getRandomBytes(32)], + ); + + systemMod.afterCommandExecute.mockImplementation(() => { + throw new Error('afterCommandExecute failed'); + }); + mod.afterCommandExecute.mockImplementation(() => { + throw new Error('afterCommandExecute failed'); + }); expect(systemMod.afterCommandExecute).toHaveBeenCalled(); expect(mod.afterCommandExecute).toHaveBeenCalled(); ctx.eventQueue.restoreSnapshot(snapshotID); - expect(snapshotID).toBe(events.length); - expect(ctx.eventQueue.getEvents()).toHaveLength(events.length); + expect(snapshotID).toBe(testEvents.length); + expect(ctx.eventQueue.getEvents()).toHaveLength(testEvents.length); }); }); From f0ff056b92437c027c494113dd9413d1091bbd60 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 11:33:19 +0200 Subject: [PATCH 13/18] :white_check_mark: Update tests --- .../test/unit/state_machine/custom_modules.ts | 1 - .../unit/state_machine/state_machine.spec.ts | 97 +++++-------------- 2 files changed, 23 insertions(+), 75 deletions(-) diff --git a/framework/test/unit/state_machine/custom_modules.ts b/framework/test/unit/state_machine/custom_modules.ts index a903db4fac1..7fb6b235130 100644 --- a/framework/test/unit/state_machine/custom_modules.ts +++ b/framework/test/unit/state_machine/custom_modules.ts @@ -72,7 +72,6 @@ export class CustomModule1 extends BaseModule { public verifyAssets = jest.fn(); public beforeTransactionsExecute = jest.fn(); - public beforeCommandExecute = jest.fn(); public afterCommandExecute = jest.fn(); public metadata(): ModuleMetadata { throw new Error('Method not implemented.'); diff --git a/framework/test/unit/state_machine/state_machine.spec.ts b/framework/test/unit/state_machine/state_machine.spec.ts index 68e68de7321..cf37ba51fa1 100644 --- a/framework/test/unit/state_machine/state_machine.spec.ts +++ b/framework/test/unit/state_machine/state_machine.spec.ts @@ -144,27 +144,6 @@ describe('state_machine', () => { }); describe('executeTransaction', () => { - const testEvents = [ - { - moduleID: utils.intToBuffer(3, 4), - typeID: Buffer.from([0, 0, 0, 0]), - data: utils.getRandomBytes(20), - topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], - }, - { - moduleID: utils.intToBuffer(4, 4), - typeID: Buffer.from([0, 0, 0, 0]), - data: utils.getRandomBytes(20), - topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], - }, - { - moduleID: utils.intToBuffer(2, 4), - typeID: Buffer.from([0, 0, 0, 0]), - data: utils.getRandomBytes(20), - topics: [utils.getRandomBytes(32)], - }, - ]; - it('should call all registered transaction execute hooks', async () => { const ctx = new TransactionContext({ eventQueue, @@ -227,52 +206,28 @@ describe('state_machine', () => { expect(events[0].toObject().topics[0]).toEqual(transaction.id); }); - it('should rollback state if beforeCommandExecute fails', async () => { - for (const e of testEvents) { - eventQueue.add(e.moduleID, e.typeID, e.data, e.topics); - } - - const snapshotID = eventQueue.createSnapshot(); - - const ctx = new TransactionContext({ - eventQueue, - logger, - stateStore, - header, - assets, - networkIdentifier, - transaction, - currentValidators: [], - impliesMaxPrevote: true, - maxHeightCertified: 0, - certificateThreshold: BigInt(0), - }); - await stateMachine.executeTransaction(ctx); - - eventQueue.add( - utils.intToBuffer(3, 4), - Buffer.from([0, 0, 0, 1]), - utils.getRandomBytes(100), - [utils.getRandomBytes(32)], - ); - - systemMod.beforeCommandExecute.mockImplementation(() => { - throw new Error('beforeCommandExecute failed'); - }); - mod.beforeCommandExecute.mockImplementation(() => { - throw new Error('beforeCommandExecute failed'); - }); - expect(systemMod.beforeCommandExecute).toHaveBeenCalled(); - expect(mod.beforeCommandExecute).toHaveBeenCalled(); - - ctx.eventQueue.restoreSnapshot(snapshotID); - - expect(snapshotID).toBe(testEvents.length); - expect(ctx.eventQueue.getEvents()).toHaveLength(testEvents.length); - }); - it('should rollback state if afterCommandExecute fails', async () => { - for (const e of testEvents) { + const events = [ + { + moduleID: utils.intToBuffer(3, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], + }, + { + moduleID: utils.intToBuffer(4, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32), utils.getRandomBytes(20)], + }, + { + moduleID: utils.intToBuffer(2, 4), + typeID: Buffer.from([0, 0, 0, 0]), + data: utils.getRandomBytes(20), + topics: [utils.getRandomBytes(32)], + }, + ]; + for (const e of events) { eventQueue.add(e.moduleID, e.typeID, e.data, e.topics); } @@ -300,19 +255,13 @@ describe('state_machine', () => { [utils.getRandomBytes(32)], ); - systemMod.afterCommandExecute.mockImplementation(() => { - throw new Error('afterCommandExecute failed'); - }); - mod.afterCommandExecute.mockImplementation(() => { - throw new Error('afterCommandExecute failed'); - }); expect(systemMod.afterCommandExecute).toHaveBeenCalled(); expect(mod.afterCommandExecute).toHaveBeenCalled(); ctx.eventQueue.restoreSnapshot(snapshotID); - expect(snapshotID).toBe(testEvents.length); - expect(ctx.eventQueue.getEvents()).toHaveLength(testEvents.length); + expect(snapshotID).toBe(events.length); + expect(ctx.eventQueue.getEvents()).toHaveLength(events.length); }); }); From b448a4762f919d1f84b025923e920106436abc42 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 11:40:44 +0200 Subject: [PATCH 14/18] :white_check_mark: Update tests --- framework/test/unit/state_machine/state_machine.spec.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/framework/test/unit/state_machine/state_machine.spec.ts b/framework/test/unit/state_machine/state_machine.spec.ts index cf37ba51fa1..a2eac0625d3 100644 --- a/framework/test/unit/state_machine/state_machine.spec.ts +++ b/framework/test/unit/state_machine/state_machine.spec.ts @@ -255,6 +255,12 @@ describe('state_machine', () => { [utils.getRandomBytes(32)], ); + systemMod.afterCommandExecute.mockImplementation(() => { + throw new Error('afterCommandExecute failed'); + }); + mod.afterCommandExecute.mockImplementation(() => { + throw new Error('afterCommandExecute failed'); + }); expect(systemMod.afterCommandExecute).toHaveBeenCalled(); expect(mod.afterCommandExecute).toHaveBeenCalled(); From dbbbf52fd3f9332ec9576a9be3bdc0f4a4be550f Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 12:50:20 +0200 Subject: [PATCH 15/18] :white_check_mark: Update test --- .../unit/state_machine/state_machine.spec.ts | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/framework/test/unit/state_machine/state_machine.spec.ts b/framework/test/unit/state_machine/state_machine.spec.ts index a2eac0625d3..fa1c5f2bc80 100644 --- a/framework/test/unit/state_machine/state_machine.spec.ts +++ b/framework/test/unit/state_machine/state_machine.spec.ts @@ -231,7 +231,21 @@ describe('state_machine', () => { eventQueue.add(e.moduleID, e.typeID, e.data, e.topics); } - const snapshotID = eventQueue.createSnapshot(); + mod.beforeCommandExecute.mockImplementation(() => { + eventQueue.add( + utils.intToBuffer(3, 4), + Buffer.from([0, 0, 0, 1]), + utils.getRandomBytes(100), + [utils.getRandomBytes(32)], + ); + }); + + systemMod.afterCommandExecute.mockImplementation(() => { + throw new Error('afterCommandExecute failed'); + }); + mod.afterCommandExecute.mockImplementation(() => { + throw new Error('afterCommandExecute failed'); + }); const ctx = new TransactionContext({ eventQueue, @@ -247,26 +261,6 @@ describe('state_machine', () => { certificateThreshold: BigInt(0), }); await stateMachine.executeTransaction(ctx); - - eventQueue.add( - utils.intToBuffer(3, 4), - Buffer.from([0, 0, 0, 1]), - utils.getRandomBytes(100), - [utils.getRandomBytes(32)], - ); - - systemMod.afterCommandExecute.mockImplementation(() => { - throw new Error('afterCommandExecute failed'); - }); - mod.afterCommandExecute.mockImplementation(() => { - throw new Error('afterCommandExecute failed'); - }); - expect(systemMod.afterCommandExecute).toHaveBeenCalled(); - expect(mod.afterCommandExecute).toHaveBeenCalled(); - - ctx.eventQueue.restoreSnapshot(snapshotID); - - expect(snapshotID).toBe(events.length); expect(ctx.eventQueue.getEvents()).toHaveLength(events.length); }); }); From 8aa9d76cb766604314efe3571fcd6ccafb1d5e97 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 16:07:29 +0200 Subject: [PATCH 16/18] :recycle: Update the transaction execution --- framework/src/state_machine/state_machine.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index b19964217dd..e71dddff387 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -192,6 +192,7 @@ export class StateMachine { [ctx.transaction.id], ); status = TransactionExecutionResult.FAIL; + return status; } // Execute after transaction hooks @@ -215,6 +216,7 @@ export class StateMachine { ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); status = TransactionExecutionResult.INVALID; + return status; } } } From 24625d6e6e420a678b83b2aca5adfb71a71ef282 Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 16:26:18 +0200 Subject: [PATCH 17/18] :recycle: Update the transaction execution --- framework/src/state_machine/state_machine.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index e71dddff387..505251457ae 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -141,7 +141,6 @@ export class StateMachine { } public async executeTransaction(ctx: TransactionContext): Promise { - let status = TransactionExecutionResult.OK; const transactionContext = ctx.createTransactionExecuteContext(); const eventQueueSnapshotID = ctx.eventQueue.createSnapshot(); const stateStoreSnapshotID = ctx.stateStore.createSnapshot(); @@ -152,8 +151,7 @@ export class StateMachine { } catch (error) { ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); - status = TransactionExecutionResult.INVALID; - return status; + return TransactionExecutionResult.INVALID; } } } @@ -162,10 +160,9 @@ export class StateMachine { try { await mod.beforeCommandExecute(transactionContext); } catch (error) { - status = TransactionExecutionResult.INVALID; ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); - return status; + return TransactionExecutionResult.INVALID; } } } @@ -191,8 +188,7 @@ export class StateMachine { codec.encode(standardEventDataSchema, { success: false }), [ctx.transaction.id], ); - status = TransactionExecutionResult.FAIL; - return status; + return TransactionExecutionResult.FAIL; } // Execute after transaction hooks @@ -203,8 +199,7 @@ export class StateMachine { } catch (error) { ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); - status = TransactionExecutionResult.INVALID; - return status; + return TransactionExecutionResult.INVALID; } } } @@ -215,13 +210,12 @@ export class StateMachine { } catch (error) { ctx.eventQueue.restoreSnapshot(eventQueueSnapshotID); ctx.stateStore.restoreSnapshot(stateStoreSnapshotID); - status = TransactionExecutionResult.INVALID; - return status; + return TransactionExecutionResult.INVALID; } } } - return status; + return TransactionExecutionResult.OK; } public async verifyAssets(ctx: BlockContext): Promise { From 2e33eedaf79e17cc869090cf8e01b4561e9e04fb Mon Sep 17 00:00:00 2001 From: Martin Macharia Date: Fri, 29 Jul 2022 16:42:32 +0200 Subject: [PATCH 18/18] :recycle: Update the transaction execution --- framework/src/state_machine/state_machine.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/framework/src/state_machine/state_machine.ts b/framework/src/state_machine/state_machine.ts index 505251457ae..ec5f4708cdf 100644 --- a/framework/src/state_machine/state_machine.ts +++ b/framework/src/state_machine/state_machine.ts @@ -141,6 +141,7 @@ export class StateMachine { } public async executeTransaction(ctx: TransactionContext): Promise { + let status = TransactionExecutionResult.OK; const transactionContext = ctx.createTransactionExecuteContext(); const eventQueueSnapshotID = ctx.eventQueue.createSnapshot(); const stateStoreSnapshotID = ctx.stateStore.createSnapshot(); @@ -188,7 +189,7 @@ export class StateMachine { codec.encode(standardEventDataSchema, { success: false }), [ctx.transaction.id], ); - return TransactionExecutionResult.FAIL; + status = TransactionExecutionResult.FAIL; } // Execute after transaction hooks @@ -215,7 +216,7 @@ export class StateMachine { } } - return TransactionExecutionResult.OK; + return status; } public async verifyAssets(ctx: BlockContext): Promise {