1
1
package io .iohk .ethereum .jsonrpc
2
2
3
3
import akka .actor .ActorRef
4
- import akka .agent .Agent
5
4
import akka .util .{ByteString , Timeout }
6
5
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 ._
10
8
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
14
10
import io .iohk .ethereum .transactions .PendingTransactionsManager
15
11
import io .iohk .ethereum .transactions .PendingTransactionsManager .PendingTransactionsResponse
16
12
import io .iohk .ethereum .utils .{BlockchainConfig , DaoForkConfig , MonetaryPolicyConfig }
@@ -53,40 +49,36 @@ object TestService {
53
49
}
54
50
55
51
class TestService (
56
- vm : VMImpl ,
57
52
blockchain : BlockchainImpl ,
58
53
pendingTransactionsManager : ActorRef ,
59
54
consensusConfig : ConsensusConfig ,
60
- initialBlockchainConfig : BlockchainConfig ,
61
- validators : Validators ,
62
- consensus : Consensus ) {
55
+ testLedgerWrapper : TestLedgerWrapper ) {
63
56
64
57
import TestService ._
65
58
import akka .pattern .ask
66
59
67
- private var blockchainConfig : BlockchainConfig = initialBlockchainConfig
68
60
private var etherbase : Address = consensusConfig.coinbase
69
61
private var testBlockTimestamp : Long = System .currentTimeMillis()
70
62
71
63
def setChainParams (request : SetChainParamsRequest ): ServiceResponse [SetChainParamsResponse ] = {
72
64
val newBlockchainConfig = new BlockchainConfig {
73
- override val frontierBlockNumber : BigInt = initialBlockchainConfig .frontierBlockNumber
65
+ override val frontierBlockNumber : BigInt = testLedgerWrapper.blockchainConfig .frontierBlockNumber
74
66
override val homesteadBlockNumber : BigInt = request.chainParams.blockchainParams.homesteadForkBlock
75
- override val eip106BlockNumber : BigInt = initialBlockchainConfig .eip106BlockNumber
67
+ override val eip106BlockNumber : BigInt = testLedgerWrapper.blockchainConfig .eip106BlockNumber
76
68
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
85
77
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
90
82
}
91
83
92
84
val genesisData = GenesisData (
@@ -106,16 +98,16 @@ class TestService(
106
98
val genesisDataLoader = new GenesisDataLoader (blockchain, newBlockchainConfig)
107
99
genesisDataLoader.loadGenesisData(genesisData)
108
100
109
- // update ledger with new config
110
- setupLedger( newBlockchainConfig)
101
+ // update test ledger with new config
102
+ testLedgerWrapper.blockchainConfig = newBlockchainConfig
111
103
112
104
Future .successful(Right (SetChainParamsResponse ()))
113
105
}
114
106
115
107
def mineBlocks (request : MineBlocksRequest ): ServiceResponse [MineBlocksResponse ] = {
116
108
def mineBlock (): Future [Unit ] = {
117
109
getBlockForMining(blockchain.getBestBlock()).map { blockForMining =>
118
- ledgerHolder() .importBlock(blockForMining.block) // TODO: check result?
110
+ testLedgerWrapper.ledger .importBlock(blockForMining.block) // TODO: check result?
119
111
pendingTransactionsManager ! PendingTransactionsManager .ClearPendingTransactions
120
112
testBlockTimestamp += 1
121
113
}
@@ -149,53 +141,29 @@ class TestService(
149
141
}
150
142
151
143
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 {
154
150
override def getEpochSecond : Long = testBlockTimestamp
155
- }
156
- )
151
+ }) {
152
+ override def withBlockTimestampProvider (blockTimestampProvider : BlockTimestampProvider ): TestBlockGenerator = this
153
+ }
157
154
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" ))
166
159
}
160
+ }
167
161
}
168
162
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)
173
165
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 ) }
200
168
}
201
169
}
0 commit comments