Skip to content

Commit

Permalink
Replace getByBlockNumber by getByBlockHeader (hyperledger#5020)
Browse files Browse the repository at this point in the history
* Replace getByBlockNumber by getByBlockHeader where trivial

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Fix tests

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Fix tests

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Rolledback change since tests will need non trivial changes

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Rollback replace

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Change other 2 trivial tests

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* spotless

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Pass ProcessableBlockHeader to the mocks

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Replace getByBlockNumber by getByBlockHeader

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Add mock of blockHeader

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Use any() for the tests

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

* Add unit test for headBlockHeader()

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>

---------

Signed-off-by: Gabriel Fukushima <gabrielfukushima@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
gfukushima and macfarla authored Feb 5, 2023
1 parent e715010 commit 9ceebc4
Show file tree
Hide file tree
Showing 46 changed files with 111 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ private void setOptionalFields(

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);
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.getByBlockNumber(blockNumber);
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(header);
final BlockHeader lastHeader = previousHeader;

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

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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 @@ -131,7 +130,7 @@ public void assertPreMergeScheduleForNotPostMerge() {
var preMergeProtocolSpec = mock(ProtocolSpec.class);
when(mergeContext.isPostMerge()).thenReturn(Boolean.FALSE);
when(mergeContext.getFinalized()).thenReturn(Optional.empty());
when(preMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(preMergeProtocolSpec);
when(preMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(preMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(preMergeProtocolSpec);
}
Expand All @@ -141,7 +140,7 @@ public void assertPostMergeScheduleForAnyBlockWhenPostMergeAndFinalized() {
var mockBlock = new BlockHeaderTestFixture().buildHeader();
var postMergeProtocolSpec = mock(ProtocolSpec.class);
when(mergeContext.getFinalized()).thenReturn(Optional.of(mockBlock));
when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec);
when(postMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(postMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(postMergeProtocolSpec);
}
Expand All @@ -164,7 +163,7 @@ public void assertPreMergeScheduleForBelowTerminalBlockWhenPostMergeIfNotFinaliz
when(mockBlockchain.getTotalDifficultyByHash(any()))
.thenReturn(Optional.of(Difficulty.of(1335L)));

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

when(postMergeProtocolSchedule.getByBlockNumber(anyLong())).thenReturn(postMergeProtocolSpec);
when(postMergeProtocolSchedule.getByBlockHeader(any())).thenReturn(postMergeProtocolSpec);
assertThat(transitionProtocolSchedule.getByBlockHeader(mockBlock))
.isEqualTo(postMergeProtocolSpec);
}
Expand All @@ -213,7 +212,7 @@ void assertDetachedRulesForPostMergeBlocks(final BlockHeaderValidator validator)
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()
Expand All @@ -235,7 +234,7 @@ void assertDetachedRulesForPostMergeBlocks(final BlockHeaderValidator validator)
var mergeFriendlyValidation =
transitionProtocolSchedule
.getPreMergeSchedule()
.getByBlockNumber(1L)
.getByBlockHeader(mockBlock)
.getBlockHeaderValidator()
.validateHeader(
mockBlock, mockParentBlock, protocolContext, HeaderValidationMode.DETACHED_ONLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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 @@ -125,7 +124,7 @@ public void setUp() {
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()))
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
.getByBlockNumber(blockHeader.getNumber())
.getByBlockHeader(blockHeader)
.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().getByBlockNumber(blockHeader.getNumber());
return getPreMergeSchedule().getByBlockHeader(blockHeader);
}

// 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().getByBlockNumber(blockHeader.getNumber());
return getPreMergeSchedule().getByBlockHeader(blockHeader);
}
}
// else return post-merge schedule
debugLambda(LOG, " for {} returning a post-merge schedule", blockHeader::toLogString);
return getPostMergeSchedule().getByBlockNumber(blockHeader.getNumber());
return getPostMergeSchedule().getByBlockHeader(blockHeader);
}

