Skip to content

Commit

Permalink
[refactor] create dedicated case class for block numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Richez committed May 27, 2021
1 parent 5157ec4 commit 9e703d3
Show file tree
Hide file tree
Showing 32 changed files with 361 additions and 308 deletions.
43 changes: 22 additions & 21 deletions src/it/scala/io/iohk/ethereum/txExecTest/ECIP1017Test.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors

import io.iohk.ethereum.domain.{Address, BlockchainImpl, Receipt, UInt256}
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.{BlockchainConfig, MonetaryPolicyConfig}
import io.iohk.ethereum.utils.{BlockchainConfig, ForkBlockNumbers, MonetaryPolicyConfig}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand All @@ -22,16 +21,26 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
maxCodeSize = None,
chainId = 0x3d.toByte,
networkId = 1,
frontierBlockNumber = 0,
homesteadBlockNumber = 1150000,
eip106BlockNumber = Long.MaxValue,
eip150BlockNumber = 2500000,
eip160BlockNumber = 3000000,
eip155BlockNumber = 3000000,
eip161BlockNumber = Long.MaxValue,
byzantiumBlockNumber = Long.MaxValue,
constantinopleBlockNumber = Long.MaxValue,
istanbulBlockNumber = Long.MaxValue,
forkBlockNumbers = ForkBlockNumbers(
frontierBlockNumber = 0,
homesteadBlockNumber = 1150000,
eip106BlockNumber = Long.MaxValue,
eip150BlockNumber = 2500000,
eip160BlockNumber = 3000000,
eip155BlockNumber = 3000000,
eip161BlockNumber = Long.MaxValue,
byzantiumBlockNumber = Long.MaxValue,
constantinopleBlockNumber = Long.MaxValue,
istanbulBlockNumber = Long.MaxValue,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue,
petersburgBlockNumber = Long.MaxValue,
ecip1098BlockNumber = Long.MaxValue,
ecip1097BlockNumber = Long.MaxValue,
ecip1099BlockNumber = Long.MaxValue,
ecip1049BlockNumber = None
),
customGenesisFileOpt = None,
customGenesisJsonOpt = None,
daoForkConfig = None,
Expand All @@ -42,15 +51,7 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
accountStartNonce = UInt256.Zero,
ethCompatibleStorage = true,
gasTieBreaker = false,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue,
petersburgBlockNumber = Long.MaxValue,
ecip1098BlockNumber = Long.MaxValue,
treasuryAddress = Address(0),
ecip1097BlockNumber = Long.MaxValue,
ecip1099BlockNumber = Long.MaxValue,
ecip1049BlockNumber = None
treasuryAddress = Address(0)
)
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))

Expand Down
41 changes: 21 additions & 20 deletions src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors

