chronicle/shards/
service.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
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
use super::tss::{Tss, TssAction, TssPeerId, VerifiableSecretSharingCommitment};
use crate::admin::AdminMsg;
use crate::network::{Message, Network, PeerId, TssMessage};
use crate::runtime::Runtime;
use crate::tasks::{TaskExecutor, TaskParams};
use anyhow::Result;
use futures::future::join_all;
use futures::SinkExt;
use futures::{
	channel::{mpsc, oneshot},
	future::poll_fn,
	stream::FuturesUnordered,
	Future, FutureExt, Stream, StreamExt,
};
use polkadot_sdk::sp_runtime::BoundedVec;
use std::sync::Arc;
use std::{
	collections::{BTreeMap, BTreeSet, HashMap},
	path::PathBuf,
	pin::Pin,
	task::Poll,
};
use time_primitives::{
	BlockHash, BlockNumber, Commitment, ShardId, ShardStatus, TaskId, TssSignature,
	TssSigningRequest,
};
use tracing::{event, span, Level, Span};

const DEFAULT_HEARTBEAT_PERIOD: BlockNumber = 100;

pub struct TimeWorkerParams<Tx, Rx> {
	pub substrate: Arc<dyn Runtime>,
	pub task_params: TaskParams,
	pub network: Tx,
	pub tss_request: mpsc::Receiver<TssSigningRequest>,
	pub net_request: Rx,
	pub tss_keyshare_cache: PathBuf,
	pub admin_request: mpsc::Sender<AdminMsg>,
}

pub struct TimeWorker<Tx, Rx> {
	substrate: Arc<dyn Runtime>,
	network: Tx,
	tss_request: mpsc::Receiver<TssSigningRequest>,
	net_request: Rx,
	task_params: TaskParams,
	tss_states: HashMap<ShardId, Tss>,
	executor_states: HashMap<ShardId, TaskExecutor>,
	messages: BTreeMap<BlockNumber, Vec<(ShardId, PeerId, TssMessage)>>,
	requests: BTreeMap<BlockNumber, Vec<(ShardId, TaskId, Vec<u8>)>>,
	channels: HashMap<TaskId, oneshot::Sender<([u8; 32], TssSignature)>>,
	#[allow(clippy::type_complexity)]
	outgoing_requests:
		FuturesUnordered<Pin<Box<dyn Future<Output = (Result<()>, Span)> + Send + 'static>>>,
	tss_keyshare_cache: PathBuf,
	admin_request: mpsc::Sender<AdminMsg>,
}

fn display_peer_id(peer_id: PeerId) -> String {
	let Ok(peer_id) = TssPeerId::new(peer_id) else {
		return hex::encode(peer_id);
	};
	peer_id.to_string()
}

