chronicle/
lib.rs

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
use crate::admin::AdminMsg;
use crate::network::{create_iroh_network, NetworkConfig};
use crate::runtime::Runtime;
use crate::shards::{TimeWorker, TimeWorkerParams};
use crate::tasks::TaskParams;
use anyhow::Result;
use futures::channel::mpsc;
use futures::{SinkExt, StreamExt};
use gmp::Backend;
use scale_codec::Decode;
use std::path::PathBuf;
use std::sync::Arc;
use time_primitives::admin::Config;
use time_primitives::NetworkId;
use tracing::{span, Level};

use opentelemetry::{trace::TracerProvider as _, KeyValue};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{
	trace::{RandomIdGenerator, Sampler, SdkTracerProvider},
	Resource,
};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};

pub mod admin;
#[cfg(test)]
mod mock;
mod network;
mod runtime;
mod shards;
mod tasks;

// Sets the attributes for tracing
fn resource() -> Resource {
	Resource::builder()
		.with_schema_url(
			[KeyValue::new("service.name", "chronicle"), KeyValue::new("service.version", "v1.0")],
			"https://opentelemetry.io/schemas/1.30.0",
		)
		.build()
}

// Initialize tracing-subscriber and return OtelGuard for opentelemetry-related termination processing
pub fn init_opentelemetry() {
	let log_subscriber = json_subscriber::fmt::layer()
		.flatten_event(true)
		.flatten_current_span_on_top_level(true)
		.flatten_span_list_on_top_level(true)
		.with_file(true)
		.with_line_number(true);
	let filter_layer = EnvFilter::try_from_default_env()
		.or_else(|_| EnvFilter::try_new("debug"))
		.unwrap();

	// Skip initializing OTLP if endpoint isn't given
	if let Ok(endpoint) = std::env::var("TRACING_ENDPOINT") {
		let exporter = opentelemetry_otlp::SpanExporter::builder()
			.with_tonic()
			.with_endpoint(endpoint)
			.build()
			.unwrap();

		let tracer_provider = SdkTracerProvider::builder()
			.with_sampler(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(1.0))))
			.with_id_generator(RandomIdGenerator::default())
			.with_resource(resource())
			.with_batch_exporter(exporter)
			.build();

		let tracer = tracer_provider.tracer("tracing-otel-subscriber");
		tracing_subscriber::registry()
			.with(filter_layer)
			.with(log_subscriber)
			.with(OpenTelemetryLayer::new(tracer))
			.init();
	} else {
		let filter = EnvFilter::from_default_env()
			.add_directive("chronicle=debug".parse().unwrap())
			.add_directive("tss=debug".parse().unwrap())
			.add_directive("peernet=debug".parse().unwrap());
		tracing_subscriber::registry()
			.with(filter_layer)
			.with(log_subscriber)
			.with(filter)
			.try_init()
			.ok();
	}
	std::panic::set_hook(Box::new(tracing_panic::panic_hook));
}

/// Configuration structure for the Chronicle application.
pub struct ChronicleConfig {
	/// Identifier for the network.
	pub network_id: NetworkId,
	/// Network private key.
	pub network_key: [u8; 32],
	/// URL for the target.
	pub target_url: String,
	/// Path to a target key file.
	pub target_mnemonic: String,
	/// Path to a cache for TSS key shares.
	pub tss_keyshare_cache: PathBuf,
	/// Backend
	pub backend: Backend,
}

