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

getByBlockNumber replaced by getByBlockHeader on tests #5259

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private void appendBlock(
final Block block) {
besuController
.getProtocolSchedule()
.getByBlockNumber(blockchain.getChainHeadBlockNumber())
.getByBlockHeader(blockchain.getChainHeadHeader())
.getBlockImporter()
.importBlock(protocolContext, block, HeaderValidationMode.NONE);
}
Expand Down
8 changes: 7 additions & 1 deletion besu/src/test/java/org/hyperledger/besu/PrivacyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
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;
Expand Down Expand Up @@ -145,8 +147,12 @@ private PrecompiledContract getPrecompile(
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public void protocolSpecsAreCreatedAtBlockDefinedInJson() {
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();
Expand All @@ -75,7 +75,7 @@ public void parametersAlignWithMainnetWithAdjustments() {
NODE_KEY,
false,
EvmConfiguration.DEFAULT)
.getByBlockNumber(0);
.getByBlockHeader(blockHeader(0));

assertThat(homestead.getName()).isEqualTo("Frontier");
assertThat(homestead.getBlockReward()).isEqualTo(Wei.ZERO);
Expand Down Expand Up @@ -180,4 +180,8 @@ private boolean validateHeaderByProtocolSchedule(
.getBlockHeaderValidator()
.validateHeader(blockHeader, parentBlockHeader, null, HeaderValidationMode.LIGHT);
}

private BlockHeader blockHeader(final long number) {
return new BlockHeaderTestFixture().number(number).buildHeader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public class CombinedProtocolScheduleFactory {
* @param chainId the chain id
* @return the protocol schedule
*/
public ProtocolSchedule create(
public BftProtocolSchedule create(
final NavigableSet<ForkSpec<ProtocolSchedule>> forkSpecs,
final Optional<BigInteger> chainId) {
final MutableProtocolSchedule combinedProtocolSchedule = new MutableProtocolSchedule(chainId);
final BftProtocolSchedule combinedProtocolSchedule =
new BftProtocolSchedule(new MutableProtocolSchedule(chainId));
for (ForkSpec<ProtocolSchedule> spec : forkSpecs) {
checkState(
spec.getValue() instanceof MutableProtocolSchedule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -58,9 +57,7 @@ public void createsCombinedProtocolScheduleWithMilestonesFromSingleProtocolSched

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))
Expand Down Expand Up @@ -101,9 +98,7 @@ public void createsCombinedProtocolScheduleWithMilestonesFromMultipleSchedules()

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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void ensureBlockRewardAndMiningBeneficiaryInProtocolSpecMatchConfig() {

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)))
Expand All @@ -76,7 +77,7 @@ public void missingMiningBeneficiaryInConfigWillPayCoinbaseInHeader() {

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);
Expand Down Expand Up @@ -131,7 +132,7 @@ private void ensureForksAreRespected(final boolean initialBeneficiaryIsEmpty) {

// 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))
Expand All @@ -140,7 +141,7 @@ private void ensureForksAreRespected(final boolean initialBeneficiaryIsEmpty) {

// 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))
Expand All @@ -149,7 +150,7 @@ private void ensureForksAreRespected(final boolean initialBeneficiaryIsEmpty) {

// 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))
Expand Down Expand Up @@ -219,9 +220,9 @@ public void blockRewardSpecifiedInTransitionCreatesNewMilestone() {
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));
}

Expand Down Expand Up @@ -256,4 +257,8 @@ private BftConfigOptions createBftConfig(

return bftConfig;
}

private BlockHeader blockHeader(final long number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used a lot in tests, might be nice to have this as a static method in the BlockHeaderTestFixture

return new BlockHeaderTestFixture().number(number).buildHeader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
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;

Expand Down Expand Up @@ -94,14 +93,13 @@ public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
private BlockNumberStreamingProtocolSchedule createProtocolSchedule(
final GenesisConfigOptions genesisConfig, final List<ForkSpec<BftConfigOptions>> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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;
Expand All @@ -39,8 +41,8 @@ public void protocolSpecsAreCreatedAtBlockDefinedInJson() {
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();
Expand All @@ -51,7 +53,7 @@ public void protocolSpecsAreCreatedAtBlockDefinedInJson() {
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);
Expand All @@ -60,4 +62,8 @@ public void parametersAlignWithMainnetWithAdjustments() {
var op = london.getEvm().getOperationsUnsafe()[0x44];
assertThat(op).isInstanceOf(PrevRanDaoOperation.class);
}

private BlockHeader blockHeader(final long number) {
return new BlockHeaderTestFixture().number(number).buildHeader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
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;

Expand Down Expand Up @@ -127,14 +126,13 @@ public void blockModeTransitionsCreatesBlockModeHeaderValidators() {
private BlockNumberStreamingProtocolSchedule createProtocolSchedule(
final GenesisConfigOptions genesisConfig, final List<ForkSpec<QbftConfigOptions>> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ public void allFieldsPresentForLatestBlock() {

@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())
Expand All @@ -117,9 +114,6 @@ public void cantGetBlockHigherThanChainHead() {

@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())
Expand All @@ -134,7 +128,10 @@ public void blockCountBounds() {
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();
Expand All @@ -148,7 +145,10 @@ public void doesntGoPastChainHeadWithHighBlockCount() {
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();
Expand All @@ -160,13 +160,16 @@ public void correctlyHandlesForkBlock() {
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}))
Expand Down
Loading