Skip to content

Commit 87f6355

Browse files
author
Aurélien Richez
committed
[refactor] put all the state in the same place and create a builder for consensus and ledger
1 parent cd61845 commit 87f6355

File tree

7 files changed

+70
-46
lines changed

7 files changed

+70
-46
lines changed

src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import io.iohk.ethereum.{crypto, domain, rlp}
1010
import io.iohk.ethereum.domain.Block._
1111
import io.iohk.ethereum.domain.{Account, Address, Block, BlockchainImpl, UInt256}
1212
import io.iohk.ethereum.ledger._
13-
import io.iohk.ethereum.testmode.{TestServiceProvider, TestmodeConsensus}
13+
import io.iohk.ethereum.testmode.{TestModeComponentsProvider, TestmodeConsensus}
1414
import io.iohk.ethereum.transactions.PendingTransactionsManager
1515
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
1616
import io.iohk.ethereum.utils.{BlockchainConfig, ByteStringUtils, ForkBlockNumbers, Logger}
@@ -108,8 +108,7 @@ class TestService(
108108
blockchain: BlockchainImpl,
109109
pendingTransactionsManager: ActorRef,
110110
consensusConfig: ConsensusConfig,
111-
consensus: TestmodeConsensus,
112-
testLedgerWrapper: TestServiceProvider,
111+
testModeComponentsProvider: TestModeComponentsProvider,
113112
initialConfig: BlockchainConfig
114113
)(implicit
115114
scheduler: Scheduler
@@ -122,6 +121,7 @@ class TestService(
122121
private var accountAddresses: List[String] = List()
123122
private var accountRangeOffset = 0
124123
private var currentConfig: BlockchainConfig = initialConfig
124+
private var blockTimestamp: Long = 0
125125

126126
def setChainParams(request: SetChainParamsRequest): ServiceResponse[SetChainParamsResponse] = {
127127
currentConfig = buildNewConfig(request.chainParams.blockchainParams)
@@ -153,9 +153,6 @@ class TestService(
153153
storeGenesisAccountCodes(genesisData.alloc)
154154
storeGenesisAccountStorageData(genesisData.alloc)
155155

156-
// update test ledger with new config
157-
consensus.blockchainConfig = currentConfig
158-
159156
accountAddresses = genesisData.alloc.keys.toList
160157
accountRangeOffset = 0
161158

@@ -217,11 +214,11 @@ class TestService(
217214
def mineBlocks(request: MineBlocksRequest): ServiceResponse[MineBlocksResponse] = {
218215
def mineBlock(): Task[Unit] = {
219216
getBlockForMining(blockchain.getBestBlock().get)
220-
.flatMap(blockForMining => testLedgerWrapper.ledger(currentConfig).importBlock(blockForMining.block))
217+
.flatMap(blockForMining => testModeComponentsProvider.ledger(currentConfig).importBlock(blockForMining.block))
221218
.map { res =>
222219
log.info("Block mining result: " + res)
223220
pendingTransactionsManager ! PendingTransactionsManager.ClearPendingTransactions
224-
consensus.blockTimestamp += 1
221+
blockTimestamp += 1
225222
}
226223
}
227224

@@ -234,7 +231,7 @@ class TestService(
234231
}
235232

236233
def modifyTimestamp(request: ModifyTimestampRequest): ServiceResponse[ModifyTimestampResponse] = {
237-
consensus.blockTimestamp = request.timestamp
234+
blockTimestamp = request.timestamp
238235
ModifyTimestampResponse().rightNow
239236
}
240237

@@ -250,7 +247,8 @@ class TestService(
250247
Try(decode(request.blockRlp).toBlock) match {
251248
case Failure(_) => Task.now(Left(JsonRpcError(-1, "block validation failed!", None)))
252249
case Success(value) =>
253-
testLedgerWrapper.ledger(currentConfig)
250+
testModeComponentsProvider
251+
.ledger(currentConfig)
254252
.importBlock(value)
255253
.flatMap(handleResult)
256254
}
@@ -277,7 +275,9 @@ class TestService(
277275
.timeout(timeout.duration)
278276
.onErrorRecover { case _ => PendingTransactionsResponse(Nil) }
279277
.map { pendingTxs =>
280-
consensus.blockGenerator
278+
testModeComponentsProvider
279+
.consensus(currentConfig, blockTimestamp)
280+
.blockGenerator
281281
.generateBlock(
282282
parentBlock,
283283
pendingTxs.pendingTransactions.map(_.stx.tx),

src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import io.iohk.ethereum.network.p2p.EthereumMessageDecoder
2828
import io.iohk.ethereum.network.rlpx.AuthHandshaker
2929
import io.iohk.ethereum.network.{PeerManagerActor, ServerActor, _}
3030
import io.iohk.ethereum.ommers.OmmersPool
31-
import io.iohk.ethereum.testmode.{TestEthBlockServiceWrapper, TestLedgerBuilder, TestmodeConsensusBuilder}
31+
import io.iohk.ethereum.testmode.{TestEthBlockServiceWrapper, TestModeServiceBuilder, TestmodeConsensusBuilder}
3232
import io.iohk.ethereum.transactions.{PendingTransactionsManager, TransactionHistoryService}
3333
import io.iohk.ethereum.utils.Config.SyncConfig
3434
import io.iohk.ethereum.utils._
@@ -363,14 +363,20 @@ trait TestServiceBuilder {
363363
with BlockchainConfigBuilder
364364
with VmBuilder
365365
with TestmodeConsensusBuilder
366-
with TestLedgerBuilder =>
366+
with TestModeServiceBuilder =>
367367

368368
lazy val testService =
369-
new TestService(blockchain, pendingTransactionsManager, consensusConfig, consensus, testLedgerWrapper, blockchainConfig)(scheduler)
369+
new TestService(
370+
blockchain,
371+
pendingTransactionsManager,
372+
consensusConfig,
373+
testModeComponentsProvider,
374+
blockchainConfig
375+
)(scheduler)
370376
}
371377

372378
trait TestEthBlockServiceBuilder extends EthBlocksServiceBuilder {
373-
self: BlockchainBuilder with TestLedgerBuilder with ConsensusBuilder =>
379+
self: BlockchainBuilder with TestModeServiceBuilder with ConsensusBuilder =>
374380
override lazy val ethBlocksService = new TestEthBlockServiceWrapper(blockchain, ledger, consensus)
375381
}
376382

src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import io.iohk.ethereum.consensus.StdConsensusBuilder
55
import io.iohk.ethereum.metrics.{Metrics, MetricsConfig}
66
import io.iohk.ethereum.network.discovery.PeerDiscoveryManager
77
import io.iohk.ethereum.network.{PeerManagerActor, ServerActor}
8-
import io.iohk.ethereum.testmode.{TestLedgerBuilder, TestmodeConsensusBuilder}
8+
import io.iohk.ethereum.testmode.{TestModeServiceBuilder, TestmodeConsensusBuilder}
99
import io.iohk.ethereum.utils.Config
1010
import scala.concurrent.ExecutionContext.Implicits.global
1111
import scala.concurrent.Await
@@ -106,7 +106,7 @@ abstract class BaseNode extends Node {
106106
class StdNode extends BaseNode with StdLedgerBuilder with StdConsensusBuilder
107107
class TestNode
108108
extends BaseNode
109-
with TestLedgerBuilder
109+
with TestModeServiceBuilder
110110
with TestmodeConsensusBuilder
111111
with TestServiceBuilder
112112
with TestEthBlockServiceBuilder
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.iohk.ethereum.testmode
2+
3+
import io.iohk.ethereum.consensus.difficulty.DifficultyCalculator
4+
import io.iohk.ethereum.consensus.{Consensus, ConsensusConfig}
5+
import io.iohk.ethereum.domain.BlockchainImpl
6+
import io.iohk.ethereum.ledger.Ledger.VMImpl
7+
import io.iohk.ethereum.ledger.{Ledger, LedgerImpl, StxLedger}
8+
import io.iohk.ethereum.utils.BlockchainConfig
9+
import io.iohk.ethereum.utils.Config.SyncConfig
10+
import monix.execution.Scheduler
11+
12+
/** Provides a ledger instance with modifiable blockchain config (used in test mode). */
13+
class TestModeComponentsProvider(
14+
blockchain: BlockchainImpl,
15+
syncConfig: SyncConfig,
16+
validationExecutionContext: Scheduler,
17+
consensusConfig: ConsensusConfig,
18+
difficultyCalculator: DifficultyCalculator,
19+
vm: VMImpl
20+
) {
21+
22+
def ledger(blockchainConfig: BlockchainConfig): Ledger =
23+
new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus(blockchainConfig), validationExecutionContext)
24+
def stxLedger(blockchainConfig: BlockchainConfig): StxLedger =
25+
new StxLedger(blockchain, blockchainConfig, consensus(blockchainConfig).blockPreparator)
26+
def consensus(blockchainConfig: BlockchainConfig, blockTimestamp: Long = 0) =
27+
new TestmodeConsensus(vm, blockchain, blockchainConfig, consensusConfig, difficultyCalculator, blockTimestamp)
28+
}

src/main/scala/io/iohk/ethereum/testmode/TestLedgerBuilder.scala renamed to src/main/scala/io/iohk/ethereum/testmode/TestModeServiceBuilder.scala

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,36 @@ package io.iohk.ethereum.testmode
22

33
import akka.util.ByteString
44
import cats.data.NonEmptyList
5-
import io.iohk.ethereum.consensus.{Consensus, ConsensusBuilder}
5+
import io.iohk.ethereum.consensus.difficulty.DifficultyCalculator
6+
import io.iohk.ethereum.consensus.{Consensus, ConsensusBuilder, ConsensusConfigBuilder}
67
import io.iohk.ethereum.domain._
78
import io.iohk.ethereum.ledger._
89
import io.iohk.ethereum.nodebuilder.{ActorSystemBuilder, _}
910
import monix.eval.Task
1011
import monix.execution.Scheduler
1112

12-
trait TestLedgerBuilder extends LedgerBuilder {
13+
trait TestModeServiceBuilder extends LedgerBuilder {
1314
self: BlockchainConfigBuilder
1415
with BlockchainBuilder
1516
with SyncConfigBuilder
1617
with ConsensusBuilder
17-
with ActorSystemBuilder =>
18+
with ActorSystemBuilder
19+
with ConsensusConfigBuilder
20+
with VmBuilder =>
1821

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

21-
lazy val testLedgerWrapper: TestServiceProvider =
22-
new TestServiceProvider(blockchain, syncConfig, consensus, scheduler)
24+
lazy val testModeComponentsProvider: TestModeComponentsProvider =
25+
new TestModeComponentsProvider(
26+
blockchain,
27+
syncConfig,
28+
scheduler,
29+
consensusConfig,
30+
DifficultyCalculator(blockchainConfig),
31+
vm
32+
)
2333

24-
private def testLedger: Ledger = testLedgerWrapper.ledger(blockchainConfig)
34+
private def testLedger: Ledger = testModeComponentsProvider.ledger(blockchainConfig)
2535

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

3747
override lazy val ledger: Ledger = new TestLedgerProxy
38-
override lazy val stxLedger: StxLedger = testLedgerWrapper.stxLedger(blockchainConfig)
48+
override lazy val stxLedger: StxLedger = testModeComponentsProvider.stxLedger(blockchainConfig)
3949
}

src/main/scala/io/iohk/ethereum/testmode/TestServiceProvider.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/main/scala/io/iohk/ethereum/testmode/TestmodeConsensus.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import io.iohk.ethereum.consensus.pow.validators.ValidatorsExecutor
2020
class TestmodeConsensus(
2121
override val vm: VMImpl,
2222
blockchain: BlockchainImpl,
23-
var blockchainConfig: BlockchainConfig,
23+
blockchainConfig: BlockchainConfig,
2424
consensusConfig: ConsensusConfig,
2525
override val difficultyCalculator: DifficultyCalculator,
26-
var blockTimestamp: Long = 0
26+
blockTimestamp: Long = 0
2727
) // var, because it can be modified by test_ RPC endpoints
2828
extends Consensus {
2929

0 commit comments

Comments
 (0)