tycho-collator 0.3.9

A collator node.
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
use std::fmt;
use std::future::IntoFuture;
use std::pin::{Pin, pin};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use std::task::{Context, Poll, Waker};

use anyhow::Result;
use arc_swap::ArcSwapOption;
use backon::BackoffBuilder;
use futures_util::stream::FuturesUnordered;
use futures_util::{Future, StreamExt};
use scc::TreeIndex;
use tokio::sync::{Notify, Semaphore};
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use tycho_crypto::ed25519::KeyPair;
use tycho_network::{OverlayId, PeerId, PrivateOverlay};
use tycho_types::models::*;
use tycho_util::FastHashMap;
use tycho_util::futures::JoinTask;
use tycho_util::metrics::HistogramGuard;

use super::ValidatorStdImplConfig;
use crate::tracing_targets;
use crate::validator::rpc::{ExchangeSignatures, ValidatorClient, ValidatorService};
use crate::validator::{
    AddSession, BriefValidatorDescr, CompositeValidationSessionId, ValidationComplete,
    ValidationSessionId, ValidationStatus, ValidatorNetworkContext, proto,
};

// Histograms
const METRIC_VALIDATE_BLOCK_TIME: &str = "tycho_validator_validate_block_time";
const METRIC_EXCHANGE_SIGNATURE_TIME: &str = "tycho_validator_exchange_signature_time";
const METRIC_RECEIVE_SIGNATURE_TIME: &str = "tycho_validator_receive_signature_time";

// Counters
const METRIC_BLOCK_EXCHANGES_IN_TOTAL: &str = "tycho_validator_block_exchanges_in_total";
const METRIC_CACHE_EXCHANGES_IN_TOTAL: &str = "tycho_validator_cache_exchanges_in_total";
const METRIC_MISS_EXCHANGES_IN_TOTAL: &str = "tycho_validator_miss_exchanges_in_total";
const METRIC_INVALID_SIGNATURES_IN_TOTAL: &str = "tycho_validator_invalid_signatures_in_total";
const METRIC_INVALID_SIGNATURES_CACHED_TOTAL: &str =
    "tycho_validator_invalid_signatures_cached_total";

// Gauges
const METRIC_SESSIONS_ACTIVE: &str = "tycho_validator_sessions_active";
const METRIC_BLOCK_SLOTS: &str = "tycho_validator_block_slots";
const METRIC_CACHE_SLOTS: &str = "tycho_validator_cache_slots";

/// Validator session is unique for each shard within each validator set.
#[derive(Clone)]
#[repr(transparent)]
pub struct ValidatorSession {
    inner: Arc<Inner>,
}

impl ValidatorSession {
    pub fn new(
        net_context: &ValidatorNetworkContext,
        key_pair: Arc<KeyPair>,
        config: &ValidatorStdImplConfig,
        info: AddSession<'_>,
    ) -> Result<Self> {
        // Prepare a map with other validators
        let mut validators = FastHashMap::default();
        for descr in info.validators {
            // TODO: Skip invalid entries? But what should we do with the total weight?
            let validator_info = BriefValidatorDescr::try_from(descr)?;
            validators.insert(validator_info.peer_id, validator_info);
        }

        let max_weight = validators.values().map(|v| v.weight).sum::<u64>();
        let weight_threshold = max_weight.saturating_mul(2) / 3 + 1;

        let peer_id = net_context.network.peer_id();
        let own_weight = match validators.remove(peer_id) {
            Some(info) => info.weight,
            None => anyhow::bail!("node is not in the validator set"),
        };

        // NOTE: At this point we are sure that our node is in the validator set

        let peer_ids = validators.values().map(|v| v.peer_id).collect::<Vec<_>>();

        // Create the session state
        let state = Arc::new(SessionState {
            shard_ident: info.shard_ident,
            weight_threshold,
            validators: Arc::new(validators),
            block_signatures: TreeIndex::new(),
            cached_signatures: TreeIndex::new(),
            cancelled: AtomicBool::new(false),
            cancelled_signal: Notify::new(),
        });

        // Create the private overlay
        let overlay_id = compute_session_overlay_id(
            &net_context.zerostate_id,
            &info.shard_ident,
            &info.session_id,
        );

        let overlay = PrivateOverlay::builder(overlay_id)
            .named("validator")
            .with_peer_resolver(net_context.peer_resolver.clone())
            .with_entries(peer_ids)
            .build(ValidatorService {
                shard_ident: info.shard_ident,
                session_id: info.session_id,
                exchanger: Arc::downgrade(&state),
            });

        if !net_context.overlays.add_private_overlay(&overlay) {
            anyhow::bail!("private overlay already exists: {overlay_id:?}");
        }

        // Create the session
        let session = Self {
            inner: Arc::new(Inner {
                start_block_seqno: info.start_block_seqno,
                session_id: info.session_id,
                config: config.clone(),
                client: ValidatorClient::new(net_context.network.clone(), overlay),
                key_pair,
                peer_id: *peer_id,
                own_weight,
                state,
                min_seqno: AtomicU32::new(info.start_block_seqno),
            }),
        };

        metrics::gauge!(METRIC_SESSIONS_ACTIVE).increment(1);

        // Prepare initial cache slots
        session.create_cache_slots(info.start_block_seqno, config.signature_cache_slots);

        // Done
        Ok(session)
    }

