address case of block values exceeding long (#4983)

* address case of block values exceeding long

Signed-off-by: garyschulte <garyschulte@gmail.com>
pull/4983/merge
garyschulte 2 years ago committed by GitHub
parent f8a0f87405
commit 48702aa12e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/BlockResultFactory.java
  2. 4
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockValueCalculator.java
  3. 36
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/BlockValueCalculatorTest.java

@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.api.query.BlockWithMetadata;
import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata;
import org.hyperledger.besu.ethereum.core.Block;
@ -106,7 +107,7 @@ public class BlockResultFactory {
.map(Bytes::toHexString)
.collect(Collectors.toList());
final long blockValue = new BlockValueCalculator().calculateBlockValue(blockWithReceipts);
final Wei blockValue = new BlockValueCalculator().calculateBlockValue(blockWithReceipts);
return new EngineGetPayloadResultV2(
blockWithReceipts.getHeader(),
txs,

@ -20,7 +20,7 @@ import java.util.List;
public class BlockValueCalculator {
public long calculateBlockValue(final BlockWithReceipts blockWithReceipts) {
public Wei calculateBlockValue(final BlockWithReceipts blockWithReceipts) {
final Block block = blockWithReceipts.getBlock();
final List<Transaction> txs = block.getBody().getTransactions();
final List<TransactionReceipt> receipts = blockWithReceipts.getReceipts();
@ -29,6 +29,6 @@ public class BlockValueCalculator {
final Wei minerFee = txs.get(i).getEffectivePriorityFeePerGas(block.getHeader().getBaseFee());
totalFee = totalFee.add(minerFee.multiply(receipts.get(i).getCumulativeGasUsed()));
}
return totalFee.toLong();
return totalFee;
}
}

@ -40,10 +40,10 @@ public class BlockValueCalculatorTest {
.buildHeader();
final Block block =
new Block(blockHeader, new BlockBody(Collections.emptyList(), Collections.emptyList()));
long blockValue =
Wei blockValue =
new BlockValueCalculator()
.calculateBlockValue(new BlockWithReceipts(block, Collections.emptyList()));
assertThat(blockValue).isEqualTo(0);
assertThat(blockValue).isEqualTo(Wei.ZERO);
}
@Test
@ -84,11 +84,39 @@ public class BlockValueCalculatorTest {
.buildHeader();
final Block block =
new Block(blockHeader, new BlockBody(List.of(tx1, tx2, tx3), Collections.emptyList()));
long blockValue =
Wei blockValue =
new BlockValueCalculator()
.calculateBlockValue(
new BlockWithReceipts(block, List.of(receipt1, receipt2, receipt3)));
// Block value = 71 * 1 + 143 * 2 + 214 * 5 = 1427
assertThat(blockValue).isEqualTo(1427);
assertThat(blockValue).isEqualTo(Wei.of(1427L));
}
@Test
public void shouldCalculateCorrectBlockValueExceedingLong() {
// Generate a block with one overpriced transaction where the resulting block value exceeds long
final long baseFee = 7L;
final long maxFee = 1L << 60L;
final Transaction tx1 =
new TransactionTestFixture()
.maxPriorityFeePerGas(Optional.of(Wei.of(maxFee - baseFee)))
.maxFeePerGas(Optional.of(Wei.of(maxFee)))
.type(TransactionType.EIP1559)
.createTransaction(SignatureAlgorithmFactory.getInstance().generateKeyPair());
final TransactionReceipt receipt1 =
new TransactionReceipt(
Hash.EMPTY_TRIE_HASH, 21000L, Collections.emptyList(), Optional.empty());
final BlockHeader blockHeader =
new BlockHeaderTestFixture()
.prevRandao(Bytes32.random())
.baseFeePerGas(Wei.of(baseFee))
.buildHeader();
final Block block =
new Block(blockHeader, new BlockBody(List.of(tx1), Collections.emptyList()));
Wei blockValue =
new BlockValueCalculator()
.calculateBlockValue(new BlockWithReceipts(block, List.of(receipt1)));
// Block value =~ max_long * 2
assertThat(blockValue).isGreaterThan(Wei.of(Long.MAX_VALUE));
}
}

Loading…
Cancel
Save