Skip to content

Commit 3221837

Browse files
author
Łukasz Gąsior
committed
Use block generator from consensus in TestService
1 parent eb18f82 commit 3221837

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.iohk.ethereum.blockchain.data.{AllocAccount, GenesisData, GenesisDataL
66
import io.iohk.ethereum.consensus.ConsensusConfig
77
import io.iohk.ethereum.consensus.blocks._
88
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
9-
import io.iohk.ethereum.testmode.TestLedgerWrapper
9+
import io.iohk.ethereum.testmode.{TestLedgerWrapper, TestmodeConsensus}
1010
import io.iohk.ethereum.transactions.PendingTransactionsManager
1111
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
1212
import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, Logger, MonetaryPolicyConfig}
@@ -52,14 +52,14 @@ class TestService(
5252
blockchain: BlockchainImpl,
5353
pendingTransactionsManager: ActorRef,
5454
consensusConfig: ConsensusConfig,
55+
consensus: TestmodeConsensus,
5556
testLedgerWrapper: TestLedgerWrapper)
5657
extends Logger {
5758

5859
import TestService._
5960
import akka.pattern.ask
6061

6162
private var etherbase: Address = consensusConfig.coinbase
62-
private var testBlockTimestamp: Long = System.currentTimeMillis()
6363

6464
def setChainParams(request: SetChainParamsRequest): ServiceResponse[SetChainParamsResponse] = {
6565
val newBlockchainConfig = new BlockchainConfig {
@@ -111,7 +111,7 @@ class TestService(
111111
val res = testLedgerWrapper.ledger.importBlock(blockForMining.block)
112112
log.info("Block mining result: " + res)
113113
pendingTransactionsManager ! PendingTransactionsManager.ClearPendingTransactions
114-
testBlockTimestamp += 1
114+
consensus.blockTimestamp += 1
115115
}
116116
}
117117

@@ -124,7 +124,7 @@ class TestService(
124124
}
125125

126126
def modifyTimestamp(request: ModifyTimestampRequest): ServiceResponse[ModifyTimestampResponse] = {
127-
testBlockTimestamp = request.timestamp
127+
consensus.blockTimestamp = request.timestamp
128128
Future.successful(Right(ModifyTimestampResponse()))
129129
}
130130

@@ -143,20 +143,12 @@ class TestService(
143143
}
144144

145145
private def getBlockForMining(parentBlock: Block): Future[PendingBlock] = {
146-
val blockGenerator =
147-
new NoOmmersBlockGenerator(blockchain, testLedgerWrapper.blockchainConfig, consensusConfig, testLedgerWrapper.ledger.consensus.blockPreparator,
148-
new BlockTimestampProvider {
149-
override def getEpochSecond: Long = testBlockTimestamp
150-
}) {
151-
override def withBlockTimestampProvider(blockTimestampProvider: BlockTimestampProvider): TestBlockGenerator = this
152-
}
153-
154146
implicit val timeout = Timeout(5.seconds)
155147
(pendingTransactionsManager ? PendingTransactionsManager.GetPendingTransactions)
156148
.mapTo[PendingTransactionsResponse]
157149
.recover { case _ => PendingTransactionsResponse(Nil) }
158150
.flatMap { pendingTxs =>
159-
blockGenerator.generateBlock(parentBlock, pendingTxs.pendingTransactions.map(_.stx), etherbase, Nil) match {
151+
consensus.blockGenerator.generateBlock(parentBlock, pendingTxs.pendingTransactions.map(_.stx), etherbase, Nil) match {
160152
case Right(pb) => Future.successful(pb)
161153
case Left(err) => Future.failed(new RuntimeException(s"Error while generating block for mining: $err"))
162154
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import io.iohk.ethereum.network.p2p.EthereumMessageDecoder
3434
import io.iohk.ethereum.network.rlpx.AuthHandshaker
3535
import io.iohk.ethereum.transactions.PendingTransactionsManager
3636
import io.iohk.ethereum.ommers.OmmersPool
37-
import io.iohk.ethereum.testmode.TestLedgerBuilder
37+
import io.iohk.ethereum.testmode.{TestLedgerBuilder, TestmodeConsensusBuilder}
3838
import io.iohk.ethereum.utils.Config.SyncConfig
3939

4040
import scala.util.{Failure, Success, Try}
@@ -297,10 +297,10 @@ trait TestServiceBuilder {
297297
ConsensusConfigBuilder with
298298
BlockchainConfigBuilder with
299299
VmBuilder with
300-
ConsensusBuilder with
300+
TestmodeConsensusBuilder with
301301
TestLedgerBuilder =>
302302

303-
lazy val testService = new TestService(blockchain, pendingTransactionsManager, consensusConfig, testLedgerWrapper)
303+
lazy val testService = new TestService(blockchain, pendingTransactionsManager, consensusConfig, consensus, testLedgerWrapper)
304304
}
305305

306306
trait EthServiceBuilder {

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

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

33
import akka.util.ByteString
44
import io.iohk.ethereum.consensus._
5-
import io.iohk.ethereum.consensus.ethash.blocks.EthashBlockGeneratorImpl
6-
import io.iohk.ethereum.consensus.ethash.validators.{EthashValidators, OmmersValidator}
5+
import io.iohk.ethereum.consensus.blocks.{BlockTimestampProvider, NoOmmersBlockGenerator, TestBlockGenerator}
76
import io.iohk.ethereum.consensus.validators._
87
import io.iohk.ethereum.consensus.validators.std.{StdBlockValidator, StdSignedTransactionValidator}
98
import io.iohk.ethereum.domain.{Block, BlockHeader, BlockchainImpl, Receipt}
@@ -17,17 +16,17 @@ class TestmodeConsensus(
1716
override val vm: VMImpl,
1817
blockchain: BlockchainImpl,
1918
blockchainConfig: BlockchainConfig,
20-
consensusConfig: ConsensusConfig)
19+
consensusConfig: ConsensusConfig,
20+
var blockTimestamp: Long = 0) // var, because it can be modified by test_ RPC endpoints
2121
extends Consensus {
2222

2323
override type Config = AnyRef
2424
override def protocol: Protocol = Protocol.Ethash
2525
override def config: FullConsensusConfig[AnyRef] = FullConsensusConfig[AnyRef](consensusConfig, "")
2626

27-
class TestValidators extends EthashValidators {
27+
class TestValidators extends Validators {
2828
override def blockHeaderValidator: BlockHeaderValidator = (_, _) => Right(BlockHeaderValid)
2929
override def signedTransactionValidator: SignedTransactionValidator = new StdSignedTransactionValidator(blockchainConfig)
30-
override def ommersValidator: OmmersValidator = (_, _, _, _, _) => Right(OmmersValidator.OmmersValid)
3130
override def validateBlockBeforeExecution(block: Block, getBlockHeaderByHash: GetBlockHeaderByHash, getNBlocksBack: GetNBlocksBack)
3231
: Either[BlockExecutionError.ValidationBeforeExecError, BlockExecutionSuccess] = Right(BlockExecutionSuccess)
3332
override def validateBlockAfterExecution(block: Block, stateRootHash: ByteString,receipts: Seq[Receipt], gasUsed: BigInt)
@@ -40,20 +39,21 @@ class TestmodeConsensus(
4039
}
4140
}
4241

43-
override def validators: EthashValidators = new TestValidators
42+
override def validators: Validators = new TestValidators
4443

4544
override val blockPreparator: BlockPreparator = new BlockPreparator(
4645
vm = vm,
4746
signedTxValidator = validators.signedTransactionValidator,
4847
blockchain = blockchain,
4948
blockchainConfig = blockchainConfig)
5049

51-
override val blockGenerator = new EthashBlockGeneratorImpl(
52-
validators = validators,
53-
blockchain = blockchain,
54-
blockchainConfig = blockchainConfig,
55-
consensusConfig = config.generic,
56-
blockPreparator = blockPreparator)
50+
override val blockGenerator =
51+
new NoOmmersBlockGenerator(blockchain, blockchainConfig, consensusConfig, blockPreparator,
52+
new BlockTimestampProvider {
53+
override def getEpochSecond: Long = blockTimestamp
54+
}) {
55+
override def withBlockTimestampProvider(blockTimestampProvider: BlockTimestampProvider): TestBlockGenerator = this
56+
}
5757

5858
override def startProtocol(node: Node): Unit = {}
5959
override def stopProtocol(): Unit = {}

0 commit comments

Comments
 (0)