/
SyncService.scala
399 lines (369 loc) · 13.2 KB
/
SyncService.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package io.iohk.metronome.hotstuff.service
import cats.implicits._
import cats.Parallel
import cats.effect.{Sync, Resource, Concurrent, ContextShift, Timer}
import io.iohk.metronome.core.fibers.FiberMap
import io.iohk.metronome.core.messages.{
RPCMessageCompanion,
RPCPair,
RPCTracker
}
import io.iohk.metronome.hotstuff.consensus.{Federation, ViewNumber}
import io.iohk.metronome.hotstuff.consensus.basic.{
Agreement,
ProtocolState,
Block,
Signing
}
import io.iohk.metronome.hotstuff.service.execution.BlockExecutor
import io.iohk.metronome.hotstuff.service.messages.SyncMessage
import io.iohk.metronome.hotstuff.service.pipes.SyncPipe
import io.iohk.metronome.hotstuff.service.storage.{
BlockStorage,
ViewStateStorage
}
import io.iohk.metronome.hotstuff.service.sync.{
BlockSynchronizer,
ViewSynchronizer
}
import io.iohk.metronome.hotstuff.service.tracing.SyncTracers
import io.iohk.metronome.networking.{ConnectionHandler, Network}
import io.iohk.metronome.storage.KVStoreRunner
import scala.util.control.NonFatal
import scala.concurrent.duration._
import scala.reflect.ClassTag
/** The `SyncService` handles the `SyncMessage`s coming from the network,
* i.e. serving block and status requests, as well as receive responses
* for outgoing requests for missing dependencies.
*
* It will match up the `requestId`s in the responses and discard any
* unsolicited message.
*
* The block and view synchronisation components will use this service
* to send requests to the network.
*/
class SyncService[F[_]: Concurrent: ContextShift, N, A <: Agreement: Block](
publicKey: A#PKey,
network: Network[F, A#PKey, SyncMessage[A]],
appService: ApplicationService[F, A],
blockExecutor: BlockExecutor[F, N, A],
blockStorage: BlockStorage[N, A],
viewStateStorage: ViewStateStorage[N, A],
syncPipe: SyncPipe[F, A]#Right,
getState: F[ProtocolState[A]],
incomingFiberMap: FiberMap[F, A#PKey],
rpcTracker: RPCTracker[F, SyncMessage[A]]
)(implicit tracers: SyncTracers[F, A], storeRunner: KVStoreRunner[F, N]) {
import SyncMessage._
type BlockSync = SyncService.BlockSynchronizerWithFiberMap[F, N, A]
private def protocolStatus: F[Status[A]] =
getState.map { state =>
Status(state.viewNumber, state.prepareQC, state.commitQC)
}
/** Request a block from a peer. */
private def getBlock(from: A#PKey, blockHash: A#Hash): F[Option[A#Block]] = {
for {
requestId <- RequestId[F]
request = GetBlockRequest(requestId, blockHash)
maybeResponse <- sendRequest(from, request)
} yield maybeResponse.map(_.block)
}
/** Request the status of a peer. */
private def getStatus(from: A#PKey): F[Option[Status[A]]] =
if (from == publicKey) {
protocolStatus.map(_.some)
} else {
for {
requestId <- RequestId[F]
request = GetStatusRequest[A](requestId)
maybeResponse <- sendRequest(from, request)
} yield maybeResponse.map(_.status)
}
/** Send a request to the peer and track the response.
*
* Returns `None` if we're not connected or the request times out.
*/
private def sendRequest[
Req <: RPCMessageCompanion#Request,
Res <: RPCMessageCompanion#Response
](from: A#PKey, request: Req)(implicit
ev1: Req <:< SyncMessage[A] with SyncMessage.Request,
ev2: RPCPair.Aux[Req, Res],
ct: ClassTag[Res]
): F[Option[Res]] = {
for {
join <- rpcTracker.register[Req, Res](request)
_ <- network.sendMessage(from, request)
res <- join
_ <- tracers.requestTimeout(from -> request).whenA(res.isEmpty)
} yield res
}
/** Process incoming network messages. */
private def processNetworkMessages: F[Unit] = {
// TODO (PM-3186): Rate limiting per source.
network.incomingMessages
.mapEval[Unit] { case ConnectionHandler.MessageReceived(from, message) =>
// Handle on a fiber dedicated to the source.
incomingFiberMap
.submit(from) {
processNetworkMessage(from, message)
}
.attemptNarrow[FiberMap.QueueFullException]
.flatMap {
case Right(_) => ().pure[F]
case Left(_) => tracers.queueFull(from)
}
}
.completedL
}
/** Process one incoming network message.
*
* It's going to be executed on a fiber.
*/
private def processNetworkMessage(
from: A#PKey,
message: SyncMessage[A]
): F[Unit] = {
val process = message match {
case GetStatusRequest(requestId) =>
protocolStatus.flatMap { status =>
network.sendMessage(
from,
GetStatusResponse(requestId, status)
)
}
case GetBlockRequest(requestId, blockHash) =>
storeRunner
.runReadOnly {
blockStorage.get(blockHash)
}
.flatMap {
case None =>
().pure[F]
case Some(block) =>
network.sendMessage(
from,
GetBlockResponse(requestId, block)
)
}
case response: SyncMessage.Response =>
rpcTracker.complete(response).flatMap {
case Right(ok) =>
tracers.responseIgnored((from, response, None)).whenA(!ok)
case Left(ex) =>
tracers.responseIgnored((from, response, Some(ex)))
}
}
process.handleErrorWith { case NonFatal(ex) =>
tracers.error(ex)
}
}
/** Read Requests from the SyncPipe and send Responses.
*
* These are coming from the `ConsensusService` asking for a
* `Prepare` message to be synchronized with the sender, or
* for the view to be synchronized with the whole federation.
*/
private def processSyncPipe(
makeBlockSync: F[BlockSync],
viewSynchronizer: ViewSynchronizer[F, A]
): F[Unit] =
syncPipe.receive.consume.use { consumer =>
def loop(
blockSync: BlockSync,
lastSyncedViewNumber: ViewNumber
): F[Unit] = {
consumer.pull.flatMap {
case Right(SyncPipe.PrepareRequest(_, prepare))
if prepare.viewNumber < lastSyncedViewNumber =>
// We have already synced to a Commit Q.C. higher than this old PrepareRequest.
loop(blockSync, lastSyncedViewNumber)
case Right(SyncPipe.StatusRequest(viewNumber))
if viewNumber < lastSyncedViewNumber =>
// We have already synced higher than this old StatusRequest.
loop(blockSync, lastSyncedViewNumber)
case Right(request @ SyncPipe.PrepareRequest(_, _)) =>
handlePrepareRequest(blockSync, request) >>
loop(blockSync, lastSyncedViewNumber)
case Right(request @ SyncPipe.StatusRequest(_)) =>
handleStatusRequest(
makeBlockSync,
blockSync,
viewSynchronizer,
request
).flatMap {
(loop _).tupled
}
case Left(maybeError) =>
blockSync.fiberMapRelease >>
maybeError.fold(().pure[F])(Sync[F].raiseError(_))
}
}
makeBlockSync.flatMap { blockSync =>
loop(blockSync, ViewNumber(0))
}
}
/** Sync with the sender up to the High Q.C. it sent, then validate the prepared block.
*
* This is done in the background, while further requests are taken from the pipe.
*/
private def handlePrepareRequest(
blockSync: BlockSync,
request: SyncPipe.PrepareRequest[A]
): F[Unit] = {
val sender = request.sender
val prepare = request.prepare
// It is enough to respond to the last block positively, it will indicate
// that the whole range can be executed later (at that point from storage).
// If the same leader is sending us newer proposals, we can ignore the
// previous pepared blocks - they are either part of the new Q.C.,
// in which case they don't need to be validated, or they have not
// gathered enough votes, and been superseded by a new proposal.
blockSync.fiberMap.cancelQueue(sender) >>
blockSync.fiberMap
.submit(sender) {
blockSync.synchronizer.sync(sender, prepare.highQC) >>
appService.validateBlock(prepare.block) >>= {
case Some(isValid) =>
syncPipe.send(SyncPipe.PrepareResponse(request, isValid))
case None =>
// We didn't have data to decide validity in time; not responding.
().pure[F]
}
}
.void
}
/** Shut down the any outstanding block downloads, sync the view,
* then create another block synchronizer instance to resume with.
*/
private def handleStatusRequest(
makeBlockSync: F[BlockSync],
blockSync: BlockSync,
viewSynchronizer: ViewSynchronizer[F, A],
request: SyncPipe.StatusRequest
): F[(BlockSync, ViewNumber)] =
for {
// Cancel all outstanding block syncing.
_ <- blockSync.fiberMapRelease
// The block synchronizer is still usable.
viewNumber <- syncStatus(
blockSync.synchronizer,
viewSynchronizer
).handleErrorWith { case NonFatal(ex) =>
tracers.error(ex).as(request.viewNumber)
}
// Create a fresh fiber and block synchronizer instance.
// When the previous goes out of scope, its ephemeral storage is freed.
newBlockSync <- makeBlockSync
} yield (newBlockSync, viewNumber)
/** Get the latest status of federation members, download the corresponding block
* and prune all existing block history, making the latest Commit Q.C. the new
* root in the block tree.
*
* This is done in the foreground, no further requests are taken from the pipe.
*/
private def syncStatus(
blockSynchronizer: BlockSynchronizer[F, N, A],
viewSynchronizer: ViewSynchronizer[F, A]
): F[ViewNumber] =
for {
// Sync to the latest Commit Q.C.
federationStatus <- viewSynchronizer.sync
status = federationStatus.status
// Download the block in the Commit Q.C.
block <- blockSynchronizer
.getBlockFromQuorumCertificate(
federationStatus.sources,
status.commitQC
)
.rethrow
// Sync any application specific state, e.g. a ledger,
// then potentially prune old blocks from the storage.
_ <- blockExecutor.syncState(federationStatus.sources, block)
// Tell the ConsensusService about the new Status.
_ <- syncPipe.send(SyncPipe.StatusResponse(status))
} yield status.viewNumber
}
object SyncService {
/** Create a `SyncService` instance and start processing messages
* in the background, shutting processing down when the resource is
* released.
*/
def apply[
F[_]: Concurrent: ContextShift: Timer: Parallel,
N,
A <: Agreement: Block: Signing
](
publicKey: A#PKey,
federation: Federation[A#PKey],
network: Network[F, A#PKey, SyncMessage[A]],
appService: ApplicationService[F, A],
blockExecutor: BlockExecutor[F, N, A],
blockStorage: BlockStorage[N, A],
viewStateStorage: ViewStateStorage[N, A],
syncPipe: SyncPipe[F, A]#Right,
getState: F[ProtocolState[A]],
timeout: FiniteDuration = 10.seconds
)(implicit
tracers: SyncTracers[F, A],
storeRunner: KVStoreRunner[F, N]
): Resource[F, SyncService[F, N, A]] =
// TODO (PM-3186): Add capacity as part of rate limiting.
for {
incomingFiberMap <- FiberMap[F, A#PKey]()
rpcTracker <- Resource.liftF {
RPCTracker[F, SyncMessage[A]](timeout)
}
service = new SyncService(
publicKey,
network,
appService,
blockExecutor,
blockStorage,
viewStateStorage,
syncPipe,
getState,
incomingFiberMap,
rpcTracker
)
blockSync = for {
(syncFiberMap, syncFiberMapRelease) <- FiberMap[F, A#PKey]().allocated
blockSynchronizer <- BlockSynchronizer[F, N, A](
publicKey,
federation,
blockStorage,
service.getBlock
)
} yield BlockSynchronizerWithFiberMap(
blockSynchronizer,
syncFiberMap,
syncFiberMapRelease
)
viewSynchronizer = new ViewSynchronizer[F, A](
federation,
service.getStatus
)
_ <- Concurrent[F].background {
service.processNetworkMessages
}
_ <- Concurrent[F].background {
service.processSyncPipe(blockSync, viewSynchronizer)
}
} yield service
/** The `SyncService` can be in two modes: either we're in sync with the federation
* and downloading the odd missing block every now and then, or we are out of sync,
* in which case we need to ask everyone to find out what the current view number
* is, and then jump straight to the latest Commit Quorum Certificate.
*
* Our implementation assumes that this is always supported by the application.
*
* When we go from block sync to view sync, the block syncs happening in the
* background on the fiber ap in this class are canceled, and the synchronizer
* instance with its ephemeral storage is discarded.
*/
case class BlockSynchronizerWithFiberMap[F[_], N, A <: Agreement](
synchronizer: BlockSynchronizer[F, N, A],
fiberMap: FiberMap[F, A#PKey],
fiberMapRelease: F[Unit]
)
}