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
#![forbid(unsafe_code)]
use consensus_notifications::ConsensusNotificationListener;
use data_streaming_service::streaming_client::StreamingServiceClient;
use diem_config::{config::NodeConfig, network_id::NetworkId};
use diem_data_client::diemnet::DiemNetDataClient;
use diem_types::{move_resource::MoveStorage, waypoint::Waypoint};
use event_notifications::{EventNotificationSender, EventSubscriptionService};
use executor_types::ChunkExecutorTrait;
use futures::executor::block_on;
use mempool_notifications::MempoolNotificationSender;
use network::protocols::network::AppConfig;
use state_sync_driver::driver_factory::DriverFactory;
use state_sync_v1::{
bootstrapper::StateSyncBootstrapper,
network::{StateSyncEvents, StateSyncSender},
};
use std::sync::Arc;
use storage_interface::DbReader;
use tokio::runtime::Runtime;
pub struct StateSyncRuntimes {
_diem_data_client: Runtime,
state_sync: StateSyncMultiplexer,
_storage_service: Runtime,
_streaming_service: Runtime,
}
impl StateSyncRuntimes {
pub fn new(
diem_data_client: Runtime,
state_sync: StateSyncMultiplexer,
storage_service: Runtime,
streaming_service: Runtime,
) -> Self {
Self {
_diem_data_client: diem_data_client,
state_sync,
_storage_service: storage_service,
_streaming_service: streaming_service,
}
}
pub fn block_until_initialized(&self) {
self.state_sync.block_until_initialized()
}
}
pub struct StateSyncMultiplexer {
activate_state_sync_v2: bool,
state_sync_v1: Option<StateSyncBootstrapper>,
state_sync_v2: Option<DriverFactory>,
}
impl StateSyncMultiplexer {
pub fn new<
ChunkExecutor: ChunkExecutorTrait + 'static,
MempoolNotifier: MempoolNotificationSender + 'static,
>(
network: Vec<(NetworkId, StateSyncSender, StateSyncEvents)>,
mempool_notifier: MempoolNotifier,
consensus_listener: ConsensusNotificationListener,
storage: Arc<dyn DbReader>,
chunk_executor: Arc<ChunkExecutor>,
node_config: &NodeConfig,
waypoint: Waypoint,
mut event_subscription_service: EventSubscriptionService,
diem_data_client: DiemNetDataClient,
streaming_service_client: StreamingServiceClient,
) -> Self {
match (&*storage).fetch_synced_version() {
Ok(synced_version) => {
if let Err(error) =
event_subscription_service.notify_initial_configs(synced_version)
{
panic!(
"Failed to notify subscribers of initial on-chain configs: {:?}",
error
)
}
}
Err(error) => panic!("Failed to fetch the initial synced version: {:?}", error),
}
let mut state_sync_v1 = None;
let mut state_sync_v2 = None;
let activate_state_sync_v2 = node_config
.state_sync
.state_sync_driver
.enable_state_sync_v2;
if activate_state_sync_v2 {
state_sync_v2 = Some(DriverFactory::create_and_spawn_driver(
true,
node_config,
waypoint,
storage,
chunk_executor,
mempool_notifier,
consensus_listener,
event_subscription_service,
diem_data_client,
streaming_service_client,
));
} else {
state_sync_v1 = Some(StateSyncBootstrapper::bootstrap(
network,
mempool_notifier,
consensus_listener,
storage,
chunk_executor,
node_config,
waypoint,
event_subscription_service,
false,
));
}
Self {
activate_state_sync_v2,
state_sync_v1,
state_sync_v2,
}
}
pub fn block_until_initialized(&self) {
if self.activate_state_sync_v2 {
let state_sync_v2_client = self
.state_sync_v2
.as_ref()
.expect("State sync v2 is not running!")
.create_driver_client();
block_on(state_sync_v2_client.notify_once_bootstrapped())
.expect("State sync v2 initialization failure");
} else {
let state_sync_v1_client = self
.state_sync_v1
.as_ref()
.expect("State sync v1 is not running!")
.create_client();
block_on(state_sync_v1_client.wait_until_initialized())
.expect("State sync v1 initialization failure");
}
}
}
pub fn state_sync_v1_network_config() -> AppConfig {
state_sync_v1::network::network_endpoint_config()
}
#[cfg(any(test, feature = "fuzzing"))]
mod tests {
use crate::StateSyncMultiplexer;
use consensus_notifications::new_consensus_notifier_listener_pair;
use data_streaming_service::streaming_client::new_streaming_service_client_listener_pair;
use diem_config::{config::RocksdbConfig, utils::get_genesis_txn};
use diem_crypto::HashValue;
use diem_data_client::diemnet::DiemNetDataClient;
use diem_genesis_tool::test_config;
use diem_infallible::RwLock;
use diem_temppath::TempPath;
use diem_time_service::TimeService;
use diem_types::{
block_info::BlockInfo, ledger_info::LedgerInfo, on_chain_config::ON_CHAIN_CONFIG_REGISTRY,
waypoint::Waypoint,
};
use diem_vm::DiemVM;
use diemdb::DiemDB;
use event_notifications::EventSubscriptionService;
use executor::chunk_executor::ChunkExecutor;
use executor_test_helpers::bootstrap_genesis;
use futures::{FutureExt, StreamExt};
use mempool_notifications::new_mempool_notifier_listener_pair;
use network::application::{interface::MultiNetworkSender, storage::PeerMetadataStorage};
use std::{collections::HashMap, sync::Arc};
use storage_interface::DbReaderWriter;
use storage_service_client::StorageServiceClient;
#[test]
fn test_new_initialized_configs() {
let tmp_dir = TempPath::new();
let db = DiemDB::open(&tmp_dir, false, None, RocksdbConfig::default(), true).unwrap();
let (_, db_rw) = DbReaderWriter::wrap(db);
let (node_config, _) = test_config();
bootstrap_genesis::<DiemVM>(&db_rw, get_genesis_txn(&node_config).unwrap()).unwrap();
let (mempool_notifier, _) = new_mempool_notifier_listener_pair();
let (_, consensus_listener) = new_consensus_notifier_listener_pair(0);
let mut event_subscription_service = EventSubscriptionService::new(
ON_CHAIN_CONFIG_REGISTRY,
Arc::new(RwLock::new(db_rw.clone())),
);
let mut reconfiguration_subscriber = event_subscription_service
.subscribe_to_reconfigurations()
.unwrap();
let (streaming_service_client, _) = new_streaming_service_client_listener_pair();
let network_client = StorageServiceClient::new(
MultiNetworkSender::new(HashMap::new()),
PeerMetadataStorage::new(&[]),
);
let (diem_data_client, _) = DiemNetDataClient::new(
node_config.state_sync.diem_data_client,
node_config.state_sync.storage_service,
TimeService::mock(),
network_client,
);
let _ = StateSyncMultiplexer::new(
vec![],
mempool_notifier,
consensus_listener,
db_rw.reader.clone(),
Arc::new(ChunkExecutor::<DiemVM>::new(db_rw).unwrap()),
&node_config,
Waypoint::new_any(&LedgerInfo::new(BlockInfo::empty(), HashValue::random())),
event_subscription_service,
diem_data_client,
streaming_service_client,
);
assert!(reconfiguration_subscriber
.select_next_some()
.now_or_never()
.is_some());
}
}