Skip to content

Commit 8697a6e

Browse files
authored
feat: add the reason when ignoring a transactions (#408)
**Request-logic:** * reasons added on ignored transactions
1 parent 54156ba commit 8697a6e

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

packages/request-logic/src/request-logic.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,35 @@ export default class RequestLogic implements Types.IRequestLogic {
229229
public async getRequestFromId(requestId: string): Promise<Types.IReturnGetRequestFromId> {
230230
const resultGetTx = await this.transactionManager.getTransactionsByChannelId(requestId);
231231
const actions = resultGetTx.result.transactions;
232+
let ignoredTransactions: any[] = [];
232233

233234
// array of transaction without duplicates to avoid replay attack
234235
const actionsConfirmedWithoutDuplicates = Utils.uniqueByProperty(
235236
actions
236237
.map((t: any) => {
237-
// We ignore the transaction.data that cannot be parsed
238238
try {
239239
return { action: JSON.parse(t.transaction.data), timestamp: t.timestamp };
240240
} catch (e) {
241+
// We ignore the transaction.data that cannot be parsed
242+
ignoredTransactions.push({
243+
reason: 'JSON parsing error',
244+
transaction: t,
245+
});
241246
return;
242247
}
243248
})
244249
.filter((elem: any) => elem !== undefined),
245250
'action',
246251
);
247252
// Keeps the transaction ignored
248-
const ignoredTransactions = actionsConfirmedWithoutDuplicates.duplicates;
253+
ignoredTransactions = ignoredTransactions.concat(
254+
actionsConfirmedWithoutDuplicates.duplicates.map(tx => {
255+
return {
256+
reason: 'Duplicated transaction',
257+
transaction: tx,
258+
};
259+
}),
260+
);
249261

250262
// second parameter is null, because the first action must be a creation (no state expected)
251263
const request = actionsConfirmedWithoutDuplicates.uniqueItems.reduce(
@@ -259,7 +271,7 @@ export default class RequestLogic implements Types.IRequestLogic {
259271
);
260272
} catch (e) {
261273
// if an error occurs during the apply we ignore the action
262-
ignoredTransactions.push(actionConfirmed.action);
274+
ignoredTransactions.push({ reason: e.message, transaction: actionConfirmed });
263275
return requestState;
264276
}
265277
},
@@ -296,21 +308,33 @@ export default class RequestLogic implements Types.IRequestLogic {
296308
const allRequestAndMeta = Object.keys(getChannelsResult.result.transactions).map(channelId => {
297309
// Parses and removes corrupted or duplicated transactions
298310

311+
let ignoredTransactions: any[] = [];
312+
299313
const actionsConfirmedWithoutDuplicates = Utils.uniqueByProperty(
300314
transactionsByChannel[channelId]
301315
.map((t: any) => {
302-
// We ignore the transaction.data that cannot be parsed
303316
try {
304317
return { action: JSON.parse(t.transaction.data), timestamp: t.timestamp };
305318
} catch (e) {
319+
// We ignore the transaction.data that cannot be parsed
320+
ignoredTransactions.push({
321+
reason: 'JSON parsing error',
322+
transaction: t,
323+
});
306324
return;
307325
}
308326
})
309327
.filter((elem: any) => elem !== undefined),
310328
'action',
311329
);
330+
312331
// Keeps the transaction ignored
313-
const ignoredTransactions = actionsConfirmedWithoutDuplicates.duplicates;
332+
ignoredTransactions = ignoredTransactions.concat(
333+
actionsConfirmedWithoutDuplicates.duplicates.map(tx => ({
334+
reason: 'Duplicated transaction',
335+
transaction: tx,
336+
})),
337+
);
314338

315339
// second parameter is null, because the first action must be a creation (no state expected)
316340
const request = actionsConfirmedWithoutDuplicates.uniqueItems.reduce(
@@ -324,7 +348,7 @@ export default class RequestLogic implements Types.IRequestLogic {
324348
);
325349
} catch (e) {
326350
// if an error occurs during the apply we ignore the action
327-
ignoredTransactions.push(actionConfirmed);
351+
ignoredTransactions.push({ reason: e.message, transaction: actionConfirmed });
328352
return requestState;
329353
}
330354
},

packages/request-logic/test/index.test.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,12 @@ describe('index', () => {
532532

533533
expect(request, 'request result is wrong').to.deep.equal({
534534
meta: {
535-
ignoredTransactions: [{ action: actionReduce2, timestamp: 4 }],
535+
ignoredTransactions: [
536+
{
537+
reason: 'Duplicated transaction',
538+
transaction: { action: actionReduce2, timestamp: 4 },
539+
},
540+
],
536541
transactionManagerMeta: meta,
537542
},
538543
result: {
@@ -732,15 +737,14 @@ describe('index', () => {
732737
});
733738

734739
it('should ignored the corrupted data (not parsable JSON)', async () => {
740+
const transactionNotParsable = {
741+
timestamp: 2,
742+
transaction: { data: '{NOT parsable}' },
743+
};
735744
const listActions: Promise<TransactionTypes.IReturnGetTransactions> = Promise.resolve({
736745
meta: {},
737746
result: {
738-
transactions: [
739-
{
740-
timestamp: 2,
741-
transaction: { data: '{NOT parsable}' },
742-
},
743-
],
747+
transactions: [transactionNotParsable],
744748
},
745749
});
746750

@@ -756,11 +760,20 @@ describe('index', () => {
756760
);
757761

758762
const request = await requestLogic.getRequestFromId(requestId);
763+
expect(
764+
request.meta.ignoredTransactions && request.meta.ignoredTransactions.length,
765+
).to.be.equal(1);
766+
expect(
767+
request.meta.ignoredTransactions && request.meta.ignoredTransactions[0],
768+
).to.be.deep.equal({
769+
reason: 'JSON parsing error',
770+
transaction: transactionNotParsable,
771+
});
759772
expect(request.result.request, 'request should be null').to.be.null;
760773
});
761774

762775
it('should ignored the corrupted data (e.g: wrong properties)', async () => {
763-
const actionCreateCorrupted: any = Utils.signature.sign(
776+
const actionCorrupted: Types.IAction = Utils.signature.sign(
764777
{
765778
name: Types.ACTION_NAME.CREATE,
766779
parameters: {
@@ -781,7 +794,7 @@ describe('index', () => {
781794
transactions: [
782795
{
783796
timestamp: 2,
784-
transaction: { data: JSON.stringify(actionCreateCorrupted) },
797+
transaction: { data: JSON.stringify(actionCorrupted) },
785798
},
786799
],
787800
},
@@ -799,12 +812,19 @@ describe('index', () => {
799812
);
800813

801814
const request = await requestLogic.getRequestFromId(requestId);
815+
802816
expect(
803817
request.meta.ignoredTransactions && request.meta.ignoredTransactions.length,
804818
).to.be.equal(1);
805819
expect(
806820
request.meta.ignoredTransactions && request.meta.ignoredTransactions[0],
807-
).to.be.deep.equal(actionCreateCorrupted);
821+
).to.be.deep.equal({
822+
reason: 'action.parameters.expectedAmount must be a string representing a positive integer',
823+
transaction: {
824+
action: actionCorrupted,
825+
timestamp: 2,
826+
},
827+
});
808828
expect(request.result.request, 'request should be null').to.be.null;
809829
});
810830
});

0 commit comments

Comments
 (0)