    pub fn id(&self) -> ValidationSessionId {
        self.inner.session_id
    }

    pub fn start_block_seqno(&self) -> u32 {
        self.inner.start_block_seqno
    }

    pub fn cancel(&self) {
        self.inner.state.cancelled.store(true, Ordering::Release);
        self.inner.state.cancelled_signal.notify_waiters();
    }

    pub fn cancel_until(&self, block_seqno: u32) {
        self.inner
            .min_seqno
            .fetch_max(block_seqno, Ordering::Release);

        let state = self.inner.state.as_ref();
        state.cached_signatures.remove_range(..=block_seqno);

        let guard = scc::ebr::Guard::new();
        for (_, validation) in state.block_signatures.range(..=block_seqno, &guard) {
            validation.cancelled.cancel();
        }
        drop(guard);

        // NOTE: Remove only blocks that are old enough.
        let until_seqno = block_seqno.saturating_sub(self.inner.config.old_blocks_to_keep);
        state.block_signatures.remove_range(..=until_seqno);
    }

    #[tracing::instrument(
        skip_all,
        fields(session_id = ?self.inner.session_id, %block_id)
    )]
    pub async fn validate_block(&self, block_id: &BlockId) -> Result<ValidationStatus> {
        let _histogram = HistogramGuard::begin(METRIC_VALIDATE_BLOCK_TIME);
        tracing::info!(target: tracing_targets::VALIDATOR, "started");

        debug_assert_eq!(self.inner.state.shard_ident, block_id.shard);

        self.inner
            .min_seqno
            .fetch_max(block_id.seqno, Ordering::Release);

        self.create_cache_slots(block_id.seqno + 1, self.inner.config.signature_cache_slots);

        let state = &self.inner.state;

        // Remove cached slot
        let cached = state
            .cached_signatures
            .peek(&block_id.seqno, &scc::ebr::Guard::new())
            .map(Arc::clone);

        // Prepare block signatures
        let block_signatures = match &cached {
            Some(cached) => self.reuse_signatures(block_id, cached.clone()).await,
            None => self.prepare_new_signatures(block_id),
        }
        .build(block_id, state.weight_threshold);

        // Allow only one validation at a time
        if state
            .block_signatures
            .insert(block_id.seqno, block_signatures.clone())
            .is_err()
        {
            // TODO: Panic here?
            anyhow::bail!(
                "block validation is already in progress. \
                session_id={:?}, block_id={}",
                self.inner.session_id,
                block_id,
            );
        }

        // NOTE: To eliminate the gap inside exchange routine, we can remove cached signatures
        // only after we have inserted the block.
        //
        // At this point the following is true:
        // - All new signatures will be stored (and validated) in the block;
        // - There might be some new signatures that were stored in the cache, but we
        //   have not yet processed them. We will use them later.
        state.cached_signatures.remove(&block_id.seqno);

        // Start collecting signatures from other validators
        let mut result = FastHashMap::default();
        result.insert(
            *self.inner.client.peer_id(),
            block_signatures.own_signature.clone(),
        );
        let mut total_weight = self.inner.own_weight;

        let semaphore = Arc::new(Semaphore::new(self.inner.config.max_parallel_requests));
        let mut futures = FuturesUnordered::new();
        for (peer_id, signature) in block_signatures.other_signatures.iter() {
            if let Some(valid_signature) = signature.load_full() {
                let validator_info = state
                    .validators
                    .get(peer_id)
                    .expect("peer info out of sync");

                result.insert(*peer_id, valid_signature);
                total_weight += validator_info.weight;
                continue;
            }

            let cached = cached
                .as_ref()
                .and_then(|c| c.other_signatures.get(peer_id))
                .and_then(|c| c.load_full());

            let fut = self.inner.clone().receive_signature(
                *peer_id,
                block_signatures.clone(),
                cached,
                semaphore.clone(),
            );
            futures.push(JoinTask::new(fut.instrument(tracing::Span::current())));
        }

        let mut session_cancelled = pin!(state.cancelled_signal.notified());
        if state.cancelled.load(Ordering::Acquire) {
            tracing::trace!(
                target: tracing_targets::VALIDATOR,
                block_seqno = block_id.seqno,
                "session cancelled",
            );
            return Ok(ValidationStatus::Skipped);
        }

        let mut block_cancelled = pin!(block_signatures.cancelled.cancelled());
        while total_weight < state.weight_threshold {
            let res = tokio::select! {
                res = futures.next() => match res {
                    Some(res) => res,
                    None => anyhow::bail!("no more signatures to collect but the threshold is not reached"),
                },
                _ = &mut session_cancelled => {
                    tracing::trace!(
                        target: tracing_targets::VALIDATOR,
                        block_seqno = block_id.seqno,
                        "session cancelled",
                    );
                    return Ok(ValidationStatus::Skipped)
                },
                _ = &mut block_cancelled => {
                    tracing::trace!(
                        target: tracing_targets::VALIDATOR,
                        block_seqno = block_id.seqno,
                        "block cancelled",
                    );
                    return Ok(ValidationStatus::Skipped)
                },
            };

            let validator_info = state
                .validators
                .get(&res.peer_id)
                .expect("peer info out of sync");

            let prev = result.insert(res.peer_id, res.signature);
            debug_assert!(prev.is_none(), "duplicate signature in result");
            total_weight += validator_info.weight;
        }

        tracing::info!(target: tracing_targets::VALIDATOR, "finished");
        Ok(ValidationStatus::Complete(ValidationComplete {
            signatures: result,
            total_weight,
        }))
    }

    fn prepare_new_signatures(&self, block_id: &BlockId) -> BlockSignaturesBuilder {
        let data = Block::build_data_for_sign(block_id);

        // Prepare our own signature
        let own_signature = Arc::new(self.inner.key_pair.sign_raw(&data));

        tracing::debug!(
            target: tracing_targets::VALIDATOR,
            %block_id,
            my_peer_id = %self.inner.peer_id,
            ?data,
            ?own_signature,
            "own signature created",
        );

        // Pre-allocate the result map which will contain all validators' signatures (only valid ones)
        let validators = self.inner.state.validators.as_ref();
        let mut other_signatures =
            SignatureSlotsMap::with_capacity_and_hasher(validators.len(), Default::default());

        for peer_id in validators.keys() {
            other_signatures.insert(*peer_id, Default::default());
        }

        BlockSignaturesBuilder {
            own_signature,
            other_signatures,
            total_weight: self.inner.own_weight,
        }
    }

    async fn reuse_signatures(
        &self,
        block_id: &BlockId,
        cached: Arc<CachedSignatures>,
    ) -> BlockSignaturesBuilder {
        let data = Block::build_data_for_sign(block_id);
        let block_id = *block_id;

        let key_pair = self.inner.key_pair.clone();
        let my_peer_id = self.inner.peer_id;
        let validators = self.inner.state.validators.clone();
        let mut total_weight = self.inner.own_weight;

        let span = tracing::Span::current();
        tycho_util::sync::rayon_run(move || {
            let _span = span.enter();

            // Prepare our own signature
            let own_signature = Arc::new(key_pair.sign_raw(&data));

            tracing::debug!(
                target: tracing_targets::VALIDATOR,
                %my_peer_id,
                %block_id,
                ?data,
                ?own_signature,
                "own signature created",
            );

            // Pre-allocate the result map which will contain all validators' signatures (only valid ones)
            let mut other_signatures = SignatureSlotsMap::with_capacity_and_hasher(
                cached.other_signatures.len(),
                Default::default(),
            );

            for (peer_id, cached) in cached.other_signatures.iter() {
                let stored = 'stored: {
                    let Some(signature) = cached.load_full() else {
                        break 'stored Default::default();
                    };

                    let validator_info = validators.get(peer_id).expect("peer info out of sync");
                    if !validator_info.public_key.verify_raw(&data, &signature) {
                        tracing::warn!(
                            target: tracing_targets::VALIDATOR,
                            %my_peer_id,
                            %peer_id,
                            public_key = %validator_info.public_key,
                            %block_id,
                            ?data,
                            cached_signature = ?signature,
                            "cached signature is invalid on reuse: {}",
                            ValidationError::InvalidSignature
                        );

                        metrics::counter!(METRIC_INVALID_SIGNATURES_CACHED_TOTAL).increment(1);

                        // TODO: Somehow mark that this validator sent an invalid signature?
                        break 'stored Default::default();
                    }

                    total_weight += validator_info.weight;
                    Some(signature)
                };

                other_signatures.insert(*peer_id, SignatureSlot::new(stored));
            }

            BlockSignaturesBuilder {
                own_signature,
                other_signatures,
                total_weight,
            }
        })
        .await
    }

    fn create_cache_slots(&self, from: u32, n: u32) {
        let inner = self.inner.as_ref();

        let to = from + n;
        let from = inner.min_seqno.load(Ordering::Acquire).max(from);

        if from >= to {
            return;
        }

        for seqno in from..to {
            let signatures = CachedSignatures::new(&inner.state.validators);
            _ = inner.state.cached_signatures.insert(seqno, signatures);
        }
    }
}

