From 0e36f661e85fca4a2bb262354174e3a4829812cd Mon Sep 17 00:00:00 2001 From: Gabriel Fukushima Date: Thu, 23 Mar 2023 17:17:42 +1100 Subject: [PATCH] getByBlockNumber replaced by getByBlockHeader on tests (#5259) * Replace getByBlockNumber by getByBlockHeader Signed-off-by: Gabriel Fukushima * Replace getByBlockNumber by getByBlockHeader Signed-off-by: Gabriel Fukushima * Replace BlockDataGenerator by BlockHeaderTestFixture Signed-off-by: Gabriel Fukushima * Replace BlockDataGenerator by BlockHeaderTestFixture Signed-off-by: Gabriel Fukushima * Add final to method param Signed-off-by: Gabriel Fukushima * Replace BlockDataGEnerator by BlockHeaderTestFixture Signed-off-by: Gabriel Fukushima * Replace BlockDataGenerator by BlockHeaderTestFixture Signed-off-by: Gabriel Fukushima * Remove BlockDataGenerator Signed-off-by: Gabriel Fukushima --------- Signed-off-by: Gabriel Fukushima --- .../hyperledger/besu/PrivacyReorgTest.java | 2 +- .../org/hyperledger/besu/PrivacyTest.java | 8 +- .../clique/CliqueProtocolScheduleTest.java | 14 +- .../CombinedProtocolScheduleFactory.java | 5 +- .../CombinedProtocolScheduleFactoryTest.java | 9 +- .../BaseBftProtocolScheduleBuilderTest.java | 19 ++- .../ibft/IbftProtocolScheduleTest.java | 16 +-- .../merge/MergeProtocolScheduleTest.java | 12 +- .../merge/TransitionProtocolScheduleTest.java | 2 - .../qbft/QbftProtocolScheduleTest.java | 16 +-- .../internal/methods/EthFeeHistoryTest.java | 21 +-- .../methods/EthGetTransactionReceiptTest.java | 12 +- .../AbstractBlockTransactionSelectorTest.java | 8 +- .../bonsai/AbstractIsolationTests.java | 7 +- .../fixed/FixedProtocolScheduleTest.java | 16 ++- .../mainnet/MainnetProtocolScheduleTest.java | 129 ++++++++++++------ .../mainnet/MutableProtocolScheduleTest.java | 2 +- .../mainnet/ProtocolScheduleBuilderTest.java | 46 ++++--- .../PrivateStorageMigrationTest.java | 3 +- .../AbstractBlockPropagationManagerTest.java | 18 ++- .../backwardsync/BackwardSyncContextTest.java | 11 +- .../AbstractTransactionPoolTest.java | 3 +- .../mainnet/DifficultyCalculatorTests.java | 2 +- 23 files changed, 231 insertions(+), 150 deletions(-) diff --git a/besu/src/test/java/org/hyperledger/besu/PrivacyReorgTest.java b/besu/src/test/java/org/hyperledger/besu/PrivacyReorgTest.java index 49a568547f..7a2d7747ac 100644 --- a/besu/src/test/java/org/hyperledger/besu/PrivacyReorgTest.java +++ b/besu/src/test/java/org/hyperledger/besu/PrivacyReorgTest.java @@ -404,7 +404,7 @@ public class PrivacyReorgTest { final Block block) { besuController .getProtocolSchedule() - .getByBlockNumber(blockchain.getChainHeadBlockNumber()) + .getByBlockHeader(blockchain.getChainHeadHeader()) .getBlockImporter() .importBlock(protocolContext, block, HeaderValidationMode.NONE); } diff --git a/besu/src/test/java/org/hyperledger/besu/PrivacyTest.java b/besu/src/test/java/org/hyperledger/besu/PrivacyTest.java index e357eb3346..2a891d26fa 100644 --- a/besu/src/test/java/org/hyperledger/besu/PrivacyTest.java +++ b/besu/src/test/java/org/hyperledger/besu/PrivacyTest.java @@ -29,6 +29,8 @@ import org.hyperledger.besu.crypto.NodeKeyUtils; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.enclave.EnclaveFactory; import org.hyperledger.besu.ethereum.GasLimitCalculator; +import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider; import org.hyperledger.besu.ethereum.core.MiningParameters; import org.hyperledger.besu.ethereum.core.PrivacyParameters; @@ -145,8 +147,12 @@ public class PrivacyTest { final BesuController besuController, final Address defaultPrivacy) { return besuController .getProtocolSchedule() - .getByBlockNumber(1) + .getByBlockHeader(blockHeader(0)) .getPrecompileContractRegistry() .get(defaultPrivacy); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java index efc5b7e287..ce483c1ecf 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java @@ -57,10 +57,10 @@ public class CliqueProtocolScheduleTest { final ProtocolSchedule protocolSchedule = CliqueProtocolSchedule.create(config, NODE_KEY, false, EvmConfiguration.DEFAULT); - final ProtocolSpec homesteadSpec = protocolSchedule.getByBlockNumber(1); - final ProtocolSpec tangerineWhistleSpec = protocolSchedule.getByBlockNumber(2); - final ProtocolSpec spuriousDragonSpec = protocolSchedule.getByBlockNumber(3); - final ProtocolSpec byzantiumSpec = protocolSchedule.getByBlockNumber(1035301); + final ProtocolSpec homesteadSpec = protocolSchedule.getByBlockHeader(blockHeader(1)); + final ProtocolSpec tangerineWhistleSpec = protocolSchedule.getByBlockHeader(blockHeader(2)); + final ProtocolSpec spuriousDragonSpec = protocolSchedule.getByBlockHeader(blockHeader(3)); + final ProtocolSpec byzantiumSpec = protocolSchedule.getByBlockHeader(blockHeader(1035301)); assertThat(homesteadSpec.equals(tangerineWhistleSpec)).isFalse(); assertThat(tangerineWhistleSpec.equals(spuriousDragonSpec)).isFalse(); @@ -75,7 +75,7 @@ public class CliqueProtocolScheduleTest { NODE_KEY, false, EvmConfiguration.DEFAULT) - .getByBlockNumber(0); + .getByBlockHeader(blockHeader(0)); assertThat(homestead.getName()).isEqualTo("Frontier"); assertThat(homestead.getBlockReward()).isEqualTo(Wei.ZERO); @@ -180,4 +180,8 @@ public class CliqueProtocolScheduleTest { .getBlockHeaderValidator() .validateHeader(blockHeader, parentBlockHeader, null, HeaderValidationMode.LIGHT); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactory.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactory.java index 549664ad34..1cb770af67 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactory.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactory.java @@ -36,10 +36,11 @@ public class CombinedProtocolScheduleFactory { * @param chainId the chain id * @return the protocol schedule */ - public ProtocolSchedule create( + public BftProtocolSchedule create( final NavigableSet> forkSpecs, final Optional chainId) { - final MutableProtocolSchedule combinedProtocolSchedule = new MutableProtocolSchedule(chainId); + final BftProtocolSchedule combinedProtocolSchedule = + new BftProtocolSchedule(new MutableProtocolSchedule(chainId)); for (ForkSpec spec : forkSpecs) { checkState( spec.getValue() instanceof MutableProtocolSchedule, diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactoryTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactoryTest.java index fd845a66a6..e4341f7d69 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactoryTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactoryTest.java @@ -20,7 +20,6 @@ import org.hyperledger.besu.config.GenesisConfigOptions; import org.hyperledger.besu.config.StubGenesisConfigOptions; import org.hyperledger.besu.ethereum.core.BlockNumberStreamingProtocolSchedule; import org.hyperledger.besu.ethereum.core.PrivacyParameters; -import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters; @@ -58,9 +57,7 @@ public class CombinedProtocolScheduleFactoryTest { final BlockNumberStreamingProtocolSchedule combinedProtocolSchedule = new BlockNumberStreamingProtocolSchedule( - (MutableProtocolSchedule) - combinedProtocolScheduleFactory.create( - consensusSchedule, Optional.of(BigInteger.TEN))); + combinedProtocolScheduleFactory.create(consensusSchedule, Optional.of(BigInteger.TEN))); assertThat(combinedProtocolSchedule.getByBlockNumber(0L).getName()).isEqualTo("Frontier"); assertThat(combinedProtocolSchedule.getByBlockNumber(0L)) @@ -101,9 +98,7 @@ public class CombinedProtocolScheduleFactoryTest { final BlockNumberStreamingProtocolSchedule combinedProtocolSchedule = new BlockNumberStreamingProtocolSchedule( - (MutableProtocolSchedule) - combinedProtocolScheduleFactory.create( - consensusSchedule, Optional.of(BigInteger.TEN))); + combinedProtocolScheduleFactory.create(consensusSchedule, Optional.of(BigInteger.TEN))); // consensus schedule 1 assertThat(combinedProtocolSchedule.getByBlockNumber(0L).getName()).isEqualTo("Frontier"); diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilderTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilderTest.java index 75482b8ec2..1bf9330fdd 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilderTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilderTest.java @@ -28,6 +28,7 @@ import org.hyperledger.besu.consensus.common.ForksSchedule; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.BlockNumberStreamingProtocolSchedule; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator; @@ -60,7 +61,7 @@ public class BaseBftProtocolScheduleBuilderTest { final ProtocolSchedule schedule = createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions))); - final ProtocolSpec spec = schedule.getByBlockNumber(1); + final ProtocolSpec spec = schedule.getByBlockHeader(blockHeader(1)); assertThat(spec.getBlockReward()).isEqualTo(Wei.of(arbitraryBlockReward)); assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(mock(BlockHeader.class))) @@ -76,7 +77,7 @@ public class BaseBftProtocolScheduleBuilderTest { final ProtocolSchedule schedule = createProtocolSchedule(List.of(new ForkSpec<>(0, configOptions))); - final ProtocolSpec spec = schedule.getByBlockNumber(1); + final ProtocolSpec spec = schedule.getByBlockHeader(blockHeader(1)); final Address headerCoinbase = Address.fromHexString("0x123"); final BlockHeader header = mock(BlockHeader.class); @@ -131,7 +132,7 @@ public class BaseBftProtocolScheduleBuilderTest { // Check initial config for (int i = 0; i < 2; i++) { - final ProtocolSpec spec = schedule.getByBlockNumber(i); + final ProtocolSpec spec = schedule.getByBlockHeader(blockHeader(i)); final Address expectedBeneficiary = initialBeneficiaryIsEmpty ? headerCoinbase : beneficiary1; assertThat(spec.getBlockReward()).isEqualTo(Wei.of(BigInteger.valueOf(3))); assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(header)) @@ -140,7 +141,7 @@ public class BaseBftProtocolScheduleBuilderTest { // Check fork1 for (int i = 2; i < 5; i++) { - final ProtocolSpec spec = schedule.getByBlockNumber(i); + final ProtocolSpec spec = schedule.getByBlockHeader(blockHeader(i)); final Address expectedBeneficiary = initialBeneficiaryIsEmpty ? beneficiary2 : headerCoinbase; assertThat(spec.getBlockReward()).isEqualTo(Wei.of(BigInteger.valueOf(2))); assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(header)) @@ -149,7 +150,7 @@ public class BaseBftProtocolScheduleBuilderTest { // Check fork2 for (int i = 5; i < 8; i++) { - final ProtocolSpec spec = schedule.getByBlockNumber(i); + final ProtocolSpec spec = schedule.getByBlockHeader(blockHeader(i)); final Address expectedBeneficiary = initialBeneficiaryIsEmpty ? headerCoinbase : beneficiary3; assertThat(spec.getBlockReward()).isEqualTo(Wei.of(BigInteger.valueOf(1))); assertThat(spec.getMiningBeneficiaryCalculator().calculateBeneficiary(header)) @@ -219,9 +220,9 @@ public class BaseBftProtocolScheduleBuilderTest { new ForkSpec<>(transitionBlock, blockRewardTransition)))); assertThat(schedule.streamMilestoneBlocks().count()).isEqualTo(2); - assertThat(schedule.getByBlockNumber(0).getBlockReward()) + assertThat(schedule.getByBlockHeader(blockHeader(0)).getBlockReward()) .isEqualTo(Wei.of(arbitraryBlockReward)); - assertThat(schedule.getByBlockNumber(transitionBlock).getBlockReward()) + assertThat(schedule.getByBlockHeader(blockHeader(transitionBlock)).getBlockReward()) .isEqualTo(Wei.of(forkBlockReward)); } @@ -256,4 +257,8 @@ public class BaseBftProtocolScheduleBuilderTest { return bftConfig; } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleTest.java b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleTest.java index d49d64780f..7960bbe403 100644 --- a/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleTest.java +++ b/consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/IbftProtocolScheduleTest.java @@ -41,7 +41,6 @@ import org.hyperledger.besu.ethereum.core.BlockNumberStreamingProtocolSchedule; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.core.Util; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; -import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -94,14 +93,13 @@ public class IbftProtocolScheduleTest { private BlockNumberStreamingProtocolSchedule createProtocolSchedule( final GenesisConfigOptions genesisConfig, final List> forks) { return new BlockNumberStreamingProtocolSchedule( - (MutableProtocolSchedule) - IbftProtocolScheduleBuilder.create( - genesisConfig, - new ForksSchedule<>(forks), - PrivacyParameters.DEFAULT, - false, - bftExtraDataCodec, - EvmConfiguration.DEFAULT)); + IbftProtocolScheduleBuilder.create( + genesisConfig, + new ForksSchedule<>(forks), + PrivacyParameters.DEFAULT, + false, + bftExtraDataCodec, + EvmConfiguration.DEFAULT)); } private boolean validateHeader( diff --git a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java index bafdf0b052..e2a8a25733 100644 --- a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java +++ b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java @@ -19,6 +19,8 @@ import static org.assertj.core.api.Assertions.assertThat; import org.hyperledger.besu.config.GenesisConfigFile; import org.hyperledger.besu.config.GenesisConfigOptions; import org.hyperledger.besu.datatypes.Wei; +import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; import org.hyperledger.besu.evm.operation.PrevRanDaoOperation; @@ -39,8 +41,8 @@ public class MergeProtocolScheduleTest { final GenesisConfigOptions config = GenesisConfigFile.fromConfig(jsonInput).getConfigOptions(); final ProtocolSchedule protocolSchedule = MergeProtocolSchedule.create(config, false); - final ProtocolSpec homesteadSpec = protocolSchedule.getByBlockNumber(1); - final ProtocolSpec londonSpec = protocolSchedule.getByBlockNumber(1559); + final ProtocolSpec homesteadSpec = protocolSchedule.getByBlockHeader(blockHeader(1)); + final ProtocolSpec londonSpec = protocolSchedule.getByBlockHeader(blockHeader(1559)); assertThat(homesteadSpec).isNotEqualTo(londonSpec); assertThat(homesteadSpec.getFeeMarket().implementsBaseFee()).isFalse(); @@ -51,7 +53,7 @@ public class MergeProtocolScheduleTest { public void parametersAlignWithMainnetWithAdjustments() { final ProtocolSpec london = MergeProtocolSchedule.create(GenesisConfigFile.DEFAULT.getConfigOptions(), false) - .getByBlockNumber(0); + .getByBlockHeader(blockHeader(0)); assertThat(london.getName()).isEqualTo("Frontier"); assertThat(london.getBlockReward()).isEqualTo(Wei.ZERO); @@ -60,4 +62,8 @@ public class MergeProtocolScheduleTest { var op = london.getEvm().getOperationsUnsafe()[0x44]; assertThat(op).isInstanceOf(PrevRanDaoOperation.class); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/TransitionProtocolScheduleTest.java b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/TransitionProtocolScheduleTest.java index ece9ab706e..c532d8887b 100644 --- a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/TransitionProtocolScheduleTest.java +++ b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/TransitionProtocolScheduleTest.java @@ -47,10 +47,8 @@ public class TransitionProtocolScheduleTest { @Mock MergeContext mergeContext; @Mock ProtocolSchedule preMergeProtocolSchedule; @Mock ProtocolSchedule postMergeProtocolSchedule; - @Mock TimestampSchedule timestampSchedule; @Mock BlockHeader blockHeader; - private static final Difficulty TTD = Difficulty.of(100L); private static final long BLOCK_NUMBER = 29L; private static final long TIMESTAMP = 1L; diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleTest.java index f0e0bfd9a6..fb3aa2ce7a 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/QbftProtocolScheduleTest.java @@ -36,7 +36,6 @@ import org.hyperledger.besu.ethereum.core.BlockNumberStreamingProtocolSchedule; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.core.Util; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; -import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -127,14 +126,13 @@ public class QbftProtocolScheduleTest { private BlockNumberStreamingProtocolSchedule createProtocolSchedule( final GenesisConfigOptions genesisConfig, final List> forks) { return new BlockNumberStreamingProtocolSchedule( - (MutableProtocolSchedule) - QbftProtocolScheduleBuilder.create( - genesisConfig, - new ForksSchedule<>(forks), - PrivacyParameters.DEFAULT, - false, - bftExtraDataCodec, - EvmConfiguration.DEFAULT)); + QbftProtocolScheduleBuilder.create( + genesisConfig, + new ForksSchedule<>(forks), + PrivacyParameters.DEFAULT, + false, + bftExtraDataCodec, + EvmConfiguration.DEFAULT)); } private boolean validateHeader( diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java index f0534ae848..5355e0d1ca 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthFeeHistoryTest.java @@ -106,9 +106,6 @@ public class EthFeeHistoryTest { @Test public void cantGetBlockHigherThanChainHead() { - final ProtocolSpec londonSpec = mock(ProtocolSpec.class); - when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5)); - when(protocolSchedule.getForNextBlockHeader(any(), anyLong())).thenReturn(londonSpec); assertThat( ((JsonRpcErrorResponse) feeHistoryRequest("0x2", "11", new double[] {100.0})) .getError()) @@ -117,9 +114,6 @@ public class EthFeeHistoryTest { @Test public void blockCountBounds() { - final ProtocolSpec londonSpec = mock(ProtocolSpec.class); - when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5)); - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(londonSpec); assertThat( ((JsonRpcErrorResponse) feeHistoryRequest("0x0", "latest", new double[] {100.0})) .getError()) @@ -134,7 +128,10 @@ public class EthFeeHistoryTest { public void doesntGoPastChainHeadWithHighBlockCount() { final ProtocolSpec londonSpec = mock(ProtocolSpec.class); when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5)); - when(protocolSchedule.getForNextBlockHeader(any(), anyLong())).thenReturn(londonSpec); + when(protocolSchedule.getForNextBlockHeader( + eq(blockchain.getChainHeadHeader()), + eq(blockchain.getChainHeadHeader().getTimestamp()))) + .thenReturn(londonSpec); final FeeHistory.FeeHistoryResult result = (ImmutableFeeHistoryResult) ((JsonRpcSuccessResponse) feeHistoryRequest("0x14", "latest")).getResult(); @@ -148,7 +145,10 @@ public class EthFeeHistoryTest { public void correctlyHandlesForkBlock() { final ProtocolSpec londonSpec = mock(ProtocolSpec.class); when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(11)); - when(protocolSchedule.getForNextBlockHeader(any(), anyLong())).thenReturn(londonSpec); + when(protocolSchedule.getForNextBlockHeader( + eq(blockchain.getChainHeadHeader()), + eq(blockchain.getChainHeadHeader().getTimestamp()))) + .thenReturn(londonSpec); final FeeHistory.FeeHistoryResult result = (FeeHistory.FeeHistoryResult) ((JsonRpcSuccessResponse) feeHistoryRequest("0x1", "latest")).getResult(); @@ -160,13 +160,16 @@ public class EthFeeHistoryTest { public void allZeroPercentilesForZeroBlock() { final ProtocolSpec londonSpec = mock(ProtocolSpec.class); when(londonSpec.getFeeMarket()).thenReturn(FeeMarket.london(5)); - when(protocolSchedule.getForNextBlockHeader(any(), anyLong())).thenReturn(londonSpec); final BlockDataGenerator.BlockOptions blockOptions = BlockDataGenerator.BlockOptions.create(); blockOptions.hasTransactions(false); blockOptions.setParentHash(blockchain.getChainHeadHash()); blockOptions.setBlockNumber(11); final Block emptyBlock = gen.block(blockOptions); blockchain.appendBlock(emptyBlock, gen.receipts(emptyBlock)); + when(protocolSchedule.getForNextBlockHeader( + eq(blockchain.getChainHeadHeader()), + eq(blockchain.getChainHeadHeader().getTimestamp()))) + .thenReturn(londonSpec); final FeeHistory.FeeHistoryResult result = (FeeHistory.FeeHistoryResult) ((JsonRpcSuccessResponse) feeHistoryRequest("0x1", "latest", new double[] {100.0})) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java index 615bf3edef..c5d19bcce1 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java @@ -33,6 +33,7 @@ import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.api.query.TransactionReceiptWithMetadata; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.Transaction; import org.hyperledger.besu.ethereum.core.TransactionReceipt; import org.hyperledger.besu.ethereum.mainnet.PoWHasher; @@ -50,7 +51,6 @@ import org.apache.tuweni.units.bigints.UInt256s; import org.junit.Test; public class EthGetTransactionReceiptTest { - private final TransactionReceipt statusReceipt = new TransactionReceipt(1, 12, Collections.emptyList(), Optional.empty()); private final Hash stateRoot = @@ -163,7 +163,7 @@ public class EthGetTransactionReceiptTest { when(blockchain.headBlockNumber()).thenReturn(1L); when(blockchain.transactionReceiptByTransactionHash(receiptHash)) .thenReturn(Optional.of(statusReceiptWithMetadata)); - when(protocolSchedule.getByBlockNumber(1)).thenReturn(statusTransactionTypeSpec); + when(protocolSchedule.getByBlockHeader(blockHeader(1))).thenReturn(statusTransactionTypeSpec); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) ethGetTransactionReceipt.response(request); @@ -179,7 +179,7 @@ public class EthGetTransactionReceiptTest { when(blockchain.headBlockNumber()).thenReturn(1L); when(blockchain.transactionReceiptByTransactionHash(receiptHash)) .thenReturn(Optional.of(rootReceiptWithMetaData)); - when(protocolSchedule.getByBlockNumber(1)).thenReturn(rootTransactionTypeSpec); + when(protocolSchedule.getByBlockHeader(blockHeader(1))).thenReturn(rootTransactionTypeSpec); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) ethGetTransactionReceipt.response(request); @@ -200,7 +200,7 @@ public class EthGetTransactionReceiptTest { statusReceipt, transaction1559, hash, 1, 2, Optional.of(baseFee), blockHash, 4); when(blockchain.transactionReceiptByTransactionHash(receiptHash)) .thenReturn(Optional.of(transactionReceiptWithMetadata)); - when(protocolSchedule.getByBlockNumber(1)).thenReturn(rootTransactionTypeSpec); + when(protocolSchedule.getByBlockHeader(blockHeader(1))).thenReturn(rootTransactionTypeSpec); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) ethGetTransactionReceipt.response(request); @@ -215,4 +215,8 @@ public class EthGetTransactionReceiptTest { baseFee.add(transaction1559.getMaxPriorityFeePerGas().get()), transaction1559.getMaxFeePerGas().get())); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockTransactionSelectorTest.java b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockTransactionSelectorTest.java index 64861c8510..cbac5409a0 100644 --- a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockTransactionSelectorTest.java +++ b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockTransactionSelectorTest.java @@ -32,6 +32,7 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.AddressHelpers; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.Difficulty; import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider; import org.hyperledger.besu.ethereum.core.MutableWorldState; @@ -77,7 +78,6 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public abstract class AbstractBlockTransactionSelectorTest { - protected static final KeyPair keyPair = SignatureAlgorithmFactory.getInstance().generateKeyPair(); protected final MetricsSystem metricsSystem = new NoOpMetricsSystem(); @@ -124,7 +124,7 @@ public abstract class AbstractBlockTransactionSelectorTest { FixedDifficultyProtocolSchedule.create( GenesisConfigFile.development().getConfigOptions(), EvmConfiguration.DEFAULT); final MainnetTransactionProcessor mainnetTransactionProcessor = - protocolSchedule.getByBlockNumber(0).getTransactionProcessor(); + protocolSchedule.getByBlockHeader(blockHeader(0)).getTransactionProcessor(); // The block should fit 5 transactions only final ProcessableBlockHeader blockHeader = createBlock(5000); @@ -522,4 +522,8 @@ public abstract class AbstractBlockTransactionSelectorTest { any(), any(), any(), eq(tx), any(), any(), anyBoolean(), any(), any())) .thenReturn(TransactionProcessingResult.invalid(ValidationResult.invalid(invalidReason))); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/AbstractIsolationTests.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/AbstractIsolationTests.java index 7daf5a3f43..0ca475e9b9 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/AbstractIsolationTests.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/AbstractIsolationTests.java @@ -35,6 +35,7 @@ import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeaderBuilder; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.Difficulty; import org.hyperledger.besu.ethereum.core.MutableWorldState; import org.hyperledger.besu.ethereum.core.SealableBlockHeader; @@ -241,10 +242,14 @@ public abstract class AbstractIsolationTests { protected BlockProcessingResult executeBlock(final MutableWorldState ws, final Block block) { var res = protocolSchedule - .getByBlockNumber(0) + .getByBlockHeader(blockHeader(0)) .getBlockProcessor() .processBlock(blockchain, ws, block); blockchain.appendBlock(block, res.getReceipts()); return res; } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java index ea311a1ff5..ea694b3396 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java @@ -39,21 +39,21 @@ public class FixedProtocolScheduleTest { assertThat( schedule - .getByBlockNumber(0) + .getByBlockHeader(blockHeader(0)) .getDifficultyCalculator() .nextDifficulty(1, parentHeader, null)) .isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY); assertThat( schedule - .getByBlockNumber(500) + .getByBlockHeader(blockHeader(500)) .getDifficultyCalculator() .nextDifficulty(1, parentHeader, null)) .isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY); assertThat( schedule - .getByBlockNumber(500_000) + .getByBlockHeader(blockHeader(500_000)) .getDifficultyCalculator() .nextDifficulty(1, parentHeader, null)) .isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY); @@ -72,23 +72,27 @@ public class FixedProtocolScheduleTest { assertThat( schedule - .getByBlockNumber(0) + .getByBlockHeader(blockHeader(0)) .getDifficultyCalculator() .nextDifficulty(1, parentHeader, null)) .isEqualTo(10000); assertThat( schedule - .getByBlockNumber(500) + .getByBlockHeader(blockHeader(500)) .getDifficultyCalculator() .nextDifficulty(1, parentHeader, null)) .isEqualTo(10000); assertThat( schedule - .getByBlockNumber(500_000) + .getByBlockHeader(blockHeader(500_000)) .getDifficultyCalculator() .nextDifficulty(1, parentHeader, null)) .isEqualTo(10000); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolScheduleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolScheduleTest.java index 9585ee066a..9dd73036f8 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolScheduleTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolScheduleTest.java @@ -15,6 +15,8 @@ package org.hyperledger.besu.ethereum.mainnet; import org.hyperledger.besu.config.GenesisConfigFile; +import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.ProtocolScheduleFixture; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -29,27 +31,39 @@ public class MainnetProtocolScheduleTest { @Test public void shouldReturnDefaultProtocolSpecsWhenCustomNumbersAreNotUsed() { final ProtocolSchedule sched = ProtocolScheduleFixture.MAINNET; - Assertions.assertThat(sched.getByBlockNumber(1L).getName()).isEqualTo("Frontier"); - Assertions.assertThat(sched.getByBlockNumber(1_150_000L).getName()).isEqualTo("Homestead"); - Assertions.assertThat(sched.getByBlockNumber(1_920_000L).getName()) + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1L)).getName()).isEqualTo("Frontier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1_150_000L)).getName()) + .isEqualTo("Homestead"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1_920_000L)).getName()) .isEqualTo("DaoRecoveryInit"); - Assertions.assertThat(sched.getByBlockNumber(1_920_001L).getName()) + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1_920_001L)).getName()) .isEqualTo("DaoRecoveryTransition"); - Assertions.assertThat(sched.getByBlockNumber(1_920_010L).getName()).isEqualTo("Homestead"); - Assertions.assertThat(sched.getByBlockNumber(2_463_000L).getName()) + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1_920_010L)).getName()) + .isEqualTo("Homestead"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(2_463_000L)).getName()) .isEqualTo("TangerineWhistle"); - Assertions.assertThat(sched.getByBlockNumber(2_675_000L).getName()).isEqualTo("SpuriousDragon"); - Assertions.assertThat(sched.getByBlockNumber(4_730_000L).getName()).isEqualTo("Byzantium"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(2_675_000L)).getName()) + .isEqualTo("SpuriousDragon"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(4_730_000L)).getName()) + .isEqualTo("Byzantium"); // Constantinople was originally scheduled for 7_080_000, but postponed - Assertions.assertThat(sched.getByBlockNumber(7_080_000L).getName()).isEqualTo("Byzantium"); - Assertions.assertThat(sched.getByBlockNumber(7_280_000L).getName()).isEqualTo("Petersburg"); - Assertions.assertThat(sched.getByBlockNumber(9_069_000L).getName()).isEqualTo("Istanbul"); - Assertions.assertThat(sched.getByBlockNumber(9_200_000L).getName()).isEqualTo("MuirGlacier"); - Assertions.assertThat(sched.getByBlockNumber(12_244_000L).getName()).isEqualTo("Berlin"); - Assertions.assertThat(sched.getByBlockNumber(12_965_000L).getName()).isEqualTo("London"); - Assertions.assertThat(sched.getByBlockNumber(13_773_000L).getName()).isEqualTo("ArrowGlacier"); - Assertions.assertThat(sched.getByBlockNumber(15_050_000L).getName()).isEqualTo("GrayGlacier"); - Assertions.assertThat(sched.getByBlockNumber(Long.MAX_VALUE).getName()) + Assertions.assertThat(sched.getByBlockHeader(blockHeader(7_080_000L)).getName()) + .isEqualTo("Byzantium"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(7_280_000L)).getName()) + .isEqualTo("Petersburg"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(9_069_000L)).getName()) + .isEqualTo("Istanbul"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(9_200_000L)).getName()) + .isEqualTo("MuirGlacier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(12_244_000L)).getName()) + .isEqualTo("Berlin"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(12_965_000L)).getName()) + .isEqualTo("London"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(13_773_000L)).getName()) + .isEqualTo("ArrowGlacier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(15_050_000L)).getName()) + .isEqualTo("GrayGlacier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(Long.MAX_VALUE)).getName()) .isEqualTo("GrayGlacier"); } @@ -58,8 +72,9 @@ public class MainnetProtocolScheduleTest { final ProtocolSchedule sched = MainnetProtocolSchedule.fromConfig( GenesisConfigFile.fromConfig("{}").getConfigOptions(), EvmConfiguration.DEFAULT); - Assertions.assertThat(sched.getByBlockNumber(1L).getName()).isEqualTo("Frontier"); - Assertions.assertThat(sched.getByBlockNumber(Long.MAX_VALUE).getName()).isEqualTo("Frontier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1L)).getName()).isEqualTo("Frontier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(Long.MAX_VALUE)).getName()) + .isEqualTo("Frontier"); } @Test @@ -69,16 +84,22 @@ public class MainnetProtocolScheduleTest { final ProtocolSchedule sched = MainnetProtocolSchedule.fromConfig( GenesisConfigFile.fromConfig(json).getConfigOptions(), EvmConfiguration.DEFAULT); - Assertions.assertThat(sched.getByBlockNumber(1).getName()).isEqualTo("Frontier"); - Assertions.assertThat(sched.getByBlockNumber(2).getName()).isEqualTo("Homestead"); - Assertions.assertThat(sched.getByBlockNumber(3).getName()).isEqualTo("DaoRecoveryInit"); - Assertions.assertThat(sched.getByBlockNumber(4).getName()).isEqualTo("DaoRecoveryTransition"); - Assertions.assertThat(sched.getByBlockNumber(13).getName()).isEqualTo("Homestead"); - Assertions.assertThat(sched.getByBlockNumber(14).getName()).isEqualTo("TangerineWhistle"); - Assertions.assertThat(sched.getByBlockNumber(15).getName()).isEqualTo("SpuriousDragon"); - Assertions.assertThat(sched.getByBlockNumber(16).getName()).isEqualTo("Byzantium"); - Assertions.assertThat(sched.getByBlockNumber(18).getName()).isEqualTo("Constantinople"); - Assertions.assertThat(sched.getByBlockNumber(19).getName()).isEqualTo("Petersburg"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1)).getName()).isEqualTo("Frontier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(2)).getName()).isEqualTo("Homestead"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(3)).getName()) + .isEqualTo("DaoRecoveryInit"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(4)).getName()) + .isEqualTo("DaoRecoveryTransition"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(13)).getName()).isEqualTo("Homestead"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(14)).getName()) + .isEqualTo("TangerineWhistle"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(15)).getName()) + .isEqualTo("SpuriousDragon"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(16)).getName()).isEqualTo("Byzantium"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(18)).getName()) + .isEqualTo("Constantinople"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(19)).getName()) + .isEqualTo("Petersburg"); } @Test @@ -104,11 +125,16 @@ public class MainnetProtocolScheduleTest { this.getClass().getResource("/goerli.json"), StandardCharsets.UTF_8)) .getConfigOptions(), EvmConfiguration.DEFAULT); - Assertions.assertThat(sched.getByBlockNumber(0L).getName()).isEqualTo("Petersburg"); - Assertions.assertThat(sched.getByBlockNumber(1_561_651L).getName()).isEqualTo("Istanbul"); - Assertions.assertThat(sched.getByBlockNumber(4_460_644L).getName()).isEqualTo("Berlin"); - Assertions.assertThat(sched.getByBlockNumber(5_062_605L).getName()).isEqualTo("London"); - Assertions.assertThat(sched.getByBlockNumber(Long.MAX_VALUE).getName()).isEqualTo("London"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(0L)).getName()) + .isEqualTo("Petersburg"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1_561_651L)).getName()) + .isEqualTo("Istanbul"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(4_460_644L)).getName()) + .isEqualTo("Berlin"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(5_062_605L)).getName()) + .isEqualTo("London"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(Long.MAX_VALUE)).getName()) + .isEqualTo("London"); } @Test @@ -120,16 +146,29 @@ public class MainnetProtocolScheduleTest { this.getClass().getResource("/rinkeby.json"), StandardCharsets.UTF_8)) .getConfigOptions(), EvmConfiguration.DEFAULT); - Assertions.assertThat(sched.getByBlockNumber(0L).getName()).isEqualTo("Frontier"); - Assertions.assertThat(sched.getByBlockNumber(1L).getName()).isEqualTo("Homestead"); - Assertions.assertThat(sched.getByBlockNumber(2L).getName()).isEqualTo("TangerineWhistle"); - Assertions.assertThat(sched.getByBlockNumber(3L).getName()).isEqualTo("SpuriousDragon"); - Assertions.assertThat(sched.getByBlockNumber(1_035_301L).getName()).isEqualTo("Byzantium"); - Assertions.assertThat(sched.getByBlockNumber(3_660_663L).getName()).isEqualTo("Constantinople"); - Assertions.assertThat(sched.getByBlockNumber(4_321_234L).getName()).isEqualTo("Petersburg"); - Assertions.assertThat(sched.getByBlockNumber(5_435_345L).getName()).isEqualTo("Istanbul"); - Assertions.assertThat(sched.getByBlockNumber(8_290_928L).getName()).isEqualTo("Berlin"); - Assertions.assertThat(sched.getByBlockNumber(8_897_988L).getName()).isEqualTo("London"); - Assertions.assertThat(sched.getByBlockNumber(Long.MAX_VALUE).getName()).isEqualTo("London"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(0L)).getName()).isEqualTo("Frontier"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1L)).getName()).isEqualTo("Homestead"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(2L)).getName()) + .isEqualTo("TangerineWhistle"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(3L)).getName()) + .isEqualTo("SpuriousDragon"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(1_035_301L)).getName()) + .isEqualTo("Byzantium"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(3_660_663L)).getName()) + .isEqualTo("Constantinople"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(4_321_234L)).getName()) + .isEqualTo("Petersburg"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(5_435_345L)).getName()) + .isEqualTo("Istanbul"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(8_290_928L)).getName()) + .isEqualTo("Berlin"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(8_897_988L)).getName()) + .isEqualTo("London"); + Assertions.assertThat(sched.getByBlockHeader(blockHeader(Long.MAX_VALUE)).getName()) + .isEqualTo("London"); + } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java index d774f7cb18..9eda930a42 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MutableProtocolScheduleTest.java @@ -100,7 +100,7 @@ public class MutableProtocolScheduleTest { final MutableProtocolSchedule protocolSchedule = new MutableProtocolSchedule(CHAIN_ID); protocolSchedule.putMilestone(0, spec1); protocolSchedule.putMilestone(0, spec2); - assertThat(protocolSchedule.getByBlockNumber(0)).isSameAs(spec2); + assertThat(protocolSchedule.getByBlockHeader(header(0))).isSameAs(spec2); } @Test diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java index 7b791415fa..e37caba4ae 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilderTest.java @@ -22,6 +22,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.config.GenesisConfigOptions; +import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.BlockNumberStreamingProtocolSchedule; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.evm.internal.EvmConfiguration; @@ -40,7 +42,6 @@ import org.mockito.stubbing.Answer; @RunWith(MockitoJUnitRunner.class) public class ProtocolScheduleBuilderTest { - @Mock GenesisConfigOptions configOptions; @Mock private Function modifier; private static final BigInteger CHAIN_ID = BigInteger.ONE; @@ -68,15 +69,17 @@ public class ProtocolScheduleBuilderTest { final ProtocolSchedule protocolSchedule = builder.createProtocolSchedule(); assertThat(protocolSchedule.getChainId()).contains(CHAIN_ID); - assertThat(protocolSchedule.getByBlockNumber(0).getName()).isEqualTo("Frontier"); - assertThat(protocolSchedule.getByBlockNumber(1).getName()).isEqualTo("Homestead"); - assertThat(protocolSchedule.getByBlockNumber(2).getName()).isEqualTo("DaoRecoveryInit"); - assertThat(protocolSchedule.getByBlockNumber(3).getName()).isEqualTo("DaoRecoveryTransition"); - assertThat(protocolSchedule.getByBlockNumber(12).getName()).isEqualTo("Homestead"); - assertThat(protocolSchedule.getByBlockNumber(13).getName()).isEqualTo("Byzantium"); - assertThat(protocolSchedule.getByBlockNumber(14).getName()).isEqualTo("Byzantium"); - assertThat(protocolSchedule.getByBlockNumber(15).getName()).isEqualTo("ParisFork"); - assertThat(protocolSchedule.getByBlockNumber(50).getName()).isEqualTo("ParisFork"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(0)).getName()).isEqualTo("Frontier"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(1)).getName()).isEqualTo("Homestead"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(2)).getName()) + .isEqualTo("DaoRecoveryInit"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(3)).getName()) + .isEqualTo("DaoRecoveryTransition"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(12)).getName()).isEqualTo("Homestead"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(13)).getName()).isEqualTo("Byzantium"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(14)).getName()).isEqualTo("Byzantium"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(15)).getName()).isEqualTo("ParisFork"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(50)).getName()).isEqualTo("ParisFork"); } @Test @@ -86,8 +89,8 @@ public class ProtocolScheduleBuilderTest { final ProtocolSchedule protocolSchedule = builder.createProtocolSchedule(); assertThat(protocolSchedule.getChainId()).contains(CHAIN_ID); - assertThat(protocolSchedule.getByBlockNumber(0).getName()).isEqualTo("Byzantium"); - assertThat(protocolSchedule.getByBlockNumber(1).getName()).isEqualTo("Byzantium"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(0)).getName()).isEqualTo("Byzantium"); + assertThat(protocolSchedule.getByBlockHeader(blockHeader(1)).getName()).isEqualTo("Byzantium"); } @Test @@ -114,9 +117,9 @@ public class ProtocolScheduleBuilderTest { // added at the point at which the modifier is applied. assertThat(schedule.streamMilestoneBlocks().collect(Collectors.toList())) .containsExactly(0L, 2L, 5L); - assertThat(schedule.getByBlockNumber(0).getName()).isEqualTo("Frontier"); - assertThat(schedule.getByBlockNumber(2).getName()).isEqualTo("Frontier"); - assertThat(schedule.getByBlockNumber(5).getName()).isEqualTo("Homestead"); + assertThat(schedule.getByBlockHeader(blockHeader(0)).getName()).isEqualTo("Frontier"); + assertThat(schedule.getByBlockHeader(blockHeader(2)).getName()).isEqualTo("Frontier"); + assertThat(schedule.getByBlockHeader(blockHeader(5)).getName()).isEqualTo("Homestead"); verify(modifier, times(2)).apply(any()); } @@ -132,8 +135,8 @@ public class ProtocolScheduleBuilderTest { // added at the point at which the modifier is applied. assertThat(schedule.streamMilestoneBlocks().collect(Collectors.toList())) .containsExactly(0L, 2L); - assertThat(schedule.getByBlockNumber(0).getName()).isEqualTo("Frontier"); - assertThat(schedule.getByBlockNumber(2).getName()).isEqualTo("Frontier"); + assertThat(schedule.getByBlockHeader(blockHeader(0)).getName()).isEqualTo("Frontier"); + assertThat(schedule.getByBlockHeader(blockHeader(2)).getName()).isEqualTo("Frontier"); verify(modifier, times(1)).apply(any()); } @@ -150,9 +153,8 @@ public class ProtocolScheduleBuilderTest { // added at the point at which the modifier is applied. assertThat(schedule.streamMilestoneBlocks().collect(Collectors.toList())) .containsExactly(0L, 5L); - assertThat(schedule.getByBlockNumber(0).getName()).isEqualTo("Frontier"); - assertThat(schedule.getByBlockNumber(5).getName()).isEqualTo("Homestead"); - + assertThat(schedule.getByBlockHeader(blockHeader(0)).getName()).isEqualTo("Frontier"); + assertThat(schedule.getByBlockHeader(blockHeader(5)).getName()).isEqualTo("Homestead"); verify(modifier, times(1)).apply(any()); } @@ -170,4 +172,8 @@ public class ProtocolScheduleBuilderTest { return new BlockNumberStreamingProtocolSchedule( (MutableProtocolSchedule) builder.createProtocolSchedule()); } + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigrationTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigrationTest.java index be97b9d07b..b17bcddab7 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigrationTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigrationTest.java @@ -22,7 +22,6 @@ import static org.hyperledger.besu.ethereum.privacy.PrivateStateRootResolver.EMP import static org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage.SCHEMA_VERSION_1_0_0; import static org.hyperledger.besu.ethereum.privacy.storage.PrivateStateKeyValueStorage.SCHEMA_VERSION_1_4_0; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.verify; @@ -103,7 +102,7 @@ public class PrivateStorageMigrationTest { privateStateStorage = new PrivateStateKeyValueStorage(kvStorage); privateStateRootResolver = new PrivateStateRootResolver(privateStateStorage); - lenient().when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec); + lenient().when(protocolSchedule.getByBlockHeader(any())).thenReturn(protocolSpec); lenient().when(protocolSpec.getTransactionProcessor()).thenReturn(transactionProcessor); lenient() .when(protocolSpec.getTransactionReceiptFactory()) diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java index d7aea52021..b96a6ad2de 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java @@ -16,7 +16,6 @@ package org.hyperledger.besu.ethereum.eth.sync; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -36,6 +35,8 @@ import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; import org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions; +import org.hyperledger.besu.ethereum.core.BlockHeader; +import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.BlockImporter; import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil; import org.hyperledger.besu.ethereum.core.Difficulty; @@ -325,10 +326,11 @@ public abstract class AbstractBlockPropagationManagerTest { @Test public void handlesDuplicateAnnouncements() { + final ProtocolSchedule stubProtocolSchedule = spy(protocolSchedule); - final ProtocolSpec stubProtocolSpec = spy(protocolSchedule.getByBlockNumber(2)); + final ProtocolSpec stubProtocolSpec = spy(protocolSchedule.getByBlockHeader(blockHeader(2))); final BlockImporter stubBlockImporter = spy(stubProtocolSpec.getBlockImporter()); - doReturn(stubProtocolSpec).when(stubProtocolSchedule).getByBlockNumber(anyLong()); + doReturn(stubProtocolSpec).when(stubProtocolSchedule).getByBlockHeader(any()); doReturn(stubBlockImporter).when(stubProtocolSpec).getBlockImporter(); final BlockPropagationManager blockPropagationManager = new BlockPropagationManager( @@ -378,9 +380,9 @@ public abstract class AbstractBlockPropagationManagerTest { @Test public void handlesPendingDuplicateAnnouncements() { final ProtocolSchedule stubProtocolSchedule = spy(protocolSchedule); - final ProtocolSpec stubProtocolSpec = spy(protocolSchedule.getByBlockNumber(2)); + final ProtocolSpec stubProtocolSpec = spy(protocolSchedule.getByBlockHeader(blockHeader(2))); final BlockImporter stubBlockImporter = spy(stubProtocolSpec.getBlockImporter()); - doReturn(stubProtocolSpec).when(stubProtocolSchedule).getByBlockNumber(anyLong()); + doReturn(stubProtocolSpec).when(stubProtocolSchedule).getByBlockHeader(any()); doReturn(stubBlockImporter).when(stubProtocolSpec).getBlockImporter(); final BlockPropagationManager blockPropagationManager = new BlockPropagationManager( @@ -772,7 +774,7 @@ public abstract class AbstractBlockPropagationManagerTest { blockchainUtil.importFirstBlocks(2); final Block firstBlock = blockchainUtil.getBlock(1); final BadBlockManager badBlocksManager = - protocolSchedule.getByBlockNumber(1).getBadBlocksManager(); + protocolSchedule.getByBlockHeader(blockHeader(1)).getBadBlocksManager(); final Block badBlock = new BlockDataGenerator() .block( @@ -983,4 +985,8 @@ public abstract class AbstractBlockPropagationManagerTest { } public abstract Blockchain getFullBlockchain(); + + private BlockHeader blockHeader(final long number) { + return new BlockHeaderTestFixture().number(number).buildHeader(); + } } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java index ea3d872cdb..80bfbba103 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java @@ -21,7 +21,6 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -83,12 +82,15 @@ public class BackwardSyncContextTest { private MutableBlockchain remoteBlockchain; private RespondingEthPeer peer; private MutableBlockchain localBlockchain; + private static final BlockDataGenerator blockDataGenerator = new BlockDataGenerator(); @Spy private ProtocolSchedule protocolSchedule = MainnetProtocolSchedule.fromConfig(new StubGenesisConfigOptions()); - @Spy private ProtocolSpec mockProtocolSpec = protocolSchedule.getByBlockNumber(0L); + @Spy + private ProtocolSpec protocolSpec = + protocolSchedule.getByBlockHeader(blockDataGenerator.header(0L)); @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ProtocolContext protocolContext; @@ -101,12 +103,11 @@ public class BackwardSyncContextTest { private BackwardChain backwardChain; private Block uncle; private Block genesisBlock; - private static final BlockDataGenerator blockDataGenerator = new BlockDataGenerator(); @Before public void setup() { - when(mockProtocolSpec.getBlockValidator()).thenReturn(blockValidator); - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(mockProtocolSpec); + when(protocolSpec.getBlockValidator()).thenReturn(blockValidator); + doReturn(protocolSpec).when(protocolSchedule).getByBlockHeader(any()); genesisBlock = blockDataGenerator.genesisBlock(); remoteBlockchain = createInMemoryBlockchain(genesisBlock); localBlockchain = createInMemoryBlockchain(genesisBlock); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/AbstractTransactionPoolTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/AbstractTransactionPoolTest.java index b4c8fa832d..3307eeaa91 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/AbstractTransactionPoolTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/AbstractTransactionPoolTest.java @@ -26,7 +26,6 @@ import static org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason import static org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason.TRANSACTION_REPLACEMENT_UNDERPRICED; import static org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason.TX_FEECAP_EXCEEDED; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.atLeastOnce; @@ -136,7 +135,7 @@ public abstract class AbstractTransactionPoolTest { when(protocolSpec.getTransactionValidator()).thenReturn(transactionValidator); when(protocolSpec.getFeeMarket()).thenReturn(getFeeMarket()); protocolSchedule = spy(executionContext.getProtocolSchedule()); - doReturn(protocolSpec).when(protocolSchedule).getByBlockNumber(anyLong()); + doReturn(protocolSpec).when(protocolSchedule).getByBlockHeader(any()); blockGasLimit = blockchain.getChainHeadBlock().getHeader().getGasLimit(); ethProtocolManager = EthProtocolManagerTestUtil.create(); ethContext = spy(ethProtocolManager.ethContext()); diff --git a/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java b/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java index aa76d8f4df..d15bf13794 100644 --- a/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java +++ b/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java @@ -171,7 +171,7 @@ public class DifficultyCalculatorTests { final long currentTime = extractLong(value, "currentTimestamp"); final UInt256 currentDifficulty = UInt256.fromHexString(value.get("currentDifficulty").asText()); - final var spec = protocolSchedule.getByBlockNumber(currentBlockNumber); + final var spec = protocolSchedule.getByBlockHeader(testHeader); final var calculator = spec.getDifficultyCalculator(); assertThat(UInt256.valueOf(calculator.nextDifficulty(currentTime, testHeader, null))) .describedAs("File %s Test %s", testFile, entry.getKey())