Skip to content

Commit 6a71cb3

Browse files
committed
[ETCM-283] Pass block validator as param
1 parent 3e6d236 commit 6a71cb3

File tree

8 files changed

+46
-19
lines changed

8 files changed

+46
-19
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ object RegularSyncItSpecUtils {
6363
"pending-transactions-manager"
6464
)
6565

66+
lazy val validators = new MockValidatorsAlwaysSucceed
67+
6668
lazy val regularSync = system.actorOf(
6769
RegularSync.props(
6870
peersClient,
@@ -71,6 +73,7 @@ object RegularSyncItSpecUtils {
7173
ledger,
7274
bl,
7375
blockchainConfig, // FIXME: remove in ETCM-280
76+
validators.blockValidator,
7477
testSyncConfig,
7578
ommersPool,
7679
pendingTransactionsManager,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class SyncController(
102102
ledger,
103103
blockchain,
104104
blockchainConfig,
105+
validators.blockValidator,
105106
syncConfig,
106107
ommersPool,
107108
pendingTransactionsManager,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import cats.data.NonEmptyList
88
import cats.instances.future._
99
import cats.instances.option._
1010
import cats.syntax.either._
11+
import io.iohk.ethereum.consensus.validators.BlockValidator
1112
import io.iohk.ethereum.blockchain.sync.PeersClient._
1213
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcherState.{
1314
AwaitingBodiesToBeIgnored,
@@ -37,6 +38,7 @@ class BlockFetcher(
3738
val peerEventBus: ActorRef,
3839
val supervisor: ActorRef,
3940
val syncConfig: SyncConfig,
41+
val blockValidator: BlockValidator,
4042
implicit val scheduler: Scheduler
4143
) extends Actor
4244
with ActorLogging {
@@ -54,7 +56,7 @@ class BlockFetcher(
5456
}
5557

5658
private def idle(): Receive = handleCommonMessages(None) orElse { case Start(importer, blockNr) =>
57-
BlockFetcherState.initial(importer, blockNr) |> fetchBlocks
59+
BlockFetcherState.initial(importer, blockValidator, blockNr) |> fetchBlocks
5860
peerEventBus ! Subscribe(
5961
MessageClassifier(
6062
Set(NewBlock.code63, NewBlock.code64, NewBlockHashes.code, BlockHeaders.code),
@@ -363,9 +365,10 @@ object BlockFetcher {
363365
peerEventBus: ActorRef,
364366
supervisor: ActorRef,
365367
syncConfig: SyncConfig,
368+
blockValidator: BlockValidator,
366369
scheduler: Scheduler
367370
): Props =
368-
Props(new BlockFetcher(peersClient, peerEventBus, supervisor, syncConfig, scheduler))
371+
Props(new BlockFetcher(peersClient, peerEventBus, supervisor, syncConfig, blockValidator, scheduler))
369372

370373
sealed trait FetchMsg
371374
case class Start(importer: ActorRef, fromBlock: BigInt) extends FetchMsg

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package io.iohk.ethereum.blockchain.sync.regular
33
import akka.actor.ActorRef
44
import akka.util.ByteString
55
import cats.data.NonEmptyList
6-
//FIXME: By using this class, we are coupling sync process with a specific consensus (the standard one).
7-
import io.iohk.ethereum.consensus.validators.std.StdBlockValidator
6+
import io.iohk.ethereum.consensus.validators.BlockValidator
87
import io.iohk.ethereum.domain.{Block, BlockHeader, BlockBody, HeadersSeq}
98
import io.iohk.ethereum.network.PeerId
109
import io.iohk.ethereum.network.p2p.messages.PV62.BlockHash
@@ -14,6 +13,7 @@ import cats.syntax.option._
1413

1514
import scala.collection.immutable.Queue
1615
import scala.annotation.tailrec
16+
import io.iohk.ethereum.consensus.validators.BlockValidator
1717

1818
// scalastyle:off number.of.methods
1919
/**
@@ -37,6 +37,7 @@ import scala.annotation.tailrec
3737
*/
3838
case class BlockFetcherState(
3939
importer: ActorRef,
40+
blockValidator: BlockValidator,
4041
readyBlocks: Queue[Block],
4142
waitingHeaders: Queue[BlockHeader],
4243
fetchingHeadersState: FetchingHeadersState,
@@ -121,7 +122,7 @@ case class BlockFetcherState(
121122
case (Seq(), _ +: _) => None
122123
case (_, Seq()) => Some(matchedBlocks)
123124
case (header +: remainingHeaders, body +: remainingBodies) =>
124-
val doMatch = StdBlockValidator.validateHeaderAndBody(header, body).isRight
125+
val doMatch = blockValidator.validateHeaderAndBody(header, body).isRight
125126
if (doMatch)
126127
bodiesAreOrderedSubsetOfRequested(remainingHeaders, remainingBodies, matchedBlocks :+ Block(header, body))
127128
else
@@ -241,17 +242,19 @@ case class BlockFetcherState(
241242
object BlockFetcherState {
242243
case class StateNodeFetcher(hash: ByteString, replyTo: ActorRef)
243244

244-
def initial(importer: ActorRef, lastBlock: BigInt): BlockFetcherState = BlockFetcherState(
245-
importer = importer,
246-
readyBlocks = Queue(),
247-
waitingHeaders = Queue(),
248-
fetchingHeadersState = NotFetchingHeaders,
249-
fetchingBodiesState = NotFetchingBodies,
250-
stateNodeFetcher = None,
251-
lastBlock = lastBlock,
252-
knownTop = lastBlock + 1,
253-
blockProviders = Map()
254-
)
245+
def initial(importer: ActorRef, blockValidator: BlockValidator, lastBlock: BigInt): BlockFetcherState =
246+
BlockFetcherState(
247+
importer = importer,
248+
blockValidator = blockValidator,
249+
readyBlocks = Queue(),
250+
waitingHeaders = Queue(),
251+
fetchingHeadersState = NotFetchingHeaders,
252+
fetchingBodiesState = NotFetchingBodies,
253+
stateNodeFetcher = None,
254+
lastBlock = lastBlock,
255+
knownTop = lastBlock + 1,
256+
blockProviders = Map()
257+
)
255258

256259
trait FetchingHeadersState
257260
case object NotFetchingHeaders extends FetchingHeadersState

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.iohk.ethereum.blockchain.sync.regular.RegularSync.{NewCheckpoint, Prog
99
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.InternalLastBlockImport
1010
import io.iohk.ethereum.blockchain.sync.BlockBroadcast
1111
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
12+
import io.iohk.ethereum.consensus.validators.BlockValidator
1213
import io.iohk.ethereum.crypto.ECDSASignature
1314
import io.iohk.ethereum.domain.Blockchain
1415
import io.iohk.ethereum.ledger.Ledger
@@ -22,6 +23,7 @@ class RegularSync(
2223
ledger: Ledger,
2324
blockchain: Blockchain,
2425
blockchainConfig: BlockchainConfig,
26+
blockValidator: BlockValidator,
2527
syncConfig: SyncConfig,
2628
ommersPool: ActorRef,
2729
pendingTransactionsManager: ActorRef,
@@ -31,7 +33,10 @@ class RegularSync(
3133
with ActorLogging {
3234

3335
val fetcher: ActorRef =
34-
context.actorOf(BlockFetcher.props(peersClient, peerEventBus, self, syncConfig, scheduler), "block-fetcher")
36+
context.actorOf(
37+
BlockFetcher.props(peersClient, peerEventBus, self, syncConfig, blockValidator, scheduler),
38+
"block-fetcher"
39+
)
3540
val broadcaster: ActorRef = context.actorOf(
3641
BlockBroadcasterActor
3742
.props(new BlockBroadcast(etcPeerManager, syncConfig), peerEventBus, etcPeerManager, syncConfig, scheduler),
@@ -122,6 +127,7 @@ object RegularSync {
122127
ledger: Ledger,
123128
blockchain: Blockchain,
124129
blockchainConfig: BlockchainConfig,
130+
blockValidator: BlockValidator,
125131
syncConfig: SyncConfig,
126132
ommersPool: ActorRef,
127133
pendingTransactionsManager: ActorRef,
@@ -136,6 +142,7 @@ object RegularSync {
136142
ledger,
137143
blockchain,
138144
blockchainConfig,
145+
blockValidator,
139146
syncConfig,
140147
ommersPool,
141148
pendingTransactionsManager,

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.net.InetSocketAddress
55
import akka.actor.ActorSystem
66
import akka.testkit.{TestKit, TestProbe}
77
import com.miguno.akka.testing.VirtualTime
8+
import io.iohk.ethereum.Mocks.MockValidatorsAlwaysSucceed
89
import io.iohk.ethereum.BlockHelpers
910
import io.iohk.ethereum.Fixtures.{Blocks => FixtureBlocks}
1011
import io.iohk.ethereum.blockchain.sync.PeersClient.BlacklistPeer
@@ -134,6 +135,8 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
134135
val importer: TestProbe = TestProbe()
135136
val regularSync: TestProbe = TestProbe()
136137

138+
val validators = new MockValidatorsAlwaysSucceed
139+
137140
override lazy val syncConfig = defaultSyncConfig.copy(
138141
// Same request size was selected for simplification purposes of the flow
139142
blockHeadersPerRequest = 10,
@@ -152,6 +155,7 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
152155
peerEventBus.ref,
153156
regularSync.ref,
154157
syncConfig,
158+
validators.blockValidator,
155159
time.scheduler
156160
)
157161
)

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherStateSpec.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ package io.iohk.ethereum.blockchain.sync.regular
22

33
import akka.actor.ActorSystem
44
import akka.testkit.{TestKit, TestProbe}
5+
import io.iohk.ethereum.Mocks.MockValidatorsAlwaysSucceed
56
import io.iohk.ethereum.domain.Block
67
import io.iohk.ethereum.Fixtures.Blocks.ValidBlock
78
import io.iohk.ethereum.network.PeerId
89
import org.scalatest.matchers.should.Matchers
910
import org.scalatest.wordspec.AnyWordSpecLike
1011

1112
class BlockFetcherStateSpec extends TestKit(ActorSystem()) with AnyWordSpecLike with Matchers {
13+
14+
lazy val validators = new MockValidatorsAlwaysSucceed
15+
1216
"BlockFetcherState" when {
1317
"invalidating blocks" should {
1418
"not allow to go to negative block number" in {
1519
val importer = TestProbe().ref
16-
val (_, actual) = BlockFetcherState.initial(importer, 10).invalidateBlocksFrom(-5, None)
20+
val (_, actual) =
21+
BlockFetcherState.initial(importer, validators.blockValidator, 10).invalidateBlocksFrom(-5, None)
1722

1823
actual.lastBlock shouldBe 0
1924
}
@@ -26,7 +31,7 @@ class BlockFetcherStateSpec extends TestKit(ActorSystem()) with AnyWordSpecLike
2631
val newBestBlock = Block(ValidBlock.header.copy(number = ValidBlock.header.number + 1), ValidBlock.body)
2732
val fakePeerId = PeerId("fake")
2833

29-
val currentState = BlockFetcherState.initial(importer, currentBestBlock.number)
34+
val currentState = BlockFetcherState.initial(importer, validators.blockValidator, currentBestBlock.number)
3035
val newState = currentState.appendNewBlock(newBestBlock, fakePeerId)
3136
newState.lastBlock shouldEqual newBestBlock.number
3237
newState.blockProviders(newBestBlock.number) shouldEqual fakePeerId

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncFixtures.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ trait RegularSyncFixtures { self: Matchers with AsyncMockFactory =>
6868
ledger,
6969
blockchain,
7070
blockchainConfig,
71+
validators.blockValidator,
7172
syncConfig,
7273
ommersPool.ref,
7374
pendingTransactionsManager.ref,

0 commit comments

Comments
 (0)