pub struct DebugLogValidatorSesssion<'a>(pub &'a ValidatorSession);
impl fmt::Debug for DebugLogValidatorSesssion<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ValidatorSession")
            .field("shard", &self.0.inner.state.shard_ident)
            .field("session_id", &self.0.inner.session_id)
            .field("public_key", &self.0.inner.key_pair.public_key)
            .field("peer_id", &self.0.inner.peer_id)
            .field("own_weight", &self.0.inner.own_weight)
            .field("weight_threshold", &self.0.inner.state.weight_threshold)
            .field("start_block_seqno", &self.0.inner.start_block_seqno)
            .field("min_seqno", &self.0.inner.min_seqno)
            .field("validators", &self.0.inner.state.validators)
            .finish()
    }
}

struct Inner {
    start_block_seqno: u32,
    session_id: ValidationSessionId,
    config: ValidatorStdImplConfig,
    client: ValidatorClient,
    key_pair: Arc<KeyPair>,
    peer_id: PeerId,
    own_weight: u64,
    state: Arc<SessionState>,
    min_seqno: AtomicU32,
}

impl Inner {
    async fn receive_signature(
        self: Arc<Self>,
        peer_id: PeerId,
        block_signatures: Arc<BlockSignatures>,
        cached: Option<Arc<[u8; 64]>>,
        semaphore: Arc<Semaphore>,
    ) -> ValidSignature {
        let _histogram = HistogramGuard::begin(METRIC_RECEIVE_SIGNATURE_TIME);

        let block_seqno = block_signatures.block_id.seqno;
        let req = (self.client).request_from_tl(proto::rpc::ExchangeSignaturesRef {
            block_seqno,
            signature: block_signatures.own_signature.as_ref(),
        });

        let slot = block_signatures
            .other_signatures
            .get(&peer_id)
            .expect("peer info out of sync");

        let state = self.state.as_ref();

        // Try to use the cached signature first
        if let Some(signature) = cached {
            match state.add_signature(&block_signatures, slot, &peer_id, &signature) {
                Ok(()) => return ValidSignature { peer_id, signature },
                Err(ValidationError::SignatureChanged) => {
                    if let Some(signature) = slot.load_full() {
                        return ValidSignature { peer_id, signature };
                    }
                }
                Err(e) => {
                    tracing::warn!(
                        target: tracing_targets::VALIDATOR,
                        %peer_id,
                        "cached signature is invalid on add_signature: {e:?}",
                    );
                }
            }
        }

        let backoff = &self.config.exchange_signatures_backoff;
        let signature = loop {
            let mut retry = backon::ExponentialBuilder::default()
                .with_min_delay(backoff.min_interval)
                .with_max_delay(backoff.max_interval)
                .with_factor(backoff.factor)
                .with_max_times(usize::MAX)
                .build();

            let signature_fut = slot.into_future();
            let recv_fut = async {
                loop {
                    let permit = semaphore.acquire().await.unwrap();

                    let timeout = self.config.exchange_signatures_timeout;
                    let query = {
                        let _histogram = HistogramGuard::begin(METRIC_EXCHANGE_SIGNATURE_TIME);
                        self.client.query(&peer_id, req.clone())
                    };

                    match tokio::time::timeout(timeout, query).await {
                        Ok(Ok(res)) => match res.parse_tl() {
                            Ok(proto::Exchange::Complete(signature)) => break signature,
                            Ok(proto::Exchange::Cached) => {
                                tracing::trace!(
                                    target: tracing_targets::VALIDATOR,
                                    "partial signature exchange",
                                );
                            }
                            Err(e) => {
                                tracing::trace!(
                                    target: tracing_targets::VALIDATOR,
                                    "failed to parse response: {e:?}",
                                );
                            }
                        },
                        Ok(Err(e)) => tracing::trace!(
                            target: tracing_targets::VALIDATOR,
                            "query failed: {e:?}",
                        ),
                        Err(_) => tracing::trace!(
                            target: tracing_targets::VALIDATOR,
                            "query timed out",
                        ),
                    }
                    drop(permit);

                    let delay = retry.next().unwrap();
                    tokio::time::sleep(delay).await;
                }
            };

            let signature = tokio::select! {
                signature = signature_fut => signature,
                signature = recv_fut => signature,
            };

            match state.add_signature(&block_signatures, slot, &peer_id, &signature) {
                Ok(()) => break signature,
                Err(ValidationError::SignatureChanged) => {
                    if let Some(signature) = slot.load_full() {
                        break signature;
                    }
                }
                Err(e) => {
                    tracing::warn!(
                        target: tracing_targets::VALIDATOR,
                        %peer_id,
                        "fetched signature is invalid: {e:?}",
                    );
                }
            }

            // NOTE: Outer interval is used for retries in case of invalid signatures.
            // It will keep this future alive until we either receive enough signatures
            // from other validators or this one finally return a valid signature.
            tokio::time::sleep(self.config.failed_exchange_interval).await;
        };

        ValidSignature { peer_id, signature }
    }
}

