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
use alloc::sync::Arc;
use core::fmt;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use tracing::error;
use crate::foreign_client::ForeignClient;
use crate::link::{Link, LinkParameters, Resubmit};
use crate::{
chain::handle::{ChainHandle, ChainHandlePair},
config::Config,
object::Object,
};
pub mod retry_strategy;
mod error;
pub use error::RunError;
mod handle;
pub use handle::{WorkerData, WorkerHandle};
mod cmd;
pub use cmd::WorkerCmd;
mod map;
pub use map::WorkerMap;
pub mod channel;
pub mod client;
pub mod connection;
pub mod packet;
pub mod wallet;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct WorkerId(u64);
impl WorkerId {
pub fn new(id: u64) -> Self {
Self(id)
}
pub fn next(self) -> Self {
Self(self.0 + 1)
}
}
impl fmt::Display for WorkerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
pub fn spawn_worker_tasks<ChainA: ChainHandle, ChainB: ChainHandle>(
chains: ChainHandlePair<ChainA, ChainB>,
id: WorkerId,
object: Object,
config: &Config,
) -> WorkerHandle {
let mut task_handles = Vec::new();
let (cmd_tx, data) = match &object {
Object::Client(client) => {
let client = ForeignClient::restore(client.dst_client_id.clone(), chains.b, chains.a);
let (mut refresh, mut misbehaviour) = (false, false);
let refresh_task = client::spawn_refresh_client(client.clone());
if let Some(refresh_task) = refresh_task {
task_handles.push(refresh_task);
refresh = true;
}
let cmd_tx = if config.mode.clients.misbehaviour {
let (cmd_tx, cmd_rx) = crossbeam_channel::unbounded();
let misbehavior_task = client::detect_misbehavior_task(cmd_rx, client);
if let Some(task) = misbehavior_task {
task_handles.push(task);
misbehaviour = true;
}
Some(cmd_tx)
} else {
None
};
let data = WorkerData::Client {
misbehaviour,
refresh,
};
(cmd_tx, Some(data))
}
Object::Connection(connection) => {
let (cmd_tx, cmd_rx) = crossbeam_channel::unbounded();
let connection_task =
connection::spawn_connection_worker(connection.clone(), chains, cmd_rx);
task_handles.push(connection_task);
(Some(cmd_tx), None)
}
Object::Channel(channel) => {
let (cmd_tx, cmd_rx) = crossbeam_channel::unbounded();
let channel_task = channel::spawn_channel_worker(channel.clone(), chains, cmd_rx);
task_handles.push(channel_task);
(Some(cmd_tx), None)
}
Object::Packet(path) => {
let packets_config = config.mode.packets;
let link_res = Link::new_from_opts(
chains.a.clone(),
chains.b,
LinkParameters {
src_port_id: path.src_port_id.clone(),
src_channel_id: path.src_channel_id,
},
packets_config.tx_confirmation,
);
match link_res {
Ok(link) => {
let (cmd_tx, cmd_rx) = crossbeam_channel::unbounded();
let link = Arc::new(Mutex::new(link));
let resubmit = Resubmit::from_clear_interval(packets_config.clear_interval);
let packet_task = packet::spawn_packet_cmd_worker(
cmd_rx,
link.clone(),
packets_config.clear_on_start,
packets_config.clear_interval,
path.clone(),
);
task_handles.push(packet_task);
let link_task = packet::spawn_packet_worker(path.clone(), link, resubmit);
task_handles.push(link_task);
(Some(cmd_tx), None)
}
Err(e) => {
error!("error initializing link object for packet worker: {}", e);
(None, None)
}
}
}
Object::Wallet(wallet) => {
assert_eq!(wallet.chain_id, chains.a.id());
let wallet_task = wallet::spawn_wallet_worker(chains.a);
task_handles.push(wallet_task);
(None, None)
}
};
WorkerHandle::new(id, object, data, cmd_tx, task_handles)
}