-
Notifications
You must be signed in to change notification settings - Fork 75
[ETCM-72] Checkpoint Block validation #706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
168 changes: 168 additions & 0 deletions
168
...main/scala/io/iohk/ethereum/consensus/validators/BlockWithCheckpointHeaderValidator.scala
This file contains hidden or 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,168 @@ | ||
package io.iohk.ethereum.consensus.validators | ||
|
||
import io.iohk.ethereum.consensus.validators.BlockHeaderError._ | ||
import io.iohk.ethereum.domain.BlockHeader | ||
import io.iohk.ethereum.ledger.BloomFilter | ||
import io.iohk.ethereum.utils.{BlockchainConfig, ByteStringUtils} | ||
|
||
/** Validator specialized for the block with checkpoint | ||
* | ||
* @param blockchainConfig | ||
*/ | ||
class BlockWithCheckpointHeaderValidator(blockchainConfig: BlockchainConfig) { | ||
|
||
def validate(blockHeader: BlockHeader, parentHeader: BlockHeader): Either[BlockHeaderError, BlockHeaderValid] = { | ||
for { | ||
_ <- validateECIP1097Number(blockHeader) | ||
_ <- validateCheckpointSignatures(blockHeader, parentHeader) | ||
_ <- validateEmptyFields(blockHeader) | ||
_ <- validateFieldsCopiedFromParent(blockHeader, parentHeader) | ||
_ <- validateGasUsed(blockHeader) | ||
_ <- validateTimestamp(blockHeader, parentHeader) | ||
} yield BlockHeaderValid | ||
} | ||
|
||
/** | ||
* Validates [[io.iohk.ethereum.domain.BlockHeader.checkpoint]] signatures | ||
* | ||
* @param blockHeader BlockHeader to validate. | ||
* @param parentHeader BlockHeader of the parent of the block to validate. | ||
* @return BlockHeader if valid, an [[io.iohk.ethereum.consensus.validators.BlockHeaderError.HeaderInvalidCheckpointSignatures]] otherwise | ||
*/ | ||
private def validateCheckpointSignatures( | ||
blockHeader: BlockHeader, | ||
parentHeader: BlockHeader | ||
): Either[BlockHeaderError, BlockHeaderValid] = { | ||
blockHeader.checkpoint | ||
.map { checkpoint => | ||
lazy val signaturesWithRecoveredKeys = checkpoint.signatures.map(s => s -> s.publicKey(parentHeader.hash)) | ||
|
||
// if at least 2 different signatures came from the same signer it will be in this set (also takes care | ||
// of duplicate signatures) | ||
lazy val repeatedSigners = signaturesWithRecoveredKeys | ||
.groupBy(_._2) | ||
.filter(_._2.size > 1) | ||
.keySet | ||
.flatten | ||
|
||
lazy val (validSignatures, invalidSignatures) = signaturesWithRecoveredKeys.partition { | ||
//signatures are valid if the signers are known AND distinct | ||
case (sig, Some(pk)) => blockchainConfig.checkpointPubKeys.contains(pk) && !repeatedSigners.contains(pk) | ||
case _ => false | ||
} | ||
|
||
// we fail fast if there are too many signatures (DoS protection) | ||
if (checkpoint.signatures.size > blockchainConfig.checkpointPubKeys.size) | ||
Left(HeaderWrongNumberOfCheckpointSignatures(checkpoint.signatures.size)) | ||
else if (invalidSignatures.nonEmpty) { | ||
val sigsWithKeys = invalidSignatures.map { | ||
case (sig, maybePk) => (sig, maybePk.map(ByteStringUtils.hash2string)) | ||
} | ||
Left(HeaderInvalidCheckpointSignatures(sigsWithKeys)) | ||
} else if (validSignatures.size < blockchainConfig.minRequireSignatures) | ||
Left(HeaderWrongNumberOfCheckpointSignatures(validSignatures.size)) | ||
else | ||
Right(BlockHeaderValid) | ||
} | ||
.getOrElse(Left(HeaderUnexpectedError("Attempted to validate a checkpoint on a block without a checkpoint"))) | ||
} | ||
|
||
/** | ||
* Validates emptiness of: | ||
* - beneficiary | ||
* - extraData | ||
* - treasuryOptOut | ||
* - ommersHash | ||
* - transactionsRoot | ||
* - receiptsRoot | ||
* - logsBloom | ||
* - nonce | ||
* - mixHash | ||
* | ||
* @param blockHeader BlockHeader to validate. | ||
* @return BlockHeader if valid, an [[io.iohk.ethereum.consensus.validators.BlockHeaderError.HeaderFieldNotEmptyError]] otherwise | ||
*/ | ||
private def validateEmptyFields(blockHeader: BlockHeader): Either[BlockHeaderError, BlockHeaderValid] = { | ||
if (blockHeader.beneficiary != BlockHeader.EmptyBeneficiary) | ||
notEmptyFieldError("beneficiary") | ||
else if (blockHeader.ommersHash != BlockHeader.EmptyOmmers) | ||
notEmptyFieldError("ommersHash") | ||
else if (blockHeader.transactionsRoot != BlockHeader.EmptyMpt) | ||
notEmptyFieldError("transactionsRoot") | ||
else if (blockHeader.receiptsRoot != BlockHeader.EmptyMpt) | ||
notEmptyFieldError("receiptsRoot") | ||
else if (blockHeader.logsBloom != BloomFilter.EmptyBloomFilter) | ||
notEmptyFieldError("logsBloom") | ||
else if (blockHeader.extraData.nonEmpty) | ||
notEmptyFieldError("extraData") | ||
else if (blockHeader.treasuryOptOut.isDefined) | ||
ntallar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
notEmptyFieldError("treasuryOptOut") | ||
else if (blockHeader.nonce.nonEmpty) | ||
notEmptyFieldError("nonce") | ||
else if (blockHeader.mixHash.nonEmpty) | ||
notEmptyFieldError("mixHash") | ||
else Right(BlockHeaderValid) | ||
} | ||
|
||
private def notEmptyFieldError(field: String) = Left(HeaderFieldNotEmptyError(s"$field is not empty")) | ||
|
||
/** | ||
* Validates fields which should be equal to parent equivalents: | ||
* - stateRoot | ||
* | ||
* @param blockHeader BlockHeader to validate. | ||
* @param parentHeader BlockHeader of the parent of the block to validate. | ||
* @return BlockHeader if valid, an [[io.iohk.ethereum.consensus.validators.BlockHeaderError.HeaderNotMatchParentError]] otherwise | ||
*/ | ||
private def validateFieldsCopiedFromParent( | ||
blockHeader: BlockHeader, | ||
parentHeader: BlockHeader | ||
): Either[BlockHeaderError, BlockHeaderValid] = { | ||
if (blockHeader.stateRoot != parentHeader.stateRoot) | ||
fieldNotMatchedParentFieldError("stateRoot") | ||
else if (blockHeader.gasLimit != parentHeader.gasLimit) | ||
fieldNotMatchedParentFieldError("gasLimit") | ||
else if (blockHeader.difficulty != parentHeader.difficulty) | ||
fieldNotMatchedParentFieldError("difficulty") | ||
else Right(BlockHeaderValid) | ||
} | ||
|
||
private def fieldNotMatchedParentFieldError(field: String) = | ||
Left(HeaderNotMatchParentError(s"$field has different value that similar parent field")) | ||
|
||
|
||
/** | ||
* Validates gasUsed equal to zero | ||
* @param blockHeader BlockHeader to validate. | ||
* @return BlockHeader if valid, an [[io.iohk.ethereum.consensus.validators.BlockHeaderError.HeaderGasUsedError]] otherwise | ||
*/ | ||
private def validateGasUsed(blockHeader: BlockHeader): Either[BlockHeaderError, BlockHeaderValid] = { | ||
if (blockHeader.gasUsed != BigInt(0)) Left(HeaderGasUsedError) | ||
else Right(BlockHeaderValid) | ||
} | ||
|
||
/** | ||
* Validates [[io.iohk.ethereum.domain.BlockHeader.unixTimestamp]] is one bigger than parent unixTimestamp | ||
* | ||
* @param blockHeader BlockHeader to validate. | ||
* @param parentHeader BlockHeader of the parent of the block to validate. | ||
* @return BlockHeader if valid, an [[HeaderTimestampError]] otherwise | ||
*/ | ||
private def validateTimestamp( | ||
blockHeader: BlockHeader, | ||
parentHeader: BlockHeader | ||
): Either[BlockHeaderError, BlockHeaderValid] = | ||
if (blockHeader.unixTimestamp == parentHeader.unixTimestamp + 1) Right(BlockHeaderValid) | ||
else Left(HeaderTimestampError) | ||
|
||
/** | ||
* Validates [[io.iohk.ethereum.domain.BlockHeader.checkpoint]] is only defined if ECIP1097 is enabled at the block's number | ||
* | ||
* @param blockHeader BlockHeader to validate. | ||
* @return BlockHeader if valid, an [[HeaderCheckpointTooEarly]] otherwise | ||
*/ | ||
private def validateECIP1097Number(blockHeader: BlockHeader): Either[BlockHeaderError, BlockHeaderValid] = { | ||
if (blockHeader.number >= blockchainConfig.ecip1097BlockNumber) Right(BlockHeaderValid) | ||
else Left(HeaderCheckpointTooEarly) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.