impl Drop for Inner {
    fn drop(&mut self) {
        metrics::gauge!(METRIC_SESSIONS_ACTIVE).decrement(1);

        tracing::debug!(
            target: tracing_targets::VALIDATOR,
            shard_ident = %self.state.shard_ident,
            session_id = ?self.session_id,
            "validator session dropped"
        );
    }
}

struct SessionState {
    shard_ident: ShardIdent,
    weight_threshold: u64,
    validators: Arc<FastHashMap<PeerId, BriefValidatorDescr>>,
    block_signatures: TreeIndex<u32, Arc<BlockSignatures>>,
    cached_signatures: TreeIndex<u32, Arc<CachedSignatures>>,
    cancelled: AtomicBool,
    cancelled_signal: Notify,
}

impl SessionState {
    fn add_signature(
        &self,
        block: &BlockSignatures,
        slot: &SignatureSlot,
        peer_id: &PeerId,
        signature: &Arc<[u8; 64]>,
    ) -> Result<(), ValidationError> {
        match &*slot.load() {
            Some(saved) if saved.as_ref() == signature.as_ref() => return Ok(()),
            Some(_) => return Err(ValidationError::SignatureChanged),
            // NOTE: Do nothing here to drop the `arc_swap::Guard` before the signature check
            None => {}
        }

        let data = Block::build_data_for_sign(&block.block_id);

        let validator_info = self.validators.get(peer_id).expect("peer info out of sync");
        if !validator_info
            .public_key
            .verify_raw(&data, signature.as_ref())
        {
            tracing::warn!(
                target: tracing_targets::VALIDATOR,
                %peer_id,
                public_key = %validator_info.public_key,
                block_id = %block.block_id,
                ?data,
                ?signature,
                "signature is invalid",
            );
            // TODO: Store that the signature is invalid to avoid further checks on retries
            // TODO: Collect statistics on invalid signatures to slash the malicious validator
            metrics::counter!(METRIC_INVALID_SIGNATURES_IN_TOTAL).increment(1);
            return Err(ValidationError::InvalidSignature);
        }

        let mut can_notify = false;
        match &*slot.compare_and_swap(&None::<Arc<[u8; 64]>>, Some(signature.clone())) {
            None => {
                slot.notify();

                // Update the total weight of the block if the signature is new
                let total_weight = block
                    .total_weight
                    .fetch_add(validator_info.weight, Ordering::Relaxed)
                    + validator_info.weight;

                can_notify = total_weight >= self.weight_threshold;
            }
            Some(saved) => {
                if saved.as_ref() != signature.as_ref() {
                    // NOTE: Ensure that other query from the same peer does
                    // not have a different signature
                    return Err(ValidationError::SignatureChanged);
                }
            }
        }

        if can_notify {
            block.validated.store(true, Ordering::Release);
        }

        Ok(())
    }
}

