Skip to content

Commit 44fcd0f

Browse files
author
Christos KK Loverdos
committed
Decouple Consensus II (delete ConsensusImpl)
1 parent f842659 commit 44fcd0f

File tree

3 files changed

+70
-106
lines changed

3 files changed

+70
-106
lines changed

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package io.iohk.ethereum.consensus
22

33
import io.iohk.ethereum.consensus.blocks.{BlockGenerator, TestBlockGenerator}
44
import io.iohk.ethereum.consensus.validators.Validators
5-
import io.iohk.ethereum.domain._
65
import io.iohk.ethereum.ledger.BlockPreparator
76
import io.iohk.ethereum.ledger.Ledger.VMImpl
87
import io.iohk.ethereum.nodebuilder.Node
9-
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
108

119
/**
1210
* Abstraction for a consensus protocol implementation.
@@ -81,33 +79,3 @@ trait TestConsensus extends Consensus {
8179
/** Internal API, used for testing */
8280
def withBlockGenerator(blockGenerator: TestBlockGenerator): TestConsensus
8381
}
84-
85-
abstract class ConsensusImpl[C <: AnyRef](
86-
theVm: VMImpl,
87-
blockchain: BlockchainImpl,
88-
blockchainConfig: BlockchainConfig,
89-
fullConsensusConfig: FullConsensusConfig[C]
90-
) extends TestConsensus with Logger {
91-
92-
final type Config = C
93-
94-
protected val _blockPreparator = new BlockPreparator(
95-
vm = vm,
96-
signedTxValidator = validators.signedTransactionValidator,
97-
blockchain = blockchain,
98-
blockchainConfig = blockchainConfig
99-
)
100-
101-
/**
102-
* This is used by the [[io.iohk.ethereum.consensus.Consensus#blockGenerator blockGenerator]].
103-
*/
104-
def blockPreparator: BlockPreparator = this._blockPreparator
105-
106-
/**
107-
* This is the VM used to prepare and generate blocks.
108-
*/
109-
def vm: VMImpl = theVm
110-
111-
def config: FullConsensusConfig[C] = fullConsensusConfig
112-
}
113-

src/main/scala/io/iohk/ethereum/consensus/atomixraft/AtomixRaftConsensus.scala

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,31 @@ import io.iohk.ethereum.domain.BlockchainImpl
2424
import io.iohk.ethereum.ledger.BlockPreparator
2525
import io.iohk.ethereum.ledger.Ledger.VMImpl
2626
import io.iohk.ethereum.nodebuilder.Node
27-
import io.iohk.ethereum.utils.BlockchainConfig
27+
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
2828

