Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

fix: Return txs in block / logs in tx in correct order #559

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions packages/api-server/src/db/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ export class Query {
return await this.getTransactions({ block_number: blockNumber.toString() });
}

// Order by `transaction_index`, now only for search txs in a block
private async getTransactions(
params: Readonly<Partial<KnexType.MaybeRawRecord<DBTransaction>>>
): Promise<Transaction[]> {
const transactions = await this.knex<DBTransaction>("transactions").where(
params
);
const transactions = await this.knex<DBTransaction>("transactions")
.where(params)
.orderBy("transaction_index", "asc");

return transactions.map((tx) => formatTransaction(tx));
}
Expand Down Expand Up @@ -225,30 +226,6 @@ export class Query {
return formatTransaction(transaction);
}

async getTransactionHashesByBlockHash(blockHash: Hash): Promise<Hash[]> {
return await this.getTransactionHashes({
block_hash: hexToBuffer(blockHash),
});
}

async getTransactionHashesByBlockNumber(
blockNumber: bigint
): Promise<Hash[]> {
return await this.getTransactionHashes({
block_number: blockNumber.toString(),
});
}

private async getTransactionHashes(
params: Readonly<Partial<KnexType.MaybeRawRecord<DBTransaction>>>
): Promise<Hash[]> {
const transactionHashes = await this.knex<DBTransaction>("transactions")
.select("hash")
.where(params);

return transactionHashes.map((tx) => bufferToHex(tx.hash));
}

async getTransactionEthHashesByBlockHash(blockHash: Hash): Promise<Hash[]> {
return await this.getTransactionEthHashes({
block_hash: hexToBuffer(blockHash),
Expand All @@ -263,12 +240,14 @@ export class Query {
});
}

// Order by `transaction_index`, only for search tx eth hashes in a block.
private async getTransactionEthHashes(
params: Readonly<Partial<KnexType.MaybeRawRecord<DBTransaction>>>
): Promise<Hash[]> {
const transactionHashes = await this.knex<DBTransaction>("transactions")
.select("eth_tx_hash")
.where(params);
.where(params)
.orderBy("transaction_index", "asc");

return transactionHashes.map((tx) => bufferToHex(tx.eth_tx_hash));
}
Expand Down Expand Up @@ -309,9 +288,11 @@ export class Query {
return undefined;
}

const logs = await this.knex<DBLog>("logs").where({
transaction_hash: hexToBuffer(txHash),
});
const logs = await this.knex<DBLog>("logs")
.where({
transaction_hash: hexToBuffer(txHash),
})
.orderBy("log_index", "asc");

return [formatTransaction(tx), logs.map((log) => formatLog(log))];
}
Expand Down