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

Encapsulate London Block transaction gas price validation #2660

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -25,6 +25,11 @@ public static FeeMarketException BaseFeePresentBeforeForkBlock() {
"Invalid block header: basefee should not be present before fee market fork block");
}

public static FeeMarketException MissingBaseFeeMarket() {
return new FeeMarketException(
"Incorrectly configured ProtocolSchedule: requires BaseFeeMarket");
}

private FeeMarketException(final String reason) {
super(reason);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.core.feemarket.TransactionPriceCalculator;

import java.util.List;
import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class BaseFeeBlockBodyValidator extends MainnetBlockBodyValidator {
private static final Logger LOG = LogManager.getLogger();

public BaseFeeBlockBodyValidator(final ProtocolSchedule protocolSchedule) {
super(protocolSchedule);
}

@Override
public boolean validateBodyLight(
final ProtocolContext context,
final Block block,
final List<TransactionReceipt> receipts,
final HeaderValidationMode ommerValidationMode) {

return super.validateBodyLight(context, block, receipts, ommerValidationMode)
&& validateTransactionGasPrice(block);
}

@VisibleForTesting
boolean validateTransactionGasPrice(final Block block) {

final BlockBody body = block.getBody();
final List<Transaction> transactions = body.getTransactions();
final TransactionPriceCalculator transactionPriceCalculator =
protocolSchedule
.getByBlockNumber(block.getHeader().getNumber())
.getFeeMarket()
.getTransactionPriceCalculator();

for (final Transaction transaction : transactions) {
final Optional<Long> baseFee = block.getHeader().getBaseFee();
final Wei price = transactionPriceCalculator.price(transaction, baseFee);
if (price.compareTo(Wei.of(baseFee.orElseThrow())) < 0) {
LOG.warn(
"Invalid block: transaction gas price {} must be greater than base fee {}",
price.toString(),
baseFee.orElseThrow());
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.core.LogsBloomFilter;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.core.feemarket.TransactionPriceCalculator;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
Expand All @@ -42,7 +37,7 @@ public class MainnetBlockBodyValidator implements BlockBodyValidator {
private static final int MAX_OMMERS = 2;

private static final int MAX_GENERATION = 6;
private final ProtocolSchedule protocolSchedule;
protected final ProtocolSchedule protocolSchedule;

public MainnetBlockBodyValidator(final ProtocolSchedule protocolSchedule) {
this.protocolSchedule = protocolSchedule;
Expand Down Expand Up @@ -104,10 +99,6 @@ public boolean validateBodyLight(
return false;
}

if (!validateTransactionGasPrice(block)) {
return false;
}

return true;
}

Expand Down Expand Up @@ -262,36 +253,4 @@ private boolean isOmmerSiblingOfAncestor(
}
return false;
}

private boolean validateTransactionGasPrice(final Block block) {

return Optional.of(
protocolSchedule.getByBlockNumber(block.getHeader().getNumber()).getFeeMarket())
.filter(FeeMarket::implementsBaseFee)
.map(
baseFeeMarket -> {
final BlockBody body = block.getBody();
final List<Transaction> transactions = body.getTransactions();
final TransactionPriceCalculator transactionPriceCalculator =
protocolSchedule
.getByBlockNumber(block.getHeader().getNumber())
.getFeeMarket()
.getTransactionPriceCalculator();

for (final Transaction transaction : transactions) {
final Optional<Long> baseFee = block.getHeader().getBaseFee();
final Wei price = transactionPriceCalculator.price(transaction, baseFee);
if (price.compareTo(Wei.of(baseFee.orElseThrow())) < 0) {
LOG.warn(
"Invalid block: transaction gas price {} must be greater than base fee {}",
price.toString(),
baseFee.orElseThrow());
return false;
}
}
return true;
})
// not a baseFeeMarket, no need to validate transaction gasPrices:
.orElse(Boolean.TRUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ static ProtocolSpecBuilder londonDefinition(
MainnetBlockHeaderValidator.createBaseFeeMarketValidator(londonFeeMarket))
.ommerHeaderValidatorBuilder(
MainnetBlockHeaderValidator.createBaseFeeMarketOmmerValidator(londonFeeMarket))
.blockBodyValidatorBuilder(BaseFeeBlockBodyValidator::new)
.name(LONDON_FORK_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.plugin.data.TransactionType;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class BaseFeeBlockBodyValidatorTest {

private static final KeyPair keyPair = SignatureAlgorithmFactory.getInstance().generateKeyPair();

@Mock ProtocolSpec protocolSpec;
@Mock ProtocolSchedule protocolSchedule;
@Mock Block block;
@Mock BlockHeader blockHeader;

BaseFeeBlockBodyValidator blockBodyValidator;

@Before
public void setup() {
when(protocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));

when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);

when(block.getHeader()).thenReturn(blockHeader);

blockBodyValidator = new BaseFeeBlockBodyValidator(protocolSchedule);
}

@Test
public void BlockBodyValidatorSucceed() {
when(blockHeader.getBaseFee()).thenReturn(Optional.of(10L));
when(block.getBody())
.thenReturn(
new BlockBody(
List.of(
// eip1559 transaction
new TransactionTestFixture()
.maxFeePerGas(Optional.of(Wei.of(10L)))
.maxPriorityFeePerGas(Optional.of(Wei.of(1L)))
.type(TransactionType.EIP1559)
.createTransaction(keyPair),
// frontier transaction
new TransactionTestFixture().gasPrice(Wei.of(10L)).createTransaction(keyPair)),
Collections.emptyList()));

assertThat(blockBodyValidator.validateTransactionGasPrice(block)).isTrue();
}

@Test
public void BlockBodyValidatorFail_GasPrice() {
when(blockHeader.getBaseFee()).thenReturn(Optional.of(10L));
when(block.getBody())
.thenReturn(
new BlockBody(
List.of(
// underpriced frontier transaction
new TransactionTestFixture().gasPrice(Wei.of(9L)).createTransaction(keyPair)),
Collections.emptyList()));

assertThat(blockBodyValidator.validateTransactionGasPrice(block)).isFalse();
}

@Test
public void BlockBodyValidatorFail_MaxFeePerGas() {
when(blockHeader.getBaseFee()).thenReturn(Optional.of(10L));
when(block.getBody())
.thenReturn(
new BlockBody(
List.of(
// underpriced eip1559 transaction
new TransactionTestFixture()
.maxFeePerGas(Optional.of(Wei.of(1L)))
.maxPriorityFeePerGas(Optional.of(Wei.of(10L)))
.type(TransactionType.EIP1559)
.createTransaction(keyPair)),
Collections.emptyList()));

assertThat(blockBodyValidator.validateTransactionGasPrice(block)).isFalse();
}
}