chronicle/network/
mod.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
use self::protocol::TssEndpoint;
use anyhow::Result;
use futures::channel::mpsc;
use futures::stream::BoxStream;
use futures::{Future, StreamExt};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::pin::Pin;
use std::sync::Arc;
use time_primitives::{BlockNumber, ShardId};
use tracing::Span;

mod protocol;

pub use time_primitives::PeerId;

pub type TssMessage = tss::TssMessage;

pub const PROTOCOL_NAME: &str = "/analog-labs/chronicle/1";

#[derive(Default)]
pub struct NetworkConfig {
	pub secret: [u8; 32],
}

#[derive(Deserialize, Serialize)]
pub struct Message {
	pub shard_id: ShardId,
	pub block: BlockNumber,
	pub payload: TssMessage,
}

pub trait Network: Send + Sync + 'static {
	fn peer_id(&self) -> PeerId;

	fn format_peer_id(&self, peer_id: PeerId) -> String;

	fn send(
		&self,
		peer_id: PeerId,
		msg: Message,
	) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>;
}

impl Network for Arc<dyn Network> {
	fn peer_id(&self) -> PeerId {
		self.deref().peer_id()
	}

	fn format_peer_id(&self, peer: PeerId) -> String {
		self.deref().format_peer_id(peer)
	}

	fn send(
		&self,
		peer_id: PeerId,
		msg: Message,
	) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
		self.deref().send(peer_id, msg)
	}
}

pub async fn create_iroh_network(
	config: NetworkConfig,
	span: &Span,
) -> Result<(Arc<dyn Network>, BoxStream<'static, (PeerId, Message)>)> {
	let (net_tx, net_rx) = mpsc::channel(10);
	let network =
		Arc::new(TssEndpoint::new(config, net_tx, span).await?) as Arc<dyn Network + Send + Sync>;
	let incoming = net_rx.boxed();
	Ok((network, incoming))
}