/// Runs the Chronicle application.
///
/// This function initializes the necessary components and starts the main
/// application loop for the Chronicle service. It sets up the network, task
/// spawner, and various workers needed to process tasks and communicate with
/// the blockchain.
///
/// # Arguments
///
/// * `config` - Configuration for the Chronicle application.
/// * `network` - Network instance.
/// * `net_request` - Stream of network requests.
/// * `substrate` - Substrate runtime instance.
///
/// # Returns
///
/// * `Result<()>` - Returns an empty result on success, or an error on failure.
pub async fn run_chronicle(
	config: ChronicleConfig,
	substrate: Arc<dyn Runtime>,
	mut admin: mpsc::Sender<AdminMsg>,
) -> Result<()> {
	let span = tracing::span!(Level::INFO, "run_chronicle");
	let mut ticker = substrate.finality_notification_stream();
	// Initialize connector
	let chain = loop {
		let Some((hash, _)) = ticker.next().await else { continue };
		let name = substrate.network(config.network_id, hash).await?;
		if let Some(name) = name {
			break String::decode(&mut name.0.to_vec().as_slice()).unwrap_or_default();
		}
		tracing::warn!(parent: &span, "network {} isn't registered", config.network_id);
	};
	tracing::info!(parent: &span, "joining network {chain}");

	let (tss_tx, tss_rx) = mpsc::channel(10);

	let chain = config.backend.chain(config.network_id, &config.target_mnemonic)?;

	let connector = loop {
		match chain.connect(config.target_url.clone()).await {
			Ok(connector) => break connector,
			Err(error) => {
				tracing::info!(
					parent: &span,
					"Initializing connector returned an error {:?}, retrying in one second",
					error
				);
				tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
			},
		}
	};

	// initialize networking
	let (network, network_requests) =
		create_iroh_network(NetworkConfig { secret: config.network_key }, &span).await?;

	// initialize wallets
	let account = time_primitives::format_address(substrate.account_id());
	let address = connector.chain().format_address(connector.chain().address());
	let peer_id = network.format_peer_id(network.peer_id());
	let span = span!(
		parent: &span,
		Level::INFO,
		"chronicle",
		tc_account = account,
		chain_address = address,
		gmp_network_id = config.network_id,
		net_peer_id = peer_id,
	);
	admin
		.send(AdminMsg::SetConfig(Config {
			network: config.network_id,
			account,
			address,
			peer_id,
			peer_id_hex: hex::encode(network.peer_id()),
		}))
		.await?;
	loop {
		let Some((hash, _)) = ticker.next().await else { continue };
		if substrate.is_registered(hash).await? {
			break;
		}
		tracing::warn!(parent: &span, "chronicle isn't registered");
	}

	let task_params = TaskParams::new(substrate.clone(), connector, tss_tx, admin.clone());
	let time_worker = TimeWorker::new(TimeWorkerParams {
		network,
		task_params,
		substrate,
		tss_request: tss_rx,
		net_request: network_requests,
		tss_keyshare_cache: config.tss_keyshare_cache,
		admin_request: admin.clone(),
	});
	time_worker.run(&span).await;
	Ok(())
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::mock::Mock;
	use futures::{Future, FutureExt, StreamExt};
	use polkadot_sdk::sp_runtime::BoundedVec;
	use scale_codec::Encode;
	use std::time::Duration;
	use time_primitives::{AccountId, BlockHash, ChainName, ShardStatus, Task};

	/// Asynchronous test helper to run Chronicle.
	///
	/// This function sets up a mock network and runs the Chronicle application
	/// for testing purposes.
	///
	/// # Arguments
	///
	/// * `mock` - Mock instance for testing.
	/// * `network_id` - Identifier for the network.
	async fn chronicle(mock: Mock, network_id: NetworkId, exit: impl Future<Output = ()> + Unpin) {
		tracing::info!("running chronicle");
		let network_key = *mock.account_id().as_ref();
		let (tx, mut rx) = mpsc::channel(10);
		let root = if std::env::var("CI").is_ok() { "." } else { "/tmp" };
		let tss_keyshare_cache = format!("{root}/chronicles/{}", hex::encode(network_key)).into();
		std::fs::create_dir_all(&tss_keyshare_cache).unwrap();
		let handle = tokio::task::spawn(run_chronicle(
			ChronicleConfig {
				network_id,
				network_key,
				target_url: "tempfile".to_string(),
				target_mnemonic: "mnemonic".into(),
				tss_keyshare_cache,
				backend: Backend::Rust,
			},
			Arc::new(mock.clone()),
			tx,
		));

		tokio::spawn(async move {
			while let Some(msg) = rx.next().await {
				if let AdminMsg::SetConfig(config) = msg {
					tracing::info!("received chronicle config");
					mock.register_member(
						network_id,
						config.account.parse().unwrap(),
						hex::decode(&config.peer_id_hex).unwrap().try_into().unwrap(),
					);
					tracing::info!("registered chronicle");
				}
			}
		});
		tracing::info!("registered chronicle");
		futures::future::select(handle, exit).await;
	}

	/// Smoke test for the Chronicle application.
	///
	/// This test initializes the logger, sets up a mock network, and runs the
	/// Chronicle application in multiple threads to ensure basic functionality.
	///
	/// # Returns
	///
	/// * `Result<()>` - Returns an empty result on success, or an error on failure.
	#[tokio::test]
	async fn chronicle_smoke() -> Result<()> {
		let (n, t) = (3, 3);
		init_opentelemetry();

		let mock = Mock::default().instance(42);
		let block: BlockHash = BlockHash::from([0u8; 32]);
		let network_id = mock.create_network(ChainName(BoundedVec::truncate_from("rust".encode())));
		// Spawn multiple threads to run the Chronicle application.
		for id in 0..n {
			let instance = mock.instance(id as u8);
			std::thread::spawn(move || {
				let rt = tokio::runtime::Runtime::new().unwrap();
				rt.block_on(chronicle(instance, network_id, futures::future::pending::<()>()));
			});
		}
		// Wait for members to register.
		loop {
			tracing::info!("waiting for members to register");
			if mock.members(network_id).len() < n {
				tokio::time::sleep(Duration::from_secs(1)).await;
				continue;
			}
			break;
		}
		// Collect member accounts.
		let members: Vec<AccountId> =
			mock.members(network_id).into_iter().map(|(public, _)| public).collect();
		// Create a shard.
		let shard_id = mock.create_shard(members.clone(), t);
		// Wait for the shard to be online.
		loop {
			tracing::info!("waiting for shard");
			if mock.shard_status(shard_id, block).await.unwrap() != ShardStatus::Online {
				tokio::time::sleep(Duration::from_secs(1)).await;
				continue;
			}
			break;
		}

		tracing::info!("creating task");
		// Create a task and assign it to the shard.
		let task_id = mock.create_task(Task::ReadGatewayEvents { blocks: 0..1 });
		tracing::info!("assigning task {task_id} {shard_id}");
		mock.assign_task(task_id, shard_id);
		// Wait for the task to complete.
		loop {
			tracing::info!("waiting for task {task_id}");
			let task = mock.task(task_id).unwrap();
			if task.result.is_none() {
				tokio::time::sleep(Duration::from_secs(10)).await;
				continue;
			}
			break;
		}
		Ok(())
	}

	#[tokio::test]
	async fn chronicle_restart() -> Result<()> {
		init_opentelemetry();

		let mock = Mock::default().instance(42);
		let block: BlockHash = BlockHash::from([0u8; 32]);
		let network_id = mock.create_network(ChainName(BoundedVec::truncate_from("rust".encode())));
		let mut shutdown = vec![];
		// Spawn multiple threads to run the Chronicle application.
		for id in 0..3 {
			let instance = mock.instance(id + 4);
			let (tx, rx) = futures::channel::oneshot::channel();
			shutdown.push(tx);
			std::thread::spawn(move || {
				let rt = tokio::runtime::Runtime::new().unwrap();
				rt.block_on(chronicle(instance, network_id, rx.map(|_| ())));
			});
		}
		// Wait for members to register.
		loop {
			tracing::info!("waiting for members to register");
			if mock.members(network_id).len() < 3 {
				tokio::time::sleep(Duration::from_secs(1)).await;
				continue;
			}
			break;
		}
		// Collect member accounts.
		let members: Vec<AccountId> =
			mock.members(network_id).into_iter().map(|(public, _)| public).collect();
		// Create a shard.
		let shard_id = mock.create_shard(members.clone(), 2);
		// Wait for the shard to be online.
		loop {
			tracing::info!("waiting for shard");
			if mock.shard_status(shard_id, block).await.unwrap() != ShardStatus::Online {
				tokio::time::sleep(Duration::from_secs(1)).await;
				continue;
			}
			break;
		}

		for tx in shutdown {
			tx.send(()).unwrap();
		}
		// Spawn multiple threads to run the Chronicle application.
		for id in 0..3 {
			let instance = mock.instance(id + 4);
			std::thread::spawn(move || {
				let rt = tokio::runtime::Runtime::new().unwrap();
				rt.block_on(chronicle(instance, network_id, futures::future::pending()));
			});
		}

		tracing::info!("creating task");
		// Create a task and assign it to the shard.
		let task_id = mock.create_task(Task::ReadGatewayEvents { blocks: 0..1 });
		tracing::info!("assigning task");
		mock.assign_task(task_id, shard_id);
		// Wait for the task to complete.
		loop {
			tracing::info!("waiting for task");
			let task = mock.task(task_id).unwrap();
			if task.result.is_none() {
				tokio::time::sleep(Duration::from_secs(1)).await;
				continue;
			}
			break;
		}
		Ok(())
	}
}