struct ValidSignature {
    peer_id: PeerId,
    signature: Arc<[u8; 64]>,
}

struct BlockSignaturesBuilder {
    own_signature: Arc<[u8; 64]>,
    other_signatures: SignatureSlotsMap,
    total_weight: u64,
}

impl BlockSignaturesBuilder {
    fn build(self, block_id: &BlockId, weight_threshold: u64) -> Arc<BlockSignatures> {
        metrics::gauge!(METRIC_BLOCK_SLOTS).increment(1);

        Arc::new(BlockSignatures {
            block_id: *block_id,
            own_signature: self.own_signature,
            other_signatures: self.other_signatures,
            total_weight: AtomicU64::new(self.total_weight),
            validated: AtomicBool::new(self.total_weight >= weight_threshold),
            cancelled: CancellationToken::new(),
        })
    }
}

struct BlockSignatures {
    block_id: BlockId,
    own_signature: Arc<[u8; 64]>,
    other_signatures: SignatureSlotsMap,
    total_weight: AtomicU64,
    validated: AtomicBool,
    cancelled: CancellationToken,
}

impl Drop for BlockSignatures {
    fn drop(&mut self) {
        metrics::gauge!(METRIC_BLOCK_SLOTS).decrement(1);
    }
}

struct CachedSignatures {
    other_signatures: FastHashMap<PeerId, ArcSwapOption<[u8; 64]>>,
}

