Skip to content
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

[EC-302] Adding maximum gasLimit validation #301

Merged
merged 6 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ForksTest extends FlatSpec with Matchers {
override val eip150BlockNumber: BigInt = 5
override val eip160BlockNumber: BigInt = 7
override val eip155BlockNumber: BigInt = 0

override val eip106BlockNumber: BigInt = Long.MaxValue
override val chainId: Byte = 0x3d
override val monetaryPolicyConfig: MonetaryPolicyConfig = MonetaryPolicyConfig(5000000, 0.2, 5000000000000000000L)

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ mantis {
# Doc: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
homestead-block-number = "1150000"

# EIP-106 fork block number
# Doc: https://github.com/ethereum/EIPs/issues/106
eip106-block-number = "9223372036854775807"

# EIP-150 fork block number
# Doc: https://github.com/ethereum/EIPs/issues/150
eip150-block-number = "2500000"
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/io/iohk/ethereum/utils/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ trait BlockchainConfig {
val eip150BlockNumber: BigInt
val eip155BlockNumber: BigInt
val eip160BlockNumber: BigInt
val eip106BlockNumber: BigInt
val difficultyBombPauseBlockNumber: BigInt
val difficultyBombContinueBlockNumber: BigInt

Expand Down Expand Up @@ -285,6 +286,8 @@ object BlockchainConfig {
}

override val monetaryPolicyConfig = MonetaryPolicyConfig(blockchainConfig.getConfig("monetary-policy"))

override val eip106BlockNumber: BigInt = BigInt(blockchainConfig.getString("eip106-block-number"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BlockHeaderValidatorImpl(blockchainConfig: BlockchainConfig) extends Block
val GasLimitBoundDivisor: Int = 1024
val MinGasLimit: BigInt = 5000 //Although the paper states this value is 125000, on the different clients 5000 is used
val difficulty = new DifficultyCalculator(blockchainConfig)
val MaxGasLimit = Long.MaxValue // max gasLimit is equal 2^63-1 according to EIP106
import BlockHeaderError._

/** This method allows validate a BlockHeader (stated on
Expand Down Expand Up @@ -101,15 +102,24 @@ class BlockHeaderValidatorImpl(blockchainConfig: BlockchainConfig) extends Block
* Validates [[io.iohk.ethereum.domain.BlockHeader.gasLimit]] follows the restrictions based on its parent gasLimit
* based on validations stated in section 4.4.4 of http://paper.gavwood.com/
*
* EIP106(https://github.com/ethereum/EIPs/issues/106) adds additional validation of maximum value for gasLimit.
*
* @param blockHeader BlockHeader to validate.
* @param parentHeader BlockHeader of the parent of the block to validate.
* @return BlockHeader if valid, an [[HeaderGasLimitError]] otherwise
*/
private def validateGasLimit(blockHeader: BlockHeader, parentHeader: BlockHeader): Either[BlockHeaderError, BlockHeader] = {
val gasLimitDiff = (blockHeader.gasLimit - parentHeader.gasLimit).abs
val gasLimitDiffLimit = parentHeader.gasLimit / GasLimitBoundDivisor
if(gasLimitDiff < gasLimitDiffLimit && blockHeader.gasLimit >= MinGasLimit) Right(blockHeader)
else Left(HeaderGasLimitError)

if (blockHeader.gasLimit > MaxGasLimit && blockHeader.number >= blockchainConfig.eip106BlockNumber)
Left(HeaderGasLimitError)
else {
val gasLimitDiff = (blockHeader.gasLimit - parentHeader.gasLimit).abs
val gasLimitDiffLimit = parentHeader.gasLimit / GasLimitBoundDivisor
if (gasLimitDiff < gasLimitDiffLimit && blockHeader.gasLimit >= MinGasLimit)
Right(blockHeader)
else
Left(HeaderGasLimitError)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class PersonalServiceSpec extends FlatSpec with Matchers with MockFactory with S
override val homesteadBlockNumber: BigInt = 0
override val eip150BlockNumber: BigInt = 0
override val eip160BlockNumber: BigInt = 0
override val eip106BlockNumber: BigInt = 0
override val difficultyBombPauseBlockNumber: BigInt = 0
override val difficultyBombContinueBlockNumber: BigInt = 0
override val customGenesisFileOpt: Option[String] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class BlockGeneratorSpec extends FlatSpec with Matchers with PropertyChecks with
override val difficultyBombPauseBlockNumber: BigInt = 3000000
override val difficultyBombContinueBlockNumber: BigInt = 5000000
override val eip155BlockNumber: BigInt = Long.MaxValue
override val eip106BlockNumber: BigInt = Long.MaxValue
override val chainId: Byte = 0x3d.toByte
override val customGenesisFileOpt: Option[String] = Some("test-genesis.json")
override val monetaryPolicyConfig: MonetaryPolicyConfig = MonetaryPolicyConfig(5000000, 0.2, BigInt("5000000000000000000"))
Expand Down Expand Up @@ -254,6 +255,7 @@ class BlockGeneratorSpec extends FlatSpec with Matchers with PropertyChecks with
override val difficultyBombPauseBlockNumber: BigInt = 3000000
override val difficultyBombContinueBlockNumber: BigInt = 5000000
override val eip155BlockNumber: BigInt = 0
override val eip106BlockNumber: BigInt = Long.MaxValue
override val chainId: Byte = 0x3d.toByte
override val customGenesisFileOpt: Option[String] = Some("test-genesis.json")
override val monetaryPolicyConfig: MonetaryPolicyConfig = MonetaryPolicyConfig(5000000, 0.2, BigInt("5000000000000000000"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class EtcHandshakerSpec extends FlatSpec with Matchers {
override val eip150BlockNumber: BigInt = 0
override val eip160BlockNumber: BigInt = 0
override val eip155BlockNumber: BigInt = 0
override val eip106BlockNumber: BigInt = 0
override val difficultyBombPauseBlockNumber: BigInt = 0
override val difficultyBombContinueBlockNumber: BigInt = 0
override val customGenesisFileOpt: Option[String] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BlockHeaderValidatorSpec extends FlatSpec with Matchers with PropertyCheck
override val eip155BlockNumber: BigInt = Long.MaxValue
override val eip160BlockNumber: BigInt = Long.MaxValue
override val eip150BlockNumber: BigInt = Long.MaxValue
override val eip106BlockNumber: BigInt = 20
override val chainId: Byte = 0x3d.toByte
override val daoForkBlockHash: ByteString = ByteString("unused")
override val monetaryPolicyConfig: MonetaryPolicyConfig = null
Expand Down Expand Up @@ -99,6 +100,12 @@ class BlockHeaderValidatorSpec extends FlatSpec with Matchers with PropertyCheck
}
}

it should "return a failure if created with gas limit above threshold and block number >= eip106 block number" in {
val validParent = validBlockParent.copy(gasLimit = Long.MaxValue)
val invalidBlockHeader = validBlockHeader.copy(gasLimit = BigInt(Long.MaxValue) + 1)
blockHeaderValidator.validate(invalidBlockHeader, validParent) shouldBe Left(HeaderGasLimitError)
}

it should "return a failure if created based on invalid number" in {
forAll(bigIntGen) { number =>
val blockHeader = validBlockHeader.copy(number = number)
Expand Down
4 changes: 4 additions & 0 deletions src/universal/conf/blockchain.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ mantis {
# Doc: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
# homestead-block-number = "1150000"

# EIP-106 fork block number
# Doc: https://github.com/ethereum/EIPs/issues/106
# eip106-block-number = "9223372036854775807"

# EIP-150 fork block number
# Doc: https://github.com/ethereum/EIPs/issues/150
# eip150-block-number = "2500000"
Expand Down