Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Split Block Validation from Importing (#579)
Browse files Browse the repository at this point in the history
Ibft is required to validate a block upon reception, but not import
it until a later time.

As such, IBFT will require validation and importing to be separated.

The validator has been exposed as part of the ProtocolSpecification.
  • Loading branch information
rain-on authored Jan 16, 2019
1 parent ab45103 commit f643dc9
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.pantheon.config.GenesisConfigOptions;
import tech.pegasys.pantheon.consensus.common.EpochManager;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.MainnetBlockValidator;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Util;
import tech.pegasys.pantheon.ethereum.core.Wei;
Expand Down Expand Up @@ -63,6 +64,7 @@ private static ProtocolSpecBuilder<CliqueContext> applyCliqueSpecificModificatio
difficultyCalculator -> cliqueBlockHeaderValidator(secondsBetweenBlocks, epochManager),
difficultyCalculator -> cliqueBlockHeaderValidator(secondsBetweenBlocks, epochManager),
MainnetBlockBodyValidator::new,
MainnetBlockValidator::new,
MainnetBlockImporter::new,
new CliqueDifficultyCalculator(localNodeAddress))
.blockReward(Wei.ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tech.pegasys.pantheon.config.IbftConfigOptions;
import tech.pegasys.pantheon.consensus.common.EpochManager;
import tech.pegasys.pantheon.consensus.common.VoteTallyUpdater;
import tech.pegasys.pantheon.ethereum.MainnetBlockValidator;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockBodyValidator;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockImporter;
Expand Down Expand Up @@ -57,10 +58,10 @@ private static ProtocolSpecBuilder<IbftContext> applyIbftChanges(
difficultyCalculator -> ibftBlockHeaderValidator(secondsBetweenBlocks),
difficultyCalculator -> ibftBlockHeaderValidator(secondsBetweenBlocks),
MainnetBlockBodyValidator::new,
(blockHeaderValidator, blockBodyValidator, blockProcessor) ->
MainnetBlockValidator::new,
(blockValidator) ->
new IbftBlockImporter(
new MainnetBlockImporter<>(
blockHeaderValidator, blockBodyValidator, blockProcessor),
new MainnetBlockImporter<>(blockValidator),
new VoteTallyUpdater(epochManager, new IbftBlockInterface())),
(time, parent, protocolContext) -> BigInteger.ONE)
.blockReward(Wei.ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.pantheon.consensus.common.VoteTallyUpdater;
import tech.pegasys.pantheon.consensus.ibft.IbftBlockImporter;
import tech.pegasys.pantheon.consensus.ibft.IbftContext;
import tech.pegasys.pantheon.ethereum.MainnetBlockValidator;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockBodyValidator;
import tech.pegasys.pantheon.ethereum.mainnet.MainnetBlockImporter;
Expand Down Expand Up @@ -59,10 +60,10 @@ private static ProtocolSpecBuilder<IbftContext> applyIbftChanges(
difficultyCalculator -> ibftBlockHeaderValidator(secondsBetweenBlocks),
difficultyCalculator -> ibftBlockHeaderValidator(secondsBetweenBlocks),
MainnetBlockBodyValidator::new,
(blockHeaderValidator, blockBodyValidator, blockProcessor) ->
MainnetBlockValidator::new,
(blockValidator) ->
new IbftBlockImporter(
new MainnetBlockImporter<>(
blockHeaderValidator, blockBodyValidator, blockProcessor),
new MainnetBlockImporter<>(blockValidator),
new VoteTallyUpdater(epochManager, new IbftLegacyBlockInterface())),
(time, parent, protocolContext) -> BigInteger.ONE)
.blockReward(Wei.ZERO)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019 ConsenSys AG.
*
* 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.
*/
package tech.pegasys.pantheon.ethereum;

import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.core.TransactionReceipt;
import tech.pegasys.pantheon.ethereum.mainnet.HeaderValidationMode;

import java.util.List;
import java.util.Optional;

public interface BlockValidator<C> {

class BlockProcessingOutputs {
public final MutableWorldState worldState;
public final List<TransactionReceipt> receipts;

public BlockProcessingOutputs(
final MutableWorldState worldState, final List<TransactionReceipt> receipts) {
this.worldState = worldState;
this.receipts = receipts;
}
}

Optional<BlockProcessingOutputs> validateAndProcessBlock(
final ProtocolContext<C> context,
final Block block,
final HeaderValidationMode headerValidationMode,
final HeaderValidationMode ommerValidationMode);

boolean fastBlockValidation(
final ProtocolContext<C> context,
final Block block,
final List<TransactionReceipt> receipts,
final HeaderValidationMode headerValidationMode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2019 ConsenSys AG.
*
* 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.
*/
package tech.pegasys.pantheon.ethereum;

import static org.apache.logging.log4j.LogManager.getLogger;

import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.core.TransactionReceipt;
import tech.pegasys.pantheon.ethereum.mainnet.BlockBodyValidator;
import tech.pegasys.pantheon.ethereum.mainnet.BlockHeaderValidator;
import tech.pegasys.pantheon.ethereum.mainnet.BlockProcessor;
import tech.pegasys.pantheon.ethereum.mainnet.HeaderValidationMode;

import java.util.List;
import java.util.Optional;

import org.apache.logging.log4j.Logger;

public class MainnetBlockValidator<C> implements BlockValidator<C> {

private static final Logger LOG = getLogger();

private final BlockHeaderValidator<C> blockHeaderValidator;

private final BlockBodyValidator<C> blockBodyValidator;

private final BlockProcessor blockProcessor;

public MainnetBlockValidator(
final BlockHeaderValidator<C> blockHeaderValidator,
final BlockBodyValidator<C> blockBodyValidator,
final BlockProcessor blockProcessor) {
this.blockHeaderValidator = blockHeaderValidator;
this.blockBodyValidator = blockBodyValidator;
this.blockProcessor = blockProcessor;
}

@Override
public Optional<BlockProcessingOutputs> validateAndProcessBlock(
final ProtocolContext<C> context,
final Block block,
final HeaderValidationMode headerValidationMode,
final HeaderValidationMode ommerValidationMode) {
final BlockHeader header = block.getHeader();

final Optional<BlockHeader> maybeParentHeader =
context.getBlockchain().getBlockHeader(header.getParentHash());
if (!maybeParentHeader.isPresent()) {
LOG.error(
"Attempted to import block {} with hash {} but parent block {} was not present",
header.getNumber(),
header.getHash(),
header.getParentHash());
return Optional.empty();
}
final BlockHeader parentHeader = maybeParentHeader.get();

if (!blockHeaderValidator.validateHeader(header, parentHeader, context, headerValidationMode)) {
return Optional.empty();
}

final MutableBlockchain blockchain = context.getBlockchain();
final MutableWorldState worldState =
context.getWorldStateArchive().getMutable(parentHeader.getStateRoot());
final BlockProcessor.Result result = blockProcessor.processBlock(blockchain, worldState, block);
if (!result.isSuccessful()) {
return Optional.empty();
}

final List<TransactionReceipt> receipts = result.getReceipts();
if (!blockBodyValidator.validateBody(
context, block, receipts, worldState.rootHash(), ommerValidationMode)) {
return Optional.empty();
}

return Optional.of(new BlockProcessingOutputs(worldState, receipts));
}

@Override
public boolean fastBlockValidation(
final ProtocolContext<C> context,
final Block block,
final List<TransactionReceipt> receipts,
final HeaderValidationMode headerValidationMode) {
final BlockHeader header = block.getHeader();
if (!blockHeaderValidator.validateHeader(header, context, headerValidationMode)) {
return false;
}

if (!blockBodyValidator.validateBodyLight(
context, block, receipts, HeaderValidationMode.FULL)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

import static org.apache.logging.log4j.LogManager.getLogger;

import tech.pegasys.pantheon.ethereum.BlockValidator;
import tech.pegasys.pantheon.ethereum.BlockValidator.BlockProcessingOutputs;
import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.chain.MutableBlockchain;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockImporter;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.core.TransactionReceipt;

import java.util.List;
Expand All @@ -28,21 +28,13 @@
import org.apache.logging.log4j.Logger;

public class MainnetBlockImporter<C> implements BlockImporter<C> {
private static final Logger LOG = getLogger();

private final BlockHeaderValidator<C> blockHeaderValidator;

private final BlockBodyValidator<C> blockBodyValidator;
private static final Logger LOG = getLogger();

private final BlockProcessor blockProcessor;
final BlockValidator<C> blockValidator;

public MainnetBlockImporter(
final BlockHeaderValidator<C> blockHeaderValidator,
final BlockBodyValidator<C> blockBodyValidator,
final BlockProcessor blockProcessor) {
this.blockHeaderValidator = blockHeaderValidator;
this.blockBodyValidator = blockBodyValidator;
this.blockProcessor = blockProcessor;
public MainnetBlockImporter(final BlockValidator<C> blockValidator) {
this.blockValidator = blockValidator;
}

@Override
Expand All @@ -51,41 +43,23 @@ public synchronized boolean importBlock(
final Block block,
final HeaderValidationMode headerValidationMode,
final HeaderValidationMode ommerValidationMode) {
final BlockHeader header = block.getHeader();

final Optional<BlockHeader> maybeParentHeader =
context.getBlockchain().getBlockHeader(header.getParentHash());
if (!maybeParentHeader.isPresent()) {
LOG.error(
"Attempted to import block {} with hash {} but parent block {} was not present",
header.getNumber(),
header.getHash(),
header.getParentHash());
return false;
}
final BlockHeader parentHeader = maybeParentHeader.get();

if (!blockHeaderValidator.validateHeader(header, parentHeader, context, headerValidationMode)) {
return false;
}
final Optional<BlockProcessingOutputs> outputs =
blockValidator.validateAndProcessBlock(
context, block, headerValidationMode, ommerValidationMode);

final MutableBlockchain blockchain = context.getBlockchain();
final MutableWorldState worldState =
context.getWorldStateArchive().getMutable(parentHeader.getStateRoot());
final BlockProcessor.Result result = blockProcessor.processBlock(blockchain, worldState, block);
if (!result.isSuccessful()) {
return false;
}
outputs.ifPresent(processingOutputs -> persistState(processingOutputs, block, context));

final List<TransactionReceipt> receipts = result.getReceipts();
if (!blockBodyValidator.validateBody(
context, block, receipts, worldState.rootHash(), ommerValidationMode)) {
return false;
}

blockchain.appendBlock(block, receipts);
return outputs.isPresent();
}

return true;
private void persistState(
final BlockProcessingOutputs processingOutputs,
final Block block,
final ProtocolContext<C> context) {
processingOutputs.worldState.persist();
final MutableBlockchain blockchain = context.getBlockchain();
blockchain.appendBlock(block, processingOutputs.receipts);
}

@Override
Expand All @@ -94,19 +68,12 @@ public boolean fastImportBlock(
final Block block,
final List<TransactionReceipt> receipts,
final HeaderValidationMode headerValidationMode) {
final BlockHeader header = block.getHeader();

if (!blockHeaderValidator.validateHeader(header, context, headerValidationMode)) {
return false;
if (blockValidator.fastBlockValidation(context, block, receipts, headerValidationMode)) {
context.getBlockchain().appendBlock(block, receipts);
return true;
}

if (!blockBodyValidator.validateBodyLight(
context, block, receipts, HeaderValidationMode.FULL)) {
return false;
}

context.getBlockchain().appendBlock(block, receipts);

return true;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.mainnet;

import tech.pegasys.pantheon.ethereum.MainnetBlockValidator;
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
Expand Down Expand Up @@ -87,6 +88,7 @@ public static ProtocolSpecBuilder<Void> frontierDefinition() {
.transactionReceiptFactory(MainnetProtocolSpecs::frontierTransactionReceiptFactory)
.blockReward(FRONTIER_BLOCK_REWARD)
.blockProcessorBuilder(MainnetBlockProcessor::new)
.blockValidatorBuilder(MainnetBlockValidator::new)
.blockImporterBuilder(MainnetBlockImporter::new)
.transactionReceiptType(TransactionReceiptType.ROOT)
.blockHashFunction(MainnetBlockHashFunction::createHash)
Expand Down
Loading

0 comments on commit f643dc9

Please sign in to comment.