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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    driver::{DriverConfiguration, StateSyncDriver},
    driver_client::{ClientNotificationListener, DriverClient, DriverNotification},
    notification_handlers::{
        CommitNotificationListener, ConsensusNotificationHandler, ErrorNotificationListener,
        MempoolNotificationHandler,
    },
    storage_synchronizer::StorageSynchronizer,
};
use consensus_notifications::ConsensusNotificationListener;
use data_streaming_service::streaming_client::StreamingServiceClient;
use diem_config::config::NodeConfig;
use diem_data_client::diemnet::DiemNetDataClient;
use diem_types::waypoint::Waypoint;
use event_notifications::EventSubscriptionService;
use executor_types::ChunkExecutorTrait;
use futures::channel::mpsc;
use mempool_notifications::MempoolNotificationSender;
use std::sync::Arc;
use storage_interface::DbReader;
use tokio::runtime::{Builder, Runtime};

/// Creates a new state sync driver and client
pub struct DriverFactory {
    client_notification_sender: mpsc::UnboundedSender<DriverNotification>,
    _driver_runtime: Option<Runtime>,
}

impl DriverFactory {
    /// Creates and spawns a new state sync driver
    pub fn create_and_spawn_driver<
        ChunkExecutor: ChunkExecutorTrait + 'static,
        MempoolNotifier: MempoolNotificationSender + 'static,
    >(
        create_runtime: bool,
        node_config: &NodeConfig,
        waypoint: Waypoint,
        storage: Arc<dyn DbReader>,
        chunk_executor: Arc<ChunkExecutor>,
        mempool_notification_sender: MempoolNotifier,
        consensus_listener: ConsensusNotificationListener,
        event_subscription_service: EventSubscriptionService,
        diem_data_client: DiemNetDataClient,
        streaming_service_client: StreamingServiceClient,
    ) -> Self {
        // Create the notification handlers
        let (client_notification_sender, client_notification_receiver) = mpsc::unbounded();
        let client_notification_listener =
            ClientNotificationListener::new(client_notification_receiver);
        let (commit_notification_sender, commit_notification_listener) =
            CommitNotificationListener::new();
        let consensus_notification_handler = ConsensusNotificationHandler::new(consensus_listener);
        let (error_notification_sender, error_notification_listener) =
            ErrorNotificationListener::new();
        let mempool_notification_handler =
            MempoolNotificationHandler::new(mempool_notification_sender);

        // Create a new runtime (if required)
        let driver_runtime = if create_runtime {
            Some(
                Builder::new_multi_thread()
                    .thread_name("state-sync-driver")
                    .enable_all()
                    .build()
                    .expect("Failed to create state sync v2 driver runtime!"),
            )
        } else {
            None
        };

        // Create the storage synchronizer
        let storage_synchronizer = StorageSynchronizer::new(
            chunk_executor,
            commit_notification_sender,
            error_notification_sender,
            driver_runtime.as_ref(),
        );

        // Create the driver configuration
        let driver_configuration = DriverConfiguration::new(
            node_config.state_sync.state_sync_driver,
            node_config.base.role,
            waypoint,
        );

        // Create the state sync driver
        let state_sync_driver = StateSyncDriver::new(
            client_notification_listener,
            commit_notification_listener,
            consensus_notification_handler,
            driver_configuration,
            error_notification_listener,
            event_subscription_service,
            mempool_notification_handler,
            storage_synchronizer,
            diem_data_client,
            streaming_service_client,
            storage,
        );

        // Spawn the driver
        if let Some(driver_runtime) = &driver_runtime {
            driver_runtime.spawn(state_sync_driver.start_driver());
        } else {
            tokio::spawn(state_sync_driver.start_driver());
        }

        Self {
            client_notification_sender,
            _driver_runtime: driver_runtime,
        }
    }

    /// Returns a new client that can be used to communicate with the driver
    pub fn create_driver_client(&self) -> DriverClient {
        DriverClient::new(self.client_notification_sender.clone())
    }
}