Skip to content

Commit d8f62df

Browse files
author
Łukasz Gąsior
committed
wip
1 parent 04958f7 commit d8f62df

File tree

16 files changed

+258
-131
lines changed

16 files changed

+258
-131
lines changed

src/main/resources/application.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ mantis {
200200
peer-manager-timeout = 5.seconds
201201
}
202202

203-
active-timeout = 5.seconds
203+
// TODO: rename to miner-active-timeot
204+
miner-active-timeout = 5.seconds
204205
}
205206
}
206207

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package io.iohk.ethereum
22

3-
import io.iohk.ethereum.nodebuilder.StdNode
3+
import io.iohk.ethereum.nodebuilder.{StdNode, TestNode}
4+
import io.iohk.ethereum.utils.Config
45

56
object Mantis {
67
def main(args: Array[String]): Unit = {
7-
val node = new StdNode
8+
val node =
9+
if (Config.testmode) new TestNode
10+
else new StdNode
11+
812
node.start()
913
}
1014
}

src/main/scala/io/iohk/ethereum/consensus/ConsensusBuilder.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import io.iohk.ethereum.consensus.ethash.EthashConsensus
55
import io.iohk.ethereum.nodebuilder._
66
import io.iohk.ethereum.utils.{Config, Logger}
77

8+
trait ConsensusBuilder {
9+
def consensus: Consensus
10+
}
11+
812
/**
913
* A consensus builder is responsible to instantiate the consensus protocol.
1014
* This is done dynamically when Mantis boots, based on its configuration.
@@ -13,7 +17,7 @@ import io.iohk.ethereum.utils.{Config, Logger}
1317
* [[io.iohk.ethereum.consensus.ethash.EthashConsensus EthashConsensus]],
1418
* [[io.iohk.ethereum.consensus.atomixraft.AtomixRaftConsensus AtomixRaftConsensus]]
1519
*/
16-
trait ConsensusBuilder {
20+
trait StdConsensusBuilder extends ConsensusBuilder {
1721
self: VmBuilder with BlockchainBuilder with BlockchainConfigBuilder with ConsensusConfigBuilder with Logger
1822

1923
private lazy val mantisConfig = Config.config
@@ -44,7 +48,8 @@ trait ConsensusBuilder {
4448
case Protocol.Ethash buildEthashConsensus()
4549
case Protocol.AtomixRaft buildAtomixRaftConsensus()
4650
}
47-
log.info(s"Using '${protocol.name}' consensus [${consensus.getClass.getName}]")
51+
// TODO: why the fuck does it fail with npe?
52+
// log.info(s"Using '${protocol.name}' consensus [${consensus.getClass.getName}]")
4853

4954
consensus
5055
}

src/main/scala/io/iohk/ethereum/consensus/TestConsensusBuilder.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import io.iohk.ethereum.utils.Logger
77
* A [[io.iohk.ethereum.consensus.ConsensusBuilder ConsensusBuilder]] that builds a
88
* [[io.iohk.ethereum.consensus.TestConsensus TestConsensus]]
99
*/
10-
trait TestConsensusBuilder { self: ConsensusBuilder
10+
trait TestConsensusBuilder { self: StdConsensusBuilder
1111
protected def buildTestConsensus(): TestConsensus =
1212
buildConsensus().asInstanceOf[TestConsensus] // we are in tests, so if we get an exception, so be it
1313
}
1414

1515
/** A standard [[TestConsensusBuilder]] cake. */
16-
trait StdTestConsensusBuilder extends ConsensusBuilder
16+
trait StdTestConsensusBuilder extends StdConsensusBuilder
1717
with TestConsensusBuilder
1818
with VmBuilder
1919
with VmConfigBuilder

src/main/scala/io/iohk/ethereum/consensus/ethash/EthashConsensus.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import io.iohk.ethereum.consensus.blocks.TestBlockGenerator
99
import io.iohk.ethereum.consensus.ethash.EthashMiner.MinerMsg
1010
import io.iohk.ethereum.consensus.ethash.blocks.{EthashBlockGenerator, EthashBlockGeneratorImpl}
1111
import io.iohk.ethereum.consensus.ethash.validators.{EthashValidators, StdEthashValidators}
12-
import io.iohk.ethereum.consensus.validators.Validators
12+
import io.iohk.ethereum.consensus.validators._
1313
import io.iohk.ethereum.domain.BlockchainImpl
1414
import io.iohk.ethereum.ledger.BlockPreparator
1515
import io.iohk.ethereum.ledger.Ledger.VMImpl
1616
import io.iohk.ethereum.nodebuilder.Node
1717
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
1818

19+
1920
/**
2021
* Implements standard Ethereum consensus (ethash PoW).
2122
*/

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ object JsonRpcController {
2727
def encodeJson(t: T): JValue
2828
}
2929

30-
3130
trait JsonRpcConfig {
3231
def apis: Seq[String]
3332
def accountTransactionsMaxBlocks: Int
@@ -38,6 +37,7 @@ object JsonRpcController {
3837

3938
object JsonRpcConfig {
4039
def apply(mantisConfig: TypesafeConfig): JsonRpcConfig = {
40+
import scala.concurrent.duration._
4141
val rpcConfig = mantisConfig.getConfig("network.rpc")
4242

4343
new JsonRpcConfig {
@@ -49,6 +49,7 @@ object JsonRpcController {
4949
}
5050

5151
override def accountTransactionsMaxBlocks: Int = rpcConfig.getInt("account-transactions-max-blocks")
52+
override def minerActiveTimeout: FiniteDuration = rpcConfig.getDuration("miner-active-timeout").toMillis.millis
5253

5354
override val httpServerConfig: JsonRpcHttpServerConfig = JsonRpcHttpServerConfig(mantisConfig)
5455
override val ipcServerConfig: JsonRpcIpcServerConfig = JsonRpcIpcServerConfig(mantisConfig)
@@ -77,7 +78,7 @@ class JsonRpcController(
7778
netService: NetService,
7879
ethService: EthService,
7980
personalService: PersonalService,
80-
testService: TestService,
81+
testServiceOpt: Option[TestService],
8182
config: JsonRpcConfig) extends Logger {
8283

8384
import JsonRpcController._
@@ -87,7 +88,7 @@ class JsonRpcController(
8788
import JsonMethodsImplicits._
8889
import JsonRpcErrors._
8990

90-
val apisHandleFns: Map[String, PartialFunction[JsonRpcRequest, Future[JsonRpcResponse]]] = Map(
91+
lazy val apisHandleFns: Map[String, PartialFunction[JsonRpcRequest, Future[JsonRpcResponse]]] = Map(
9192
Apis.Eth -> handleEthRequest,
9293
Apis.Web3 -> handleWeb3Request,
9394
Apis.Net -> handleNetRequest,
@@ -208,6 +209,13 @@ class JsonRpcController(
208209
}
209210

210211
private def handleTestRequest: PartialFunction[JsonRpcRequest, Future[JsonRpcResponse]] = {
212+
testServiceOpt match {
213+
case Some(testService) => handleTestRequest(testService)
214+
case None => PartialFunction.empty
215+
}
216+
}
217+
218+
private def handleTestRequest(testService: TestService): PartialFunction[JsonRpcRequest, Future[JsonRpcResponse]] = {
211219
case req@JsonRpcRequest(_, "test_setChainParams", _, _) =>
212220
handle[SetChainParamsRequest, SetChainParamsResponse](testService.setChainParams, req)
213221
case req@JsonRpcRequest(_, "test_mineBlocks", _, _) =>

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

Lines changed: 39 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package io.iohk.ethereum.jsonrpc
22

33
import akka.actor.ActorRef
4-
import akka.agent.Agent
54
import akka.util.{ByteString, Timeout}
65
import io.iohk.ethereum.blockchain.data.{AllocAccount, GenesisData, GenesisDataLoader}
7-
import io.iohk.ethereum.consensus.{Consensus, ConsensusConfig, FullConsensusConfig, Protocol}
8-
import io.iohk.ethereum.consensus.blocks.{BlockGenerator, BlockTimestampProvider, PendingBlock}
9-
import io.iohk.ethereum.consensus.validators.Validators
6+
import io.iohk.ethereum.consensus.ConsensusConfig
7+
import io.iohk.ethereum.consensus.blocks._
108
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
11-
import io.iohk.ethereum.ledger.Ledger.VMImpl
12-
import io.iohk.ethereum.ledger._
13-
import io.iohk.ethereum.nodebuilder.Node
9+
import io.iohk.ethereum.testmode.TestLedgerWrapper
1410
import io.iohk.ethereum.transactions.PendingTransactionsManager
1511
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
1612
import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, MonetaryPolicyConfig}
@@ -53,40 +49,36 @@ object TestService {
5349
}
5450

5551
class TestService(
56-
vm: VMImpl,
5752
blockchain: BlockchainImpl,
5853
pendingTransactionsManager: ActorRef,
5954
consensusConfig: ConsensusConfig,
60-
initialBlockchainConfig: BlockchainConfig,
61-
validators: Validators,
62-
consensus: Consensus) {
55+
testLedgerWrapper: TestLedgerWrapper) {
6356

6457
import TestService._
6558
import akka.pattern.ask
6659

67-
private var blockchainConfig: BlockchainConfig = initialBlockchainConfig
6860
private var etherbase: Address = consensusConfig.coinbase
6961
private var testBlockTimestamp: Long = System.currentTimeMillis()
7062

7163
def setChainParams(request: SetChainParamsRequest): ServiceResponse[SetChainParamsResponse] = {
7264
val newBlockchainConfig = new BlockchainConfig {
73-
override val frontierBlockNumber: BigInt = initialBlockchainConfig.frontierBlockNumber
65+
override val frontierBlockNumber: BigInt = testLedgerWrapper.blockchainConfig.frontierBlockNumber
7466
override val homesteadBlockNumber: BigInt = request.chainParams.blockchainParams.homesteadForkBlock
75-
override val eip106BlockNumber: BigInt = initialBlockchainConfig.eip106BlockNumber
67+
override val eip106BlockNumber: BigInt = testLedgerWrapper.blockchainConfig.eip106BlockNumber
7668
override val eip150BlockNumber: BigInt = request.chainParams.blockchainParams.EIP150ForkBlock
77-
override val eip155BlockNumber: BigInt = initialBlockchainConfig.eip155BlockNumber
78-
override val eip160BlockNumber: BigInt = initialBlockchainConfig.eip160BlockNumber
79-
override val eip161BlockNumber: BigInt = initialBlockchainConfig.eip161BlockNumber
80-
override val maxCodeSize: Option[BigInt] = initialBlockchainConfig.maxCodeSize
81-
override val difficultyBombPauseBlockNumber: BigInt = initialBlockchainConfig.difficultyBombPauseBlockNumber
82-
override val difficultyBombContinueBlockNumber: BigInt = initialBlockchainConfig.difficultyBombContinueBlockNumber
83-
override val customGenesisFileOpt: Option[String] = initialBlockchainConfig.customGenesisFileOpt
84-
override val daoForkConfig: Option[DaoForkConfig] = initialBlockchainConfig.daoForkConfig
69+
override val eip155BlockNumber: BigInt = testLedgerWrapper.blockchainConfig.eip155BlockNumber
70+
override val eip160BlockNumber: BigInt = testLedgerWrapper.blockchainConfig.eip160BlockNumber
71+
override val eip161BlockNumber: BigInt = testLedgerWrapper.blockchainConfig.eip161BlockNumber
72+
override val maxCodeSize: Option[BigInt] = testLedgerWrapper.blockchainConfig.maxCodeSize
73+
override val difficultyBombPauseBlockNumber: BigInt = testLedgerWrapper.blockchainConfig.difficultyBombPauseBlockNumber
74+
override val difficultyBombContinueBlockNumber: BigInt = testLedgerWrapper.blockchainConfig.difficultyBombContinueBlockNumber
75+
override val customGenesisFileOpt: Option[String] = testLedgerWrapper.blockchainConfig.customGenesisFileOpt
76+
override val daoForkConfig: Option[DaoForkConfig] = testLedgerWrapper.blockchainConfig.daoForkConfig
8577
override val accountStartNonce: UInt256 = UInt256(request.chainParams.blockchainParams.accountStartNonce)
86-
override val chainId: Byte = initialBlockchainConfig.chainId
87-
override val monetaryPolicyConfig: MonetaryPolicyConfig = initialBlockchainConfig.monetaryPolicyConfig
88-
override val gasTieBreaker: Boolean = initialBlockchainConfig.gasTieBreaker
89-
override val ethCompatibleStorage: Boolean = initialBlockchainConfig.ethCompatibleStorage
78+
override val chainId: Byte = testLedgerWrapper.blockchainConfig.chainId
79+
override val monetaryPolicyConfig: MonetaryPolicyConfig = testLedgerWrapper.blockchainConfig.monetaryPolicyConfig
80+
override val gasTieBreaker: Boolean = testLedgerWrapper.blockchainConfig.gasTieBreaker
81+
override val ethCompatibleStorage: Boolean = testLedgerWrapper.blockchainConfig.ethCompatibleStorage
9082
}
9183

9284
val genesisData = GenesisData(
@@ -106,16 +98,16 @@ class TestService(
10698
val genesisDataLoader = new GenesisDataLoader(blockchain, newBlockchainConfig)
10799
genesisDataLoader.loadGenesisData(genesisData)
108100

109-
// update ledger with new config
110-
setupLedger(newBlockchainConfig)
101+
// update test ledger with new config
102+
testLedgerWrapper.blockchainConfig = newBlockchainConfig
111103

112104
Future.successful(Right(SetChainParamsResponse()))
113105
}
114106

115107
def mineBlocks(request: MineBlocksRequest): ServiceResponse[MineBlocksResponse] = {
116108
def mineBlock(): Future[Unit] = {
117109
getBlockForMining(blockchain.getBestBlock()).map { blockForMining =>
118-
ledgerHolder().importBlock(blockForMining.block) // TODO: check result?
110+
testLedgerWrapper.ledger.importBlock(blockForMining.block) // TODO: check result?
119111
pendingTransactionsManager ! PendingTransactionsManager.ClearPendingTransactions
120112
testBlockTimestamp += 1
121113
}
@@ -149,53 +141,29 @@ class TestService(
149141
}
150142

151143
private def getBlockForMining(parentBlock: Block): Future[PendingBlock] = {
152-
val blockGenerator = new BlockGenerator(
153-
blockchain, blockchainConfig, consensusConfig.headerExtraData, 5, ledgerHolder, validators, new BlockTimestampProvider {
144+
val blockGenerator = new NoOmmersBlockGenerator(
145+
blockchain,
146+
testLedgerWrapper.blockchainConfig,
147+
consensusConfig,
148+
testLedgerWrapper.ledger.consensus.blockPreparator,
149+
new BlockTimestampProvider {
154150
override def getEpochSecond: Long = testBlockTimestamp
155-
}
156-
)
151+
}) {
152+
override def withBlockTimestampProvider(blockTimestampProvider: BlockTimestampProvider): TestBlockGenerator = this
153+
}
157154

158-
implicit val timeout = Timeout(5.seconds)
159-
(pendingTransactionsManager ? PendingTransactionsManager.GetPendingTransactions)
160-
.mapTo[PendingTransactionsResponse]
161-
.flatMap { pendingTxs =>
162-
blockGenerator.generateBlockForMining(parentBlock, pendingTxs.pendingTransactions.map(_.stx), Nil, etherbase) match {
163-
case Right(pb) => Future.successful(pb)
164-
case Left(err) => Future.failed(new RuntimeException(s"Error while generating block for mining: $err"))
165-
}
155+
getTransactionsFromPool.flatMap { pendingTxs =>
156+
blockGenerator.generateBlock(parentBlock, pendingTxs.pendingTransactions.map(_.stx), etherbase, Nil) match {
157+
case Right(pb) => Future.successful(pb)
158+
case Left(err) => Future.failed(new RuntimeException(s"Error while generating block for mining: $err"))
166159
}
160+
}
167161
}
168162

169-
private def setupLedger(newBlockchainConfig: BlockchainConfig): Unit = {
170-
blockchainConfig = newBlockchainConfig
171-
ledgerHolder.send(new LedgerImpl(blockchain, new BlockQueue(blockchain, 5, 5), blockchainConfig, prepareConsensus()))
172-
}
163+
private def getTransactionsFromPool: Future[PendingTransactionsResponse] = {
164+
implicit val timeout = Timeout(5.seconds)
173165

174-
private def testCnsensus(): Consensus = {
175-
new Consensus {
176-
/**
177-
* The type of configuration [[io.iohk.ethereum.consensus.FullConsensusConfig#specific specific]]
178-
* to this consensus protocol implementation.
179-
*/
180-
override type Config = this.type
181-
182-
override def protocol: Protocol = ???
183-
184-
override def config: FullConsensusConfig[this.type] = ???
185-
186-
/**
187-
* This is the VM used while preparing and generating blocks.
188-
*/
189-
override def vm: VMImpl = ???
190-
191-
/**
192-
* Provides the set of validators specific to this consensus protocol.
193-
*/
194-
override def validators: Validators = ???
195-
override def blockPreparator: BlockPreparator = consensus.blockPreparator
196-
override def blockGenerator: BlockGenerator = consensus.blockGenerator
197-
override def startProtocol(node: Node): Unit = consensus.startProtocol(node)
198-
override def stopProtocol(): Unit = consensus.stopProtocol()
199-
}
166+
(pendingTransactionsManager ? PendingTransactionsManager.GetPendingTransactions).mapTo[PendingTransactionsResponse]
167+
.recover { case _ => PendingTransactionsResponse(Nil) }
200168
}
201169
}

0 commit comments

Comments
 (0)