-
Notifications
You must be signed in to change notification settings - Fork 296
/
lib.rs
238 lines (213 loc) · 7.25 KB
/
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
use std::sync::{Arc, RwLock};
use crate::{
metrics::ConsensusManagerMetrics,
receiver::{build_axum_router, ConsensusManagerReceiver},
sender::ConsensusManagerSender,
};
use axum::Router;
use crossbeam_channel::Sender as CrossbeamSender;
use ic_base_types::NodeId;
use ic_interfaces::p2p::{
artifact_manager::ArtifactProcessorEvent,
consensus::{PriorityFnAndFilterProducer, ValidatedPoolReader},
};
use ic_logger::ReplicaLogger;
use ic_metrics::MetricsRegistry;
use ic_protobuf::{
p2p::v1 as pb,
proxy::{try_from_option_field, ProtoProxy, ProxyDecodeError},
};
use ic_quic_transport::{ConnId, SubnetTopology, Transport};
use ic_types::artifact::{ArtifactKind, UnvalidatedArtifactMutation};
use phantom_newtype::AmountOf;
use tokio::{
runtime::Handle,
sync::{mpsc::Receiver, watch},
};
mod metrics;
mod receiver;
mod sender;
type StartConsensusManagerFn = Box<dyn FnOnce(Arc<dyn Transport>, watch::Receiver<SubnetTopology>)>;
pub struct ConsensusManagerBuilder {
log: ReplicaLogger,
metrics_registry: MetricsRegistry,
rt_handle: Handle,
clients: Vec<StartConsensusManagerFn>,
router: Option<Router>,
}
impl ConsensusManagerBuilder {
pub fn new(log: ReplicaLogger, rt_handle: Handle, metrics_registry: MetricsRegistry) -> Self {
Self {
log,
metrics_registry,
rt_handle,
clients: Vec::new(),
router: None,
}
}
pub fn add_client<Artifact, Pool>(
&mut self,
adverts_to_send: Receiver<ArtifactProcessorEvent<Artifact>>,
raw_pool: Arc<RwLock<Pool>>,
priority_fn_producer: Arc<dyn PriorityFnAndFilterProducer<Artifact, Pool>>,
sender: CrossbeamSender<UnvalidatedArtifactMutation<Artifact>>,
) where
Pool: 'static + Send + Sync + ValidatedPoolReader<Artifact>,
Artifact: ArtifactKind,
{
let (router, adverts_from_peers_rx) = build_axum_router(self.log.clone(), raw_pool.clone());
let log = self.log.clone();
let rt_handle = self.rt_handle.clone();
let metrics_registry = self.metrics_registry.clone();
let builder = move |transport: Arc<dyn Transport>, topology_watcher| {
start_consensus_manager(
log,
&metrics_registry,
rt_handle,
adverts_to_send,
adverts_from_peers_rx,
raw_pool,
priority_fn_producer,
sender,
transport,
topology_watcher,
)
};
self.router = Some(self.router.take().unwrap_or_default().merge(router));
self.clients.push(Box::new(builder));
}
pub fn router(&mut self) -> Router {
self.router.take().unwrap_or_default()
}
pub fn run(
self,
transport: Arc<dyn Transport>,
topology_watcher: watch::Receiver<SubnetTopology>,
) {
for client in self.clients {
client(transport.clone(), topology_watcher.clone());
}
}
}
fn start_consensus_manager<Artifact, Pool>(
log: ReplicaLogger,
metrics_registry: &MetricsRegistry,
rt_handle: Handle,
// Locally produced adverts to send to the node's peers.
adverts_to_send: Receiver<ArtifactProcessorEvent<Artifact>>,
// Adverts received from peers
adverts_received: Receiver<(SlotUpdate<Artifact>, NodeId, ConnId)>,
raw_pool: Arc<RwLock<Pool>>,
priority_fn_producer: Arc<dyn PriorityFnAndFilterProducer<Artifact, Pool>>,
sender: CrossbeamSender<UnvalidatedArtifactMutation<Artifact>>,
transport: Arc<dyn Transport>,
topology_watcher: watch::Receiver<SubnetTopology>,
) where
Pool: 'static + Send + Sync + ValidatedPoolReader<Artifact>,
Artifact: ArtifactKind,
{
let metrics = ConsensusManagerMetrics::new::<Artifact>(metrics_registry);
ConsensusManagerSender::run(
log.clone(),
metrics.clone(),
rt_handle.clone(),
raw_pool.clone(),
transport.clone(),
adverts_to_send,
);
ConsensusManagerReceiver::run(
log,
metrics,
rt_handle,
adverts_received,
raw_pool,
priority_fn_producer,
sender,
transport,
topology_watcher,
);
}
pub(crate) struct SlotUpdate<Artifact: ArtifactKind> {
slot_number: SlotNumber,
commit_id: CommitId,
update: Update<Artifact>,
}
pub(crate) enum Update<Artifact: ArtifactKind> {
Artifact(Artifact::Message),
Advert((Artifact::Id, Artifact::Attribute)),
}
impl<Artifact: ArtifactKind> From<SlotUpdate<Artifact>> for pb::SlotUpdate {
fn from(
SlotUpdate {
slot_number,
commit_id,
update,
}: SlotUpdate<Artifact>,
) -> Self {
Self {
commit_id: commit_id.get(),
slot_id: slot_number.get(),
update: Some(match update {
Update::Artifact(artifact) => {
pb::slot_update::Update::Artifact(Artifact::PbMessage::proxy_encode(artifact))
}
Update::Advert((id, attribute)) => pb::slot_update::Update::Advert(pb::Advert {
id: Artifact::PbId::proxy_encode(id),
attribute: Artifact::PbAttribute::proxy_encode(attribute),
}),
}),
}
}
}
impl<Artifact: ArtifactKind> TryFrom<pb::SlotUpdate> for SlotUpdate<Artifact> {
type Error = ProxyDecodeError;
fn try_from(value: pb::SlotUpdate) -> Result<Self, Self::Error> {
Ok(Self {
slot_number: SlotNumber::from(value.slot_id),
commit_id: CommitId::from(value.commit_id),
update: match try_from_option_field(value.update, "update")? {
pb::slot_update::Update::Artifact(artifact) => {
Update::Artifact(Artifact::PbMessage::proxy_decode(&artifact)?)
}
pb::slot_update::Update::Advert(pb::Advert { id, attribute }) => Update::Advert((
Artifact::PbId::proxy_decode(&id)?,
Artifact::PbAttribute::proxy_decode(&attribute)?,
)),
},
})
}
}
pub(crate) fn uri_prefix<Artifact: ArtifactKind>() -> String {
Artifact::TAG.to_string().to_lowercase()
}
struct SlotNumberTag;
pub(crate) type SlotNumber = AmountOf<SlotNumberTag, u64>;
struct CommitIdTag;
pub(crate) type CommitId = AmountOf<CommitIdTag, u64>;
#[cfg(test)]
mod tests {
use ic_types::artifact_kind::{
CanisterHttpArtifact, CertificationArtifact, ConsensusArtifact, DkgArtifact, EcdsaArtifact,
IngressArtifact,
};
use crate::uri_prefix;
#[test]
fn no_special_chars_in_uri() {
assert!(uri_prefix::<ConsensusArtifact>()
.chars()
.all(char::is_alphabetic));
assert!(uri_prefix::<CertificationArtifact>()
.chars()
.all(char::is_alphabetic));
assert!(uri_prefix::<DkgArtifact>().chars().all(char::is_alphabetic));
assert!(uri_prefix::<IngressArtifact>()
.chars()
.all(char::is_alphabetic));
assert!(uri_prefix::<EcdsaArtifact>()
.chars()
.all(char::is_alphabetic));
assert!(uri_prefix::<CanisterHttpArtifact>()
.chars()
.all(char::is_alphabetic));
}
}