Skip to content

Commit

Permalink
address case of block values exceeding long (hyperledger#4983)
Browse files Browse the repository at this point in the history
* address case of block values exceeding long

Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte authored and elenduuche committed Aug 16, 2023
1 parent 260a33b commit 1fc4ad4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,7 +107,7 @@ public EngineGetPayloadResultV2 payloadTransactionCompleteV2(
.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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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();
Expand All @@ -29,6 +29,6 @@ public long calculateBlockValue(final BlockWithReceipts blockWithReceipts) {
final Wei minerFee = txs.get(i).getEffectivePriorityFeePerGas(block.getHeader().getBaseFee());
totalFee = totalFee.add(minerFee.multiply(receipts.get(i).getCumulativeGasUsed()));
}
return totalFee.toLong();
return totalFee;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void shouldCalculateZeroBlockValueForEmptyTransactions() {
.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
Expand Down Expand Up @@ -84,11 +84,39 @@ public void shouldCalculateCorrectBlockValue() {
.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));
}
}

0 comments on commit 1fc4ad4

Please sign in to comment.