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
pub use crate::intercom::WatchMsg as Message;
use crate::{
    blockcfg::HeaderHash,
    blockchain::{Blockchain, Storage},
    intercom::{self, ReplyStream, ReplyStreamHandle},
    utils::{
        async_msg::{MessageBox, MessageQueue},
        task::TokioServiceInfo,
    },
};
use chain_core::{
    packer::Codec,
    property::{Block as _, Deserialize, Serialize},
};
use chain_impl_mockchain::header;
use chain_network::{
    core::watch::server::Watch,
    data::{Block, BlockIds, Header},
    error::Code,
    grpc::watch::{server, server::WatchService},
};
use futures::{
    stream::{Map, MapErr},
    SinkExt, Stream, StreamExt, TryStream, TryStreamExt,
};
use std::{collections::HashSet, sync::Arc};
use tokio::sync::{broadcast, watch};
use tokio_stream::wrappers::{BroadcastStream, WatchStream};
use tracing::{instrument, span, Instrument, Level};

#[derive(Clone)]
pub struct WatchClient {
    tip_receiver: watch::Receiver<Header>,
    block_sender: Arc<broadcast::Sender<Block>>,
    request_tx: MessageBox<RequestMsg>,
}

pub struct MessageProcessor {
    tip_sender: Arc<watch::Sender<Header>>,
    block_sender: Arc<broadcast::Sender<Block>>,
    requests: MessageQueue<RequestMsg>,
    storage: Storage,
    blockchain: Blockchain,
}

enum RequestMsg {
    SyncMultiverse {
        from: BlockIds,
        handle: ReplyStreamHandle<Block>,
    },
}

impl MessageProcessor {
    pub async fn start(self, info: TokioServiceInfo, mut queue: MessageQueue<Message>) {
        let span = span!(Level::TRACE, "watch client message processor");

        let storage = self.storage;
        let requests = self.requests;
        let blockchain = self.blockchain.clone();
        info.spawn(
            "watch client",
            async move {
                requests
                    .for_each(|msg| async {
                        match msg {
                            RequestMsg::SyncMultiverse { from, handle } => {
                                let mut sink = handle.start_sending();

                                if let Err(e) =
                                    handle_sync_multiverse(from, &blockchain, &storage, &mut sink)
                                        .await
                                {
                                    tracing::warn!(
                                        "sync multiverse call finished with error: {:?}",
                                        e
                                    );
                                    let _ = sink.feed(Err(e)).await;
                                }

                                let _ = sink.close().await;
                            }
                        }
                    })
                    .await;
            }
            .instrument(tracing::info_span!(
                parent: span.clone(),
                "received sync multiverse request"
            )),
        );

        while let Some(input) = queue.next().await {
            match input {
                Message::NewBlock(block) => {
                    let block_sender = Arc::clone(&self.block_sender);
                    let block_id = block.id();
                    info.spawn(
                        "notifier broadcast block",
                        async move {
                            if let Err(_err) = block_sender
                                .send(Block::from_bytes(block.serialize_as_vec().unwrap()))
                            {
                                tracing::trace!("there are no subscribers to broadcast block");
                            } else {
                                tracing::debug!("block broadcasted to subscribers");
                            }
                        }
                        .instrument(tracing::debug_span!(
                            parent: span.clone(),
                            "block propagation message",
                            block_id = %block_id
                        )),
                    );
                }
                Message::NewTip(header) => {
                    let tip_sender = Arc::clone(&self.tip_sender);
                    let tip_id = header.id();
                    info.spawn(
                        "notifier broadcast new tip",
                        async move {
                            if let Err(err) = tip_sender.send(Header::from_bytes(
                                header.serialize_as_vec().unwrap().as_ref(),
                            )) {
                                tracing::debug!(
                                    "notifier failed to broadcast tip {}, {}",
                                    header.id(),
                                    err
                                );
                            }
                        }
                        .instrument(tracing::debug_span!(
                            parent: span.clone(),
                            "tip propagation message",
                            ?tip_id
                        )),
                    );
                }
            }
        }
    }
}

impl WatchClient {
    pub fn new(
        current_tip: header::Header,
        blockchain: Blockchain,
    ) -> (WatchClient, MessageProcessor) {
        let storage = blockchain.storage().clone();
        let (tip_sender, tip_receiver) = watch::channel(Header::from_bytes(
            current_tip.serialize_as_vec().unwrap().as_ref(),
        ));

        let (block_sender, _block_receiver) = broadcast::channel(16);

        let tip_sender = Arc::new(tip_sender);
        let block_sender = Arc::new(block_sender);

        let (request_tx, requests) = crate::utils::async_msg::channel(16);

        let client = WatchClient {
            tip_receiver,
            block_sender: Arc::clone(&block_sender),
            request_tx,
        };

        let message_processor = MessageProcessor {
            tip_sender,
            block_sender: Arc::clone(&block_sender),
            storage,
            blockchain,
            requests,
        };

        (client, message_processor)
    }

    pub fn into_server(self) -> server::Server<Self> {
        server::Server::new(WatchService::new(self))
    }
}

type SubscriptionTryStream<S> =
    MapErr<S, fn(<S as TryStream>::Error) -> chain_network::error::Error>;
type SubscriptionStream<S> =
    Map<S, fn(<S as Stream>::Item) -> Result<<S as Stream>::Item, chain_network::error::Error>>;

#[tonic::async_trait]
impl Watch for WatchClient {
    type BlockSubscriptionStream = SubscriptionTryStream<BroadcastStream<Block>>;
    type TipSubscriptionStream = SubscriptionStream<WatchStream<Header>>;
    type SyncMultiverseStream = SubscriptionTryStream<ReplyStream<Block, intercom::Error>>;

