Skip to content

Commit 1e221d3

Browse files
refactor: changed cumulativeGasUsed calculation for synthetic receipts in getBlockReceipts (#4921)
Signed-off-by: Bartosz Solka <bartosz.solka@blockydevs.com>
1 parent b0be256 commit 1e221d3

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

Lines changed: 2 additions & 5 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,10 +105,7 @@ class TransactionReceiptFactory {
105105
blockNumber: numberTo0x(receiptResponse.block_number),
106106
from: from,
107107
to: to,
108-
cumulativeGasUsed:
109-
blockGasUsedBeforeTransaction != null
110-
? numberTo0x(blockGasUsedBeforeTransaction + receiptResponse.gas_used)
111-
: constants.ZERO_HEX,
108+
cumulativeGasUsed: numberTo0x(blockGasUsedBeforeTransaction + receiptResponse.gas_used),
112109
gasUsed: nanOrNumberTo0x(receiptResponse.gas_used),
113110
contractAddress: contractAddress,
114111
logs: logs,

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

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

398+
let blockGasUsedBeforeTransaction = 0;
398399
const receiptPromises = contractResults.map(async (contractResult) => {
399400
if (Utils.isRejectedDueToHederaSpecificValidation(contractResult)) {
400401
logger.debug(
@@ -417,9 +418,11 @@ export async function getBlockReceipts(
417418
logs: contractResult.logs,
418419
receiptResponse: contractResult,
419420
to,
421+
blockGasUsedBeforeTransaction,
420422
};
421423

422424
const receipt = TransactionReceiptFactory.createRegularReceipt(transactionReceiptParams) as ITransactionReceipt;
425+
blockGasUsedBeforeTransaction += contractResult.gas_used;
423426
return receipt;
424427
});
425428

@@ -439,15 +442,18 @@ export async function getBlockReceipts(
439442
}
440443
}
441444

442-
// after all the receipts are created, we need to sort them by transaction index and calculate the cumulative gas used
445+
// after all the receipts are created, we need to sort them by transaction index and calculate the cumulative gas used for synthetic receipts
443446
const sortedReceipts = receipts.sort(
444447
(a, b) => parseInt(a.transactionIndex ?? '0', 16) - parseInt(b.transactionIndex ?? '0', 16),
445448
);
446-
let blockGasUsedBeforeTransaction = 0;
447-
for (const receipt of sortedReceipts) {
448-
blockGasUsedBeforeTransaction += parseInt(receipt.gasUsed, 16);
449-
receipt.cumulativeGasUsed = numberTo0x(blockGasUsedBeforeTransaction);
450-
}
449+
450+
sortedReceipts.forEach((receipt, index) => {
451+
const isSynthetic = receipt.cumulativeGasUsed === constants.ZERO_HEX; // assuming that synthetic receipts have 0 gas used and regular receipts have non-zero gas used
452+
if (index > 0 && isSynthetic) {
453+
// if the index is 0, we don't need to set the cumulative gas used for the synthetic receipt, as it's already set to 0
454+
receipt.cumulativeGasUsed = sortedReceipts[index - 1].cumulativeGasUsed; // assign the cumulative gas of previous transaction, as the synthetic transaction uses 0 gas
455+
}
456+
});
451457

452458
return sortedReceipts as ITransactionReceipt[];
453459
} catch (e: unknown) {

0 commit comments

Comments
 (0)