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

Revert "Replace getByBlockNumber by getByBlockHeader (#5020)" #5063

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ private void setOptionalFields(

private void importBlock(final Block block) {
final BlockImporter importer =
controller.getProtocolSchedule().getByBlockHeader(block.getHeader()).getBlockImporter();
controller
.getProtocolSchedule()
.getByBlockNumber(block.getHeader().getNumber())
.getBlockImporter();

final BlockImportResult importResult =
importer.importBlock(controller.getProtocolContext(), block, HeaderValidationMode.NONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public RlpBlockImporter.ImportResult importBlockchain(
if (previousHeader == null) {
previousHeader = lookupPreviousHeader(blockchain, header);
}
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(header);
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockNumber);
final BlockHeader lastHeader = previousHeader;

final CompletableFuture<Void> validationFuture =
Expand Down
3 changes: 2 additions & 1 deletion besu/src/test/java/org/hyperledger/besu/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ private static void setupState(

for (int i = 1; i < count + 1; ++i) {
final Block block = blocks.get(i);
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader());
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final BlockImportResult result =
blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void assertPreMergeScheduleForNotPostMerge() {
var preMergeProtocolSpec = mock(ProtocolSpec.class);
when(mergeContext.isPostMerge()).thenReturn(Boolean.FALSE);
when(mergeContext.getFinalized()).thenReturn(Optional.empty());
when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec);
when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(preMergeProtocolSpec);
}
Expand All @@ -140,7 +141,7 @@ public void assertPostMergeScheduleForAnyBlockWhenPostMergeAndFinalized() {
var mockBlock = new BlockHeaderTestFixture().buildHeader();
var postMergeProtocolSpec = mock(ProtocolSpec.class);
when(mergeContext.getFinalized()).thenReturn(Optional.of(mockBlock));
when(postMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(postMergeProtocolSpec);
when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(postMergeProtocolSpec);
}
Expand All @@ -163,7 +164,7 @@ public void assertPreMergeScheduleForBelowTerminalBlockWhenPostMergeIfNotFinaliz
when(mockBlockchain.getTotalDifficultyByHash(any()))
.thenReturn(Optional.of(Difficulty.of(1335L)));

when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec);
when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(preMergeProtocolSpec);
}
Expand All @@ -186,7 +187,7 @@ public void assertPostMergeScheduleForPostMergeExactlyAtTerminalDifficultyIfNotF
when(mockBlockchain.getTotalDifficultyByHash(any()))
.thenReturn(Optional.of(Difficulty.of(1337L)));

when(postMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(postMergeProtocolSpec);
when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(postMergeProtocolSpec);
}
Expand All @@ -212,7 +213,7 @@ void assertDetachedRulesForPostMergeBlocks(final BlockHeaderValidator validator)
var preMergeProtocolSpec = mock(ProtocolSpec.class);
when(preMergeProtocolSpec.getBlockHeaderValidator()).thenReturn(validator);

when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec);
when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec);

