Skip to content

Commit db7776d

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

File tree

8 files changed

+50
-64
lines changed

8 files changed

+50
-64
lines changed

src/main/resources/application.conf

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

203-
// TODO: rename to miner-active-timeot
204203
miner-active-timeout = 5.seconds
205204
}
206205
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import scala.concurrent.duration.FiniteDuration
3535
import scala.util.{Failure, Success, Try}
3636
import scala.language.existentials
3737

38-
// scalastyle:off number.of.methods number.of.types
38+
// scalastyle:off number.of.methods number.of.types file.size.limit
3939
object EthService {
4040

4141
case class ProtocolVersionRequest()

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
99
import io.iohk.ethereum.testmode.TestLedgerWrapper
1010
import io.iohk.ethereum.transactions.PendingTransactionsManager
1111
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
12-
import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, MonetaryPolicyConfig}
12+
import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, Logger, MonetaryPolicyConfig}
1313
import org.spongycastle.util.encoders.Hex
1414

1515
import scala.concurrent.ExecutionContext.Implicits.global
@@ -52,7 +52,8 @@ class TestService(
5252
blockchain: BlockchainImpl,
5353
pendingTransactionsManager: ActorRef,
5454
consensusConfig: ConsensusConfig,
55-
testLedgerWrapper: TestLedgerWrapper) {
55+
testLedgerWrapper: TestLedgerWrapper)
56+
extends Logger {
5657

5758
import TestService._
5859
import akka.pattern.ask
@@ -107,7 +108,8 @@ class TestService(
107108
def mineBlocks(request: MineBlocksRequest): ServiceResponse[MineBlocksResponse] = {
108109
def mineBlock(): Future[Unit] = {
109110
getBlockForMining(blockchain.getBestBlock()).map { blockForMining =>
110-
testLedgerWrapper.ledger.importBlock(blockForMining.block) // TODO: check result?
111+
val res = testLedgerWrapper.ledger.importBlock(blockForMining.block)
112+
log.info("Block mining result: " + res)
111113
pendingTransactionsManager ! PendingTransactionsManager.ClearPendingTransactions
112114
testBlockTimestamp += 1
113115
}
@@ -141,29 +143,23 @@ class TestService(
141143
}
142144

143145
private def getBlockForMining(parentBlock: Block): Future[PendingBlock] = {
144-
val blockGenerator = new NoOmmersBlockGenerator(
145-
blockchain,
146-
testLedgerWrapper.blockchainConfig,
147-
consensusConfig,
148-
testLedgerWrapper.ledger.consensus.blockPreparator,
149-
new BlockTimestampProvider {
150-
override def getEpochSecond: Long = testBlockTimestamp
151-
}) {
146+
val blockGenerator =
147+
new NoOmmersBlockGenerator(blockchain, testLedgerWrapper.blockchainConfig, consensusConfig, testLedgerWrapper.ledger.consensus.blockPreparator,
148+
new BlockTimestampProvider {
149+
override def getEpochSecond: Long = testBlockTimestamp
150+
}) {
152151
override def withBlockTimestampProvider(blockTimestampProvider: BlockTimestampProvider): TestBlockGenerator = this
153152
}
154153

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"))
159-
}
160-
}
161-
}
162-
163-
private def getTransactionsFromPool: Future[PendingTransactionsResponse] = {
164154
implicit val timeout = Timeout(5.seconds)
165-
166-
(pendingTransactionsManager ? PendingTransactionsManager.GetPendingTransactions).mapTo[PendingTransactionsResponse]
155+
(pendingTransactionsManager ? PendingTransactionsManager.GetPendingTransactions)
156+
.mapTo[PendingTransactionsResponse]
167157
.recover { case _ => PendingTransactionsResponse(Nil) }
158+
.flatMap { pendingTxs =>
159+
blockGenerator.generateBlock(parentBlock, pendingTxs.pendingTransactions.map(_.stx), etherbase, Nil) match {
160+
case Right(pb) => Future.successful(pb)
161+
case Left(err) => Future.failed(new RuntimeException(s"Error while generating block for mining: $err"))
162+
}
163+
}
168164
}
169165
}

src/main/scala/io/iohk/ethereum/ledger/Ledger.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ trait Ledger {
4040
*
4141
* @param block - block to be imported
4242
* @return One of:
43-
* - [[BlockImportedToTop]] - if the block was added as the new best block
44-
* - [[BlockEnqueued]] - block is stored in the [[BlockQueue]]
45-
* - [[ChainReorganised]] - a better new branch was found causing chain reorganisation
46-
* - [[DuplicateBlock]] - block already exists either in the main chain or in the queue
47-
* - [[BlockImportFailed]] - block failed to execute (when importing to top or reorganising the chain)
43+
* - [[io.iohk.ethereum.ledger.BlockImportedToTop]] - if the block was added as the new best block
44+
* - [[io.iohk.ethereum.ledger.BlockEnqueued]] - block is stored in the [[io.iohk.ethereum.ledger.BlockQueue]]
45+
* - [[io.iohk.ethereum.ledger.ChainReorganised]] - a better new branch was found causing chain reorganisation
46+
* - [[io.iohk.ethereum.ledger.DuplicateBlock]] - block already exists either in the main chain or in the queue
47+
* - [[io.iohk.ethereum.ledger.BlockImportFailed]] - block failed to execute (when importing to top or reorganising the chain)
4848
*/
4949
def importBlock(block: Block): BlockImportResult
5050

@@ -55,10 +55,10 @@ trait Ledger {
5555
* - last header number should be greater or equal than current best block number
5656
* @param headers - a list of headers to be checked
5757
* @return One of:
58-
* - [[NewBetterBranch]] - the headers form a better branch than our current main chain
59-
* - [[NoChainSwitch]] - the headers do not form a better branch
60-
* - [[UnknownBranch]] - the parent of the first header is unknown (caller should obtain more headers)
61-
* - [[InvalidBranch]] - headers do not form a chain or last header number is less than current best block number
58+
* - [[io.iohk.ethereum.ledger.NewBetterBranch]] - the headers form a better branch than our current main chain
59+
* - [[io.iohk.ethereum.ledger.NoChainSwitch]] - the headers do not form a better branch
60+
* - [[io.iohk.ethereum.ledger.UnknownBranch]] - the parent of the first header is unknown (caller should obtain more headers)
61+
* - [[io.iohk.ethereum.ledger.InvalidBranch]] - headers do not form a chain or last header number is less than current best block number
6262
*/
6363
def resolveBranch(headers: Seq[BlockHeader]): BranchResolutionResult
6464

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import io.iohk.ethereum.ledger.{Ledger, LedgerImpl}
66
import io.iohk.ethereum.utils.BlockchainConfig
77
import io.iohk.ethereum.utils.Config.SyncConfig
88

9+
/**
10+
* Provides a ledger instance with modifiable blockchain config (used in testmode).
11+
*/
912
class TestLedgerWrapper(
1013
blockchain: BlockchainImpl,
1114
syncConfig: SyncConfig,
Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.iohk.ethereum.testmode
22

33
import akka.util.ByteString
4-
import io.iohk.ethereum.consensus.{ConsensusBuilder, ConsensusConfig, ConsensusConfigBuilder, FullConsensusConfig, GetBlockHeaderByHash, GetNBlocksBack, Protocol, TestConsensus}
5-
import io.iohk.ethereum.consensus.blocks.TestBlockGenerator
4+
import io.iohk.ethereum.consensus._
65
import io.iohk.ethereum.consensus.ethash.blocks.EthashBlockGeneratorImpl
76
import io.iohk.ethereum.consensus.ethash.validators.{EthashValidators, OmmersValidator}
87
import io.iohk.ethereum.consensus.validators._
@@ -14,59 +13,50 @@ import io.iohk.ethereum.network.p2p.messages.PV62
1413
import io.iohk.ethereum.nodebuilder._
1514
import io.iohk.ethereum.utils.BlockchainConfig
1615

17-
// scalastyle:off
1816
class TestmodeConsensus(
1917
override val vm: VMImpl,
2018
blockchain: BlockchainImpl,
2119
blockchainConfig: BlockchainConfig,
2220
consensusConfig: ConsensusConfig)
23-
extends TestConsensus {
21+
extends Consensus {
2422

2523
override type Config = AnyRef
2624
override def protocol: Protocol = Protocol.Ethash
2725
override def config: FullConsensusConfig[AnyRef] = FullConsensusConfig[AnyRef](consensusConfig, "")
2826

2927
class TestValidators extends EthashValidators {
30-
override def blockValidator: BlockValidator = new BlockValidator {
31-
override def validateBlockAndReceipts(blockHeader: BlockHeader, receipts: Seq[Receipt]): Either[StdBlockValidator.BlockError, StdBlockValidator.BlockValid] = Right(StdBlockValidator.BlockValid)
32-
override def validateHeaderAndBody(blockHeader: BlockHeader, blockBody: PV62.BlockBody): Either[StdBlockValidator.BlockError, StdBlockValidator.BlockValid] = Right(StdBlockValidator.BlockValid)
33-
}
34-
3528
override def blockHeaderValidator: BlockHeaderValidator = (_, _) => Right(BlockHeaderValid)
36-
37-
override def validateBlockBeforeExecution(block: Block, getBlockHeaderByHash: GetBlockHeaderByHash, getNBlocksBack: GetNBlocksBack): Either[BlockExecutionError.ValidationBeforeExecError, BlockExecutionSuccess] = Right(BlockExecutionSuccess)
38-
39-
override def validateBlockAfterExecution(block: Block, stateRootHash: ByteString,receipts: Seq[Receipt], gasUsed: BigInt): Either[BlockExecutionError, BlockExecutionSuccess] = Right(BlockExecutionSuccess)
40-
4129
override def signedTransactionValidator: SignedTransactionValidator = new StdSignedTransactionValidator(blockchainConfig)
42-
4330
override def ommersValidator: OmmersValidator = (_, _, _, _, _) => Right(OmmersValidator.OmmersValid)
31+
override def validateBlockBeforeExecution(block: Block, getBlockHeaderByHash: GetBlockHeaderByHash, getNBlocksBack: GetNBlocksBack)
32+
: Either[BlockExecutionError.ValidationBeforeExecError, BlockExecutionSuccess] = Right(BlockExecutionSuccess)
33+
override def validateBlockAfterExecution(block: Block, stateRootHash: ByteString,receipts: Seq[Receipt], gasUsed: BigInt)
34+
: Either[BlockExecutionError, BlockExecutionSuccess] = Right(BlockExecutionSuccess)
35+
override def blockValidator: BlockValidator = new BlockValidator {
36+
override def validateBlockAndReceipts(blockHeader: BlockHeader, receipts: Seq[Receipt])
37+
: Either[StdBlockValidator.BlockError, StdBlockValidator.BlockValid] = Right(StdBlockValidator.BlockValid)
38+
override def validateHeaderAndBody(blockHeader: BlockHeader, blockBody: PV62.BlockBody)
39+
: Either[StdBlockValidator.BlockError, StdBlockValidator.BlockValid] = Right(StdBlockValidator.BlockValid)
40+
}
4441
}
45-
override def validators: EthashValidators = new TestValidators
4642

43+
override def validators: EthashValidators = new TestValidators
4744

4845
override val blockPreparator: BlockPreparator = new BlockPreparator(
4946
vm = vm,
5047
signedTxValidator = validators.signedTransactionValidator,
5148
blockchain = blockchain,
52-
blockchainConfig = blockchainConfig) // TODO: blockchain config is subject to change....
53-
49+
blockchainConfig = blockchainConfig)
5450

5551
override val blockGenerator = new EthashBlockGeneratorImpl(
5652
validators = validators,
5753
blockchain = blockchain,
5854
blockchainConfig = blockchainConfig,
5955
consensusConfig = config.generic,
60-
blockPreparator = blockPreparator
61-
)
56+
blockPreparator = blockPreparator)
6257

6358
override def startProtocol(node: Node): Unit = {}
6459
override def stopProtocol(): Unit = {}
65-
66-
override protected def newBlockGenerator(validators: Validators): TestBlockGenerator = ??? // not used
67-
override def withValidators(validators: Validators): TestConsensus = ??? // not used
68-
override def withVM(vm: VMImpl): TestConsensus = ??? // not used
69-
override def withBlockGenerator(blockGenerator: TestBlockGenerator): TestConsensus = ??? // not used
7060
}
7161

7262
trait TestmodeConsensusBuilder extends ConsensusBuilder {
@@ -75,6 +65,5 @@ trait TestmodeConsensusBuilder extends ConsensusBuilder {
7565
BlockchainConfigBuilder with
7666
ConsensusConfigBuilder =>
7767

78-
7968
override lazy val consensus = new TestmodeConsensus(vm, blockchain, blockchainConfig, consensusConfig)
8069
}

src/test/scala/io/iohk/ethereum/jsonrpc/EthServiceSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,13 @@ class EthServiceSpec extends FlatSpec with Matchers with ScalaFutures with MockF
525525
val id2 = ByteString("id2")
526526

527527
ethService.submitHashRate(SubmitHashRateRequest(rate, id1)).futureValue shouldEqual Right(SubmitHashRateResponse(true))
528-
Thread.sleep(activeTimeout.toMillis / 2)
528+
Thread.sleep(minerActiveTimeout.toMillis / 2)
529529
ethService.submitHashRate(SubmitHashRateRequest(rate, id2)).futureValue shouldEqual Right(SubmitHashRateResponse(true))
530530

531531
val response1 = ethService.getHashRate(GetHashRateRequest())
532532
response1.futureValue shouldEqual Right(GetHashRateResponse(rate * 2))
533533

534-
Thread.sleep(activeTimeout.toMillis / 2)
534+
Thread.sleep(minerActiveTimeout.toMillis / 2)
535535
val response2 = ethService.getHashRate(GetHashRateRequest())
536536
response2.futureValue shouldEqual Right(GetHashRateResponse(rate))
537537
}
@@ -583,7 +583,7 @@ class EthServiceSpec extends FlatSpec with Matchers with ScalaFutures with MockF
583583
blockchain.save(parentBlock)
584584
ethService.getWork(GetWorkRequest())
585585

586-
Thread.sleep(activeTimeout.toMillis)
586+
Thread.sleep(minerActiveTimeout.toMillis)
587587

588588
val response = ethService.getMining(GetMiningRequest())
589589

@@ -898,7 +898,7 @@ class EthServiceSpec extends FlatSpec with Matchers with ScalaFutures with MockF
898898
override lazy val consensusConfig = ConsensusConfigs.consensusConfig
899899
val miningConfig = ConsensusConfigs.ethashConfig
900900
val fullConsensusConfig = ConsensusConfigs.fullConsensusConfig
901-
val activeTimeout: FiniteDuration = 5.seconds
901+
val minerActiveTimeout: FiniteDuration = 5.seconds
902902
val getTransactionFromPoolTimeout: FiniteDuration = 5.seconds
903903

904904
val filterConfig = new FilterConfig {

src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,6 @@ class JsonRpcControllerSpec extends FlatSpec with Matchers with PropertyChecks w
14601460
val ethashConfig = ConsensusConfigs.ethashConfig
14611461
override lazy val consensusConfig = ConsensusConfigs.consensusConfig
14621462
val fullConsensusConfig = ConsensusConfigs.fullConsensusConfig
1463-
val activeTimeout: FiniteDuration = 5.seconds
14641463
val getTransactionFromPoolTimeout: FiniteDuration = 5.seconds
14651464

14661465
val filterConfig = new FilterConfig {

0 commit comments

Comments
 (0)