Skip to content

Commit b0be256

Browse files
fix: moved cumulativeGas calculation after synthetic receipts are created (#4921)
Signed-off-by: Bartosz Solka <bartosz.solka@blockydevs.com>
1 parent f69593a commit b0be256

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

packages/relay/src/lib/factories/transactionReceiptFactory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface IRegularTransactionReceiptParams {
2323
logs: Log[];
2424
receiptResponse: any;
2525
to: string | null;
26-
blockGasUsedBeforeTransaction: number;
26+
blockGasUsedBeforeTransaction?: number;
2727
}
2828

2929
/**
@@ -105,7 +105,10 @@ class TransactionReceiptFactory {
105105
blockNumber: numberTo0x(receiptResponse.block_number),
106106
from: from,
107107
to: to,
108-
cumulativeGasUsed: numberTo0x(blockGasUsedBeforeTransaction + receiptResponse.gas_used),
108+
cumulativeGasUsed:
109+
blockGasUsedBeforeTransaction != null
110+
? numberTo0x(blockGasUsedBeforeTransaction + receiptResponse.gas_used)
111+
: constants.ZERO_HEX,
109112
gasUsed: nanOrNumberTo0x(receiptResponse.gas_used),
110113
contractAddress: contractAddress,
111114
logs: logs,

packages/relay/src/lib/services/ethService/blockService/blockWorker.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,7 @@ export async function getBlockReceipts(
395395
logsByHash.set(log.transactionHash, existingLogs);
396396
}
397397

398-
// Ensure contract results are processed in transaction_index (block) order
399-
const sortedContractResults = [...contractResults].sort((a, b) => {
400-
const aIdx = a.transaction_index ?? Number.MAX_SAFE_INTEGER;
401-
const bIdx = b.transaction_index ?? Number.MAX_SAFE_INTEGER;
402-
return aIdx - bIdx;
403-
});
404-
405-
let blockGasUsedBeforeTransaction = 0;
406-
const receiptPromises = sortedContractResults.map(async (contractResult) => {
398+
const receiptPromises = contractResults.map(async (contractResult) => {
407399
if (Utils.isRejectedDueToHederaSpecificValidation(contractResult)) {
408400
logger.debug(
409401
`Transaction with hash %s is skipped due to hedera-specific validation failure (%s)`,
@@ -425,11 +417,9 @@ export async function getBlockReceipts(
425417
logs: contractResult.logs,
426418
receiptResponse: contractResult,
427419
to,
428-
blockGasUsedBeforeTransaction,
429420
};
430421

431422
const receipt = TransactionReceiptFactory.createRegularReceipt(transactionReceiptParams) as ITransactionReceipt;
432-
blockGasUsedBeforeTransaction += contractResult.gas_used;
433423
return receipt;
434424
});
435425

@@ -449,7 +439,17 @@ export async function getBlockReceipts(
449439
}
450440
}
451441

452-
return receipts;
442+
// after all the receipts are created, we need to sort them by transaction index and calculate the cumulative gas used
443+
const sortedReceipts = receipts.sort(
444+
(a, b) => parseInt(a.transactionIndex ?? '0', 16) - parseInt(b.transactionIndex ?? '0', 16),
445+
);
446+
let blockGasUsedBeforeTransaction = 0;
447+
for (const receipt of sortedReceipts) {
448+
blockGasUsedBeforeTransaction += parseInt(receipt.gasUsed, 16);
449+
receipt.cumulativeGasUsed = numberTo0x(blockGasUsedBeforeTransaction);
450+
}
451+
452+
return sortedReceipts as ITransactionReceipt[];
453453
} catch (e: unknown) {
454454
throw WorkersPool.wrapError(e);
455455
}

0 commit comments

Comments
 (0)