impl CachedSignatures {
    fn new(validators: &FastHashMap<PeerId, BriefValidatorDescr>) -> Arc<Self> {
        let signatures = validators
            .keys()
            .map(|peer_id| (*peer_id, Default::default()))
            .collect();

        metrics::gauge!(METRIC_CACHE_SLOTS).increment(1);

        Arc::new(Self {
            other_signatures: signatures,
        })
    }
}

impl Drop for CachedSignatures {
    fn drop(&mut self) {
        metrics::gauge!(METRIC_CACHE_SLOTS).decrement(1);
    }
}

type SignatureSlotsMap = FastHashMap<PeerId, SignatureSlot>;

impl ExchangeSignatures for SessionState {
    type Err = ValidationError;

    async fn exchange_signatures(
        &self,
        peer_id: &PeerId,
        block_seqno: u32,
        signature: Arc<[u8; 64]>,
    ) -> Result<proto::Exchange, Self::Err> {
        if self.cancelled.load(Ordering::Acquire) {
            return Err(ValidationError::Cancelled);
        }

        let guard = scc::ebr::Guard::new();

        // Full signature exchange if we know the block.
        // Otherwise, cache the signature for the block to use it later.
        //
        // NOTE: scc's `peek` does not lock the tree
        let result = if let Some(signatures) = self.block_signatures.peek(&block_seqno, &guard) {
            metrics::counter!(METRIC_BLOCK_EXCHANGES_IN_TOTAL).increment(1);

            let Some(slot) = signatures.other_signatures.get(peer_id) else {
                return Err(ValidationError::UnknownPeer);
            };

            // If more signatures are still needed, validate and store new to the block
            if !signatures.validated.load(Ordering::Acquire) {
                self.add_signature(signatures, slot, peer_id, &signature)?;
            }

            proto::Exchange::Complete(signatures.own_signature.clone())
        } else {
            // Find the slot for the specified block seqno.
            let Some(slot) = self.cached_signatures.peek(&block_seqno, &guard) else {
                metrics::counter!(METRIC_MISS_EXCHANGES_IN_TOTAL).increment(1);
                return Err(ValidationError::NoSlot);
            };
            metrics::counter!(METRIC_CACHE_EXCHANGES_IN_TOTAL).increment(1);

            let Some(saved_signature) = slot.other_signatures.get(peer_id) else {
                return Err(ValidationError::UnknownPeer);
            };

            // Store the signature in the cache (allow to overwrite the signature)
            saved_signature.store(Some(signature.clone()));

            proto::Exchange::Cached
        };

        drop(guard);

        let action = match &result {
            proto::Exchange::Complete(_) => "complete",
            proto::Exchange::Cached => "cached",
        };

        tracing::trace!(
            target: tracing_targets::VALIDATOR,
            %peer_id,
            block_seqno,
            action,
            "exchanged signatures"
        );

        Ok(result)
    }
}

