Skip to content

Commit e27f251

Browse files
author
Leonor Boga
authored
[ETCM-1058] Replace block import by consensus (#1078)
* ETCM-1058 Add new Consensus, by just moving the code in BlockImport * ETCM-1018 Replace references to BlockImport by Consensus/ConsensusImpl * ETCM-1018 Remove unused references to blockchain * ETCM-1058 Fix unit tests * ETCM-1018 Fix integration tests * ETCM-1018 Rename BlockImportSpec to ConsensusSpec * ETCM-1058 Extend ConsensusSpec error handling tests * ETCM-1058 Refactor ConsensusImpl * ETCM-1058 Extract duplicated code for block pre validation * ETCM-1058 Run scalafixAll * ETCM-1058 Remove file BlockImport
1 parent 1377cb6 commit e27f251

31 files changed

+455
-324
lines changed

src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher
2626
import io.iohk.ethereum.blockchain.sync.regular.BlockImporter
2727
import io.iohk.ethereum.blockchain.sync.regular.BlockImporter.NewCheckpoint
2828
import io.iohk.ethereum.checkpointing.CheckpointingTestHelpers
29+
import io.iohk.ethereum.consensus.Consensus
2930
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
3031
import io.iohk.ethereum.consensus.pow.validators.OmmersValidator
3132
import io.iohk.ethereum.consensus.pow.validators.StdOmmersValidator
@@ -57,8 +58,7 @@ class BlockImporterItSpec
5758
override val blockImporter = system.actorOf(
5859
BlockImporter.props(
5960
fetcherProbe.ref,
60-
mkBlockImport(validators = successValidators),
61-
blockchain,
61+
mkConsensus(validators = successValidators),
6262
blockchainReader,
6363
storagesInstance.storages.stateStorage,
6464
new BranchResolution(blockchain, blockchainReader),
@@ -171,8 +171,7 @@ class BlockImporterItSpec
171171
override val blockImporter = system.actorOf(
172172
BlockImporter.props(
173173
fetcherProbe.ref,
174-
mkBlockImport(validators = successValidators),
175-
blockchain,
174+
mkConsensus(validators = successValidators),
176175
blockchainReader,
177176
storagesInstance.storages.stateStorage,
178177
new BranchResolution(blockchain, blockchainReader),
@@ -236,7 +235,7 @@ class TestFixture extends TestSetupWithVmAndValidators {
236235
override val ommersValidator: OmmersValidator = new StdOmmersValidator(blockHeaderValidator)
237236
}
238237

239-
override lazy val blockImport: BlockImport = mkBlockImport(
238+
override lazy val consensus: Consensus = mkConsensus(
240239
validators = successValidators,
241240
blockExecutionOpt = Some(
242241
new BlockExecution(
@@ -259,8 +258,7 @@ class TestFixture extends TestSetupWithVmAndValidators {
259258
val blockImporter: ActorRef = system.actorOf(
260259
BlockImporter.props(
261260
fetcherProbe.ref,
262-
blockImport,
263-
blockchain,
261+
consensus,
264262
blockchainReader,
265263
storagesInstance.storages.stateStorage,
266264
new BranchResolution(blockchain, blockchainReader),

src/it/scala/io/iohk/ethereum/sync/util/RegularSyncItSpecUtils.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import io.iohk.ethereum.blockchain.sync.regular.BlockImporter.Start
2626
import io.iohk.ethereum.blockchain.sync.regular.RegularSync
2727
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.NewCheckpoint
2828
import io.iohk.ethereum.checkpointing.CheckpointingTestHelpers
29+
import io.iohk.ethereum.consensus.Consensus
30+
import io.iohk.ethereum.consensus.ConsensusImpl
2931
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
3032
import io.iohk.ethereum.consensus.mining.FullMiningConfig
3133
import io.iohk.ethereum.consensus.mining.MiningConfig
@@ -103,8 +105,8 @@ object RegularSyncItSpecUtils {
103105
mining.blockPreparator,
104106
blockValidation
105107
)
106-
lazy val blockImport: BlockImport =
107-
new BlockImport(
108+
lazy val blockImport: Consensus =
109+
new ConsensusImpl(
108110
bl,
109111
blockchainReader,
110112
blockchainWriter,
@@ -146,7 +148,6 @@ object RegularSyncItSpecUtils {
146148
BlockImporter.props(
147149
fetcher.toClassic,
148150
blockImport,
149-
bl,
150151
blockchainReader,
151152
storagesInstance.storages.stateStorage,
152153
new BranchResolution(bl, blockchainReader),
@@ -165,7 +166,6 @@ object RegularSyncItSpecUtils {
165166
etcPeerManager,
166167
peerEventBus,
167168
blockImport,
168-
bl,
169169
blockchainReader,
170170
storagesInstance.storages.stateStorage,
171171
new BranchResolution(bl, blockchainReader),

src/it/scala/io/iohk/ethereum/txExecTest/ContractTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class ContractTest extends AnyFlatSpec with Matchers {
2121
lazy val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)
2222

2323
//block only with ether transfers
24-
val blockValidation =
24+
override lazy val blockValidation =
2525
new BlockValidation(mining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
26-
val blockExecution =
26+
override lazy val blockExecution =
2727
new BlockExecution(
2828
blockchain,
2929
blockchainReader,

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncController.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import akka.actor.Scheduler
99

1010
import io.iohk.ethereum.blockchain.sync.fast.FastSync
1111
import io.iohk.ethereum.blockchain.sync.regular.RegularSync
12+
import io.iohk.ethereum.consensus.Consensus
1213
import io.iohk.ethereum.consensus.validators.Validators
1314
import io.iohk.ethereum.db.storage.AppStateStorage
1415
import io.iohk.ethereum.db.storage.EvmCodeStorage
@@ -18,21 +19,20 @@ import io.iohk.ethereum.db.storage.StateStorage
1819
import io.iohk.ethereum.domain.Blockchain
1920
import io.iohk.ethereum.domain.BlockchainReader
2021
import io.iohk.ethereum.domain.BlockchainWriter
21-
import io.iohk.ethereum.ledger.BlockImport
2222
import io.iohk.ethereum.ledger.BranchResolution
2323
import io.iohk.ethereum.nodebuilder.BlockchainConfigBuilder
2424
import io.iohk.ethereum.utils.Config.SyncConfig
2525

2626
class SyncController(
27-
appStateStorage: AppStateStorage,
2827
blockchain: Blockchain,
2928
blockchainReader: BlockchainReader,
3029
blockchainWriter: BlockchainWriter,
30+
appStateStorage: AppStateStorage,
3131
evmCodeStorage: EvmCodeStorage,
3232
stateStorage: StateStorage,
3333
nodeStorage: NodeStorage,
3434
fastSyncStateStorage: FastSyncStateStorage,
35-
blockImport: BlockImport,
35+
consensus: Consensus,
3636
validators: Validators,
3737
peerEventBus: ActorRef,
3838
pendingTransactionsManager: ActorRef,
@@ -124,8 +124,7 @@ class SyncController(
124124
peersClient,
125125
etcPeerManager,
126126
peerEventBus,
127-
blockImport,
128-
blockchain,
127+
consensus,
129128
blockchainReader,
130129
stateStorage,
131130
new BranchResolution(blockchain, blockchainReader),
@@ -148,15 +147,15 @@ class SyncController(
148147
object SyncController {
149148
// scalastyle:off parameter.number
150149
def props(
151-
appStateStorage: AppStateStorage,
152150
blockchain: Blockchain,
153151
blockchainReader: BlockchainReader,
154152
blockchainWriter: BlockchainWriter,
153+
appStateStorage: AppStateStorage,
155154
evmCodeStorage: EvmCodeStorage,
156155
stateStorage: StateStorage,
157156
nodeStorage: NodeStorage,
158157
syncStateStorage: FastSyncStateStorage,
159-
blockImport: BlockImport,
158+
consensus: Consensus,
160159
validators: Validators,
161160
peerEventBus: ActorRef,
162161
pendingTransactionsManager: ActorRef,
@@ -168,15 +167,15 @@ object SyncController {
168167
): Props =
169168
Props(
170169
new SyncController(
171-
appStateStorage,
172170
blockchain,
173171
blockchainReader,
174172
blockchainWriter,
173+
appStateStorage,
175174
evmCodeStorage,
176175
stateStorage,
177176
nodeStorage,
178177
syncStateStorage,
179-
blockImport,
178+
consensus,
180179
validators,
181180
peerEventBus,
182181
pendingTransactionsManager,

src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSync.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ class FastSync(
178178
SyncStateSchedulerActor
179179
.props(
180180
SyncStateScheduler(
181-
blockchain,
182181
blockchainReader,
183182
evmCodeStorage,
184183
stateStorage,

src/main/scala/io/iohk/ethereum/blockchain/sync/fast/SyncStateScheduler.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import io.iohk.ethereum.db.storage.EvmCodeStorage
2323
import io.iohk.ethereum.db.storage.NodeStorage
2424
import io.iohk.ethereum.db.storage.StateStorage
2525
import io.iohk.ethereum.domain.Account
26-
import io.iohk.ethereum.domain.Blockchain
2726
import io.iohk.ethereum.domain.BlockchainReader
2827
import io.iohk.ethereum.mpt.BranchNode
2928
import io.iohk.ethereum.mpt.ExtensionNode
@@ -292,7 +291,6 @@ object SyncStateScheduler {
292291
BloomFilter.create[ByteString](ByteStringFunnel, expectedFilterSize)
293292

294293
def apply(
295-
blockchain: Blockchain,
296294
blockchainReader: BlockchainReader,
297295
evmCodeStorage: EvmCodeStorage,
298296
stateStorage: StateStorage,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.iohk.ethereum.blockchain.sync.regular
2+
3+
import io.iohk.ethereum.domain.Block
4+
import io.iohk.ethereum.domain.ChainWeight
5+
import io.iohk.ethereum.ledger.BlockData
6+
import io.iohk.ethereum.mpt.MerklePatriciaTrie.MissingNodeException
7+
8+
sealed trait BlockImportResult
9+
10+
case class BlockImportedToTop(blockImportData: List[BlockData]) extends BlockImportResult
11+
12+
case object BlockEnqueued extends BlockImportResult
13+
14+
case object DuplicateBlock extends BlockImportResult
15+
16+
case class ChainReorganised(
17+
oldBranch: List[Block],
18+
newBranch: List[Block],
19+
weights: List[ChainWeight]
20+
) extends BlockImportResult
21+
22+
case class BlockImportFailed(error: String) extends BlockImportResult
23+
24+
case class BlockImportFailedDueToMissingNode(reason: MissingNodeException) extends BlockImportResult
25+
26+
case object UnknownParent extends BlockImportResult

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockImporter.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.iohk.ethereum.blockchain.sync.Blacklist.BlacklistReason
2020
import io.iohk.ethereum.blockchain.sync.regular.BlockBroadcast.BlockToBroadcast
2121
import io.iohk.ethereum.blockchain.sync.regular.BlockBroadcasterActor.BroadcastBlocks
2222
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.ProgressProtocol
23+
import io.iohk.ethereum.consensus.Consensus
2324
import io.iohk.ethereum.crypto.kec256
2425
import io.iohk.ethereum.db.storage.StateStorage
2526
import io.iohk.ethereum.domain._
@@ -37,8 +38,7 @@ import io.iohk.ethereum.utils.FunctorOps._
3738

3839
class BlockImporter(
3940
fetcher: ActorRef,
40-
blockImport: BlockImport,
41-
blockchain: Blockchain,
41+
consensus: Consensus,
4242
blockchainReader: BlockchainReader,
4343
stateStorage: StateStorage,
4444
branchResolution: BranchResolution,
@@ -50,6 +50,7 @@ class BlockImporter(
5050
configBuilder: BlockchainConfigBuilder
5151
) extends Actor
5252
with ActorLogging {
53+
5354
import BlockImporter._
5455
import configBuilder._
5556

@@ -199,8 +200,8 @@ class BlockImporter(
199200
Task.now((importedBlocks, None))
200201
} else {
201202
val restOfBlocks = blocks.tail
202-
blockImport
203-
.importBlock(blocks.head)
203+
consensus
204+
.evaluateBranchBlock(blocks.head)
204205
.flatMap {
205206
case BlockImportedToTop(_) =>
206207
tryImportBlocks(restOfBlocks, blocks.head :: importedBlocks)
@@ -238,7 +239,7 @@ class BlockImporter(
238239
def doLog(entry: ImportMessages.LogEntry): Unit = log.log(entry._1, entry._2)
239240
importWith(
240241
Task(doLog(importMessages.preImport()))
241-
.flatMap(_ => blockImport.importBlock(block))
242+
.flatMap(_ => consensus.evaluateBranchBlock(block))
242243
.tap((importMessages.messageForImportResult _).andThen(doLog))
243244
.tap {
244245
case BlockImportedToTop(importedBlocksData) =>
@@ -330,8 +331,7 @@ object BlockImporter {
330331
// scalastyle:off parameter.number
331332
def props(
332333
fetcher: ActorRef,
333-
blockImport: BlockImport,
334-
blockchain: Blockchain,
334+
consensus: Consensus,
335335
blockchainReader: BlockchainReader,
336336
stateStorage: StateStorage,
337337
branchResolution: BranchResolution,
@@ -345,8 +345,7 @@ object BlockImporter {
345345
Props(
346346
new BlockImporter(
347347
fetcher,
348-
blockImport,
349-
blockchain,
348+
consensus,
350349
blockchainReader,
351350
stateStorage,
352351
branchResolution,

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/ImportMessages.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import akka.event.Logging._
44
import akka.util.ByteString
55

66
import io.iohk.ethereum.domain.Block
7-
import io.iohk.ethereum.ledger._
87
import io.iohk.ethereum.mpt.MerklePatriciaTrie.MissingNodeException
98
import io.iohk.ethereum.network.PeerId
109
import io.iohk.ethereum.utils.ByteStringUtils._

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSync.scala

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.InternalLastBlockIm
1919
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.NewCheckpoint
2020
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.ProgressProtocol
2121
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.ProgressState
22+
import io.iohk.ethereum.consensus.Consensus
2223
import io.iohk.ethereum.consensus.validators.BlockValidator
2324
import io.iohk.ethereum.db.storage.StateStorage
2425
import io.iohk.ethereum.domain.Block
25-
import io.iohk.ethereum.domain.Blockchain
2626
import io.iohk.ethereum.domain.BlockchainReader
27-
import io.iohk.ethereum.ledger.BlockImport
2827
import io.iohk.ethereum.ledger.BranchResolution
2928
import io.iohk.ethereum.nodebuilder.BlockchainConfigBuilder
3029
import io.iohk.ethereum.utils.ByteStringUtils
@@ -34,8 +33,7 @@ class RegularSync(
3433
peersClient: ActorRef,
3534
etcPeerManager: ActorRef,
3635
peerEventBus: ActorRef,
37-
blockImport: BlockImport,
38-
blockchain: Blockchain,
36+
consensus: Consensus,
3937
blockchainReader: BlockchainReader,
4038
stateStorage: StateStorage,
4139
branchResolution: BranchResolution,
@@ -66,8 +64,7 @@ class RegularSync(
6664
context.actorOf(
6765
BlockImporter.props(
6866
fetcher.toClassic,
69-
blockImport,
70-
blockchain,
67+
consensus,
7168
blockchainReader,
7269
stateStorage,
7370
branchResolution,
@@ -140,8 +137,7 @@ object RegularSync {
140137
peersClient: ActorRef,
141138
etcPeerManager: ActorRef,
142139
peerEventBus: ActorRef,
143-
blockImport: BlockImport,
144-
blockchain: Blockchain,
140+
consensus: Consensus,
145141
blockchainReader: BlockchainReader,
146142
stateStorage: StateStorage,
147143
branchResolution: BranchResolution,
@@ -158,8 +154,7 @@ object RegularSync {
158154
peersClient,
159155
etcPeerManager,
160156
peerEventBus,
161-
blockImport,
162-
blockchain,
157+
consensus,
163158
blockchainReader,
164159
stateStorage,
165160
branchResolution,

0 commit comments

Comments
 (0)