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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use std::sync::Arc;
use sui_types::sui_system_state::{SuiSystemState, SuiSystemStateTrait};
use tokio::sync::broadcast::error::RecvError;
use tracing::{info, warn};

use crate::{
    authority_aggregator::{AuthAggMetrics, AuthorityAggregator},
    authority_client::{AuthorityAPI, NetworkAuthorityClient},
    epoch::committee_store::CommitteeStore,
    execution_cache::ExecutionCacheRead,
    safe_client::SafeClientMetricsBase,
};

use super::QuorumDriver;

#[async_trait]
pub trait ReconfigObserver<A: Clone> {
    async fn run(&mut self, quorum_driver: Arc<QuorumDriver<A>>);
    fn clone_boxed(&self) -> Box<dyn ReconfigObserver<A> + Send + Sync>;
}

/// A ReconfigObserver that subscribes to a reconfig channel of new committee.
/// This is used in TransactionOrchestrator.
pub struct OnsiteReconfigObserver {
    reconfig_rx: tokio::sync::broadcast::Receiver<SuiSystemState>,
    execution_cache: Arc<dyn ExecutionCacheRead>,
    committee_store: Arc<CommitteeStore>,
    safe_client_metrics_base: SafeClientMetricsBase,
    auth_agg_metrics: AuthAggMetrics,
}

impl OnsiteReconfigObserver {
    pub fn new(
        reconfig_rx: tokio::sync::broadcast::Receiver<SuiSystemState>,
        execution_cache: Arc<dyn ExecutionCacheRead>,
        committee_store: Arc<CommitteeStore>,
        safe_client_metrics_base: SafeClientMetricsBase,
        auth_agg_metrics: AuthAggMetrics,
    ) -> Self {
        Self {
            reconfig_rx,
            execution_cache,
            committee_store,
            safe_client_metrics_base,
            auth_agg_metrics,
        }
    }

    async fn create_authority_aggregator_from_system_state(
        &self,
    ) -> AuthorityAggregator<NetworkAuthorityClient> {
        AuthorityAggregator::new_from_local_system_state(
            &self.execution_cache,
            &self.committee_store,
            self.safe_client_metrics_base.clone(),
            self.auth_agg_metrics.clone(),
        )
        .unwrap_or_else(|e| {
            panic!(
                "Failed to create AuthorityAggregator from System State: {:?}",
                e
            )
        })
    }
}

#[async_trait]
impl ReconfigObserver<NetworkAuthorityClient> for OnsiteReconfigObserver {
    fn clone_boxed(&self) -> Box<dyn ReconfigObserver<NetworkAuthorityClient> + Send + Sync> {
        Box::new(Self {
            reconfig_rx: self.reconfig_rx.resubscribe(),
            execution_cache: self.execution_cache.clone(),
            committee_store: self.committee_store.clone(),
            safe_client_metrics_base: self.safe_client_metrics_base.clone(),
            auth_agg_metrics: self.auth_agg_metrics.clone(),
        })
    }

    async fn run(&mut self, quorum_driver: Arc<QuorumDriver<NetworkAuthorityClient>>) {
        // A tiny optimization: when a very stale node just starts, the
        // channel may fill up committees quickly. Here we skip directly to
        // the last known committee by looking at SuiSystemState.
        let authority_agg = self.create_authority_aggregator_from_system_state().await;
        if authority_agg.committee.epoch > quorum_driver.current_epoch() {
            quorum_driver
                .update_validators(Arc::new(authority_agg))
                .await;
        }
        loop {
            match self.reconfig_rx.recv().await {
                Ok(system_state) => {
                    let committee = system_state.get_current_epoch_committee();
                    info!(
                        "Got reconfig message. New committee: {}",
                        committee.committee
                    );
                    if committee.epoch() > quorum_driver.current_epoch() {
                        let authority_agg =
                            self.create_authority_aggregator_from_system_state().await;
                        quorum_driver
                            .update_validators(Arc::new(authority_agg))
                            .await;
                    } else {
                        // This should only happen when the node just starts
                        warn!("Epoch number decreased - ignoring committee: {}", committee);
                    }
                }
                // It's ok to miss messages due to overflow here
                Err(RecvError::Lagged(_)) => {
                    continue;
                }
                Err(RecvError::Closed) => {
                    // Closing the channel only happens in simtest when a node is shut down.
                    if cfg!(msim) {
                        return;
                    } else {
                        panic!("Do not expect the channel to be closed")
                    }
                }
            }
        }
    }
}
/// A dummy ReconfigObserver for testing.
pub struct DummyReconfigObserver;

#[async_trait]
impl<A> ReconfigObserver<A> for DummyReconfigObserver
where
    A: AuthorityAPI + Send + Sync + Clone + 'static,
{
    fn clone_boxed(&self) -> Box<dyn ReconfigObserver<A> + Send + Sync> {
        Box::new(Self {})
    }

    async fn run(&mut self, _quorum_driver: Arc<QuorumDriver<A>>) {}
}