var mockParentBlock =
new BlockHeaderTestFixture()
Expand All @@ -234,7 +235,7 @@ void assertDetachedRulesForPostMergeBlocks(final BlockHeaderValidator validator)
var mergeFriendlyValidation =
transitionProtocolSchedule
.getPreMergeSchedule()
.getByBlockHeader(mockBlock)
.getByBlockNumber(1L)
.getBlockHeaderValidator()
.validateHeader(
mockBlock, mockParentBlock, protocolContext, HeaderValidationMode.DETACHED_ONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -124,7 +125,7 @@ public void setUp() {
when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty());
when(mockProtocolContext.getBlockchain()).thenReturn(blockchain);
when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive);
when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec);
when(mockProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(mockProtocolSpec);
when(mockProtocolSpec.getTransactionValidator()).thenReturn(mockTransactionValidator);
when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
when(mockTransactionValidator.validate(any(), any(Optional.class), any()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private boolean validateHeaderByProtocolSchedule(
final BlockHeader parentBlockHeader) {

return schedule
.getByBlockHeader(blockHeader)
.getByBlockNumber(blockHeader.getNumber())
.getBlockHeaderValidator()
.validateHeader(blockHeader, parentBlockHeader, null, HeaderValidationMode.LIGHT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private ProtocolSpec getByBlockHeaderFromTransitionUtils(
LOG,
"for {} returning a pre-merge schedule because we are not post-merge",
blockHeader::toLogString);
return getPreMergeSchedule().getByBlockHeader(blockHeader);
return getPreMergeSchedule().getByBlockNumber(blockHeader.getNumber());
}

// otherwise check to see if this block represents a re-org TTD block:
Expand All @@ -128,12 +128,12 @@ private ProtocolSpec getByBlockHeaderFromTransitionUtils(
LOG,
"returning a pre-merge schedule because block {} is pre-merge or TTD",
blockHeader::toLogString);
return getPreMergeSchedule().getByBlockHeader(blockHeader);
return getPreMergeSchedule().getByBlockNumber(blockHeader.getNumber());
}
}
// else return post-merge schedule
debugLambda(LOG, " for {} returning a post-merge schedule", blockHeader::toLogString);
return getPostMergeSchedule().getByBlockHeader(blockHeader);
return getPostMergeSchedule().getByBlockNumber(blockHeader.getNumber());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ boolean ancestorIsValidTerminalProofOfWork(final BlockHeader blockheader) {
@Override
public Optional<Hash> getLatestValidAncestor(final Hash blockHash) {
final var chain = protocolContext.getBlockchain();
final var chainHeadHeader = chain.getChainHeadHeader();
final var chainHeadNum = chain.getChainHeadBlockNumber();
return findValidAncestor(
chain, blockHash, protocolSchedule.getByBlockHeader(chainHeadHeader).getBadBlocksManager());
chain, blockHash, protocolSchedule.getByBlockNumber(chainHeadNum).getBadBlocksManager());
}

@Override
Expand All @@ -692,7 +692,8 @@ public Optional<Hash> getLatestValidAncestor(final BlockHeader blockHeader) {
final var self = chain.getBlockHeader(blockHeader.getHash());

if (self.isEmpty()) {
final var badBlocks = protocolSchedule.getByBlockHeader(blockHeader).getBadBlocksManager();
final var badBlocks =
protocolSchedule.getByBlockNumber(blockHeader.getNumber()).getBadBlocksManager();
return findValidAncestor(chain, blockHeader.getParentHash(), badBlocks);
}
return self.map(BlockHeader::getHash);
Expand Down Expand Up @@ -823,7 +824,7 @@ protected interface MergeBlockCreatorFactory {
@Override
public void addBadBlock(final Block block, final Optional<Throwable> maybeCause) {
protocolSchedule
.getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader())
.getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber())
.getBadBlocksManager()
.addBadBlock(block, maybeCause);
}
Expand All @@ -838,15 +839,15 @@ public boolean isBadBlock(final Hash blockHash) {
private BadBlockManager getBadBlockManager() {
final BadBlockManager badBlocksManager =
protocolSchedule
.getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader())
.getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber())
.getBadBlocksManager();
return badBlocksManager;
}

@Override
public Optional<Hash> getLatestValidHashOfBadBlock(Hash blockHash) {
return protocolSchedule
.getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader())
.getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber())
.getBadBlocksManager()
.getLatestValidHash(blockHash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.hyperledger.besu.consensus.merge;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
Expand Down Expand Up @@ -72,6 +71,7 @@ public void setUp() {
@Test
public void returnPostMergeIfFinalizedExists() {
when(mergeContext.getFinalized()).thenReturn(Optional.of(mock(BlockHeader.class)));
when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER);

transitionProtocolSchedule.getByBlockHeader(blockHeader);

Expand All @@ -83,6 +83,8 @@ public void returnPreMergeIfBeforeMerge() {
when(mergeContext.getFinalized()).thenReturn(Optional.empty());
when(mergeContext.isPostMerge()).thenReturn(false);

when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER);

transitionProtocolSchedule.getByBlockHeader(blockHeader);

verifyPreMergeProtocolScheduleReturned();
Expand All @@ -96,6 +98,7 @@ public void returnPreMergeIfTerminalPoWBlock() {

final Hash parentHash = Hash.fromHexStringLenient("0xabc123");

when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER);
when(blockHeader.getParentHash()).thenReturn(parentHash);
when(blockHeader.getDifficulty()).thenReturn(Difficulty.of(10L));
when(blockchain.getTotalDifficultyByHash(parentHash))
Expand All @@ -114,6 +117,7 @@ public void returnPreMergeIfAfterMergeButReorgPreTTD() {

final Hash parentHash = Hash.fromHexStringLenient("0xabc123");

when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER);
when(blockHeader.getParentHash()).thenReturn(parentHash);
when(blockHeader.getDifficulty()).thenReturn(Difficulty.of(2L));
when(blockchain.getTotalDifficultyByHash(parentHash))
Expand Down Expand Up @@ -155,45 +159,52 @@ public void getByBlockHeader_returnsTimestampScheduleIfPresent() {
}

@Test
public void getByBlockHeader_delegatesToPreMergeScheduleWhenBlockNotFound() {
public void getByBlockNumber_returnsTimestampScheduleIfPresent() {
when(blockchain.getBlockHeader(BLOCK_NUMBER)).thenReturn(Optional.of(blockHeader));
when(timestampSchedule.getByBlockHeader(blockHeader)).thenReturn(mock(ProtocolSpec.class));

assertThat(transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER)).isNotNull();

verify(timestampSchedule).getByBlockHeader(blockHeader);
verifyNoMergeScheduleInteractions();
}

@Test
public void getByBlockNumber_delegatesToPreMergeScheduleWhenBlockNotFound() {
when(blockchain.getBlockHeader(BLOCK_NUMBER)).thenReturn(Optional.empty());
when(mergeContext.isPostMerge()).thenReturn(false);

transitionProtocolSchedule.getByBlockHeader(blockHeader);
transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER);

verifyPreMergeProtocolScheduleReturned();
}

@Test
public void getByBlockHeader_delegatesToPostMergeScheduleWhenBlockNotFound() {
when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER);
when(blockHeader.getDifficulty()).thenReturn(Difficulty.ZERO);
when(blockchain.getTotalDifficultyByHash(any())).thenReturn(Optional.of(TTD));
public void getByBlockNumber_delegatesToPostMergeScheduleWhenBlockNotFound() {
when(blockchain.getBlockHeader(BLOCK_NUMBER)).thenReturn(Optional.empty());
when(mergeContext.isPostMerge()).thenReturn(true);

transitionProtocolSchedule.getByBlockHeader(blockHeader);
transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER);

verifyPostMergeProtocolScheduleReturned();
}

