Skip to content

Commit 1232fbc

Browse files
author
Leonor Boga
committed
ETCM-1058 Add new Consensus, by just moving the code in BlockImport
1 parent 778bcf8 commit 1232fbc

File tree

5 files changed

+802
-376
lines changed

5 files changed

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

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ import akka.actor.ActorRef
77
import akka.actor.NotInfluenceReceiveTimeout
88
import akka.actor.Props
99
import akka.actor.ReceiveTimeout
10-
1110
import cats.data.NonEmptyList
1211
import cats.implicits._
13-
1412
import monix.eval.Task
1513
import monix.execution.Scheduler
1614

1715
import scala.concurrent.duration._
18-
1916
import io.iohk.ethereum.blockchain.sync.Blacklist.BlacklistReason
2017
import io.iohk.ethereum.blockchain.sync.regular.BlockBroadcast.BlockToBroadcast
2118
import io.iohk.ethereum.blockchain.sync.regular.BlockBroadcasterActor.BroadcastBlocks
2219
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.ProgressProtocol
20+
import io.iohk.ethereum.consensus.Consensus
2321
import io.iohk.ethereum.crypto.kec256
2422
import io.iohk.ethereum.db.storage.StateStorage
2523
import io.iohk.ethereum.domain._
@@ -37,8 +35,7 @@ import io.iohk.ethereum.utils.FunctorOps._
3735

3836
class BlockImporter(
3937
fetcher: ActorRef,
40-
blockImport: BlockImport,
41-
blockchain: Blockchain,
38+
consensus: Consensus,
4239
blockchainReader: BlockchainReader,
4340
stateStorage: StateStorage,
4441
branchResolution: BranchResolution,
@@ -50,6 +47,7 @@ class BlockImporter(
5047
configBuilder: BlockchainConfigBuilder
5148
) extends Actor
5249
with ActorLogging {
50+
5351
import BlockImporter._
5452
import configBuilder._
5553

@@ -199,8 +197,8 @@ class BlockImporter(
199197
Task.now((importedBlocks, None))
200198
} else {
201199
val restOfBlocks = blocks.tail
202-
blockImport
203-
.importBlock(blocks.head)
200+
consensus
201+
.evaluateBranchBlock(blocks.head)
204202
.flatMap {
205203
case BlockImportedToTop(_) =>
206204
tryImportBlocks(restOfBlocks, blocks.head :: importedBlocks)
@@ -238,7 +236,7 @@ class BlockImporter(
238236
def doLog(entry: ImportMessages.LogEntry): Unit = log.log(entry._1, entry._2)
239237
importWith(
240238
Task(doLog(importMessages.preImport()))
241-
.flatMap(_ => blockImport.importBlock(block))
239+
.flatMap(_ => consensus.evaluateBranchBlock(block))
242240
.tap((importMessages.messageForImportResult _).andThen(doLog))
243241
.tap {
244242
case BlockImportedToTop(importedBlocksData) =>
@@ -330,8 +328,7 @@ object BlockImporter {
330328
// scalastyle:off parameter.number
331329
def props(
332330
fetcher: ActorRef,
333-
blockImport: BlockImport,
334-
blockchain: Blockchain,
331+
consensus: Consensus,
335332
blockchainReader: BlockchainReader,
336333
stateStorage: StateStorage,
337334
branchResolution: BranchResolution,
@@ -345,8 +342,7 @@ object BlockImporter {
345342
Props(
346343
new BlockImporter(
347344
fetcher,
348-
blockImport,
349-
blockchain,
345+
consensus,
350346
blockchainReader,
351347
stateStorage,
352348
branchResolution,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.iohk.ethereum.consensus
2+
3+
import io.iohk.ethereum.blockchain.sync.regular.BlockImportResult
4+
import io.iohk.ethereum.domain.Block
5+
import io.iohk.ethereum.utils.BlockchainConfig
6+
import monix.eval.Task
7+
import monix.execution.Scheduler
8+
9+
/** This file documents the original interface that was designed at ETCM-1018
10+
* but implements a different one to be used as a stepping stone to the new architecture
11+
* still in progress
12+
*/
13+
trait Consensus {
14+
def evaluateBranchBlock(
15+
block: Block
16+
)(implicit blockExecutionScheduler: Scheduler, blockchainConfig: BlockchainConfig): Task[BlockImportResult]
17+
18+
/** Original interface from ETCM-1018, for temporary documentation purposes
19+
*/
20+
/** Answer which branch is best
21+
* @return branch.Branch
22+
*/
23+
// def getBestBranch(): branch.Branch = blockchainReader.getBestBranch()
24+
25+
/** @param branch
26+
* This methods received a Branch that was updated by ChainManagement.
27+
* When a Branch is updated we need to compare the weight of the current best branch with the
28+
* updated one.
29+
* If the current best branch is still the best then nothing needs to be done.
30+
* If the updated branch is heavier than an attempt to set the updated branch as best branch is done by
31+
* executing the blocks in the updated branch to see if it is a valid branch.
32+
* If it is not a valid branch then ExecutingSync has to be informed, otherwise update state with new best branch.
33+
*/
34+
// def evaluateBranch(branch: UpdatedBranch): Either[BlockExecutionError, Boolean] =
35+
// if (extendsBestBranch()) {
36+
// // just validate the latest block
37+
// Right(true)
38+
// } else {
39+
// if (isHeavierThanBestBranch(branch)) {
40+
// // create a queue of (branchTip, CancelableFuture)
41+
// // if any branch is being executed at the moment while a better one comes is then call the cancellation hook
42+
// attemptToSetNewBestBranch(branch) match {
43+
// case Right(result) => // save pointer to new best branch
44+
// Right(true)
45+
// case Left(error) => Left(error)
46+
// }
47+
// } else {
48+
// // nothing
49+
// Right(true)
50+
// }
51+
// }
52+
53+
// private def extendsBestBranch(): Boolean = ???
54+
55+
/** Compares the weight of the updatedBranch with the weight of the current best branch
56+
* @param updatedBranch
57+
* @return true if updatedBranch is heavier than current best branch, false otherwise
58+
*/
59+
// private def isHeavierThanBestBranch(updatedBranch: UpdatedBranch): Boolean = ???
60+
61+
/** Tries to set a new best branch by executing all blocks in the branch, from the HCB to the branch tip.
62+
* We assume the pre validation of the blocks of the branch was done already
63+
* @param branch
64+
* @return Either[BlockExecutionError, Boolean]
65+
*/
66+
// private def attemptToSetNewBestBranch(branch: UpdatedBranch): Either[BlockExecutionError, Boolean] = ???
67+
68+
}

0 commit comments

Comments
 (0)