2929
class AtomixRaftConsensus private(
30-
vm: VMImpl,
30+
val vm: VMImpl,
3131
blockchain: BlockchainImpl,
3232
blockchainConfig: BlockchainConfig,
33-
fullConsensusConfig: FullConsensusConfig[AtomixRaftConfig],
34-
_validators: Validators,
35-
_blockGenerator: AtomixRaftBlockGenerator
36-
) extends ConsensusImpl[AtomixRaftConfig](
37-
vm,
38-
blockchain,
39-
blockchainConfig,
40-
fullConsensusConfig
41-
) {
33+
val config: FullConsensusConfig[AtomixRaftConfig],
34+
val validators: Validators,
35+
val blockGenerator: AtomixRaftBlockGenerator
36+
) extends TestConsensus with Logger {
37+
38+
type Config = AtomixRaftConfig
4239

4340
private[this] final val miner = new AtomixRaftForgerRef
4441

4542
private[this] final val raftServer = new RaftServerRef
4643

47-
private[this] val raftConfig: AtomixRaftConfig = config.specific
44+
private[this] final val raftConfig: AtomixRaftConfig = config.specific
45+
46+
private[this] final val _blockPreparator = new BlockPreparator(
47+
vm = vm,
48+
signedTxValidator = validators.signedTransactionValidator,
49+
blockchain = blockchain,
50+
blockchainConfig = blockchainConfig
51+
)
4852

4953
private[this] def setupMiner(node: Node): Unit = {
5054
miner.setOnce {
@@ -131,6 +135,11 @@ class AtomixRaftConsensus private(
131135

132136
def promoteToLeader(): Unit = raftServer.run(_.promote().join())
133137

138+
/**
139+
* This is used by the [[io.iohk.ethereum.consensus.Consensus#blockGenerator blockGenerator]].
140+
*/
141+
def blockPreparator: BlockPreparator = this._blockPreparator
142+
134143
/**
135144
* Starts the consensus protocol on the current `node`.
136145
*/
@@ -146,12 +155,6 @@ class AtomixRaftConsensus private(
146155

147156
def protocol: Protocol = Protocol.AtomixRaft
148157

149-
/**
150-
* Provides the set of validators specific to this consensus protocol.
151-
*/
152-
def validators: Validators = this._validators
153-
154-
155158
/** Internal API, used for testing */
156159
protected def newBlockGenerator(validators: Validators): AtomixRaftBlockGenerator = {
157160
val blockPreparator = new BlockPreparator(
@@ -178,9 +181,9 @@ class AtomixRaftConsensus private(
178181
vm = vm,
179182
blockchain = blockchain,
180183
blockchainConfig = blockchainConfig,
181-
fullConsensusConfig = fullConsensusConfig,
182-
_validators = validators,
183-
_blockGenerator = blockGenerator
184+
config = config,
185+
validators = validators,
186+
blockGenerator = blockGenerator
184187
)
185188
}
186189

@@ -190,9 +193,9 @@ class AtomixRaftConsensus private(
190193
vm = vm,
191194
blockchain = blockchain,
192195
blockchainConfig = blockchainConfig,
193-
fullConsensusConfig = fullConsensusConfig,
194-
_validators = _validators,
195-
_blockGenerator = blockGenerator
196+
config = config,
197+
validators = validators,
198+
blockGenerator = blockGenerator
196199
)
197200

198201

@@ -202,24 +205,18 @@ class AtomixRaftConsensus private(
202205
vm = vm,
203206
blockchain = blockchain,
204207
blockchainConfig = blockchainConfig,
205-
fullConsensusConfig = fullConsensusConfig,
206-
_validators = validators,
207-
_blockGenerator = blockGenerator.asInstanceOf[AtomixRaftBlockGenerator]
208+
config = config,
209+
validators = validators,
210+
blockGenerator = blockGenerator.asInstanceOf[AtomixRaftBlockGenerator]
208211
)
209-
210-
/**
211-
* Returns the [[io.iohk.ethereum.consensus.blocks.BlockGenerator BlockGenerator]]
212-
* this consensus protocol uses.
213-
*/
214-
def blockGenerator: AtomixRaftBlockGenerator = this._blockGenerator
215212
}
216213

217214
object AtomixRaftConsensus {
218215
def apply(
219216
vm: VMImpl,
220217
blockchain: BlockchainImpl,
221218
blockchainConfig: BlockchainConfig,
222-
fullConsensusConfig: FullConsensusConfig[AtomixRaftConfig]
219+
config: FullConsensusConfig[AtomixRaftConfig]
223220
): AtomixRaftConsensus = {
224221

225222
val validators = StdValidators(blockchainConfig)
@@ -234,17 +231,17 @@ object AtomixRaftConsensus {
234231
val blockGenerator = new AtomixRaftBlockGenerator(
235232
blockchain = blockchain,
236233
blockchainConfig = blockchainConfig,
237-
consensusConfig = fullConsensusConfig.generic,
234+
consensusConfig = config.generic,
238235
blockPreparator = blockPreparator
239236
)
240237

241238
new AtomixRaftConsensus(
242239
vm = vm,
243240
blockchain = blockchain,
244241
blockchainConfig = blockchainConfig,
245-
fullConsensusConfig = fullConsensusConfig,
246-
_validators = validators,
247-
_blockGenerator = blockGenerator
242+
config = config,
243+
validators = validators,
244+
blockGenerator = blockGenerator
248245
)
249246
}
250247
}

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

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ 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
17-
import io.iohk.ethereum.utils.BlockchainConfig
17+
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
1818

1919
/**
2020
* Implements standard Ethereum consensus (ethash PoW).
2121
*/
2222
class EthashConsensus private(
23-
vm: VMImpl,
23+
val vm: VMImpl,
2424
blockchain: BlockchainImpl,
2525
blockchainConfig: BlockchainConfig,
26-
fullConsensusConfig: FullConsensusConfig[EthashConfig],
27-
_validators: EthashValidators,
28-
_blockGenerator: EthashBlockGenerator
29-
) extends ConsensusImpl[EthashConfig](
30-
vm,
31-
blockchain,
32-
blockchainConfig,
33-
fullConsensusConfig
34-
) {
26+
val config: FullConsensusConfig[EthashConfig],
27+
val validators: EthashValidators,
28+
val blockGenerator: EthashBlockGenerator
29+
) extends TestConsensus with Logger {
30+
31+
type Config = EthashConfig
32+
33+
private[this] final val _blockPreparator = new BlockPreparator(
34+
vm = vm,
35+
signedTxValidator = validators.signedTransactionValidator,
36+
blockchain = blockchain,
37+
blockchainConfig = blockchainConfig
38+
)
3539

3640
private[this] val atomicMiner = new AtomicReference[Option[ActorRef]](None)
3741
private[this] def sendMiner(msg: MinerMsg): Unit =
@@ -53,6 +57,11 @@ class EthashConsensus private(
5357
sendMiner(EthashMiner.StopMining)
5458
}
5559

60+
/**
61+
* This is used by the [[io.iohk.ethereum.consensus.Consensus#blockGenerator blockGenerator]].
62+
*/
63+
def blockPreparator: BlockPreparator = this._blockPreparator
64+
5665
/**
5766
* Starts the consensus protocol on the current `node`.
5867
*/
@@ -70,12 +79,6 @@ class EthashConsensus private(
7079

7180
def protocol: Protocol = Protocol.Ethash
7281

73-
74-
/**
75-
* Provides the set of validators specific to this consensus protocol.
76-
*/
77-
def validators: EthashValidators = this._validators
78-
7982
/** Internal API, used for testing */
8083
protected def newBlockGenerator(validators: Validators): EthashBlockGenerator = {
8184
validators match {
@@ -112,9 +115,9 @@ class EthashConsensus private(
112115
vm = vm,
113116
blockchain = blockchain,
114117
blockchainConfig = blockchainConfig,
115-
fullConsensusConfig = fullConsensusConfig,
116-
_validators = _validators,
117-
_blockGenerator = blockGenerator
118+
config = config,
119+
validators = _validators,
120+
blockGenerator = blockGenerator
118121
)
119122

120123
case _
@@ -127,9 +130,9 @@ class EthashConsensus private(
127130
vm = vm,
128131
blockchain = blockchain,
129132
blockchainConfig = blockchainConfig,
130-
fullConsensusConfig = fullConsensusConfig,
131-
_validators = validators,
132-
_blockGenerator = blockGenerator
133+
config = config,
134+
validators = validators,
135+
blockGenerator = blockGenerator
133136
)
134137

135138
/** Internal API, used for testing */
@@ -138,24 +141,20 @@ class EthashConsensus private(
138141
vm = vm,
139142
blockchain = blockchain,
140143
blockchainConfig = blockchainConfig,
141-
fullConsensusConfig = fullConsensusConfig,
142-
_validators = validators,
143-
_blockGenerator = blockGenerator.asInstanceOf[EthashBlockGenerator]
144+
config = config,
145+
validators = validators,
146+
blockGenerator = blockGenerator.asInstanceOf[EthashBlockGenerator]
144147
)
145148

146-
/**
147-
* Returns the [[io.iohk.ethereum.consensus.blocks.BlockGenerator BlockGenerator]]
148-
* this consensus protocol uses.
149-
*/
150-
def blockGenerator: EthashBlockGenerator = this._blockGenerator
149+
151150
}
152151

153152
object EthashConsensus {
154153
def apply(
155154
vm: VMImpl,
156155
blockchain: BlockchainImpl,
157156
blockchainConfig: BlockchainConfig,
158-
fullConsensusConfig: FullConsensusConfig[EthashConfig]
157+
config: FullConsensusConfig[EthashConfig]
159158
): EthashConsensus = {
160159

161160
val validators = StdEthashValidators(blockchainConfig)
@@ -171,17 +170,17 @@ object EthashConsensus {
171170
validators = validators,
172171
blockchain = blockchain,
173172
blockchainConfig = blockchainConfig,
174-
consensusConfig = fullConsensusConfig.generic,
173+
consensusConfig = config.generic,
175174
blockPreparator = blockPreparator
176175
)
177176

178177
new EthashConsensus(
179178
vm = vm,
180179
blockchain = blockchain,
181180
blockchainConfig = blockchainConfig,
182-
fullConsensusConfig = fullConsensusConfig,
183-
_validators = validators,
184-
_blockGenerator = blockGenerator
181+
config = config,
182+
validators = validators,
183+
blockGenerator = blockGenerator
185184
)
186185
}
187186
}

0 commit comments

Comments
 (0)