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

Fix and restore disabled block creation unit tests #7331

Merged
merged 1 commit into from
Jul 16, 2024
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
Fix and restore disabled block creation unit tests
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Jul 16, 2024
commit 475fb5a7d8e8879e7497d7c98c8e2f356631038d
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
import static org.mockito.Mockito.when;

import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SECPPrivateKey;
import org.hyperledger.besu.crypto.SignatureAlgorithm;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.BLSPublicKey;
Expand Down Expand Up @@ -73,13 +74,18 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidator;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidatorFactory;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.mainnet.WithdrawalsProcessor;
import org.hyperledger.besu.ethereum.mainnet.feemarket.CancunFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.requests.DepositRequestProcessor;
import org.hyperledger.besu.ethereum.mainnet.requests.DepositRequestValidator;
import org.hyperledger.besu.ethereum.mainnet.requests.ProcessRequestContext;
import org.hyperledger.besu.ethereum.mainnet.requests.RequestProcessorCoordinator;
import org.hyperledger.besu.ethereum.mainnet.requests.RequestsValidatorCoordinator;
import org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.log.Log;
import org.hyperledger.besu.evm.log.LogTopic;
Expand All @@ -89,20 +95,31 @@
import java.math.BigInteger;
import java.time.Clock;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt64;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
abstract class AbstractBlockCreatorTest {
private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
Suppliers.memoize(SignatureAlgorithmFactory::getInstance);
private static final SECPPrivateKey PRIVATE_KEY1 =
SIGNATURE_ALGORITHM
.get()
.createPrivateKey(
Bytes32.fromHexString(
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"));
private static final KeyPair KEYS1 =
new KeyPair(PRIVATE_KEY1, SIGNATURE_ALGORITHM.get().createPublicKey(PRIVATE_KEY1));

@Mock private WithdrawalsProcessor withdrawalsProcessor;
protected EthScheduler ethScheduler = new DeterministicEthScheduler();

Expand Down Expand Up @@ -301,10 +318,8 @@ void withNoProcessorAndWithdrawals_WithdrawalsAreNotProcessed() {
assertThat(blockCreationResult.getBlock().getBody().getWithdrawals()).isEmpty();
}

@Disabled
@Test
public void computesGasUsageFromIncludedTransactions() {
final KeyPair senderKeys = SignatureAlgorithmFactory.getInstance().generateKeyPair();
final AbstractBlockCreator blockCreator = blockCreatorWithBlobGasSupport();
BlobTestFixture blobTestFixture = new BlobTestFixture();
BlobsWithCommitments bwc = blobTestFixture.createBlobsWithCommitments(6);
Expand All @@ -313,13 +328,14 @@ public void computesGasUsageFromIncludedTransactions() {
ttf.to(Optional.of(Address.ZERO))
.type(TransactionType.BLOB)
.chainId(Optional.of(BigInteger.valueOf(42)))
.gasLimit(21000)
.maxFeePerGas(Optional.of(Wei.of(15)))
.maxFeePerBlobGas(Optional.of(Wei.of(128)))
.maxPriorityFeePerGas(Optional.of(Wei.of(1)))
.versionedHashes(Optional.of(bwc.getVersionedHashes()))
.createTransaction(senderKeys);
.blobsWithCommitments(Optional.of(bwc))
.createTransaction(KEYS1);

ttf.blobsWithCommitments(Optional.of(bwc));
final BlockCreationResult blockCreationResult =
blockCreator.createBlock(
Optional.of(List.of(fullOfBlobs)),
Expand All @@ -336,13 +352,17 @@ public void computesGasUsageFromIncludedTransactions() {
}

private AbstractBlockCreator blockCreatorWithBlobGasSupport() {
final var alwaysValidTransactionValidatorFactory = mock(TransactionValidatorFactory.class);
when(alwaysValidTransactionValidatorFactory.get())
.thenReturn(new AlwaysValidTransactionValidator());
final ProtocolSpecAdapters protocolSpecAdapters =
ProtocolSpecAdapters.create(
0,
specBuilder -> {
specBuilder.feeMarket(new CancunFeeMarket(0, Optional.empty()));
specBuilder.isReplayProtectionSupported(true);
specBuilder.withdrawalsProcessor(withdrawalsProcessor);
specBuilder.transactionValidatorFactoryBuilder(
(evm, gasLimitCalculator, feeMarket) -> alwaysValidTransactionValidatorFactory);
return specBuilder;
});
return createBlockCreator(protocolSpecAdapters);
Expand All @@ -356,16 +376,19 @@ private AbstractBlockCreator blockCreatorWithWithdrawalsProcessor() {
}

private AbstractBlockCreator blockCreatorWithoutWithdrawalsProcessor() {
return createBlockCreator(new ProtocolSpecAdapters(Map.of()));
final ProtocolSpecAdapters protocolSpecAdapters =
ProtocolSpecAdapters.create(0, specBuilder -> specBuilder.withdrawalsProcessor(null));
return createBlockCreator(protocolSpecAdapters);
}

private AbstractBlockCreator createBlockCreator(final ProtocolSpecAdapters protocolSpecAdapters) {
final GenesisConfigOptions genesisConfigOptions = GenesisConfigFile.DEFAULT.getConfigOptions();

final var genesisConfigFile = GenesisConfigFile.fromResource("/block-creation-genesis.json");
final ExecutionContextTestFixture executionContextTestFixture =
ExecutionContextTestFixture.builder()
ExecutionContextTestFixture.builder(genesisConfigFile)
.protocolSchedule(
new ProtocolScheduleBuilder(
genesisConfigOptions,
genesisConfigFile.getConfigOptions(),
BigInteger.valueOf(42),
protocolSpecAdapters,
PrivacyParameters.DEFAULT,
Expand Down Expand Up @@ -452,4 +475,24 @@ protected BlockHeader createFinalBlockHeader(final SealableBlockHeader sealableB
.buildBlockHeader();
}
}

static class AlwaysValidTransactionValidator implements TransactionValidator {

@Override
public ValidationResult<TransactionInvalidReason> validate(
final Transaction transaction,
final Optional<Wei> baseFee,
final Optional<Wei> blobBaseFee,
final TransactionValidationParams transactionValidationParams) {
return ValidationResult.valid();
}

@Override
public ValidationResult<TransactionInvalidReason> validateForSender(
final Transaction transaction,
final Account sender,
final TransactionValidationParams validationParams) {
return ValidationResult.valid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
import java.time.ZoneId;
import java.util.function.Function;

import org.junit.jupiter.api.Disabled;

@Disabled(
"disabled since it's flaky with a timeout see https://github.com/hyperledger/besu/issues/6850")
public class LegacyFeeMarketBlockTransactionSelectorTest
extends AbstractBlockTransactionSelectorTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.mockito.Mockito.when;

import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
Expand All @@ -38,6 +37,7 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.difficulty.fixed.FixedDifficultyCalculators;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionBroadcaster;
Expand Down Expand Up @@ -75,31 +75,33 @@ class PoWBlockCreatorTest extends AbstractBlockCreatorTest {
private static final long BLOCK_1_TIMESTAMP = Long.parseUnsignedLong("55ba4224", 16);

private static final long BLOCK_1_NONCE = Long.parseLong("539bd4979fef1ec4", 16);
private static final long FIXED_DIFFICULTY_NONCE = 26;

private static final Bytes BLOCK_1_EXTRA_DATA =
Bytes.fromHexString("0x476574682f76312e302e302f6c696e75782f676f312e342e32");
private final MetricsSystem metricsSystem = new NoOpMetricsSystem();

@Test
void createMainnetBlock1() throws IOException {
final GenesisConfigOptions genesisConfigOptions = GenesisConfigFile.DEFAULT.getConfigOptions();
final var genesisConfigFile = GenesisConfigFile.mainnet();

final MiningParameters miningParameters = createMiningParameters(BLOCK_1_NONCE);

final ExecutionContextTestFixture executionContextTestFixture =
ExecutionContextTestFixture.builder()
ExecutionContextTestFixture.builder(genesisConfigFile)
.protocolSchedule(
new ProtocolScheduleBuilder(
genesisConfigOptions,
genesisConfigFile.getConfigOptions(),
BigInteger.valueOf(42),
ProtocolSpecAdapters.create(0, Function.identity()),
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningParameters.MINING_DISABLED,
miningParameters,
new BadBlockManager())
.createProtocolSchedule())
.build();

final MiningParameters miningParameters = createMiningParameters();

final PoWSolver solver =
new PoWSolver(
miningParameters,
Expand Down Expand Up @@ -136,26 +138,31 @@ void createMainnetBlock1() throws IOException {

@Test
void createMainnetBlock1_fixedDifficulty1() {
final GenesisConfigOptions genesisConfigOptions =
GenesisConfigFile.fromConfig("{\"config\": {\"ethash\": {\"fixeddifficulty\":1}}}")
.getConfigOptions();
final var genesisConfigFile =
GenesisConfigFile.fromResource("/block-creation-fixed-difficulty-genesis.json");

final MiningParameters miningParameters = createMiningParameters(FIXED_DIFFICULTY_NONCE);

final ExecutionContextTestFixture executionContextTestFixture =
ExecutionContextTestFixture.builder()
ExecutionContextTestFixture.builder(genesisConfigFile)
.protocolSchedule(
new ProtocolScheduleBuilder(
genesisConfigOptions,
genesisConfigFile.getConfigOptions(),
BigInteger.valueOf(42),
ProtocolSpecAdapters.create(0, Function.identity()),
ProtocolSpecAdapters.create(
0,
specBuilder ->
specBuilder.difficultyCalculator(
FixedDifficultyCalculators.calculator(
genesisConfigFile.getConfigOptions()))),
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningParameters.MINING_DISABLED,
miningParameters,
new BadBlockManager())
.createProtocolSchedule())
.build();

final MiningParameters miningParameters = createMiningParameters();

final PoWSolver solver =
new PoWSolver(
miningParameters,
Expand Down Expand Up @@ -184,24 +191,31 @@ void createMainnetBlock1_fixedDifficulty1() {

@Test
void rewardBeneficiary_zeroReward_skipZeroRewardsFalse() {
final GenesisConfigOptions genesisConfigOptions =
GenesisConfigFile.fromConfig("{\"config\": {\"ethash\": {\"fixeddifficulty\":1}}}")
.getConfigOptions();
final var genesisConfigFile =
GenesisConfigFile.fromResource("/block-creation-fixed-difficulty-genesis.json");

final MiningParameters miningParameters = createMiningParameters(FIXED_DIFFICULTY_NONCE);

ProtocolSchedule protocolSchedule =
new ProtocolScheduleBuilder(
genesisConfigOptions,
genesisConfigFile.getConfigOptions(),
BigInteger.valueOf(42),
ProtocolSpecAdapters.create(0, Function.identity()),
ProtocolSpecAdapters.create(
0,
specBuilder ->
specBuilder.difficultyCalculator(
FixedDifficultyCalculators.calculator(
genesisConfigFile.getConfigOptions()))),
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningParameters.MINING_DISABLED,
miningParameters,
new BadBlockManager())
.createProtocolSchedule();
final ExecutionContextTestFixture executionContextTestFixture =
ExecutionContextTestFixture.builder().protocolSchedule(protocolSchedule).build();

final MiningParameters miningParameters = createMiningParameters();
ExecutionContextTestFixture.builder(genesisConfigFile)
.protocolSchedule(protocolSchedule)
.build();

final PoWSolver solver =
new PoWSolver(
Expand Down Expand Up @@ -253,24 +267,31 @@ void rewardBeneficiary_zeroReward_skipZeroRewardsFalse() {

@Test
void rewardBeneficiary_zeroReward_skipZeroRewardsTrue() {
final GenesisConfigOptions genesisConfigOptions =
GenesisConfigFile.fromConfig("{\"config\": {\"ethash\": {\"fixeddifficulty\":1}}}")
.getConfigOptions();
final var genesisConfigFile =
GenesisConfigFile.fromResource("/block-creation-fixed-difficulty-genesis.json");

final MiningParameters miningParameters = createMiningParameters(FIXED_DIFFICULTY_NONCE);

ProtocolSchedule protocolSchedule =
new ProtocolScheduleBuilder(
genesisConfigOptions,
genesisConfigFile.getConfigOptions(),
BigInteger.valueOf(42),
ProtocolSpecAdapters.create(0, Function.identity()),
ProtocolSpecAdapters.create(
0,
specBuilder ->
specBuilder.difficultyCalculator(
FixedDifficultyCalculators.calculator(
genesisConfigFile.getConfigOptions()))),
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningParameters.MINING_DISABLED,
miningParameters,
new BadBlockManager())
.createProtocolSchedule();
final ExecutionContextTestFixture executionContextTestFixture =
ExecutionContextTestFixture.builder().protocolSchedule(protocolSchedule).build();

final MiningParameters miningParameters = createMiningParameters();
ExecutionContextTestFixture.builder(genesisConfigFile)
.protocolSchedule(protocolSchedule)
.build();

final PoWSolver solver =
new PoWSolver(
Expand Down Expand Up @@ -348,11 +369,12 @@ private TransactionPool createTransactionPool(
return transactionPool;
}

private MiningParameters createMiningParameters() {
private MiningParameters createMiningParameters(final long nonce) {
return ImmutableMiningParameters.builder()
.mutableInitValues(
MutableInitValues.builder()
.nonceGenerator(Lists.newArrayList(BLOCK_1_NONCE))
.nonceGenerator(Lists.newArrayList(nonce))
// .nonceGenerator(new IncrementingNonceGenerator(0))
.extraData(BLOCK_1_EXTRA_DATA)
.minTransactionGasPrice(Wei.ONE)
.coinbase(BLOCK_1_COINBASE)
Expand Down
Loading
Loading