diff --git a/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java b/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java index 435d7737dc..13c239d6d8 100644 --- a/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java +++ b/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java @@ -153,10 +153,7 @@ public class JsonBlockImporter { private void importBlock(final Block block) { final BlockImporter importer = - controller - .getProtocolSchedule() - .getByBlockNumber(block.getHeader().getNumber()) - .getBlockImporter(); + controller.getProtocolSchedule().getByBlockHeader(block.getHeader()).getBlockImporter(); final BlockImportResult importResult = importer.importBlock(controller.getProtocolContext(), block, HeaderValidationMode.NONE); diff --git a/besu/src/main/java/org/hyperledger/besu/chainimport/RlpBlockImporter.java b/besu/src/main/java/org/hyperledger/besu/chainimport/RlpBlockImporter.java index d65425c8cc..b9fd93c13c 100644 --- a/besu/src/main/java/org/hyperledger/besu/chainimport/RlpBlockImporter.java +++ b/besu/src/main/java/org/hyperledger/besu/chainimport/RlpBlockImporter.java @@ -127,7 +127,7 @@ public class RlpBlockImporter implements Closeable { if (previousHeader == null) { previousHeader = lookupPreviousHeader(blockchain, header); } - final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockNumber); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(header); final BlockHeader lastHeader = previousHeader; final CompletableFuture validationFuture = diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerTest.java index 99d78857c7..76acf53747 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerTest.java @@ -427,8 +427,7 @@ public final class RunnerTest { for (int i = 1; i < count + 1; ++i) { final Block block = blocks.get(i); - final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader()); final BlockImporter blockImporter = protocolSpec.getBlockImporter(); final BlockImportResult result = blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL); diff --git a/besu/src/test/java/org/hyperledger/besu/controller/TransitionControllerBuilderTest.java b/besu/src/test/java/org/hyperledger/besu/controller/TransitionControllerBuilderTest.java index 02a37fff38..83b6167d5b 100644 --- a/besu/src/test/java/org/hyperledger/besu/controller/TransitionControllerBuilderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/controller/TransitionControllerBuilderTest.java @@ -16,7 +16,6 @@ package org.hyperledger.besu.controller; 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; @@ -131,8 +130,8 @@ public class TransitionControllerBuilderTest { var preMergeProtocolSpec = mock(ProtocolSpec.class); when(mergeContext.isPostMerge()).thenReturn(Boolean.FALSE); when(mergeContext.getFinalized()).thenReturn(Optional.empty()); - when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec); - assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock)) + when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec); + assertThat(transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(mockBlock)) .isEqualTo(preMergeProtocolSpec); } @@ -141,8 +140,8 @@ public class TransitionControllerBuilderTest { var mockBlock = new BlockHeaderTestFixture().buildHeader(); var postMergeProtocolSpec = mock(ProtocolSpec.class); when(mergeContext.getFinalized()).thenReturn(Optional.of(mockBlock)); - when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec); - assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock)) + when(postMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(postMergeProtocolSpec); + assertThat(transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(mockBlock)) .isEqualTo(postMergeProtocolSpec); } @@ -164,8 +163,8 @@ public class TransitionControllerBuilderTest { when(mockBlockchain.getTotalDifficultyByHash(any())) .thenReturn(Optional.of(Difficulty.of(1335L))); - when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec); - assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock)) + when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec); + assertThat(transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(mockBlock)) .isEqualTo(preMergeProtocolSpec); } @@ -187,8 +186,8 @@ public class TransitionControllerBuilderTest { when(mockBlockchain.getTotalDifficultyByHash(any())) .thenReturn(Optional.of(Difficulty.of(1337L))); - when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec); - assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock)) + when(postMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(postMergeProtocolSpec); + assertThat(transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(mockBlock)) .isEqualTo(postMergeProtocolSpec); } @@ -213,7 +212,7 @@ public class TransitionControllerBuilderTest { var preMergeProtocolSpec = mock(ProtocolSpec.class); when(preMergeProtocolSpec.getBlockHeaderValidator()).thenReturn(validator); - when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec); + when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec); var mockParentBlock = new BlockHeaderTestFixture() @@ -235,7 +234,7 @@ public class TransitionControllerBuilderTest { var mergeFriendlyValidation = transitionProtocolSchedule .getPreMergeSchedule() - .getByBlockNumber(1L) + .getByBlockHeader(mockBlock) .getBlockHeaderValidator() .validateHeader( mockBlock, mockParentBlock, protocolContext, HeaderValidationMode.DETACHED_ONLY); diff --git a/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java b/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java index 1e53652b25..59a14b9384 100644 --- a/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java +++ b/besu/src/test/java/org/hyperledger/besu/services/BesuEventsImplTest.java @@ -17,7 +17,6 @@ package org.hyperledger.besu.services; 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; @@ -125,7 +124,7 @@ public class BesuEventsImplTest { when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty()); when(mockProtocolContext.getBlockchain()).thenReturn(blockchain); when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive); - when(mockProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(mockProtocolSpec); + when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec); when(mockProtocolSpec.getTransactionValidator()).thenReturn(mockTransactionValidator); when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L)); when(mockTransactionValidator.validate(any(), any(Optional.class), any())) 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 515f04ddff..efc5b7e287 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 @@ -176,7 +176,7 @@ public class CliqueProtocolScheduleTest { final BlockHeader parentBlockHeader) { return schedule - .getByBlockNumber(blockHeader.getNumber()) + .getByBlockHeader(blockHeader) .getBlockHeaderValidator() .validateHeader(blockHeader, parentBlockHeader, null, HeaderValidationMode.LIGHT); } diff --git a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionBackwardSyncContext.java b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionBackwardSyncContext.java index 237624f56e..9af4d8d6b9 100644 --- a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionBackwardSyncContext.java +++ b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionBackwardSyncContext.java @@ -64,6 +64,8 @@ public class TransitionBackwardSyncContext extends BackwardSyncContext { */ @Override public BlockValidator getBlockValidatorForBlock(final Block block) { - return transitionProtocolSchedule.getByBlockHeader(block.getHeader()).getBlockValidator(); + return transitionProtocolSchedule + .getByBlockHeaderWithTransitionReorgHandling(block.getHeader()) + .getBlockValidator(); } } diff --git a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionProtocolSchedule.java b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionProtocolSchedule.java index ca6133df21..0d7146444b 100644 --- a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionProtocolSchedule.java +++ b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/TransitionProtocolSchedule.java @@ -103,13 +103,29 @@ public class TransitionProtocolSchedule implements ProtocolSchedule { } /** - * Gets by block header. + * Gets protocol spec by block header. * * @param blockHeader the block header - * @return the by block header + * @return the ProtocolSpec to be used by the provided block */ @Override public ProtocolSpec getByBlockHeader(final ProcessableBlockHeader blockHeader) { + return this.timestampSchedule + .getByTimestamp(blockHeader.getTimestamp()) + .orElseGet( + () -> + transitionUtils.dispatchFunctionAccordingToMergeState( + protocolSchedule -> protocolSchedule.getByBlockHeader(blockHeader))); + } + + /** + * Gets the protocol spec by block header, with some additional logic used by backwards sync (BWS) + * + * @param blockHeader the block header + * @return the ProtocolSpec to be used by the provided block + */ + public ProtocolSpec getByBlockHeaderWithTransitionReorgHandling( + final ProcessableBlockHeader blockHeader) { return this.timestampSchedule .getByTimestamp(blockHeader.getTimestamp()) .orElseGet(() -> getByBlockHeaderFromTransitionUtils(blockHeader)); @@ -126,7 +142,7 @@ public class TransitionProtocolSchedule implements ProtocolSchedule { .setMessage("for {} returning a pre-merge schedule because we are not post-merge") .addArgument(blockHeader::toLogString) .log(); - return getPreMergeSchedule().getByBlockNumber(blockHeader.getNumber()); + return getPreMergeSchedule().getByBlockHeader(blockHeader); } // otherwise check to see if this block represents a re-org TTD block: @@ -152,7 +168,7 @@ public class TransitionProtocolSchedule implements ProtocolSchedule { .setMessage("returning a pre-merge schedule because block {} is pre-merge or TTD") .addArgument(blockHeader::toLogString) .log(); - return getPreMergeSchedule().getByBlockNumber(blockHeader.getNumber()); + return getPreMergeSchedule().getByBlockHeader(blockHeader); } } // else return post-merge schedule @@ -160,7 +176,7 @@ public class TransitionProtocolSchedule implements ProtocolSchedule { .setMessage(" for {} returning a post-merge schedule") .addArgument(blockHeader::toLogString) .log(); - return getPostMergeSchedule().getByBlockNumber(blockHeader.getNumber()); + return getPostMergeSchedule().getByBlockHeader(blockHeader); } /** diff --git a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java index 4dc9afb8a5..3f896f002a 100644 --- a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java +++ b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinator.java @@ -729,9 +729,9 @@ public class MergeCoordinator implements MergeMiningCoordinator, BadChainListene @Override public Optional getLatestValidAncestor(final Hash blockHash) { final var chain = protocolContext.getBlockchain(); - final var chainHeadNum = chain.getChainHeadBlockNumber(); + final var chainHeadHeader = chain.getChainHeadHeader(); return findValidAncestor( - chain, blockHash, protocolSchedule.getByBlockNumber(chainHeadNum).getBadBlocksManager()); + chain, blockHash, protocolSchedule.getByBlockHeader(chainHeadHeader).getBadBlocksManager()); } @Override @@ -740,8 +740,7 @@ public class MergeCoordinator implements MergeMiningCoordinator, BadChainListene final var self = chain.getBlockHeader(blockHeader.getHash()); if (self.isEmpty()) { - final var badBlocks = - protocolSchedule.getByBlockNumber(blockHeader.getNumber()).getBadBlocksManager(); + final var badBlocks = protocolSchedule.getByBlockHeader(blockHeader).getBadBlocksManager(); return findValidAncestor(chain, blockHeader.getParentHash(), badBlocks); } return self.map(BlockHeader::getHash); @@ -872,7 +871,7 @@ public class MergeCoordinator implements MergeMiningCoordinator, BadChainListene @Override public void addBadBlock(final Block block, final Optional maybeCause) { protocolSchedule - .getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber()) + .getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader()) .getBadBlocksManager() .addBadBlock(block, maybeCause); } @@ -887,7 +886,7 @@ public class MergeCoordinator implements MergeMiningCoordinator, BadChainListene private BadBlockManager getBadBlockManager() { final BadBlockManager badBlocksManager = protocolSchedule - .getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber()) + .getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader()) .getBadBlocksManager(); return badBlocksManager; } @@ -895,7 +894,7 @@ public class MergeCoordinator implements MergeMiningCoordinator, BadChainListene @Override public Optional getLatestValidHashOfBadBlock(Hash blockHash) { return protocolSchedule - .getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber()) + .getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader()) .getBadBlocksManager() .getLatestValidHash(blockHash); } 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 6015fb58d8..4030bae6c6 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 @@ -15,6 +15,8 @@ package org.hyperledger.besu.consensus.merge; 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.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -71,11 +73,10 @@ public class TransitionProtocolScheduleTest { @Test public void returnPostMergeIfFinalizedExists() { when(mergeContext.getFinalized()).thenReturn(Optional.of(mock(BlockHeader.class))); - when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER); - transitionProtocolSchedule.getByBlockHeader(blockHeader); + transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(blockHeader); - verifyPostMergeProtocolScheduleReturned(); + verifyPostMergeProtocolScheduleReturnedUsingBlockHeader(); } @Test @@ -83,11 +84,9 @@ public class TransitionProtocolScheduleTest { when(mergeContext.getFinalized()).thenReturn(Optional.empty()); when(mergeContext.isPostMerge()).thenReturn(false); - when(blockHeader.getNumber()).thenReturn(BLOCK_NUMBER); - - transitionProtocolSchedule.getByBlockHeader(blockHeader); + transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(blockHeader); - verifyPreMergeProtocolScheduleReturned(); + verifyPreMergeProtocolScheduleReturnedUsingBlockHeader(); } @Test @@ -98,15 +97,14 @@ public class TransitionProtocolScheduleTest { 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)) .thenReturn(Optional.of(Difficulty.of(95L))); - transitionProtocolSchedule.getByBlockHeader(blockHeader); + transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(blockHeader); - verifyPreMergeProtocolScheduleReturned(); + verifyPreMergeProtocolScheduleReturnedUsingBlockHeader(); } @Test @@ -117,15 +115,14 @@ public class TransitionProtocolScheduleTest { 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)) .thenReturn(Optional.of(Difficulty.of(95L))); - transitionProtocolSchedule.getByBlockHeader(blockHeader); + transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(blockHeader); - verifyPreMergeProtocolScheduleReturned(); + verifyPreMergeProtocolScheduleReturnedUsingBlockHeader(); } @Test @@ -142,20 +139,9 @@ public class TransitionProtocolScheduleTest { when(blockchain.getTotalDifficultyByHash(parentHash)) .thenReturn(Optional.of(Difficulty.of(105L))); - transitionProtocolSchedule.getByBlockHeader(blockHeader); + transitionProtocolSchedule.getByBlockHeaderWithTransitionReorgHandling(blockHeader); - verifyPostMergeProtocolScheduleReturned(); - } - - @Test - public void getByBlockHeader_returnsTimestampScheduleIfPresent() { - when(timestampSchedule.getByTimestamp(TIMESTAMP)) - .thenReturn(Optional.of(mock(ProtocolSpec.class))); - - assertThat(transitionProtocolSchedule.getByBlockHeader(blockHeader)).isNotNull(); - - verify(timestampSchedule).getByTimestamp(TIMESTAMP); - verifyNoMergeScheduleInteractions(); + verifyPostMergeProtocolScheduleReturnedUsingBlockHeader(); } @Test @@ -176,7 +162,7 @@ public class TransitionProtocolScheduleTest { transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER); - verifyPreMergeProtocolScheduleReturned(); + verifyPreMergeProtocolScheduleReturnedUsingBlockNumber(); } @Test @@ -186,25 +172,75 @@ public class TransitionProtocolScheduleTest { transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER); - verifyPostMergeProtocolScheduleReturned(); + verifyPostMergeProtocolScheduleReturnedUsingBlockNumber(); + } + + @Test + public void getByBlockNumber_delegatesToPreMergeScheduleWhenTimestampScheduleDoesNotExist() { + when(blockchain.getBlockHeader(BLOCK_NUMBER)).thenReturn(Optional.of(blockHeader)); + when(mergeContext.isPostMerge()).thenReturn(false); + + transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER); + + verifyPreMergeProtocolScheduleReturnedUsingBlockNumber(); } @Test public void getByBlockNumber_delegatesToPostMergeScheduleWhenTimestampScheduleDoesNotExist() { + when(blockchain.getBlockHeader(BLOCK_NUMBER)).thenReturn(Optional.of(blockHeader)); when(mergeContext.isPostMerge()).thenReturn(true); transitionProtocolSchedule.getByBlockNumber(BLOCK_NUMBER); - verifyPostMergeProtocolScheduleReturned(); + verifyPostMergeProtocolScheduleReturnedUsingBlockNumber(); + } + + @Test + public void getByBlockHeader_returnsTimestampScheduleIfPresent() { + when(timestampSchedule.getByTimestamp(TIMESTAMP)) + .thenReturn(Optional.of(mock(ProtocolSpec.class))); + + assertThat(transitionProtocolSchedule.getByBlockHeader(blockHeader)).isNotNull(); + + verify(timestampSchedule).getByTimestamp(TIMESTAMP); + verifyNoMergeScheduleInteractions(); + } + + @Test + public void getByBlockHeader_delegatesToPreMergeScheduleWhenTimestampScheduleDoesNotExist() { + when(mergeContext.isPostMerge()).thenReturn(false); + + transitionProtocolSchedule.getByBlockHeader(blockHeader); + + verifyPreMergeProtocolScheduleReturnedUsingBlockHeader(); + } + + @Test + public void getByBlockHeader_delegatesToPostMergeScheduleWhenTimestampScheduleDoesNotExist() { + when(mergeContext.isPostMerge()).thenReturn(true); + + transitionProtocolSchedule.getByBlockHeader(blockHeader); + + verifyPostMergeProtocolScheduleReturnedUsingBlockHeader(); + } + + private void verifyPreMergeProtocolScheduleReturnedUsingBlockNumber() { + verify(preMergeProtocolSchedule).getByBlockNumber(anyLong()); + verifyNoInteractions(postMergeProtocolSchedule); + } + + private void verifyPostMergeProtocolScheduleReturnedUsingBlockNumber() { + verify(postMergeProtocolSchedule).getByBlockNumber(anyLong()); + verifyNoInteractions(preMergeProtocolSchedule); } - private void verifyPreMergeProtocolScheduleReturned() { - verify(preMergeProtocolSchedule).getByBlockNumber(BLOCK_NUMBER); + private void verifyPreMergeProtocolScheduleReturnedUsingBlockHeader() { + verify(preMergeProtocolSchedule).getByBlockHeader(any()); verifyNoInteractions(postMergeProtocolSchedule); } - private void verifyPostMergeProtocolScheduleReturned() { - verify(postMergeProtocolSchedule).getByBlockNumber(BLOCK_NUMBER); + private void verifyPostMergeProtocolScheduleReturnedUsingBlockHeader() { + verify(postMergeProtocolSchedule).getByBlockHeader(any()); verifyNoInteractions(preMergeProtocolSchedule); } diff --git a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinatorTest.java b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinatorTest.java index 48f4d66fa9..6c107d0785 100644 --- a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinatorTest.java +++ b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeCoordinatorTest.java @@ -181,7 +181,7 @@ public class MergeCoordinatorTest implements MergeGenesisConfigHelper { return spec; }) .when(protocolSchedule) - .getByBlockNumber(anyLong()); + .getByBlockHeader(any(BlockHeader.class)); protocolContext = new ProtocolContext(blockchain, worldStateArchive, mergeContext); var mutable = worldStateArchive.getMutable(); diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java index caa6842a72..5df625a4a8 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java @@ -82,8 +82,7 @@ public class JsonRpcTestMethodsFactory { this.synchronizer = mock(Synchronizer.class); for (final Block block : importer.getBlocks()) { - final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader()); final BlockImporter blockImporter = protocolSpec.getBlockImporter(); blockImporter.importBlock(context, block, HeaderValidationMode.FULL); } diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java index 5cda3c77d1..b9293d25de 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java @@ -71,8 +71,7 @@ public class EthGetBlockByNumberLatestDesyncIntegrationTest { for (final Block block : importer.getBlocks()) { final ProtocolSchedule protocolSchedule = importer.getProtocolSchedule(); - final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader()); final BlockImporter blockImporter = protocolSpec.getBlockImporter(); blockImporter.importBlock(context, block, HeaderValidationMode.FULL); } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/DebugReplayBlock.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/DebugReplayBlock.java index 2ed8959052..ab14cb9412 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/DebugReplayBlock.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/DebugReplayBlock.java @@ -69,20 +69,18 @@ public class DebugReplayBlock extends AbstractBlockParameterMethod { 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 - .getByBlockNumber(blockNumber) + .getByBlockHeader(block.getHeader()) .getBlockValidator() .validateAndProcessBlock( - protocolContext, - maybeBlock.get(), - HeaderValidationMode.FULL, - HeaderValidationMode.NONE, - true); + protocolContext, block, HeaderValidationMode.FULL, HeaderValidationMode.NONE, true); } catch (Exception e) { LOG.error(e.getMessage()); return new JsonRpcErrorResponse(request.getRequest().getId(), INTERNAL_ERROR); diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlocks.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlocks.java index 8c49f9a9b5..6f049d485e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlocks.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlocks.java @@ -50,7 +50,7 @@ public class DebugGetBadBlocks implements JsonRpcMethod { public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final List response = protocolSchedule - .getByBlockNumber(blockchain.headBlockNumber()) + .getByBlockHeader(blockchain.headBlockHeader()) .getBadBlocksManager() .getBadBlocks() .stream() diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugResyncWorldstate.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugResyncWorldstate.java index 578fdbf070..c7befa050f 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugResyncWorldstate.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugResyncWorldstate.java @@ -44,7 +44,7 @@ public class DebugResyncWorldstate implements JsonRpcMethod { @Override public JsonRpcResponse response(final JsonRpcRequestContext request) { protocolSchedule - .getByBlockNumber(blockchain.getChainHeadBlockNumber()) + .getByBlockHeader(blockchain.getChainHeadHeader()) .getBadBlocksManager() .reset(); return new JsonRpcSuccessResponse( diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFile.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFile.java index 4115e67371..65e486a9ca 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFile.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFile.java @@ -60,7 +60,7 @@ public class DebugStandardTraceBadBlockToFile extends DebugStandardTraceBlockToF final Blockchain blockchain = blockchainQueries.get().getBlockchain(); final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(blockchain.getChainHeadHeader().getNumber()); + protocolSchedule.getByBlockHeader(blockchain.getChainHeadHeader()); final BadBlockManager badBlockManager = protocolSpec.getBadBlocksManager(); return badBlockManager diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHash.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHash.java index e8aceb8512..424f0f772e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHash.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHash.java @@ -79,7 +79,7 @@ public class EthGetMinerDataByBlockHash implements JsonRpcMethod { final ProtocolSchedule protocolSchedule, final BlockchainQueries blockchainQueries) { final BlockHeader blockHeader = block.getHeader(); - final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockHeader.getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader); final Wei staticBlockReward = protocolSpec.getBlockReward(); final Wei transactionFee = block.getTransactions().stream() diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/FlatTraceGenerator.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/FlatTraceGenerator.java index 09bdbdc51b..e888178fde 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/FlatTraceGenerator.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/FlatTraceGenerator.java @@ -336,7 +336,7 @@ public class FlatTraceGenerator { if ("STOP".equals(traceFrame.getOpcode()) && resultBuilder.isGasUsedEmpty()) { final long callStipend = protocolSchedule - .getByBlockNumber(block.getHeader().getNumber()) + .getByBlockHeader(block.getHeader()) .getGasCalculator() .getAdditionalCallStipend(); tracesContexts.stream() diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGenerator.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGenerator.java index e0a8514e98..db84938924 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGenerator.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGenerator.java @@ -46,7 +46,7 @@ public class RewardTraceGenerator { final BlockHeader blockHeader = block.getHeader(); final List ommers = block.getBody().getOmmers(); - final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockHeader.getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader); final Wei blockReward = protocolSpec.getBlockReward(); final MiningBeneficiaryCalculator miningBeneficiaryCalculator = protocolSpec.getMiningBeneficiaryCalculator(); diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java index 67ff71697e..cf717b7e28 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java @@ -89,7 +89,7 @@ public abstract class PrivacyApiGroupJsonRpcMethods extends ApiGroupJsonRpcMetho public GasCalculator getGasCalculator() { return protocolSchedule - .getByBlockNumber(blockchainQueries.headBlockNumber()) + .getByBlockHeader(blockchainQueries.headBlockHeader()) .getGasCalculator(); } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java index 776f590539..c0f14dbf6b 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java @@ -141,6 +141,15 @@ public class BlockchainQueries { return blockchain.getChainHeadBlockNumber(); } + /** + * Return the header of the head of the chain. + * + * @return The header of the head of the chain. + */ + public BlockHeader headBlockHeader() { + return blockchain.getChainHeadHeader(); + } + /** * Return the header of the last finalized block. * diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java index a0cb0efa00..3045c27213 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java @@ -214,8 +214,7 @@ public abstract class AbstractEthGraphQLHttpServiceTest { void importBlock(final int n) { final Block block = BLOCKS.get(n); - final ProtocolSpec protocolSpec = - PROTOCOL_SCHEDULE.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = PROTOCOL_SCHEDULE.getByBlockHeader(block.getHeader()); final BlockImporter blockImporter = protocolSpec.getBlockImporter(); blockImporter.importBlock(context, block, HeaderValidationMode.FULL); } diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlockTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlockTest.java index 6ec7b79b5e..21fa5c50be 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlockTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetBadBlockTest.java @@ -16,7 +16,7 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; -import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -95,9 +95,8 @@ public class DebugGetBadBlockTest { badBlockManager.addBadBlock(badBlockWithTransaction, Optional.empty()); badBlockManager.addBadBlock(badBlockWoTransaction, Optional.empty()); - final ProtocolSpec protocolSpec = mock(ProtocolSpec.class); - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(any())).thenReturn(protocolSpec); when(protocolSpec.getBadBlocksManager()).thenReturn(badBlockManager); final JsonRpcRequestContext request = @@ -125,7 +124,8 @@ public class DebugGetBadBlockTest { @Test public void shouldReturnCorrectResponseWhenNoInvalidBlockFound() { final ProtocolSpec protocolSpec = mock(ProtocolSpec.class); - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec); + + when(protocolSchedule.getByBlockHeader(any())).thenReturn(protocolSpec); when(protocolSpec.getBadBlocksManager()).thenReturn(badBlockManager); final JsonRpcRequestContext request = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFileTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFileTest.java index d04dbff1f9..27e48157c8 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFileTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugStandardTraceBadBlockToFileTest.java @@ -88,7 +88,7 @@ public class DebugStandardTraceBadBlockToFileTest { when(protocolSpec.getBadBlocksManager()).thenReturn(badBlockManager); when(blockchainQueries.getBlockchain()).thenReturn(blockchain); when(blockchain.getChainHeadHeader()).thenReturn(new BlockHeaderTestFixture().buildHeader()); - when(protocolSchedule.getByBlockNumber(blockHeader.getNumber())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(blockHeader)).thenReturn(protocolSpec); when(transactionTracer.traceTransactionToFile(eq(block.getHash()), any(), any())) .thenReturn(paths); final JsonRpcSuccessResponse response = diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHashTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHashTest.java index 81e1191178..f0fa298f0b 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHashTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockHashTest.java @@ -78,7 +78,7 @@ public class EthGetMinerDataByBlockHashTest { header, Collections.emptyList(), Collections.emptyList(), Difficulty.of(100L), 5); when(blockchainQueries.blockByHash(any())).thenReturn(Optional.of(blockWithMetadata)); - when(protocolSchedule.getByBlockNumber(header.getNumber())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(header)).thenReturn(protocolSpec); when(protocolSpec.getBlockReward()).thenReturn(Wei.fromEth(2)); when(blockchainQueries.getBlockchain()).thenReturn(blockChain); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockNumberTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockNumberTest.java index ff0bfc1f42..99e6e6889e 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockNumberTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetMinerDataByBlockNumberTest.java @@ -76,7 +76,7 @@ public class EthGetMinerDataByBlockNumberTest { header, Collections.emptyList(), Collections.emptyList(), Difficulty.of(100L), 5); when(blockchainQueries.blockByNumber(anyLong())).thenReturn(Optional.of(blockWithMetadata)); - when(protocolSchedule.getByBlockNumber(header.getNumber())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(header)).thenReturn(protocolSpec); when(protocolSpec.getBlockReward()).thenReturn(Wei.fromEth(2)); when(blockchainQueries.getBlockchain()).thenReturn(blockChain); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGeneratorTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGeneratorTest.java index 5bfb60e329..3cfed8a780 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGeneratorTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/RewardTraceGeneratorTest.java @@ -72,7 +72,7 @@ public class RewardTraceGeneratorTest { final BlockHeader blockHeader = gen.header(0x0A, blockBody, new BlockDataGenerator.BlockOptions()); block = new Block(blockHeader, blockBody); - when(protocolSchedule.getByBlockNumber(block.getHeader().getNumber())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(block.getHeader())).thenReturn(protocolSpec); when(protocolSpec.getBlockReward()).thenReturn(blockReward); when(protocolSpec.getMiningBeneficiaryCalculator()).thenReturn(miningBeneficiaryCalculator); when(miningBeneficiaryCalculator.calculateBeneficiary(block.getHeader())) diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java index f0f23147d4..c4e64c7a43 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueriesTest.java @@ -196,6 +196,17 @@ public class BlockchainQueriesTest { assertThat(result).isEqualTo(3L); } + @Test + public void getHeadBlockHeader() { + final BlockchainWithData data = setupBlockchain(3); + final BlockchainQueries queries = data.blockchainQueries; + + final BlockHeader targetBlockHeader = data.blockData.get(2).block.getHeader(); + + BlockHeader result = queries.headBlockHeader(); + assertThat(targetBlockHeader).isEqualTo(result); + } + @Test public void getAccountStorageBlockNumber() { final List
addresses = Arrays.asList(gen.address(), gen.address(), gen.address()); diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/BlockMiner.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/BlockMiner.java index 422dc6bb5e..ccbfb52ab6 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/BlockMiner.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/BlockMiner.java @@ -141,7 +141,7 @@ public class BlockMiner implements Runnable { block.getBody().getTransactions().size()); final BlockImporter importer = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()).getBlockImporter(); + protocolSchedule.getByBlockHeader(block.getHeader()).getBlockImporter(); final BlockImportResult blockImportResult = importer.importBlock(protocolContext, block, HeaderValidationMode.FULL); if (blockImportResult.isImported()) { diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionSimulator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionSimulator.java index 433e763de5..061c56353f 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionSimulator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/PrivateTransactionSimulator.java @@ -125,7 +125,7 @@ public class PrivateTransactionSimulator { final PrivateTransaction transaction = getPrivateTransaction(callParams, header, privacyGroupId, disposablePrivateState); - final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(header.getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(header); final PrivateTransactionProcessor privateTransactionProcessor = protocolSpec.getPrivateTransactionProcessor(); diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigration.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigration.java index 808431eac1..af9fc214b8 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigration.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/storage/migration/PrivateStorageMigration.java @@ -93,7 +93,7 @@ public class PrivateStorageMigration { final int lastPmtIndex = findLastPMTIndexInBlock(block); if (lastPmtIndex >= 0) { - final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockNumber); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader); final PrivateMigrationBlockProcessor privateMigrationBlockProcessor = privateMigrationBlockProcessorBuilder.apply(protocolSpec); diff --git a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java index be2643af5e..46fe93554c 100644 --- a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java +++ b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java @@ -243,8 +243,7 @@ public class BlockchainSetupUtil { if (block.getHeader().getNumber() == BlockHeader.GENESIS_BLOCK_NUMBER) { continue; } - final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader()); final BlockImporter blockImporter = protocolSpec.getBlockImporter(); final BlockImportResult result = blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/BlockPropagationManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/BlockPropagationManager.java index f097b41ea2..2e9e13b45d 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/BlockPropagationManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/BlockPropagationManager.java @@ -607,8 +607,7 @@ public class BlockPropagationManager implements UnverifiedForkchoiceListener { new IllegalArgumentException( "Incapable of retrieving header from non-existent parent of " + block.toLogString())); - final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader()); final BlockHeaderValidator blockHeaderValidator = protocolSpec.getBlockHeaderValidator(); final BadBlockManager badBlockManager = protocolSpec.getBadBlocksManager(); return ethContext diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java index 93143fcf58..6574d48e67 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContext.java @@ -261,12 +261,12 @@ public class BackwardSyncContext { return protocolContext; } - public BlockValidator getBlockValidator(final long blockNumber) { - return protocolSchedule.getByBlockNumber(blockNumber).getBlockValidator(); + public BlockValidator getBlockValidator(final BlockHeader blockHeader) { + return protocolSchedule.getByBlockHeader(blockHeader).getBlockValidator(); } public BlockValidator getBlockValidatorForBlock(final Block block) { - return getBlockValidator(block.getHeader().getNumber()); + return getBlockValidator(block.getHeader()); } public boolean isReady() { diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStep.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStep.java index b42933e813..c2297faa19 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStep.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStep.java @@ -115,7 +115,7 @@ public class FastImportBlocksStep implements Consumer> { protected boolean importBlock(final BlockWithReceipts blockWithReceipts) { final BlockImporter importer = - protocolSchedule.getByBlockNumber(blockWithReceipts.getNumber()).getBlockImporter(); + protocolSchedule.getByBlockHeader(blockWithReceipts.getHeader()).getBlockImporter(); final BlockImportResult blockImportResult = importer.fastImportBlock( protocolContext, diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStep.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStep.java index 87d04530db..a43646d4a5 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStep.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStep.java @@ -58,7 +58,7 @@ public class FullImportBlockStep implements Consumer { final long blockNumber = block.getHeader().getNumber(); final String blockHash = block.getHash().toHexString(); final BlockImporter importer = - protocolSchedule.getByBlockNumber(blockNumber).getBlockImporter(); + protocolSchedule.getByBlockHeader(block.getHeader()).getBlockImporter(); final BlockImportResult blockImportResult = importer.importBlock(protocolContext, block, HeaderValidationMode.SKIP_DETACHED); if (!blockImportResult.isImported()) { diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/range/RangeHeadersValidationStep.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/range/RangeHeadersValidationStep.java index 79ed3c5310..80530286da 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/range/RangeHeadersValidationStep.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/range/RangeHeadersValidationStep.java @@ -75,8 +75,7 @@ public class RangeHeadersValidationStep implements Function { @Override protected void executeTask() { try { - final ProtocolSpec protocolSpec = - protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); + final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(block.getHeader()); final BlockImporter blockImporter = protocolSpec.getBlockImporter(); LOG.atDebug() .setMessage("Running import task for block {}") diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java index d8b99cb894..b3f0dda81d 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java @@ -264,7 +264,7 @@ public class TransactionPool implements BlockAddedObserver { private MainnetTransactionValidator getTransactionValidator() { return protocolSchedule - .getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber()) + .getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader()) .getTransactionValidator(); } @@ -293,7 +293,7 @@ public class TransactionPool implements BlockAddedObserver { } final FeeMarket feeMarket = - protocolSchedule.getByBlockNumber(chainHeadBlockHeader.getNumber()).getFeeMarket(); + protocolSchedule.getByBlockHeader(chainHeadBlockHeader).getFeeMarket(); final TransactionInvalidReason priceInvalidReason = validatePrice(transaction, isLocal, feeMarket); @@ -404,8 +404,8 @@ public class TransactionPool implements BlockAddedObserver { && transactionReplaySupportedAtBlock(chainHeadBlockHeader); } - private boolean transactionReplaySupportedAtBlock(final BlockHeader block) { - return protocolSchedule.getByBlockNumber(block.getNumber()).isReplayProtectionSupported(); + private boolean transactionReplaySupportedAtBlock(final BlockHeader blockHeader) { + return protocolSchedule.getByBlockHeader(blockHeader).isReplayProtectionSupported(); } public Optional getTransactionByHash(final Hash hash) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/RangeHeadersValidationStepTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/RangeHeadersValidationStepTest.java index 478adbfdc6..a0adf39da4 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/RangeHeadersValidationStepTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/RangeHeadersValidationStepTest.java @@ -18,7 +18,7 @@ import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.DETACHED_ONLY; -import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -52,6 +52,7 @@ public class RangeHeadersValidationStepTest { @Mock private BlockHeaderValidator headerValidator; @Mock private ValidationPolicy validationPolicy; @Mock private EthPeer syncTarget; + private final BlockDataGenerator gen = new BlockDataGenerator(); private RangeHeadersValidationStep validationStep; @@ -65,7 +66,7 @@ public class RangeHeadersValidationStepTest { @Before public void setUp() { - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(any(BlockHeader.class))).thenReturn(protocolSpec); when(protocolSpec.getBlockHeaderValidator()).thenReturn(headerValidator); when(validationPolicy.getValidationModeForNextBlock()).thenReturn(DETACHED_ONLY); @@ -79,7 +80,7 @@ public class RangeHeadersValidationStepTest { .thenReturn(true); final Stream result = validationStep.apply(rangeHeaders); - verify(protocolSchedule).getByBlockNumber(firstHeader.getNumber()); + verify(protocolSchedule).getByBlockHeader(firstHeader); verify(validationPolicy).getValidationModeForNextBlock(); verify(headerValidator).validateHeader(firstHeader, rangeStart, protocolContext, DETACHED_ONLY); verifyNoMoreInteractions(headerValidator, validationPolicy); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStepTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStepTest.java index b5fa7cc062..448608df71 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStepTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStepTest.java @@ -19,7 +19,7 @@ import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.FULL; import static org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.LIGHT; -import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -60,7 +60,7 @@ public class FastImportBlocksStepTest { @BeforeEach public void setUp() { - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(any())).thenReturn(protocolSpec); when(protocolSpec.getBlockImporter()).thenReturn(blockImporter); when(validationPolicy.getValidationModeForNextBlock()).thenReturn(FULL); when(ommerValidationPolicy.getValidationModeForNextBlock()).thenReturn(LIGHT); @@ -95,7 +95,7 @@ public class FastImportBlocksStepTest { importBlocksStep.accept(blocksWithReceipts); for (final BlockWithReceipts blockWithReceipts : blocksWithReceipts) { - verify(protocolSchedule).getByBlockNumber(blockWithReceipts.getNumber()); + verify(protocolSchedule).getByBlockHeader(blockWithReceipts.getHeader()); } verify(validationPolicy, times(blocks.size())).getValidationModeForNextBlock(); } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStepTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStepTest.java index c22822af78..ddb8695b32 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStepTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStepTest.java @@ -16,13 +16,14 @@ package org.hyperledger.besu.ethereum.eth.sync.fullsync; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode.SKIP_DETACHED; -import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; +import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockImporter; import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException; import org.hyperledger.besu.ethereum.mainnet.BlockImportResult; @@ -48,7 +49,7 @@ public class FullImportBlockStepTest { @Before public void setUp() { - when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec); + when(protocolSchedule.getByBlockHeader(any(BlockHeader.class))).thenReturn(protocolSpec); when(protocolSpec.getBlockImporter()).thenReturn(blockImporter); importBlocksStep = @@ -64,7 +65,7 @@ public class FullImportBlockStepTest { .thenReturn(new BlockImportResult(true)); importBlocksStep.accept(block); - verify(protocolSchedule).getByBlockNumber(block.getHeader().getNumber()); + verify(protocolSchedule).getByBlockHeader(block.getHeader()); verify(blockImporter).importBlock(protocolContext, block, SKIP_DETACHED); } diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java index fda9c80db0..270cc1e777 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java @@ -272,8 +272,8 @@ public class RetestethContext { return blockchain.getChainHeadBlockNumber(); } - public ProtocolSpec getProtocolSpec(final long blockNumber) { - return getProtocolSchedule().getByBlockNumber(blockNumber); + public ProtocolSpec getProtocolSpec(final BlockHeader blockHeader) { + return getProtocolSchedule().getByBlockHeader(blockHeader); } public BlockHeader getBlockHeader(final long blockNumber) { diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestImportRawBlock.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestImportRawBlock.java index 7c4c77da86..32430eb416 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestImportRawBlock.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestImportRawBlock.java @@ -79,7 +79,7 @@ public class TestImportRawBlock implements JsonRpcMethod { } else { // otherwise attempt to import the block final BlockImporter blockImporter = - context.getProtocolSpec(block.getHeader().getNumber()).getBlockImporter(); + context.getProtocolSpec(block.getHeader()).getBlockImporter(); final BlockImportResult result = blockImporter.importBlock( protocolContext, diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestMineBlocks.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestMineBlocks.java index c261f65ae0..59ac6df240 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestMineBlocks.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestMineBlocks.java @@ -81,7 +81,7 @@ public class TestMineBlocks implements JsonRpcMethod { retesethClock.advanceSeconds(1); final BlockImporter blockImporter = - protocolSchedule.getByBlockNumber(blockchain.getChainHeadBlockNumber()).getBlockImporter(); + protocolSchedule.getByBlockHeader(blockchain.getChainHeadHeader()).getBlockImporter(); final BlockImportResult result = blockImporter.importBlock( protocolContext, block, headerValidationMode, headerValidationMode);