/**
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 chainHeadNum = chain.getChainHeadBlockNumber();
final var chainHeadHeader = chain.getChainHeadHeader();
return findValidAncestor(
chain, blockHash, protocolSchedule.getByBlockNumber(chainHeadNum).getBadBlocksManager());
chain, blockHash, protocolSchedule.getByBlockHeader(chainHeadHeader).getBadBlocksManager());
}

@Override
Expand All @@ -692,8 +692,7 @@ public Optional<Hash> getLatestValidAncestor(final BlockHeader blockHeader) {
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);
Expand Down Expand Up @@ -824,7 +823,7 @@ protected interface MergeBlockCreatorFactory {
@Override
public void addBadBlock(final Block block, final Optional<Throwable> maybeCause) {
protocolSchedule
.getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber())
.getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader())
.getBadBlocksManager()
.addBadBlock(block, maybeCause);
}
Expand All @@ -839,15 +838,15 @@ public boolean isBadBlock(final Hash blockHash) {
private BadBlockManager getBadBlockManager() {
final BadBlockManager badBlocksManager =
protocolSchedule
.getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber())
.getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader())
.getBadBlocksManager();
return badBlocksManager;
}

@Override
public Optional<Hash> getLatestValidHashOfBadBlock(Hash blockHash) {
return protocolSchedule
.getByBlockNumber(protocolContext.getBlockchain().getChainHeadBlockNumber())
.getByBlockHeader(protocolContext.getBlockchain().getChainHeadHeader())
.getBadBlocksManager()
.getLatestValidHash(blockHash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 @@ -71,7 +72,6 @@ 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,8 +83,6 @@ 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 @@ -98,7 +96,6 @@ 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 @@ -117,7 +114,6 @@ 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 @@ -159,52 +155,45 @@ public void getByBlockHeader_returnsTimestampScheduleIfPresent() {
}

@Test
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());
public void getByBlockHeader_delegatesToPreMergeScheduleWhenBlockNotFound() {
when(mergeContext.isPostMerge()).thenReturn(false);

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

verifyPreMergeProtocolScheduleReturned();
}

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

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

verifyPostMergeProtocolScheduleReturned();
}

@Test
public void getByBlockNumber_delegatesToPostMergeScheduleWhenTimestampScheduleDoesNotExist() {
public void getByBlockHeader_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.getByBlockNumber(BLOCK_NUMBER);
transitionProtocolSchedule.getByBlockHeader(blockHeader);

verifyPostMergeProtocolScheduleReturned();
}

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

private void verifyPostMergeProtocolScheduleReturned() {
verify(postMergeProtocolSchedule).getByBlockNumber(BLOCK_NUMBER);
verify(postMergeProtocolSchedule).getByBlockHeader(any());
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)
.getByBlockNumber(anyLong());
.getByBlockHeader(any(BlockHeader.class));

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,8 +82,7 @@ public JsonRpcTestMethodsFactory(final BlockchainImporter importer) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public static void setUpOnce() throws Exception {

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,18 @@ 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
.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);
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
.getByBlockNumber(blockchain.headBlockNumber())
.getByBlockHeader(blockchain.headBlockHeader())
.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
.getByBlockNumber(blockchain.getChainHeadBlockNumber())
.getByBlockHeader(blockchain.getChainHeadHeader())
.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.getByBlockNumber(blockchain.getChainHeadHeader().getNumber());
protocolSchedule.getByBlockHeader(blockchain.getChainHeadHeader());
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.getByBlockNumber(blockHeader.getNumber());
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader);
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
.getByBlockNumber(block.getHeader().getNumber())
.getByBlockHeader(block.getHeader())
.getGasCalculator()
.getAdditionalCallStipend();
tracesContexts.stream()
Expand Down
Loading

0 comments on commit 9ceebc4

Please sign in to comment.