@@ -27,6 +27,7 @@ import io.iohk.ethereum.network.p2p.messages.ETH62.BlockHeaders
2727import io .iohk .ethereum .utils .Config .SyncConfig
2828import akka .stream .scaladsl .SourceQueue
2929import akka .stream .QueueOfferResult .Enqueued
30+ import akka .NotUsed
3031
3132// not used atm, a part of the future ExecutionSync
3233class FetcherService (
@@ -48,10 +49,10 @@ class FetcherService(
4849
4950// TODO: add private def requestStateNode(hash: ByteString): Task[Either[RequestFailed, Seq[ByteString]]] = ???
5051
51- private def placeBlockInPeerStream (peer : Peer , block : Block ): Task [Either [RequestFailed , Unit ]] =
52+ def placeBlockInPeerStream (block : Block ): Task [Either [String , Unit ]] =
5253 Task .deferFuture(sourceQueue.offer(block)).map {
5354 case Enqueued => Right (())
54- case result => Left (RequestFailed (peer, result.toString()) )
55+ case reason => Left (s " SourceQueue.offer failed: $reason " )
5556 }
5657
5758 def fetchBlocksUntil (
@@ -87,13 +88,11 @@ class FetcherService(
8788 bodies <- EitherT (requestBodies(headers.headers.map(_.hash)))
8889 blocks = buildBlocks(headers.headers, bodies.bodies)
8990 _ <- EitherT .cond[Task ](blocks.length == headers.headers.length, (), RequestFailed (peer, " Unmatching bodies" ))
90- _ <- blocks.traverse(block => EitherT (placeBlockInPeerStream(peer, block )))
91+ _ <- blocks.traverse(block => EitherT (placeBlockInPeerStream(block)).leftMap( RequestFailed ( peer, _ )))
9192 } yield peer
9293}
9394
9495object FetcherService {
95- type Hashes = Seq [ByteString ]
96-
9796 case class BlockIdentifier (transactionsRoot : ByteString , ommersHash : ByteString )
9897 object BlockIdentifier {
9998 def apply (blockHeader : BlockHeader ): BlockIdentifier =
@@ -117,31 +116,25 @@ object FetcherService {
117116 /** State of block fetching stream after processing a given incoming message with block headers or bodies
118117 *
119118 * @param outstanding headers that are yet to be matched to bodies
120- * @param bodiesRequest information for requesting bodies corresponding to newly outstanding headers
121119 * @param result blocks produced by matching received headers with received bodies
122120 */
123121 case class FetchState (
124122 outstanding : Set [BlockHeader ],
125- bodiesRequest : Option [(PeerId , Hashes )],
126123 result : Seq [Block ]
127124 )
128125 object FetchState {
129- val initial : FetchState = FetchState (Set .empty, None , Nil )
126+ val initial : FetchState = FetchState (Set .empty, Nil )
130127 }
131128
132- def fetchBlocksForHeaders [M ](bodyRequestor : Sink [(PeerId , Hashes ), M ]): Flow [MessageFromPeer , Seq [Block ], M ] =
129+ // TODO: remove once we have the FetcherService instance integrated
130+ val tempFlow : Flow [MessageFromPeer , Seq [Block ], NotUsed ] =
133131 Flow [MessageFromPeer ]
134132 .scan(FetchState .initial) {
135- case (FetchState (outstanding, _, _ ), MessageFromPeer (BlockHeaders (headers), peerId)) =>
136- FetchState (outstanding.concat(headers), Some (peerId -> headers.map(_.hash)), Nil )
137- case (FetchState (outstanding, _, _ ), MessageFromPeer (BlockBodies (bodies), _)) =>
133+ case (FetchState (outstanding, _), MessageFromPeer (BlockHeaders (headers), peerId)) =>
134+ FetchState (outstanding.concat(headers), Nil )
135+ case (FetchState (outstanding, _), MessageFromPeer (BlockBodies (bodies), _)) =>
138136 val blocks = buildBlocks(outstanding.toSeq, bodies)
139- FetchState (outstanding.removedAll(blocks.map(_.header)), None , blocks)
137+ FetchState (outstanding.removedAll(blocks.map(_.header)), blocks)
140138 }
141- .alsoToMat(
142- Flow [FetchState ]
143- .collect { case FetchState (_, Some (bodiesRequest), _) => bodiesRequest }
144- .toMat(bodyRequestor)(Keep .right)
145- )(Keep .right)
146- .collect { case FetchState (_, _, blocks) if blocks.nonEmpty => blocks }
139+ .collect { case FetchState (_, blocks) if blocks.nonEmpty => blocks }
147140}
0 commit comments