Skip to content

Commit 0a0843d

Browse files
committed
Remove the blockHash and blockNumber fields from getTransactions()
The function getTransactions() is too flexible to optimize. This patch is a groundwork before optimizing the performance of getTransactions().
1 parent f0c27f5 commit 0a0843d

File tree

3 files changed

+36
-47
lines changed

3 files changed

+36
-47
lines changed

src/models/logic/transaction.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -392,28 +392,13 @@ async function getTransactionHashes(params: {
392392
page: number;
393393
itemsPerPage: number;
394394
type?: string[] | null;
395-
tracker?: H256 | null;
396-
blockNumber?: number | null;
397-
blockHash?: H256 | null;
398395
includePending?: boolean | null;
399396
onlySuccessful?: boolean | null;
400397
}): Promise<string[]> {
401398
const where: Sequelize.WhereOptions<TransactionAttribute> = {};
402399
if (params.type != null) {
403400
where.type = { [Sequelize.Op.in]: params.type };
404401
}
405-
if (params.tracker != null) {
406-
where.tracker = params.tracker.value;
407-
}
408-
if (params.blockNumber != null) {
409-
where.blockNumber = params.blockNumber;
410-
}
411-
if (params.blockHash != null) {
412-
where.blockHash = params.blockHash.value;
413-
}
414-
if (params.onlySuccessful) {
415-
where.errorHint = null;
416-
}
417402
if (params.includePending !== true) {
418403
where.isPending = false;
419404
}
@@ -451,8 +436,6 @@ export async function getTransactions(params: {
451436
assetType?: H160 | null;
452437
type?: string[] | null;
453438
tracker?: H256 | null;
454-
blockNumber?: number | null;
455-
blockHash?: H256 | null;
456439
page: number;
457440
itemsPerPage: number;
458441
includePending?: boolean | null;
@@ -488,6 +471,27 @@ export async function getTransactions(params: {
488471
}
489472
}
490473

474+
export async function getTransactionsOfBlock(params: {
475+
page: number;
476+
itemsPerPage: number;
477+
blockNumber: number;
478+
}) {
479+
try {
480+
const { page, itemsPerPage, blockNumber } = params;
481+
482+
return models.Transaction.findAll({
483+
where: { blockNumber },
484+
order: [["transactionIndex", "DESC"]],
485+
limit: itemsPerPage,
486+
offset: (page - 1) * itemsPerPage,
487+
include: [...fullIncludeArray]
488+
});
489+
} catch (err) {
490+
console.error(err);
491+
throw Exception.DBError();
492+
}
493+
}
494+
491495
export async function getNumberOfEachTransactionType(
492496
params: {
493497
blockNumber: number;
@@ -513,22 +517,12 @@ export async function getNumberOfEachTransactionType(
513517
function buildQueryForTransactions(params: {
514518
type?: string[] | null;
515519
tracker?: H256 | null;
516-
blockNumber?: number | null;
517-
blockHash?: H256 | null;
518520
includePending?: boolean | null;
519521
onlySuccessful?: boolean | null;
520522
}) {
521523
return {
522524
...(params.type ? { type: { [Sequelize.Op.in]: params.type } } : {}),
523525
...(params.tracker ? { tracker: params.tracker.value } : {}),
524-
...(params.blockNumber != null
525-
? { blockNumber: params.blockNumber }
526-
: {}),
527-
...(params.blockHash
528-
? {
529-
blockHash: params.blockHash.value
530-
}
531-
: {}),
532526
...(params.onlySuccessful ? { errorHint: null } : {}),
533527
...(params.includePending !== true ? { isPending: false } : {})
534528
/* FIXME: onlyConfirmed, confirmThreshold */

src/routers/block.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export function handle(context: IndexerContext, router: Router) {
215215
router.get(
216216
"/block/:hashOrNumber/tx",
217217
validate({
218+
// FIXME: Throw an error if hashOrNumber is not hash or number
218219
query: {
219220
...paginationSchema
220221
}
@@ -226,23 +227,15 @@ export function handle(context: IndexerContext, router: Router) {
226227
(req.query.itemsPerPage &&
227228
parseInt(req.query.itemsPerPage, 10)) ||
228229
15;
229-
let hashValue;
230-
let numberValue;
231-
// FIXME: Throw an error if hashOrNumber is not hash or number
232-
try {
233-
hashValue = new H256(hashOrNumber);
234-
} catch (e) {
235-
if (!isNaN(hashOrNumber)) {
236-
numberValue = parseInt(hashOrNumber, 10);
237-
}
238-
}
230+
239231
try {
240-
const txs = await TransactionModel.getTransactions({
232+
const block =
233+
H256.check(hashOrNumber) &&
234+
(await BlockModel.getByHash(new H256(hashOrNumber)));
235+
const txs = await TransactionModel.getTransactionsOfBlock({
241236
page,
242237
itemsPerPage,
243-
...(hashValue
244-
? { blockHash: hashValue }
245-
: { blockNumber: numberValue })
238+
blockNumber: block ? block.get().number : hashOrNumber
246239
});
247240
res.json(txs.map(tx => tx.get({ plain: true })));
248241
} catch (e) {

test/mint-transfer.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ describe("mint-transfer-synchronized", function() {
115115
expect(mintBlockInst.get({ plain: true }).hash).equal(
116116
mintBlock.hash.value
117117
);
118-
const mintBlockTransactions = await TransactionModel.getTransactions({
119-
blockNumber: bestBlockNumber - 1,
120-
page: 1,
121-
itemsPerPage: 15
122-
}).then(txs => txs.map(tx => tx.get({ plain: true })));
118+
const mintBlockTransactions = await TransactionModel.getTransactionsOfBlock(
119+
{
120+
blockNumber: bestBlockNumber - 1,
121+
page: 1,
122+
itemsPerPage: 15
123+
}
124+
).then(txs => txs.map(tx => tx.get({ plain: true })));
123125
expect(mintBlockTransactions[0].hash).equal(
124126
mintBlock.transactions[0].hash().value
125127
);
@@ -132,7 +134,7 @@ describe("mint-transfer-synchronized", function() {
132134
expect(transferBlockInst.get({ plain: true }).hash).equal(
133135
transferBlock.hash.value
134136
);
135-
const transferBlockTransactions = await TransactionModel.getTransactions(
137+
const transferBlockTransactions = await TransactionModel.getTransactionsOfBlock(
136138
{
137139
blockNumber: bestBlockNumber,
138140
page: 1,

0 commit comments

Comments
 (0)