Skip to content

Commit

Permalink
[refactor] put all the state in the same place and create a builder f…
Browse files Browse the repository at this point in the history
…or consensus and ledger
  • Loading branch information
Aurélien Richez committed May 27, 2021
1 parent cd61845 commit 8beb53d
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 66 deletions.
22 changes: 11 additions & 11 deletions src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.iohk.ethereum.{crypto, domain, rlp}
import io.iohk.ethereum.domain.Block._
import io.iohk.ethereum.domain.{Account, Address, Block, BlockchainImpl, UInt256}
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.testmode.{TestServiceProvider, TestmodeConsensus}
import io.iohk.ethereum.testmode.{TestModeComponentsProvider, TestmodeConsensus}
import io.iohk.ethereum.transactions.PendingTransactionsManager
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
import io.iohk.ethereum.utils.{BlockchainConfig, ByteStringUtils, ForkBlockNumbers, Logger}
Expand Down Expand Up @@ -108,8 +108,7 @@ class TestService(
blockchain: BlockchainImpl,
pendingTransactionsManager: ActorRef,
consensusConfig: ConsensusConfig,
consensus: TestmodeConsensus,
testLedgerWrapper: TestServiceProvider,
testModeComponentsProvider: TestModeComponentsProvider,
initialConfig: BlockchainConfig
)(implicit
scheduler: Scheduler
Expand All @@ -122,6 +121,7 @@ class TestService(
private var accountAddresses: List[String] = List()
private var accountRangeOffset = 0
private var currentConfig: BlockchainConfig = initialConfig
private var blockTimestamp: Long = 0

def setChainParams(request: SetChainParamsRequest): ServiceResponse[SetChainParamsResponse] = {
currentConfig = buildNewConfig(request.chainParams.blockchainParams)
Expand Down Expand Up @@ -153,9 +153,6 @@ class TestService(
storeGenesisAccountCodes(genesisData.alloc)
storeGenesisAccountStorageData(genesisData.alloc)

// update test ledger with new config
consensus.blockchainConfig = currentConfig

accountAddresses = genesisData.alloc.keys.toList
accountRangeOffset = 0

Expand Down Expand Up @@ -217,11 +214,11 @@ class TestService(
def mineBlocks(request: MineBlocksRequest): ServiceResponse[MineBlocksResponse] = {
def mineBlock(): Task[Unit] = {
getBlockForMining(blockchain.getBestBlock().get)
.flatMap(blockForMining => testLedgerWrapper.ledger(currentConfig).importBlock(blockForMining.block))
.flatMap(blockForMining => testModeComponentsProvider.ledger(currentConfig).importBlock(blockForMining.block))
.map { res =>
log.info("Block mining result: " + res)
pendingTransactionsManager ! PendingTransactionsManager.ClearPendingTransactions
consensus.blockTimestamp += 1
blockTimestamp += 1
}
}

Expand All @@ -234,7 +231,7 @@ class TestService(
}

def modifyTimestamp(request: ModifyTimestampRequest): ServiceResponse[ModifyTimestampResponse] = {
consensus.blockTimestamp = request.timestamp
blockTimestamp = request.timestamp
ModifyTimestampResponse().rightNow
}

Expand All @@ -250,7 +247,8 @@ class TestService(
Try(decode(request.blockRlp).toBlock) match {
case Failure(_) => Task.now(Left(JsonRpcError(-1, "block validation failed!", None)))
case Success(value) =>
testLedgerWrapper.ledger(currentConfig)
testModeComponentsProvider
.ledger(currentConfig)
.importBlock(value)
.flatMap(handleResult)
}
Expand All @@ -277,7 +275,9 @@ class TestService(
.timeout(timeout.duration)
.onErrorRecover { case _ => PendingTransactionsResponse(Nil) }
.map { pendingTxs =>
consensus.blockGenerator
testModeComponentsProvider
.consensus(currentConfig, blockTimestamp)
.blockGenerator
.generateBlock(
parentBlock,
pendingTxs.pendingTransactions.map(_.stx.tx),
Expand Down
14 changes: 10 additions & 4 deletions src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import io.iohk.ethereum.network.p2p.EthereumMessageDecoder
import io.iohk.ethereum.network.rlpx.AuthHandshaker
import io.iohk.ethereum.network.{PeerManagerActor, ServerActor, _}
import io.iohk.ethereum.ommers.OmmersPool
import io.iohk.ethereum.testmode.{TestEthBlockServiceWrapper, TestLedgerBuilder, TestmodeConsensusBuilder}
import io.iohk.ethereum.testmode.{TestEthBlockServiceWrapper, TestModeServiceBuilder, TestmodeConsensusBuilder}
import io.iohk.ethereum.transactions.{PendingTransactionsManager, TransactionHistoryService}
import io.iohk.ethereum.utils.Config.SyncConfig
import io.iohk.ethereum.utils._
Expand Down Expand Up @@ -363,14 +363,20 @@ trait TestServiceBuilder {
with BlockchainConfigBuilder
with VmBuilder
with TestmodeConsensusBuilder
with TestLedgerBuilder =>
with TestModeServiceBuilder =>

lazy val testService =
new TestService(blockchain, pendingTransactionsManager, consensusConfig, consensus, testLedgerWrapper, blockchainConfig)(scheduler)
new TestService(
blockchain,
pendingTransactionsManager,
consensusConfig,
testModeComponentsProvider,
blockchainConfig
)(scheduler)
}

trait TestEthBlockServiceBuilder extends EthBlocksServiceBuilder {
self: BlockchainBuilder with TestLedgerBuilder with ConsensusBuilder =>
self: BlockchainBuilder with TestModeServiceBuilder with ConsensusBuilder =>
override lazy val ethBlocksService = new TestEthBlockServiceWrapper(blockchain, ledger, consensus)
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.iohk.ethereum.consensus.StdConsensusBuilder
import io.iohk.ethereum.metrics.{Metrics, MetricsConfig}
import io.iohk.ethereum.network.discovery.PeerDiscoveryManager
import io.iohk.ethereum.network.{PeerManagerActor, ServerActor}
import io.iohk.ethereum.testmode.{TestLedgerBuilder, TestmodeConsensusBuilder}
import io.iohk.ethereum.testmode.{TestModeServiceBuilder, TestmodeConsensusBuilder}
import io.iohk.ethereum.utils.Config
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
Expand Down Expand Up @@ -106,7 +106,7 @@ abstract class BaseNode extends Node {
class StdNode extends BaseNode with StdLedgerBuilder with StdConsensusBuilder
class TestNode
extends BaseNode
with TestLedgerBuilder
with TestModeServiceBuilder
with TestmodeConsensusBuilder
with TestServiceBuilder
with TestEthBlockServiceBuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.iohk.ethereum.testmode

import io.iohk.ethereum.consensus.difficulty.DifficultyCalculator
import io.iohk.ethereum.consensus.{Consensus, ConsensusConfig}
import io.iohk.ethereum.domain.BlockchainImpl
import io.iohk.ethereum.ledger.Ledger.VMImpl
import io.iohk.ethereum.ledger.{Ledger, LedgerImpl, StxLedger}
import io.iohk.ethereum.utils.BlockchainConfig
import io.iohk.ethereum.utils.Config.SyncConfig
import monix.execution.Scheduler

/** Provides a ledger instance with modifiable blockchain config (used in test mode). */
class TestModeComponentsProvider(
blockchain: BlockchainImpl,
syncConfig: SyncConfig,
validationExecutionContext: Scheduler,
consensusConfig: ConsensusConfig,
difficultyCalculator: DifficultyCalculator,
vm: VMImpl
) {

def ledger(blockchainConfig: BlockchainConfig): Ledger =
new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus(blockchainConfig), validationExecutionContext)
def stxLedger(blockchainConfig: BlockchainConfig): StxLedger =
new StxLedger(blockchain, blockchainConfig, consensus(blockchainConfig).blockPreparator)
def consensus(blockchainConfig: BlockchainConfig, blockTimestamp: Long = 0): TestmodeConsensus =
new TestmodeConsensus(vm, blockchain, blockchainConfig, consensusConfig, difficultyCalculator, blockTimestamp)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@ package io.iohk.ethereum.testmode

import akka.util.ByteString
import cats.data.NonEmptyList
import io.iohk.ethereum.consensus.{Consensus, ConsensusBuilder}
import io.iohk.ethereum.consensus.difficulty.DifficultyCalculator
import io.iohk.ethereum.consensus.{Consensus, ConsensusBuilder, ConsensusConfigBuilder}
import io.iohk.ethereum.domain._
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.nodebuilder.{ActorSystemBuilder, _}
import monix.eval.Task
import monix.execution.Scheduler

trait TestLedgerBuilder extends LedgerBuilder {
trait TestModeServiceBuilder extends LedgerBuilder {
self: BlockchainConfigBuilder
with BlockchainBuilder
with SyncConfigBuilder
with ConsensusBuilder
with ActorSystemBuilder =>
with ActorSystemBuilder
with ConsensusConfigBuilder
with VmBuilder =>

val scheduler = Scheduler(system.dispatchers.lookup("validation-context"))

lazy val testLedgerWrapper: TestServiceProvider =
new TestServiceProvider(blockchain, syncConfig, consensus, scheduler)
lazy val testModeComponentsProvider: TestModeComponentsProvider =
new TestModeComponentsProvider(
blockchain,
syncConfig,
scheduler,
consensusConfig,
DifficultyCalculator(blockchainConfig),
vm
)

private def testLedger: Ledger = testLedgerWrapper.ledger(blockchainConfig)
private def testLedger: Ledger = testModeComponentsProvider.ledger(blockchainConfig)

class TestLedgerProxy extends Ledger {
override def consensus: Consensus = testLedger.consensus
Expand All @@ -35,5 +45,5 @@ trait TestLedgerBuilder extends LedgerBuilder {
}

override lazy val ledger: Ledger = new TestLedgerProxy
override lazy val stxLedger: StxLedger = testLedgerWrapper.stxLedger(blockchainConfig)
override lazy val stxLedger: StxLedger = testModeComponentsProvider.stxLedger(blockchainConfig)
}
20 changes: 0 additions & 20 deletions src/main/scala/io/iohk/ethereum/testmode/TestServiceProvider.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import io.iohk.ethereum.consensus.pow.validators.ValidatorsExecutor
class TestmodeConsensus(
override val vm: VMImpl,
blockchain: BlockchainImpl,
var blockchainConfig: BlockchainConfig,
blockchainConfig: BlockchainConfig,
consensusConfig: ConsensusConfig,
override val difficultyCalculator: DifficultyCalculator,
var blockTimestamp: Long = 0
blockTimestamp: Long = 0
) // var, because it can be modified by test_ RPC endpoints
extends Consensus {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class EthashBlockHeaderValidatorSpec
byzantiumBlockNumber = 4370000,
constantinopleBlockNumber = 7280000,
istanbulBlockNumber = 9069000,
eip155BlockNumber = Long.MaxValue,
eip155BlockNumber = Long.MaxValue,
eip160BlockNumber = Long.MaxValue,
eip161BlockNumber = Long.MaxValue,
eip150BlockNumber = Long.MaxValue,
Expand Down
13 changes: 10 additions & 3 deletions src/test/scala/io/iohk/ethereum/ledger/LedgerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ class LedgerSpec extends AnyFlatSpec with ScalaCheckPropertyChecks with Matchers
payload = ByteString.empty
)
val stx: SignedTransactionWithSender = SignedTransaction.sign(tx, originKeyPair, Some(blockchainConfig.chainId))
val header: BlockHeader = defaultBlockHeader.copy(number = blockchainConfig.forkBlockNumbers.byzantiumBlockNumber - 1)
val header: BlockHeader =
defaultBlockHeader.copy(number = blockchainConfig.forkBlockNumbers.byzantiumBlockNumber - 1)

val result: Either[BlockExecutionError.TxsExecutionError, BlockResult] =
consensus.blockPreparator.executeTransactions(Seq(stx.tx), initialWorld, header)
Expand All @@ -374,7 +375,10 @@ class LedgerSpec extends AnyFlatSpec with ScalaCheckPropertyChecks with Matchers
)
val stx: SignedTransaction = SignedTransaction.sign(tx, originKeyPair, Some(blockchainConfig.chainId)).tx
val header: BlockHeader =
defaultBlockHeader.copy(beneficiary = minerAddress.bytes, number = blockchainConfig.forkBlockNumbers.byzantiumBlockNumber)
defaultBlockHeader.copy(
beneficiary = minerAddress.bytes,
number = blockchainConfig.forkBlockNumbers.byzantiumBlockNumber
)

val result: Either[BlockExecutionError.TxsExecutionError, BlockResult] =
consensus.blockPreparator.executeTransactions(Seq(stx), initialWorld, header)
Expand All @@ -400,7 +404,10 @@ class LedgerSpec extends AnyFlatSpec with ScalaCheckPropertyChecks with Matchers
)
val stx: SignedTransactionWithSender = SignedTransaction.sign(tx, originKeyPair, Some(blockchainConfig.chainId))
val header: BlockHeader =
defaultBlockHeader.copy(beneficiary = minerAddress.bytes, number = blockchainConfig.forkBlockNumbers.byzantiumBlockNumber)
defaultBlockHeader.copy(
beneficiary = minerAddress.bytes,
number = blockchainConfig.forkBlockNumbers.byzantiumBlockNumber
)

val result: Either[BlockExecutionError.TxsExecutionError, BlockResult] =
testConsensus.blockPreparator.executeTransactions(Seq(stx.tx), initialWorld, header)
Expand Down
36 changes: 20 additions & 16 deletions src/test/scala/io/iohk/ethereum/ledger/LedgerTestSetup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,26 @@ trait DaoForkTestSetup extends TestSetup with MockFactory {
override val refundContract: Option[Address] = Some(Address(4))
}

val proDaoBlockchainConfig: BlockchainConfig = blockchainConfig.withUpdatedForkBlocks(_.copy(
eip106BlockNumber = Long.MaxValue,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue,
petersburgBlockNumber = Long.MaxValue
)).copy(
chainId = 0x01.toByte,
networkId = 1,
daoForkConfig = Some(supportDaoForkConfig),
customGenesisFileOpt = None,
maxCodeSize = None,
bootstrapNodes = Set(),
gasTieBreaker = false,
ethCompatibleStorage = true,
)
val proDaoBlockchainConfig: BlockchainConfig = blockchainConfig
.withUpdatedForkBlocks(
_.copy(
eip106BlockNumber = Long.MaxValue,
atlantisBlockNumber = Long.MaxValue,
aghartaBlockNumber = Long.MaxValue,
phoenixBlockNumber = Long.MaxValue,
petersburgBlockNumber = Long.MaxValue
)
)
.copy(
chainId = 0x01.toByte,
networkId = 1,
daoForkConfig = Some(supportDaoForkConfig),
customGenesisFileOpt = None,
maxCodeSize = None,
bootstrapNodes = Set(),
gasTieBreaker = false,
ethCompatibleStorage = true
)

val parentBlockHeader = Fixtures.Blocks.DaoParentBlock.header

Expand Down

0 comments on commit 8beb53d

Please sign in to comment.