#[derive(Default)]
struct SignatureSlot {
    value: ArcSwapOption<[u8; 64]>,
    waker: parking_lot::Mutex<Option<Waker>>,
}

impl SignatureSlot {
    fn new(value: Option<Arc<[u8; 64]>>) -> Self {
        Self {
            value: ArcSwapOption::new(value),
            waker: parking_lot::Mutex::new(None),
        }
    }

    fn notify(&self) {
        if let Some(waker) = self.waker.lock().take() {
            waker.wake();
        }
    }
}

impl std::ops::Deref for SignatureSlot {
    type Target = ArcSwapOption<[u8; 64]>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<'a> IntoFuture for &'a SignatureSlot {
    type Output = Arc<[u8; 64]>;
    type IntoFuture = SignatureFuture<'a>;

    #[inline]
    fn into_future(self) -> Self::IntoFuture {
        SignatureFuture(self)
    }
}

struct SignatureFuture<'a>(&'a SignatureSlot);

impl Future for SignatureFuture<'_> {
    type Output = Arc<[u8; 64]>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // Fast path
        if let Some(value) = self.0.value.load_full() {
            return Poll::Ready(value);
        }

        let mut waker = self.0.waker.lock();

        // Double check while holding the lock
        if let Some(value) = self.0.value.load_full() {
            return Poll::Ready(value);
        }

        match &mut *waker {
            Some(waker) => {
                waker.clone_from(cx.waker());
            }
            _ => *waker = Some(cx.waker().clone()),
        }

        Poll::Pending
    }
}

fn compute_session_overlay_id(
    zerostate_id: &BlockId,
    shard_ident: &ShardIdent,
    session_id: &ValidationSessionId,
) -> OverlayId {
    OverlayId(tl_proto::hash(proto::OverlayIdData {
        zerostate_root_hash: zerostate_id.root_hash.0,
        zerostate_file_hash: zerostate_id.file_hash.0,
        shard_ident: *shard_ident,
        session_seqno: session_id.seqno(),
    }))
}

#[derive(Debug, thiserror::Error)]
enum ValidationError {
    #[error("session is cancelled")]
    Cancelled,
    #[error("no slot available for the specified seqno")]
    NoSlot,
    #[error("peer is not a known validator")]
    UnknownPeer,
    #[error("invalid signature")]
    InvalidSignature,
    #[error("signature has changed since the last exchange")]
    SignatureChanged,
}