diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockValueCalculator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockValueCalculator.java index c8cd8c5039c..cbf6a7c6941 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockValueCalculator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockValueCalculator.java @@ -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; } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/BlockValueCalculatorTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/BlockValueCalculatorTest.java index 3116d3181b0..2d2b4c75f2b 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/BlockValueCalculatorTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/BlockValueCalculatorTest.java @@ -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