Replace boolean return with BlockImportResult object (#4386)

* Replace boolean return with BlockImportResult object

Signed-off-by: Gabriel Trintinalia <gabriel.trintinalia@consensys.net>
Co-authored-by: Gabriel Trintinalia <gabriel.trintinalia@consensys.net>
pull/4483/head
Gabriel-Trintinalia 2 years ago committed by GitHub
parent 016016d4f7
commit a80ed4e35d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java
  2. 5
      besu/src/main/java/org/hyperledger/besu/chainimport/RlpBlockImporter.java
  3. 5
      besu/src/test/java/org/hyperledger/besu/RunnerTest.java
  4. 5
      consensus/ibft/src/integration-test/java/org/hyperledger/besu/consensus/ibft/tests/round/IbftRoundIntegrationTest.java
  5. 7
      consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftRound.java
  6. 3
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/statemachine/IbftRoundTest.java
  7. 3
      consensus/qbft/src/integration-test/java/org/hyperledger/besu/consensus/qbft/test/round/QbftRoundIntegrationTest.java
  8. 7
      consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/statemachine/QbftRound.java
  9. 2
      consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/statemachine/QbftBlockHeightManagerTest.java
  10. 3
      consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/statemachine/QbftRoundTest.java
  11. 7
      ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/BlockMiner.java
  12. 9
      ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java
  13. 19
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockImporter.java
  14. 48
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/BlockImportResult.java
  15. 16
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockImporter.java
  16. 5
      ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java
  17. 10
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetBlockImporterTest.java
  18. 15
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStep.java
  19. 5
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStep.java
  20. 42
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/tasks/PersistBlockTask.java
  21. 5
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastImportBlocksStepTest.java
  22. 7
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullImportBlockStepTest.java
  23. 5
      ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/vm/BlockchainReferenceTestTools.java
  24. 13
      ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestImportRawBlock.java
  25. 7
      ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/methods/TestMineBlocks.java

@ -26,6 +26,7 @@ import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.evm.worldstate.WorldState;
@ -146,9 +147,9 @@ public class JsonBlockImporter {
.getByBlockNumber(block.getHeader().getNumber())
.getBlockImporter();
final boolean imported =
final BlockImportResult importResult =
importer.importBlock(controller.getProtocolContext(), block, HeaderValidationMode.NONE);
if (imported) {
if (importResult.isImported()) {
LOG.info(
"Successfully created and imported block at height {} ({})",
block.getHeader().getNumber(),
@ -220,7 +221,7 @@ public class JsonBlockImporter {
if (importedBlocks.size() > 0 && blockData.getNumber().isPresent()) {
final long targetParentBlockNumber = blockData.getNumber().get() - 1L;
Optional<BlockHeader> maybeHeader =
final Optional<BlockHeader> maybeHeader =
importedBlocks.stream()
.map(Block::getHeader)
.filter(h -> h.getNumber() == targetParentBlockNumber)
@ -230,7 +231,7 @@ public class JsonBlockImporter {
}
}
long blockNumber;
final long blockNumber;
if (blockData.getNumber().isPresent()) {
blockNumber = blockData.getNumber().get() - 1L;
} else if (importedBlocks.size() > 0) {

@ -25,6 +25,7 @@ import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
@ -208,7 +209,7 @@ public class RlpBlockImporter implements Closeable {
cumulativeTimer.start();
segmentTimer.start();
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final boolean blockImported =
final BlockImportResult blockImported =
blockImporter.importBlock(
context,
block,
@ -216,7 +217,7 @@ public class RlpBlockImporter implements Closeable {
? HeaderValidationMode.LIGHT_SKIP_DETACHED
: HeaderValidationMode.SKIP_DETACHED,
skipPowValidation ? HeaderValidationMode.LIGHT : HeaderValidationMode.FULL);
if (!blockImported) {
if (!blockImported.isImported()) {
throw new IllegalStateException(
"Invalid block at block number " + header.getNumber() + ".");
}

@ -48,6 +48,7 @@ import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.sync.SyncMode;
import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
@ -465,9 +466,9 @@ public final class RunnerTest {
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final boolean result =
final BlockImportResult result =
blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL);
if (!result) {
if (!result.isImported()) {
throw new IllegalStateException("Unable to import block " + block.getHeader().getNumber());
}
}

@ -47,6 +47,7 @@ import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.util.Subscribers;
@ -103,7 +104,7 @@ public class IbftRoundIntegrationTest {
throwingMessageFactory = new MessageFactory(nodeKey);
transmitter = new IbftMessageTransmitter(throwingMessageFactory, multicaster);
BftExtraData proposedExtraData =
final BftExtraData proposedExtraData =
new BftExtraData(Bytes.wrap(new byte[32]), emptyList(), empty(), 0, emptyList());
final BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture();
headerTestFixture.extraData(bftExtraDataEncoder.encode(proposedExtraData));
@ -111,7 +112,7 @@ public class IbftRoundIntegrationTest {
final BlockHeader header = headerTestFixture.buildHeader();
proposedBlock = new Block(header, new BlockBody(emptyList(), emptyList()));
when(blockImporter.importBlock(any(), any(), any())).thenReturn(true);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(new BlockImportResult(true));
protocolContext =
new ProtocolContext(

@ -38,6 +38,7 @@ import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.util.Subscribers;
@ -103,7 +104,7 @@ public class IbftRound {
final RoundChangeCertificate roundChangeCertificate =
roundChangeArtifacts.getRoundChangeCertificate();
Block blockToPublish;
final Block blockToPublish;
if (!bestBlockFromRoundChange.isPresent()) {
LOG.debug("Sending proposal with new block. round={}", roundState.getRoundIdentifier());
blockToPublish = blockCreator.createBlock(headerTimestamp);
@ -260,9 +261,9 @@ public class IbftRound {
blockToImport.getHash());
}
LOG.trace("Importing block with extraData={}", extraData);
final boolean result =
final BlockImportResult result =
blockImporter.importBlock(protocolContext, blockToImport, HeaderValidationMode.FULL);
if (!result) {
if (!result.isImported()) {
LOG.error(
"Failed to import block to chain. block={} extraData={} blockHeader={}",
blockNumber,

@ -52,6 +52,7 @@ import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.util.Subscribers;
@ -120,7 +121,7 @@ public class IbftRoundTest {
when(blockCreator.createBlock(anyLong())).thenReturn(proposedBlock);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(true);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(new BlockImportResult(true));
subscribers.subscribe(minedBlockObserver);
}

@ -49,6 +49,7 @@ import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.util.Subscribers;
@ -113,7 +114,7 @@ public class QbftRoundIntegrationTest {
final BlockHeader header = headerTestFixture.buildHeader();
proposedBlock = new Block(header, new BlockBody(emptyList(), emptyList()));
when(blockImporter.importBlock(any(), any(), any())).thenReturn(true);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(new BlockImportResult(true));
protocolContext =
new ProtocolContext(

@ -42,6 +42,7 @@ import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.util.Subscribers;
@ -107,7 +108,7 @@ public class QbftRound {
final Optional<PreparedCertificate> bestPreparedCertificate =
roundChangeArtifacts.getBestPreparedPeer();
Block blockToPublish;
final Block blockToPublish;
if (bestPreparedCertificate.isEmpty()) {
LOG.debug("Sending proposal with new block. round={}", roundState.getRoundIdentifier());
blockToPublish = blockCreator.createBlock(headerTimestamp);
@ -278,9 +279,9 @@ public class QbftRound {
blockToImport.getHash());
}
LOG.trace("Importing proposed block with extraData={}", extraData);
final boolean result =
final BlockImportResult result =
blockImporter.importBlock(protocolContext, blockToImport, HeaderValidationMode.FULL);
if (!result) {
if (!result.isImported()) {
LOG.error(
"Failed to import proposed block to chain. block={} extraData={} blockHeader={}",
blockNumber,

@ -64,6 +64,7 @@ import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.Util;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.util.Subscribers;
@ -146,6 +147,7 @@ public class QbftBlockHeightManagerTest {
when(messageValidatorFactory.createFutureRoundProposalMessageValidator(anyLong(), any()))
.thenReturn(futureRoundProposalMessageValidator);
when(messageValidatorFactory.createMessageValidator(any(), any())).thenReturn(messageValidator);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(new BlockImportResult(false));
protocolContext =
new ProtocolContext(

@ -56,6 +56,7 @@ import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
import org.hyperledger.besu.util.Subscribers;
@ -128,7 +129,7 @@ public class QbftRoundTest {
when(blockCreator.createBlock(anyLong())).thenReturn(proposedBlock);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(true);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(new BlockImportResult(true));
subscribers.subscribe(minedBlockObserver);
}

@ -20,6 +20,7 @@ import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.util.Subscribers;
@ -140,9 +141,9 @@ public class BlockMiner<M extends AbstractBlockCreator> implements Runnable {
final BlockImporter importer =
protocolSchedule.getByBlockNumber(block.getHeader().getNumber()).getBlockImporter();
final boolean blockImported =
final BlockImportResult blockImportResult =
importer.importBlock(protocolContext, block, HeaderValidationMode.FULL);
if (blockImported) {
if (blockImportResult.isImported()) {
notifyNewBlockListeners(block);
final double taskTimeInSec = stopwatch.elapsed(TimeUnit.MILLISECONDS) / 1000.0;
LOG.info(
@ -159,7 +160,7 @@ public class BlockMiner<M extends AbstractBlockCreator> implements Runnable {
LOG.error("Illegal block mined, could not be imported to local chain.");
}
return blockImported;
return blockImportResult.isImported();
}
public void cancel() {

@ -28,6 +28,7 @@ import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
@ -64,7 +65,7 @@ public class BlockMinerTest {
final ProtocolSchedule protocolSchedule = singleSpecSchedule(protocolSpec);
when(protocolSpec.getBlockImporter()).thenReturn(blockImporter);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(true);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(new BlockImportResult(true));
final MinedBlockObserver observer = mock(MinedBlockObserver.class);
final DefaultBlockScheduler scheduler = mock(DefaultBlockScheduler.class);
@ -103,7 +104,11 @@ public class BlockMinerTest {
final ProtocolSchedule protocolSchedule = singleSpecSchedule(protocolSpec);
when(protocolSpec.getBlockImporter()).thenReturn(blockImporter);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(false, false, true);
when(blockImporter.importBlock(any(), any(), any()))
.thenReturn(
new BlockImportResult(false),
new BlockImportResult(false),
new BlockImportResult(true));
final MinedBlockObserver observer = mock(MinedBlockObserver.class);
final DefaultBlockScheduler scheduler = mock(DefaultBlockScheduler.class);

@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.core;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import java.util.List;
@ -34,10 +35,10 @@ public interface BlockImporter {
* @param context The context to attempt to update
* @param block The block
* @param headerValidationMode Determines the validation to perform on this header.
* @return {@code true} if the block was added somewhere in the blockchain; otherwise {@code
* false}
* @return {@code BlockImportResult}
* @see BlockImportResult
*/
default boolean importBlock(
default BlockImportResult importBlock(
final ProtocolContext context,
final Block block,
final HeaderValidationMode headerValidationMode) {
@ -51,10 +52,10 @@ public interface BlockImporter {
* @param block The block
* @param headerValidationMode Determines the validation to perform on this header.
* @param ommerValidationMode Determines the validation to perform on ommer headers.
* @return {@code true} if the block was added somewhere in the blockchain; otherwise {@code
* false}
* @return {@code BlockImportResult}
* @see BlockImportResult
*/
boolean importBlock(
BlockImportResult importBlock(
ProtocolContext context,
Block block,
HeaderValidationMode headerValidationMode,
@ -69,10 +70,10 @@ public interface BlockImporter {
* @param receipts The receipts associated with this block.
* @param headerValidationMode Determines the validation to perform on this header.
* @param ommerValidationMode Determines the validation to perform on ommer headers.
* @return {@code true} if the block was added somewhere in the blockchain; otherwise {@code
* false}
* @return {@code BlockImportResult}
* @see BlockImportResult
*/
boolean fastImportBlock(
BlockImportResult fastImportBlock(
ProtocolContext context,
Block block,
List<TransactionReceipt> receipts,

@ -0,0 +1,48 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet;
/** The result of a block import. */
public class BlockImportResult {
private final BlockImportStatus status;
public enum BlockImportStatus {
IMPORTED,
NOT_IMPORTED,
ALREADY_IMPORTED
}
public BlockImportResult(final boolean status) {
this.status = status ? BlockImportStatus.IMPORTED : BlockImportStatus.NOT_IMPORTED;
}
public BlockImportResult(final BlockImportStatus status) {
this.status = status;
}
/**
* The result of the block import call
*
* @return {@code true} if the block was added somewhere in the blockchain; otherwise {@code
* false}
*/
public boolean isImported() {
return status == BlockImportStatus.IMPORTED || status == BlockImportStatus.ALREADY_IMPORTED;
}
public BlockImportStatus getStatus() {
return status;
}
}

@ -19,6 +19,7 @@ import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult.BlockImportStatus;
import java.util.List;
@ -31,13 +32,13 @@ public class MainnetBlockImporter implements BlockImporter {
}
@Override
public synchronized boolean importBlock(
public synchronized BlockImportResult importBlock(
final ProtocolContext context,
final Block block,
final HeaderValidationMode headerValidationMode,
final HeaderValidationMode ommerValidationMode) {
if (context.getBlockchain().contains(block.getHash())) {
return true;
return new BlockImportResult(BlockImportStatus.ALREADY_IMPORTED);
}
final var result =
@ -48,11 +49,14 @@ public class MainnetBlockImporter implements BlockImporter {
processingOutputs ->
context.getBlockchain().appendBlock(block, processingOutputs.receipts));
return result.blockProcessingOutputs.isPresent();
return result
.blockProcessingOutputs
.map(blockProcessingOutputs -> new BlockImportResult(true))
.orElseGet(() -> new BlockImportResult(false));
}
@Override
public boolean fastImportBlock(
public BlockImportResult fastImportBlock(
final ProtocolContext context,
final Block block,
final List<TransactionReceipt> receipts,
@ -62,9 +66,9 @@ public class MainnetBlockImporter implements BlockImporter {
if (blockValidator.fastBlockValidation(
context, block, receipts, headerValidationMode, ommerValidationMode)) {
context.getBlockchain().appendBlock(block, receipts);
return true;
return new BlockImportResult(true);
}
return false;
return new BlockImportResult(false);
}
}

@ -28,6 +28,7 @@ import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
@ -245,9 +246,9 @@ public class BlockchainSetupUtil {
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final boolean result =
final BlockImportResult result =
blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL);
if (!result) {
if (!result.isImported()) {
throw new IllegalStateException("Unable to import block " + block.getHeader().getNumber());
}
}

@ -53,10 +53,12 @@ public class MainnetBlockImporterTest {
public void doNotImportBlockIfBlockchainAlreadyHasBlock() {
when(blockchain.contains(hash)).thenReturn(true);
assertThat(
blockImporter.importBlock(
context, block, HeaderValidationMode.FULL, HeaderValidationMode.FULL))
.isTrue();
final BlockImportResult result =
blockImporter.importBlock(
context, block, HeaderValidationMode.FULL, HeaderValidationMode.FULL);
assertThat(result.isImported()).isTrue();
verify(blockValidator, never())
.validateAndProcessBlock(
context, block, HeaderValidationMode.FULL, HeaderValidationMode.FULL);

@ -22,6 +22,7 @@ import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy;
import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import java.util.List;
@ -96,11 +97,13 @@ public class FastImportBlocksStep implements Consumer<List<BlockWithReceipts>> {
protected boolean importBlock(final BlockWithReceipts blockWithReceipts) {
final BlockImporter importer =
protocolSchedule.getByBlockNumber(blockWithReceipts.getNumber()).getBlockImporter();
return importer.fastImportBlock(
protocolContext,
blockWithReceipts.getBlock(),
blockWithReceipts.getReceipts(),
headerValidationPolicy.getValidationModeForNextBlock(),
ommerValidationPolicy.getValidationModeForNextBlock());
final BlockImportResult blockImportResult =
importer.fastImportBlock(
protocolContext,
blockWithReceipts.getBlock(),
blockWithReceipts.getReceipts(),
headerValidationPolicy.getValidationModeForNextBlock(),
ommerValidationPolicy.getValidationModeForNextBlock());
return blockImportResult.isImported();
}
}

@ -19,6 +19,7 @@ import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
@ -58,7 +59,9 @@ public class FullImportBlockStep implements Consumer<Block> {
final String blockHash = block.getHash().toHexString();
final BlockImporter importer =
protocolSchedule.getByBlockNumber(blockNumber).getBlockImporter();
if (!importer.importBlock(protocolContext, block, HeaderValidationMode.SKIP_DETACHED)) {
final BlockImportResult blockImportResult =
importer.importBlock(protocolContext, block, HeaderValidationMode.SKIP_DETACHED);
if (!blockImportResult.isImported()) {
throw new InvalidBlockException("Failed to import block", blockNumber, block.getHash());
}
gasAccumulator += block.getHeader().getGasUsed();

@ -23,6 +23,7 @@ import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.task.AbstractEthTask;
import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
@ -46,7 +47,7 @@ public class PersistBlockTask extends AbstractEthTask<Block> {
private final EthContext ethContext;
private final Block block;
private final HeaderValidationMode validateHeaders;
private boolean blockImported;
private BlockImportResult blockImportResult;
private PersistBlockTask(
final ProtocolSchedule protocolSchedule,
@ -61,7 +62,7 @@ public class PersistBlockTask extends AbstractEthTask<Block> {
this.ethContext = ethContext;
this.block = block;
this.validateHeaders = headerValidationMode;
blockImported = false;
blockImportResult = new BlockImportResult(false);
}
public static PersistBlockTask create(
@ -198,8 +199,8 @@ public class PersistBlockTask extends AbstractEthTask<Block> {
protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
debugLambda(LOG, "Running import task for block {}", block::toLogString);
blockImported = blockImporter.importBlock(protocolContext, block, validateHeaders);
if (!blockImported) {
blockImportResult = blockImporter.importBlock(protocolContext, block, validateHeaders);
if (!blockImportResult.isImported()) {
result.completeExceptionally(
new InvalidBlockException(
"Failed to import block", block.getHeader().getNumber(), block.getHash()));
@ -213,19 +214,26 @@ public class PersistBlockTask extends AbstractEthTask<Block> {
@Override
protected void cleanup() {
if (blockImported) {
final double timeInS = getTaskTimeInSec();
LOG.info(
String.format(
"Imported #%,d / %d tx / %d om / %,d (%01.1f%%) gas / (%s) in %01.3fs. Peers: %d",
block.getHeader().getNumber(),
block.getBody().getTransactions().size(),
block.getBody().getOmmers().size(),
block.getHeader().getGasUsed(),
(block.getHeader().getGasUsed() * 100.0) / block.getHeader().getGasLimit(),
block.getHash().toHexString(),
timeInS,
ethContext.getEthPeers().peerCount()));
final double timeInS = getTaskTimeInSec();
switch (blockImportResult.getStatus()) {
case IMPORTED:
LOG.info(
String.format(
"Imported #%,d / %d tx / %d om / %,d (%01.1f%%) gas / (%s) in %01.3fs. Peers: %d",
block.getHeader().getNumber(),
block.getBody().getTransactions().size(),
block.getBody().getOmmers().size(),
block.getHeader().getGasUsed(),
(block.getHeader().getGasUsed() * 100.0) / block.getHeader().getGasLimit(),
block.getHash().toHexString(),
timeInS,
ethContext.getEthPeers().peerCount()));
break;
case ALREADY_IMPORTED:
LOG.info("Block {} is already imported", block.toLogString());
break;
default:
break;
}
}
}

@ -31,6 +31,7 @@ import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.BlockWithReceipts;
import org.hyperledger.besu.ethereum.eth.sync.ValidationPolicy;
import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
@ -82,7 +83,7 @@ public class FastImportBlocksStepTest {
blockWithReceipts.getReceipts(),
FULL,
LIGHT))
.thenReturn(true);
.thenReturn(new BlockImportResult(true));
}
importBlocksStep.accept(blocksWithReceipts);
@ -99,7 +100,7 @@ public class FastImportBlocksStepTest {
when(blockImporter.fastImportBlock(
protocolContext, block, blockWithReceipts.getReceipts(), FULL, LIGHT))
.thenReturn(false);
.thenReturn(new BlockImportResult(false));
assertThatThrownBy(() -> importBlocksStep.accept(singletonList(blockWithReceipts)))
.isInstanceOf(InvalidBlockException.class);
}

@ -25,6 +25,7 @@ import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
@ -59,7 +60,8 @@ public class FullImportBlockStepTest {
public void shouldImportBlock() {
final Block block = gen.block();
when(blockImporter.importBlock(protocolContext, block, SKIP_DETACHED)).thenReturn(true);
when(blockImporter.importBlock(protocolContext, block, SKIP_DETACHED))
.thenReturn(new BlockImportResult(true));
importBlocksStep.accept(block);
verify(protocolSchedule).getByBlockNumber(block.getHeader().getNumber());
@ -70,7 +72,8 @@ public class FullImportBlockStepTest {
public void shouldThrowExceptionWhenValidationFails() {
final Block block = gen.block();
when(blockImporter.importBlock(protocolContext, block, SKIP_DETACHED)).thenReturn(false);
when(blockImporter.importBlock(protocolContext, block, SKIP_DETACHED))
.thenReturn(new BlockImportResult(false));
assertThatThrownBy(() -> importBlocksStep.accept(block))
.isInstanceOf(InvalidBlockException.class);
}

@ -22,6 +22,7 @@ import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
@ -113,10 +114,10 @@ public class BlockchainReferenceTestTools {
"NoProof".equalsIgnoreCase(spec.getSealEngine())
? HeaderValidationMode.LIGHT
: HeaderValidationMode.FULL;
final boolean imported =
final BlockImportResult importResult =
blockImporter.importBlock(context, block, validationMode, validationMode);
assertThat(imported).isEqualTo(candidateBlock.isValid());
assertThat(importResult.isImported()).isEqualTo(candidateBlock.isValid());
} catch (final RLPException e) {
assertThat(candidateBlock.isValid()).isFalse();
}

@ -23,6 +23,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcRespon
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.retesteth.RetestethContext;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPException;
@ -79,11 +80,13 @@ public class TestImportRawBlock implements JsonRpcMethod {
// otherwise attempt to import the block
final BlockImporter blockImporter =
context.getProtocolSpec(block.getHeader().getNumber()).getBlockImporter();
if (!blockImporter.importBlock(
protocolContext,
block,
context.getHeaderValidationMode(),
context.getHeaderValidationMode())) {
final BlockImportResult result =
blockImporter.importBlock(
protocolContext,
block,
context.getHeaderValidationMode(),
context.getHeaderValidationMode());
if (!result.isImported()) {
LOG.debug("Failed to import block.");
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.BLOCK_IMPORT_ERROR);

@ -24,6 +24,7 @@ import org.hyperledger.besu.ethereum.blockcreation.PoWBlockCreator;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockImporter;
import org.hyperledger.besu.ethereum.mainnet.BlockImportResult;
import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.retesteth.RetestethClock;
@ -80,7 +81,9 @@ public class TestMineBlocks implements JsonRpcMethod {
final BlockImporter blockImporter =
protocolSchedule.getByBlockNumber(blockchain.getChainHeadBlockNumber()).getBlockImporter();
return blockImporter.importBlock(
protocolContext, block, headerValidationMode, headerValidationMode);
final BlockImportResult result =
blockImporter.importBlock(
protocolContext, block, headerValidationMode, headerValidationMode);
return result.isImported();
}
}

Loading…
Cancel
Save