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

Commit

Permalink
🐛 Ensure no duplicate entries
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Feb 5, 2024
1 parent da085fe commit 1118863
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions services/export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"ioredis": "^5.3.2",
"json2csv": "^5.0.6",
"lisk-service-framework": "https://github.com/LiskHQ/lisk-service/raw/5cb6fc8e9b9798595d1a4652b9148afcbfaaed1f/framework/dist/lisk-service-framework-1.6.11.tgz",
"lodash": "^4.17.21",
"minio": "^7.0.21",
"moment": "^2.29.4",
"moment-range": "^4.0.2",
Expand Down
22 changes: 22 additions & 0 deletions services/export/shared/helpers/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* LiskHQ/lisk-service
* Copyright © 2024 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const _ = require('lodash');

const dropDuplicatesDeep = arr => arr.filter((v, i, a) => a.findIndex(t => _.isEqual(v, t)) === i);

module.exports = {
dropDuplicatesDeep,
};
4 changes: 2 additions & 2 deletions services/export/shared/helpers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ const normalizeTransactionAmount = (address, tx, currentChainID) => {
return (sign * amount).toString();
};

const normalizeTransactionFee = (address, tx) => {
const normalizeTransactionFee = (addressFromParams, tx) => {
const txFee = (BigInt('-1') * BigInt(tx.fee)).toString(); // Fee reduces the balance

const isTokenTransfer = tx.moduleCommand === `${MODULE.TOKEN}:${COMMAND.TRANSFER}`;
if (!isTokenTransfer) return txFee;

const { isIncomingCrossChainTransferTransaction } = tx;
const isRecipient = address === tx.params.recipientAddress;
const isRecipient = addressFromParams === tx.params.recipientAddress;
return isRecipient || isIncomingCrossChainTransferTransaction ? BigInt('0').toString() : txFee;
};

Expand Down
3 changes: 2 additions & 1 deletion services/export/shared/transactionsExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
getUniqueChainIDs,
} = require('./helpers');

const { dropDuplicatesDeep } = require('./helpers/array');
const { checkIfIndexReadyForInterval } = require('./helpers/ready');

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: javascript.lang.correctness.useless-assign.useless-assignment Note

const is assigned twice; the first assignment is useless
const { standardizeIntervalFromParams } = require('./helpers/time');
const { requestIndexer, requestAppRegistry } = require('./helpers/request');
Expand Down Expand Up @@ -678,7 +679,7 @@ const getEntriesByChronology = async (params, sortedBlocks, sortedTransactions,
}
}

return entries;
return dropDuplicatesDeep(entries);
};

const rescheduleExportOnTimeout = async params => {
Expand Down
55 changes: 55 additions & 0 deletions services/export/tests/unit/shared/helpers/array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* LiskHQ/lisk-service
* Copyright © 2024 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const _ = require('lodash');
const { dropDuplicatesDeep } = require('../../../../shared/helpers/array');

describe('Unit tests for array utilities', () => {
describe('Test dropDuplicates method', () => {
const isEveryElementUnique = array =>
array.every((e, i, a) => a.filter(n => _.isEqual(e, n)).length === 1);

it('Array with duplicates', async () => {
const input1 = [2, 3, 4, 5, 6, 7];
const input2 = [5, 7, 9, 11, 13];
const result = dropDuplicatesDeep(input1.concat(input2));
expect(result).toBeInstanceOf(Array);
expect(result.length).toBeLessThanOrEqual(input1.length + input2.length);
expect(isEveryElementUnique(result)).toBeTruthy();
});

it('Array with duplicate objects', async () => {
const input = [{ a: 1 }, { a: 1 }, { a: 1 }, { b: { c: 2 } }, { b: { c: 2 } }];
const result = dropDuplicatesDeep(input);
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(2);
});

it('Array with no duplicates', async () => {
const input = [2, 3, 4, 5, 6, 7];
const result = dropDuplicatesDeep(input);
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(input.length);
expect(isEveryElementUnique(result)).toBeTruthy();
});

it('Array with no duplicate objects', async () => {
const input = [{ a: 1 }, { b: 1 }, { c: 1 }, { b: { c: 2 } }, { b: { c: 200 } }];
const result = dropDuplicatesDeep(input);
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(5);
});
});
});

0 comments on commit 1118863

Please sign in to comment.