impl<Tx, Rx> TimeWorker<Tx, Rx>
where
	Tx: Network + Clone,
	Rx: Stream<Item = (PeerId, Message)> + Send + Unpin,
{
	pub fn new(worker_params: TimeWorkerParams<Tx, Rx>) -> Self {
		let TimeWorkerParams {
			substrate,
			task_params,
			network,
			tss_request,
			net_request,
			tss_keyshare_cache,
			admin_request,
		} = worker_params;
		Self {
			substrate,
			task_params,
			network,
			tss_request,
			net_request,
			tss_states: Default::default(),
			executor_states: Default::default(),
			messages: Default::default(),
			requests: Default::default(),
			channels: Default::default(),
			outgoing_requests: Default::default(),
			tss_keyshare_cache,
			admin_request,
		}
	}

	async fn on_finality(
		&mut self,
		span: &Span,
		block_hash: BlockHash,
		block: BlockNumber,
	) -> Result<()> {
		let span = span!(
			parent: span,
			Level::DEBUG,
			"on_finality",
			tc_block = block,
			tc_block_hash = format!("{block_hash:?}"),
		);
		event!(parent: &span, Level::DEBUG, "on_finality");
		let account_id = self.substrate.account_id();
		let shards = self.substrate.shards(account_id, block_hash).await?;
		self.tss_states.retain(|shard_id, _| shards.contains(shard_id));
		self.executor_states.retain(|shard_id, _| shards.contains(shard_id));
		for shard_id in shards.iter().copied() {
			if self.tss_states.contains_key(&shard_id) {
				continue;
			}
			let span = span!(parent: &span, Level::DEBUG, "joining", gmp_shard_id = shard_id);
			let members = self.substrate.shard_members(shard_id, block_hash).await?;
			let threshold = self.substrate.shard_threshold(shard_id, block_hash).await?;
			let futures: Vec<_> = members
				.into_iter()
				.map(|(account, _)| {
					let substrate = self.substrate.clone();
					async move {
						match substrate.member_peer_id(&account, block_hash).await {
							Ok(Some(peer_id)) => Some(peer_id),
							Ok(None) | Err(_) => None,
						}
					}
				})
				.collect();
			let members =
				join_all(futures).await.into_iter().flatten().collect::<BTreeSet<PeerId>>();

			let commitment = if let Some(commitment) =
				self.substrate.shard_commitment(shard_id, block_hash).await?
			{
				let commitment =
					VerifiableSecretSharingCommitment::deserialize(commitment.0.to_vec())?;
				Some(commitment)
			} else {
				None
			};
			self.tss_states.insert(
				shard_id,
				Tss::new(
					self.network.peer_id(),
					members,
					threshold,
					commitment,
					&self.tss_keyshare_cache,
					&span,
				)?,
			);
			self.poll_actions(&span, shard_id, block).await;
		}
		for shard_id in shards.iter().copied() {
			let Some(tss) = self.tss_states.get_mut(&shard_id) else {
				continue;
			};
			if tss.committed() {
				continue;
			}
			if self.substrate.shard_status(shard_id, block_hash).await? != ShardStatus::Committed {
				continue;
			}
			let span = span!(parent: &span, Level::DEBUG, "committing", gmp_shard_id = shard_id);
			let commitment = self.substrate.shard_commitment(shard_id, block_hash).await?.unwrap();
			let commitment = VerifiableSecretSharingCommitment::deserialize(commitment.0.to_vec())?;
			tss.on_commit(commitment, &span);
			self.poll_actions(&span, shard_id, block).await;
		}
		while let Some(n) = self.requests.keys().copied().next() {
			if n > block {
				break;
			}
			for (shard_id, task_id, data) in self.requests.remove(&n).unwrap() {
				let span = span!(
					parent: &span,
					Level::DEBUG,
					"received signing request from task executor",
					gmp_shard_id = shard_id,
					gmp_task_id = task_id,
				);
				let Some(tss) = self.tss_states.get_mut(&shard_id) else {
					event!(
						parent: &span,
						Level::ERROR,
						"trying to run task on unknown shard, dropping channel",
					);
					self.channels.remove(&task_id);
					continue;
				};
				event!(parent: &span, Level::DEBUG, "signing");
				tss.on_sign(task_id, data.to_vec(), &span);
				self.poll_actions(&span, shard_id, block).await;
			}
		}
		for shard_id in shards.iter().copied() {
			if self.substrate.shard_status(shard_id, block_hash).await? != ShardStatus::Online {
				continue;
			}
			let executor = self
				.executor_states
				.entry(shard_id)
				.or_insert(TaskExecutor::new(self.task_params.clone()));
			let span = span!(
				parent: &span,
				Level::DEBUG,
				"shard",
				gmp_shard_id = shard_id,
			);
			let (start_sessions, complete_sessions, _failed_tasks) =
				match executor.process_tasks(block_hash, block, shard_id, &span).await {
					Ok((start_sessions, complete_sessions, failed_tasks)) => {
						(start_sessions, complete_sessions, failed_tasks)
					},
					Err(error) => {
						event!(
							parent: &span,
							Level::INFO,
							"failed to start tasks: {:?}",
							error,
						);
						continue;
					},
				};

			let Some(tss) = self.tss_states.get_mut(&shard_id) else {
				continue;
			};
			for session in complete_sessions {
				let span = span!(parent: &span, Level::DEBUG, "completing", gmp_task_id = session);
				tss.on_complete(session, &span);
			}
			for session in start_sessions {
				let span = span!(parent: &span, Level::DEBUG, "starting", gmp_task_id = session);
				tss.on_start(session, &span);
			}
		}
		while let Some(n) = self.messages.keys().copied().next() {
			if n > block {
				break;
			}
			for (shard_id, peer_id, msg) in self.messages.remove(&n).unwrap() {
				let span = span!(parent: &span, Level::DEBUG, "messages", gmp_shard_id = shard_id,
					net_from = display_peer_id(peer_id), net_message = msg.to_string());
				let Some(tss) = self.tss_states.get_mut(&shard_id) else {
					event!(
						parent: &span,
						Level::INFO,
						"dropping message",
					);
					continue;
				};
				tss.on_message(peer_id, msg, &span)?;
				self.poll_actions(&span, shard_id, n).await;
			}
		}
		if let Err(e) = self.admin_request.send(AdminMsg::SetShards(shards)).await {
			event!(parent: &span, Level::ERROR, "admin request failed: {:?}", e);
		};
		Ok(())
	}

	async fn poll_actions(&mut self, span: &Span, shard_id: ShardId, block: BlockNumber) {
		while let Some(action) = self
			.tss_states
			.get_mut(&shard_id)
			.unwrap()
			.next_action(&self.tss_keyshare_cache, span)
		{
			match action {
				TssAction::Send(msgs) => {
					for (peer_id, payload) in msgs {
						let span = span!(
							parent: span,
							Level::DEBUG,
							"tx",
							net_to = display_peer_id(peer_id),
							net_message = payload.to_string(),
						);
						let msg = Message {
							shard_id,
							block: if payload.is_response() { 0 } else { block },
							payload,
						};
						let endpoint = self.network.clone();
						self.outgoing_requests.push(Box::pin(async move {
							event!(parent: &span, Level::DEBUG, "send");
							let result = endpoint.send(peer_id, msg).await;
							(result, span)
						}));
					}
				},
				TssAction::Commit(commitment, proof_of_knowledge) => {
					event!(
						parent: span,
						Level::DEBUG,
						"commit",
					);
					self.substrate
						.submit_commitment(
							shard_id,
							Commitment(BoundedVec::truncate_from(commitment.serialize())),
							proof_of_knowledge.serialize(),
						)
						.await
						.unwrap();
				},
				TssAction::PublicKey(tss_public_key) => {
					let public_key = tss_public_key.to_bytes().unwrap();
					event!(
						parent: span,
						Level::DEBUG,
						"public key 0x{}",
						hex::encode(public_key),
					);
					self.substrate.submit_online(shard_id).await.unwrap();
				},
				TssAction::Signature(task_id, hash, tss_signature) => {
					let tss_signature = tss_signature.to_bytes();
					event!(
						parent: span,
						Level::DEBUG,
						gmp_task_id = task_id,
						"signature 0x{}",
						hex::encode(tss_signature),
					);
					if let Some(tx) = self.channels.remove(&task_id) {
						tx.send((hash, tss_signature)).ok();
					}
				},
			}
		}
	}

	pub async fn run(mut self, span: &Span) {
		event!(
			parent: span,
			Level::DEBUG,
			"starting tss",
		);

		// add a future that never resolves to keep outgoing requests alive
		self.outgoing_requests.push(Box::pin(poll_fn(|_| Poll::Pending)));

		let mut block_notifications = self.substrate.block_notification_stream();
		let mut finality_notifications = self.substrate.finality_notification_stream();
		let block = finality_notifications.next().await.expect("Finality stream is not active");
		let heartbeat_period = self
			.substrate
			.heartbeat_timeout(block.0)
			.await
			.ok()
			.and_then(|t| t.gt(&1).then_some(t / 2))
			.unwrap_or(DEFAULT_HEARTBEAT_PERIOD);
		event!(parent: span, Level::INFO, "Started chronicle loop");
		let mut send_heartbeat = true;
		loop {
			futures::select! {
				notification = block_notifications.next().fuse() => {
					let _enter = span.enter();
					let Some((block_hash, block)) = notification else {
						event!(
							parent: span,
							Level::DEBUG,
							"no new block notifications"
						);
						continue;
					};
					if let Err(e) = self.admin_request.send(AdminMsg::NewBlock(block as _)).await {
						event!(
							parent: span,
							Level::ERROR,
							"Admin request error: {e:?}",
						);
					};
					if block % heartbeat_period == 0 {
						if send_heartbeat {
							event!(
								parent: span,
								Level::ERROR,
								"missed heartbeat period",
							);
						}
						send_heartbeat = true;
					}
					if send_heartbeat {
						// Check if heartbeat has already been submitted for this account
						let account_id = self.substrate.account_id();
						match self.substrate.is_heartbeat_submitted(account_id, block_hash).await {
							Ok(true) => {
								// Heartbeat already submitted, no need to submit again
								send_heartbeat = false;
								event!(parent: span, Level::INFO, "heartbeat already submitted");
							},
							Ok(false) => {
								// Heartbeat not submitted yet, proceed with submission
								event!(
									parent: span,
									Level::INFO,
									"submitting heartbeat",
								);
								match self.substrate.submit_heartbeat().await {
									Ok(()) => {
										send_heartbeat = false;
										event!(parent: span, Level::INFO, "submitted heartbeat");
									}
									Err(e) => {
										event!(
											parent: span,
											Level::INFO,
											"Error submitting heartbeat: {:?}",
											e
										);
									}
								}
							},
							Err(e) => {
								event!(
									parent: span,
									Level::ERROR,
									"Error checking heartbeat status: {:?}",
									e
								);
							}
						}
					}
				},
				notification = finality_notifications.next().fuse() => {
					let _enter = span.enter();
					let Some((block_hash, block)) = notification else {
						event!(
							parent: span,
							Level::DEBUG,
							"no new finality notifications"
						);
						continue;
					};
					if let Err(e) = self.on_finality(span, block_hash, block).await {
						event!(
							parent: span,
							Level::ERROR,
							"Error running on_finality {:?}",
							e
						);
					}
				},
				tss_request = self.tss_request.next().fuse() => {
					let _enter = span.enter();
					let Some(TssSigningRequest { task_id, shard_id, data, tx, block }) = tss_request else {
						continue;
					};
					event!(
						parent: span,
						Level::DEBUG,
						gmp_shard_id = shard_id,
						gmp_task_id = task_id,
						tc_block = block,
						"received signing request",
					);
					self.requests.entry(block).or_default().push((shard_id, task_id, data));
					self.channels.insert(task_id, tx);
				},
				msg = self.net_request.next().fuse() => {
					let _enter = span.enter();
					let Some((peer, Message { shard_id, block, payload })) = msg else {
						continue;
					};
					event!(
						parent: span,
						Level::DEBUG,
						gmp_shard_id = shard_id,
						tc_block = block,
						net_from = display_peer_id(peer),
						"rx {}",
						payload,
					);
					self.messages.entry(block).or_default().push((shard_id, peer, payload));
				},
				outgoing_request = self.outgoing_requests.next().fuse() => {
					let Some((result, span)) = outgoing_request else {
						continue;
					};
					let _enter = span.enter();
					if let Err(error) = result {
						event!(
							parent: &span,
							Level::ERROR,
							"error {:?}",
							error,
						);
					} else {
						event!(
							parent: &span,
							Level::DEBUG,
							"sent",
						);
					}
				}
			}
		}
	}
}