Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block value calculation #5100

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public Wei calculateBlockValue(final BlockWithReceipts blockWithReceipts) {
Wei totalFee = Wei.ZERO;
for (int i = 0; i < txs.size(); i++) {
final Wei minerFee = txs.get(i).getEffectivePriorityFeePerGas(block.getHeader().getBaseFee());
totalFee = totalFee.add(minerFee.multiply(receipts.get(i).getCumulativeGasUsed()));
// we don't store gasUsed and need to calculate that on the fly
jframe marked this conversation as resolved.
Show resolved Hide resolved
// receipts are fetched in ascendant sorted by cumulativeGasUsed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// we don't store gasUsed and need to calculate that on the fly
// receipts are fetched in ascendant sorted by cumulativeGasUsed
// we don't store gasUsed per transaction and need to calculate that on the fly
// receipts are fetched in ascending sorted by cumulativeGasUsed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had the same thought, did a bit of digging into the code, and even the BlockchainQuery does this same calculation to create instances of TransactionReceiptWithMetadata

long gasUsed = receipts.get(i).getCumulativeGasUsed();
if (i > 0) {
gasUsed = gasUsed - receipts.get(i - 1).getCumulativeGasUsed();
}
totalFee = totalFee.add(minerFee.multiply(gasUsed));
}
return totalFee;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public void shouldCalculateCorrectBlockValue() {
new BlockValueCalculator()
.calculateBlockValue(
new BlockWithReceipts(block, List.of(receipt1, receipt2, receipt3)));
// Block value = 71 * 1 + 143 * 2 + 214 * 5 = 1427
assertThat(blockValue).isEqualTo(Wei.of(1427L));
// Block value = 71 * 1 + (143-71) * 2 + (214-143) * 5 = 1427
assertThat(blockValue).isEqualTo(Wei.of(570L));
}

@Test
Expand Down