import io.iohk.ethereum.domain.{Address, BlockchainImpl, Receipt, UInt256}
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.{BlockchainConfig, MonetaryPolicyConfig}
import io.iohk.ethereum.utils.{BlockchainConfig, ForkBlockNumbers, MonetaryPolicyConfig}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand All @@ -16,40 +15,42 @@ class ForksTest extends AnyFlatSpec with Matchers {

trait TestSetup extends ScenarioSetup {
override lazy val blockchainConfig = BlockchainConfig(
frontierBlockNumber = 0,
homesteadBlockNumber = 3,
eip150BlockNumber = 5,
eip160BlockNumber = 7,
eip155BlockNumber = 0,
eip106BlockNumber = Long.MaxValue,
forkBlockNumbers = ForkBlockNumbers(
frontierBlockNumber = 0,
homesteadBlockNumber = 3,
eip150BlockNumber = 5,
eip160BlockNumber = 7,
eip155BlockNumber = 0,
eip106BlockNumber = Long.MaxValue,
eip161BlockNumber = Long.MaxValue,
byzantiumBlockNumber = Long.MaxValue,
constantinopleBlockNumber = Long.MaxValue,
istanbulBlockNumber = Long.MaxValue,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue,
petersburgBlockNumber = Long.MaxValue,
ecip1098BlockNumber = Long.MaxValue,
ecip1097BlockNumber = Long.MaxValue,
ecip1099BlockNumber = Long.MaxValue,
ecip1049BlockNumber = None
),
chainId = 0x3d.toByte,
monetaryPolicyConfig = MonetaryPolicyConfig(5000000, 0.2, 5000000000000000000L, 3000000000000000000L),
// unused
bootstrapNodes = Set(),
networkId = 1,
maxCodeSize = None,
eip161BlockNumber = Long.MaxValue,
customGenesisFileOpt = None,
customGenesisJsonOpt = None,
difficultyBombPauseBlockNumber = Long.MaxValue,
difficultyBombContinueBlockNumber = Long.MaxValue,
difficultyBombRemovalBlockNumber = Long.MaxValue,
byzantiumBlockNumber = Long.MaxValue,
constantinopleBlockNumber = Long.MaxValue,
istanbulBlockNumber = Long.MaxValue,
accountStartNonce = UInt256.Zero,
daoForkConfig = None,
gasTieBreaker = false,
ethCompatibleStorage = true,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue,
petersburgBlockNumber = Long.MaxValue,
ecip1098BlockNumber = Long.MaxValue,
treasuryAddress = Address(0),
ecip1097BlockNumber = Long.MaxValue,
ecip1099BlockNumber = Long.MaxValue,
ecip1049BlockNumber = None
)

val noErrors = a[Right[_, Seq[Receipt]]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class BlockGeneratorSkeleton(
x: Ommers
): BlockHeader = {
val extraFields =
if (blockNumber >= blockchainConfig.ecip1097BlockNumber)
if (blockNumber >= blockchainConfig.forkBlockNumbers.ecip1097BlockNumber)
HefPostEcip1097(None)
else
HefEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PoWConsensus private (
node.ethMiningService,
blockCreator,
blockchain,
blockchainConfig.ecip1049BlockNumber
blockchainConfig.forkBlockNumbers.ecip1049BlockNumber
),
"PoWMinerCoordinator",
DispatcherSelector.fromConfig(BlockForgerDispatcherId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.iohk.ethereum.utils.BlockchainConfig

class EthashDifficultyCalculator(blockchainConfig: BlockchainConfig) extends DifficultyCalculator {
import blockchainConfig._
import blockchainConfig.forkBlockNumbers._
import DifficultyCalculator._

private val ExpDifficultyPeriod: Int = 100000
Expand All @@ -19,7 +20,7 @@ class EthashDifficultyCalculator(blockchainConfig: BlockchainConfig) extends Dif
val c: BigInt =
if (blockNumber < homesteadBlockNumber) {
if (blockTimestamp < parentHeader.unixTimestamp + 13) 1 else -1
} else if (blockNumber >= byzantiumBlockNumber || blockNumber >= blockchainConfig.atlantisBlockNumber) {
} else if (blockNumber >= byzantiumBlockNumber || blockNumber >= atlantisBlockNumber) {
val parentUncleFactor = if (parentHeader.ommersHash == BlockHeader.EmptyOmmers) 1 else 2
math.max(parentUncleFactor - (timestampDiff / 9), FrontierTimestampDiffLimit)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class EthashDAGManager(blockCreator: PoWBlockCreator) extends Logger {
(currentEpoch, currentEpochDag, currentEpochDagSize) match {
case (_, Some(dag), Some(dagSize)) => (dag, dagSize)
case _ =>
val seed = EthashUtils.seed(blockNumber, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong)
val seed =
EthashUtils.seed(blockNumber, blockCreator.blockchainConfig.forkBlockNumbers.ecip1099BlockNumber.toLong)
val dagSize = EthashUtils.dagSize(epoch)
val dagNumHashes = (dagSize / EthashUtils.HASH_BYTES).toInt
val dag =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EthashMiner(

private def doMining(blockNumber: Long, block: Block): (Long, MiningResult) = {
val epoch =
EthashUtils.epoch(blockNumber, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong)
EthashUtils.epoch(blockNumber, blockCreator.blockchainConfig.forkBlockNumbers.ecip1099BlockNumber.toLong)
val (dag, dagSize) = dagManager.calculateDagSize(blockNumber, epoch)
val headerHash = crypto.kec256(BlockHeader.getEncodedWithoutNonce(block.header))
val startTime = System.nanoTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class EthashBlockHeaderValidator(blockchainConfig: BlockchainConfig) {
}
}

val epoch = EthashUtils.epoch(blockHeader.number.toLong, blockchainConfig.ecip1099BlockNumber.toLong)
val seed = EthashUtils.seed(blockHeader.number.toLong, blockchainConfig.ecip1099BlockNumber.toLong)
val epoch =
EthashUtils.epoch(blockHeader.number.toLong, blockchainConfig.forkBlockNumbers.ecip1099BlockNumber.toLong)
val seed = EthashUtils.seed(blockHeader.number.toLong, blockchainConfig.forkBlockNumbers.ecip1099BlockNumber.toLong)
val powCacheData = getPowCacheData(epoch, seed)

val proofOfWork = hashimotoLight(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PoWBlockHeaderValidator(blockchainConfig: BlockchainConfig)
else ethashBlockHeaderValidator.validateHeader(blockHeader)

private def isKeccak(currentBlockNumber: BigInt): Boolean =
blockchainConfig.ecip1049BlockNumber match {
blockchainConfig.forkBlockNumbers.ecip1049BlockNumber match {
case Some(keccakBlock) => currentBlockNumber >= keccakBlock
case None => false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ abstract class BlockHeaderValidatorSkeleton(blockchainConfig: BlockchainConfig)
parentHeader: BlockHeader
): Either[BlockHeaderError, BlockHeaderValid] = {

if (blockHeader.gasLimit > MaxGasLimit && blockHeader.number >= blockchainConfig.eip106BlockNumber)
if (blockHeader.gasLimit > MaxGasLimit && blockHeader.number >= blockchainConfig.forkBlockNumbers.eip106BlockNumber)
Left(HeaderGasLimitError)
else {
val gasLimitDiff = (blockHeader.gasLimit - parentHeader.gasLimit).abs
Expand Down Expand Up @@ -226,8 +226,8 @@ abstract class BlockHeaderValidatorSkeleton(blockchainConfig: BlockchainConfig)
* @return BlockHeader if valid, an [[HeaderExtraFieldsError]] otherwise
*/
private def validateExtraFields(blockHeader: BlockHeader): Either[BlockHeaderError, BlockHeaderValid] = {
val isECIP1098Activated = blockHeader.number >= blockchainConfig.ecip1098BlockNumber
val isECIP1097Activated = blockHeader.number >= blockchainConfig.ecip1097BlockNumber
val isECIP1098Activated = blockHeader.number >= blockchainConfig.forkBlockNumbers.ecip1098BlockNumber
val isECIP1097Activated = blockHeader.number >= blockchainConfig.forkBlockNumbers.ecip1097BlockNumber

blockHeader.extraFields match {
case HefPostEcip1097(_) if isECIP1097Activated && isECIP1098Activated => Right(BlockHeaderValid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class StdSignedTransactionValidator(blockchainConfig: BlockchainConfig) extends
val r = stx.signature.r
val s = stx.signature.s

val beforeHomestead = blockNumber < blockchainConfig.homesteadBlockNumber
val beforeEIP155 = blockNumber < blockchainConfig.eip155BlockNumber
val beforeHomestead = blockNumber < blockchainConfig.forkBlockNumbers.homesteadBlockNumber
val beforeEIP155 = blockNumber < blockchainConfig.forkBlockNumbers.eip155BlockNumber

val validR = r > 0 && r < secp256k1n
val validS = s > 0 && s < (if (beforeHomestead) secp256k1n else secp256k1n / 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class EthMiningService(
Right(
GetWorkResponse(
powHeaderHash = ByteString(kec256(BlockHeader.getEncodedWithoutNonce(pb.block.header))),
dagSeed = EthashUtils.seed(pb.block.header.number.toLong, blockchainConfig.ecip1099BlockNumber.toLong),
dagSeed = EthashUtils
.seed(pb.block.header.number.toLong, blockchainConfig.forkBlockNumbers.ecip1099BlockNumber.toLong),
target = ByteString((BigInt(2).pow(256) / pb.block.header.difficulty).toByteArray)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class PersonalService(
val maybeNextTxNonce = maybeLatestPendingTxNonce.map(_ + 1) orElse maybeCurrentNonce
val tx = request.toTransaction(maybeNextTxNonce.getOrElse(blockchainConfig.accountStartNonce))

val stx = if (blockchain.getBestBlockNumber() >= blockchainConfig.eip155BlockNumber) {
val stx = if (blockchain.getBestBlockNumber() >= blockchainConfig.forkBlockNumbers.eip155BlockNumber) {
wallet.signTx(tx, Some(blockchainConfig.chainId))
} else {
wallet.signTx(tx, None)
Expand Down
35 changes: 22 additions & 13 deletions src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.iohk.ethereum.ledger._
import io.iohk.ethereum.testmode.{TestLedgerWrapper, TestmodeConsensus}
import io.iohk.ethereum.transactions.PendingTransactionsManager
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
import io.iohk.ethereum.utils.{ByteStringUtils, Logger}
import io.iohk.ethereum.utils.{ByteStringUtils, ForkBlockNumbers, Logger}
import monix.eval.Task
import monix.execution.Scheduler
import org.bouncycastle.util.encoders.Hex
Expand Down Expand Up @@ -170,24 +170,33 @@ class TestService(
SetChainParamsResponse().rightNow
}

val neverOccuringBlock: Int = Int.MaxValue
private def buildNewConfig(blockchainParams: BlockchainParams) = {
val neverOccuringBlock = Int.MaxValue
val byzantiumBlockNumber: BigInt = blockchainParams.byzantiumForkBlock.getOrElse(neverOccuringBlock)
val istanbulForkBlockNumber: BigInt = blockchainParams.istanbulForkBlock.getOrElse(neverOccuringBlock)

// For block number which are not specified by retesteth, we try to align the number to another fork
testLedgerWrapper.blockchainConfig.copy(
homesteadBlockNumber = blockchainParams.homesteadForkBlock.getOrElse(neverOccuringBlock),
eip150BlockNumber = blockchainParams.EIP150ForkBlock.getOrElse(neverOccuringBlock),
eip160BlockNumber = byzantiumBlockNumber,
eip161BlockNumber = byzantiumBlockNumber,
byzantiumBlockNumber = byzantiumBlockNumber,
constantinopleBlockNumber = blockchainParams.constantinopleForkBlock.getOrElse(neverOccuringBlock),
petersburgBlockNumber = istanbulForkBlockNumber,
aghartaBlockNumber = istanbulForkBlockNumber,
istanbulBlockNumber = istanbulForkBlockNumber,
atlantisBlockNumber = istanbulForkBlockNumber,
phoenixBlockNumber = istanbulForkBlockNumber,
forkBlockNumbers = ForkBlockNumbers(
frontierBlockNumber = 0,
homesteadBlockNumber = blockchainParams.homesteadForkBlock.getOrElse(neverOccuringBlock),
eip106BlockNumber = neverOccuringBlock,
eip150BlockNumber = blockchainParams.EIP150ForkBlock.getOrElse(neverOccuringBlock),
eip155BlockNumber = byzantiumBlockNumber,
eip160BlockNumber = byzantiumBlockNumber,
eip161BlockNumber = byzantiumBlockNumber,
byzantiumBlockNumber = byzantiumBlockNumber,
ecip1049BlockNumber = None,
ecip1097BlockNumber = neverOccuringBlock,
ecip1098BlockNumber = neverOccuringBlock,
constantinopleBlockNumber = blockchainParams.constantinopleForkBlock.getOrElse(neverOccuringBlock),
petersburgBlockNumber = istanbulForkBlockNumber,
aghartaBlockNumber = istanbulForkBlockNumber,
istanbulBlockNumber = istanbulForkBlockNumber,
atlantisBlockNumber = istanbulForkBlockNumber,
phoenixBlockNumber = istanbulForkBlockNumber,
ecip1099BlockNumber = neverOccuringBlock
),
accountStartNonce = UInt256(blockchainParams.accountStartNonce),
networkId = 1,
bootstrapNodes = Set()
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class BlockPreparator(
// in some irrelevant test can throw an exception.
private[ledger] lazy val blockRewardCalculator = new BlockRewardCalculator(
blockchainConfig.monetaryPolicyConfig,
blockchainConfig.byzantiumBlockNumber,
blockchainConfig.constantinopleBlockNumber
blockchainConfig.forkBlockNumbers.byzantiumBlockNumber,
blockchainConfig.forkBlockNumbers.constantinopleBlockNumber
)

/**
Expand Down Expand Up @@ -98,7 +98,7 @@ class BlockPreparator(
}

private def treasuryEnabled(blockNo: BigInt): Boolean =
blockNo >= blockchainConfig.ecip1098BlockNumber
blockNo >= blockchainConfig.forkBlockNumbers.ecip1098BlockNumber

/**
* v0 ≡ Tg (Tx gas limit) * Tp (Tx gas price). See YP equation number (68)
Expand Down Expand Up @@ -318,8 +318,8 @@ class BlockPreparator(
// spec: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-658.md
val transactionOutcome =
if (
blockHeader.number >= blockchainConfig.byzantiumBlockNumber ||
blockHeader.number >= blockchainConfig.atlantisBlockNumber
blockHeader.number >= blockchainConfig.forkBlockNumbers.byzantiumBlockNumber ||
blockHeader.number >= blockchainConfig.forkBlockNumbers.atlantisBlockNumber
) {
if (vmError.isDefined) FailureOutcome else SuccessOutcome
} else {
Expand Down
Loading

0 comments on commit 9e703d3

Please sign in to comment.