@Test
public void getByBlockHeader_delegatesToPostMergeScheduleWhenTimestampScheduleDoesNotExist() {
public void getByBlockNumber_delegatesToPostMergeScheduleWhenTimestampScheduleDoesNotExist() {
when(mergeContext.isPostMerge()).thenReturn(true);
when(blockHeader.getDifficulty()).thenReturn(Difficulty.ZERO);
when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER);
when(blockchain.getTotalDifficultyByHash(any())).thenReturn(Optional.of(TTD));

transitionProtocolSchedule.getByBlockHeader(blockHeader);
transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER);

verifyPostMergeProtocolScheduleReturned();
}

private void verifyPreMergeProtocolScheduleReturned() {
verify(preMergeProtocolSchedule).getByBlockHeader(any());
verify(preMergeProtocolSchedule).getByBlockNumber(BLOCK_NUMBER);
verifyNoInteractions(postMergeProtocolSchedule);
}

private void verifyPostMergeProtocolScheduleReturned() {
verify(postMergeProtocolSchedule).getByBlockHeader(any());
verify(postMergeProtocolSchedule).getByBlockNumber(BLOCK_NUMBER);
verifyNoInteractions(preMergeProtocolSchedule);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void setUp() {
return spec;
})
.when(protocolSchedule)
.getByBlockHeader(any(BlockHeader.class));
.getByBlockNumber(anyLong());

protocolContext = new ProtocolContext(blockchain, worldStateArchive, mergeContext);
var mutable = worldStateArchive.getMutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public JsonRpcTestMethodsFactory(final BlockchainImporter importer) {
this.synchronizer = mock(Synchronizer.class);

for (final Block block : importer.getBlocks()) {
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader());
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
blockImporter.importBlock(context, block, HeaderValidationMode.FULL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public static void setUpOnce() throws Exception {

for (final Block block : importer.getBlocks()) {
final ProtocolSchedule protocolSchedule = importer.getProtocolSchedule();
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader());
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
blockImporter.importBlock(context, block, HeaderValidationMode.FULL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ protected Object resultByBlockNumber(
return new JsonRpcErrorResponse(request.getRequest().getId(), UNKNOWN_BLOCK);
}

final Block block = maybeBlock.get();

// rewind to the block before the one we want to replay
protocolContext.getBlockchain().rewindToBlock(blockNumber - 1);

try {
// replay block and persist it
protocolSchedule
.getByBlockHeader(block.getHeader())
.getByBlockNumber(blockNumber)
.getBlockValidator()
.validateAndProcessBlock(
protocolContext, block, HeaderValidationMode.FULL, HeaderValidationMode.NONE, true);
protocolContext,
maybeBlock.get(),
HeaderValidationMode.FULL,
HeaderValidationMode.NONE,
true);
} catch (Exception e) {
LOG.error(e.getMessage());
return new JsonRpcErrorResponse(request.getRequest().getId(), INTERNAL_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final List<BadBlockResult> response =
protocolSchedule
.getByBlockHeader(blockchain.headBlockHeader())
.getByBlockNumber(blockchain.headBlockNumber())
.getBadBlocksManager()
.getBadBlocks()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public String getName() {
@Override
public JsonRpcResponse response(final JsonRpcRequestContext request) {
protocolSchedule
.getByBlockHeader(blockchain.getChainHeadHeader())
.getByBlockNumber(blockchain.getChainHeadBlockNumber())
.getBadBlocksManager()
.reset();
return new JsonRpcSuccessResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

final Blockchain blockchain = blockchainQueries.get().getBlockchain();
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockHeader(blockchain.getChainHeadHeader());
protocolSchedule.getByBlockNumber(blockchain.getChainHeadHeader().getNumber());
final BadBlockManager badBlockManager = protocolSpec.getBadBlocksManager();

return badBlockManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static MinerDataResult createMinerDataResult(
final ProtocolSchedule protocolSchedule,
final BlockchainQueries blockchainQueries) {
final BlockHeader blockHeader = block.getHeader();
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader);
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockHeader.getNumber());
final Wei staticBlockReward = protocolSpec.getBlockReward();
final Wei transactionFee =
block.getTransactions().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private static FlatTrace.Context handleReturn(
if ("STOP".equals(traceFrame.getOpcode()) && resultBuilder.isGasUsedEmpty()) {
final long callStipend =
protocolSchedule
.getByBlockHeader(block.getHeader())
.getByBlockNumber(block.getHeader().getNumber())
.getGasCalculator()
.getAdditionalCallStipend();
tracesContexts.stream()
Expand Down
Loading