This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Block Validation from Importing (#579)
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
Showing
10 changed files
with
229 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/BlockValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
109 changes: 109 additions & 0 deletions
109
ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/MainnetBlockValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.