    #[instrument(skip(self))]
    async fn block_subscription(
        &self,
    ) -> Result<Self::BlockSubscriptionStream, chain_network::error::Error> {
        let block_receiver = BroadcastStream::new(self.block_sender.subscribe());

        // there are two possible errors for the block_receiver.
        // one occurs when there are no more senders, but that won't happen here.
        // the other is when the receiver is lagging.
        let live_stream: SubscriptionTryStream<BroadcastStream<Block>> =
            block_receiver.map_err(|e| chain_network::error::Error::new(Code::Aborted, e));

        Ok(live_stream)
    }

    #[instrument(skip(self))]
    async fn tip_subscription(
        &self,
    ) -> Result<Self::TipSubscriptionStream, chain_network::error::Error> {
        let tip_receiver: SubscriptionStream<_> = WatchStream::new(self.tip_receiver.clone())
            .map::<Result<Header, chain_network::error::Error>, _>(Ok);

        Ok(tip_receiver)
    }

    #[instrument(skip(self))]
    async fn sync_multiverse(
        &self,
        from: BlockIds,
    ) -> Result<Self::SyncMultiverseStream, chain_network::error::Error> {
        let (handle, future) = intercom::stream_reply(32);

        self.request_tx
            .clone()
            .send(RequestMsg::SyncMultiverse { from, handle })
            .await
            .map_err(|e| chain_network::error::Error::new(Code::Unavailable, e))?;

        let stream = future
            .await
            .map_err(|e| chain_network::error::Error::new(Code::Internal, e))?;

        Ok(stream.map_err(|e| chain_network::error::Error::new(Code::Internal, e)))
    }
}

async fn handle_sync_multiverse(
    checkpoints: BlockIds,
    blockchain: &Blockchain,
    storage: &Storage,
    sink: &mut intercom::ReplyStreamSink<Block>,
) -> Result<(), intercom::Error> {
    let block0 = blockchain.block0();

    let (checkpoints, lsb_length, lsb_id) = {
        let mut filtered = Vec::with_capacity(checkpoints.len());

        let mut min_length = u32::MAX;
        let mut min_index = None;

        for id_raw in checkpoints.iter() {
            let id = HeaderHash::deserialize(&mut Codec::new(id_raw.as_bytes()))
                .map_err(intercom::Error::invalid_argument)?;

            // the checkpoint could be unknown to the node because it was part of a branch that
            // didn't survive the selection, in that case, we just ignore it and let the client
            // realize that it can forget about it.
            if let Some(chain_length) = storage.get_chain_length(id) {
                filtered.push((chain_length, id));

                // keep track of the min length in order to find the (expected) lsb
                if chain_length < min_length {
                    min_length = chain_length;

                    // we called `push` up just before, so `len()` should be >= 1, and this won't
                    // overflow
                    min_index.replace(filtered.len() - 1);
                }
            }
        }

        // min_index will be None if there were no checkpoints, or if none of them were known to
        // the node.
        let (lsb_length, lsb_id) = min_index
            .map(|idx| filtered.swap_remove(idx))
            .unwrap_or((0, *block0));

        (filtered, lsb_length, lsb_id)
    };

    // we are adding 1 to the lsb later, because if it is present then we don't need to send it.
    // but if the checkpoints are empty then it means the last stable block is the block0, and we
    // send it here.
    if checkpoints.is_empty() {
        let block = storage
            .get(*block0)
            .map_err(intercom::Error::failed)
            .and_then(|maybe_block0| {
                maybe_block0.ok_or_else(|| intercom::Error::failed("block0 not found in storage"))
            })?;

        sink.send(Ok(chain_network::data::Block::from_bytes(
            block.serialize_as_vec().unwrap(),
        )))
        .await
        .map_err(intercom::Error::failed)?;
    }

    let mut known_unstable_blocks_by_client = HashSet::new();

    for (checkpoint_length, checkpoint) in checkpoints {
        let mut current = checkpoint;
        let mut current_length = checkpoint_length;

        while current != lsb_id {
            // this would mean the lsb is not an ancestor of the checkpoint
            // which shouldn't happen.
            if current_length < lsb_length {
                return Err(intercom::Error::invalid_argument(
                    "checkpoint is not a succesor of the last stable block",
                ));
            }

            // if a block is in the set, then the predecesors should be also there (added by a
            // previous iteration).
            // and because this should be converging to the lsb then we can exit early.
            if !known_unstable_blocks_by_client.insert(current) {
                break;
            }

            current = storage
                .get_parent(current)
                .map_err(intercom::Error::failed_precondition)?
                .ok_or_else(|| intercom::Error::aborted("reached block0"))?;

            // current_length is not 0 because we know that current != lsb, and
            //
            // a) the chain lengths come from the storage, not the client.
            // b) there should be only one block with a chain length of 0 in the storage.
            current_length -= 1;
        }
    }

    let mut current_length = lsb_length + 1;

    loop {
        let blocks = storage
            .get_blocks_by_chain_length(current_length)
            .map_err(intercom::Error::aborted)?;

        current_length += 1;

        if blocks.is_empty() {
            break;
        }

        for block in blocks
            .iter()
            .filter(|b| !known_unstable_blocks_by_client.contains(&b.header().id()))
        {
            sink.send(Ok(chain_network::data::Block::from_bytes(
                block.serialize_as_vec().unwrap(),
            )))
            .await
            .map_err(intercom::Error::failed)?;
        }
    }

    Ok(())
}