Skip to content

Commit

Permalink
Fix block value calculation (hyperledger#5100)
Browse files Browse the repository at this point in the history
* Add gasUsed calculation

Signed-off-by: Gabriel Fukushima <[email protected]>

* Fix unit test

Signed-off-by: Gabriel Fukushima <[email protected]>

* Fix typo

Signed-off-by: Gabriel Fukushima <[email protected]>

---------

Signed-off-by: Gabriel Fukushima <[email protected]>
  • Loading branch information
gfukushima authored and eum602 committed Nov 3, 2023
1 parent 9e76ea5 commit be3a545
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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
// receipts are fetched in ascending sorted by cumulativeGasUsed
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

0 comments on commit be3a545

Please sign in to comment.