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
use actix::{AsyncContext, WrapFuture};
use calimero_network_primitives::messages::NetworkEvent;
use calimero_network_primitives::specialized_node_invite::SpecializedNodeType;
use calimero_node_primitives::sync::BroadcastMessage;
use tracing::{debug, error, info, warn};
use crate::handlers::{specialized_node_invite, tee_attestation_admission};
use crate::run::NodeMode;
use crate::NodeManager;
pub(super) fn handle_specialized_broadcast(
this: &mut NodeManager,
ctx: &mut actix::Context<NodeManager>,
source: libp2p::PeerId,
topic: &libp2p::gossipsub::TopicHash,
message: &BroadcastMessage<'_>,
) -> bool {
match message {
BroadcastMessage::SpecializedNodeDiscovery { nonce, node_type } => {
let should_respond = matches!(
(this.state.node_mode(), *node_type),
(NodeMode::ReadOnly, SpecializedNodeType::ReadOnly)
);
if !should_respond {
debug!(
%source,
nonce = %hex::encode(*nonce),
?node_type,
node_mode = ?this.state.node_mode(),
"Ignoring specialized node discovery (not a matching specialized node)"
);
return true;
}
info!(
%source,
nonce = %hex::encode(*nonce),
?node_type,
"Received specialized node discovery - responding as read-only node"
);
let network_client = this.managers.sync.network_client.clone();
let context_client = this.clients.context.clone();
let nonce = *nonce;
let _ignored = ctx.spawn(
async move {
match specialized_node_invite::handle_specialized_node_discovery(
nonce,
source,
&context_client,
) {
Ok(request) => {
if let Err(err) = network_client
.send_specialized_node_verification_request(source, request)
.await
{
error!(
%source,
error = %err,
"Failed to send specialized node verification request"
);
}
}
Err(err) => {
debug!(
error = %err,
"Failed to handle specialized node discovery (not a TEE node?)"
);
}
}
}
.into_actor(this),
);
true
}
BroadcastMessage::TeeAttestationAnnounce {
quote_bytes,
public_key,
nonce,
node_type: _,
} => {
let topic_str = topic.as_str();
let group_id_bytes = match topic_str.strip_prefix("group/") {
Some(hex) => {
let mut bytes = [0u8; 32];
if hex::decode_to_slice(hex, &mut bytes).is_err() {
warn!(
%source,
topic = %topic_str,
"Invalid group topic hex in TeeAttestationAnnounce"
);
return true;
}
bytes
}
None => {
warn!(
%source,
topic = %topic_str,
"TeeAttestationAnnounce received on non-group topic"
);
return true;
}
};
info!(
%source,
%public_key,
nonce = %hex::encode(*nonce),
group_id = %hex::encode(group_id_bytes),
"Received TEE attestation announce on group topic"
);
let context_client = this.clients.context.clone();
let quote_bytes = quote_bytes.clone();
let public_key = *public_key;
let nonce = *nonce;
let _ignored = ctx.spawn(
async move {
if let Err(err) = tee_attestation_admission::handle_tee_attestation_announce(
&context_client,
source,
quote_bytes,
public_key,
nonce,
group_id_bytes,
)
.await
{
warn!(
%source,
error = %err,
"Failed to handle TEE attestation announce"
);
}
}
.into_actor(this),
);
true
}
BroadcastMessage::SpecializedNodeJoinConfirmation { nonce } => {
info!(
%source,
nonce = %hex::encode(*nonce),
"Received specialized node join confirmation"
);
let pending_invites = this.state.pending_specialized_node_invites_handle();
specialized_node_invite::handle_join_confirmation(&pending_invites, *nonce);
true
}
_ => false,
}
}
pub(super) fn handle_specialized_network_event(
this: &mut NodeManager,
ctx: &mut actix::Context<NodeManager>,
msg: NetworkEvent,
) -> bool {
match msg {
NetworkEvent::SpecializedNodeVerificationRequest {
peer_id,
request_id,
request,
channel,
} => {
info!(
%peer_id,
?request_id,
nonce = %hex::encode(request.nonce()),
public_key = %request.public_key(),
"Received specialized node verification request"
);
let pending_invites = this.state.pending_specialized_node_invites_handle();
let network_client = this.managers.sync.network_client.clone();
let context_client = this.clients.context.clone();
let accept_mock_tee = this.state.accept_mock_tee();
let _ignored = ctx.spawn(
async move {
let response = specialized_node_invite::handle_verification_request(
peer_id,
request,
&pending_invites,
&context_client,
accept_mock_tee,
)
.await;
if let Err(err) = network_client
.send_specialized_node_invitation_response(channel, response)
.await
{
error!(
%peer_id,
error = %err,
"Failed to send specialized node invitation response"
);
}
}
.into_actor(this),
);
true
}
NetworkEvent::SpecializedNodeInvitationResponse {
peer_id,
request_id,
response,
} => {
let nonce = response.nonce;
info!(
%peer_id,
?request_id,
nonce = %hex::encode(nonce),
has_invitation = response.invitation_bytes.is_some(),
has_error = response.error.is_some(),
"Received specialized node invitation response"
);
let context_client = this.clients.context.clone();
let network_client = this.managers.sync.network_client.clone();
let _ignored = ctx.spawn(
async move {
match specialized_node_invite::handle_specialized_node_invitation_response(
peer_id,
nonce,
response,
&context_client,
)
.await
{
Ok(Some(context_id)) => {
info!(
%peer_id,
%context_id,
nonce = %hex::encode(nonce),
"Joined context, broadcasting join confirmation"
);
let payload =
BroadcastMessage::SpecializedNodeJoinConfirmation { nonce };
if let Ok(payload_bytes) = borsh::to_vec(&payload) {
let topic = libp2p::gossipsub::TopicHash::from_raw(context_id);
if let Err(err) = network_client.publish(topic, payload_bytes).await
{
error!(
%context_id,
error = %err,
"Failed to broadcast join confirmation"
);
}
}
}
Ok(None) => {
debug!(
%peer_id,
nonce = %hex::encode(nonce),
"Specialized node invitation response handled but join failed"
);
}
Err(err) => {
error!(
%peer_id,
error = %err,
"Failed to handle specialized node invitation response"
);
}
}
}
.into_actor(this),
);
true
}
_ => false,
}
}