veilid-core 0.5.3

Core library used to create a Veilid node and operate it as part of an application
Documentation
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
use super::*;

impl_veilid_log_facility!("rpc");

#[derive(Clone, Debug)]
pub struct TransactBeginAnswer {
    pub accepted: bool,
    pub descriptor_mode: DescriptorMode,
    pub transaction_id: Option<u64>,
    pub expiration: Timestamp,
    pub seqs: Vec<ValueSeqNum>,
    pub peers: Vec<Arc<PeerInfo>>,
}

impl RPCProcessor {
    /// Sends an transact begin request and wait for response
    /// Can be sent via all methods including relays
    /// Safety routes may be used, but never private routes.
    /// Because this leaks information about the identity of the node itself,
    /// replying to this request received over a private route will leak
    /// the identity of the node and defeat the private route.
    /// The number of subkey sequence numbers returned may either be:
    ///  * an amount truncated to MAX_TRANSACT_BEGIN_A_SEQS_LEN subkeys
    ///  * zero if nothing was found
    #[
        instrument(level = "trace", target = "rpc", skip(self, descriptor_mode),
            fields(
                ret.peers.len,
                ret.latency,
                ret.accepted
            ),err(level=Level::DEBUG))
    ]
    pub async fn rpc_call_transact_begin(
        &self,
        dest: Destination,
        opaque_record_key: OpaqueRecordKey,
        descriptor_mode: DescriptorMode,
        signing_keypair: KeyPair,
    ) -> RPCNetworkResult<Answer<TransactBeginAnswer>> {
        let _guard = self
            .startup_context
            .startup_lock
            .enter()
            .map_err(RPCError::map_try_again("not started up"))?;

        // Ensure destination never has a private route
        // and get the target noderef so we can validate the response
        let Some(target_node_ids) = dest.get_target_node_ids() else {
            return Err(RPCError::internal(
                "Never send transact value requests over private routes",
            ));
        };

        // Get the target node id
        let Some(target_node_id) = target_node_ids.get(opaque_record_key.kind()) else {
            return Err(RPCError::internal("No node id for crypto kind"));
        };

        let debug_string = format!(
            "OUT ==> TransactBeginQ({} {}) => {} (signer={}) ",
            opaque_record_key,
            descriptor_mode,
            dest,
            signing_keypair.key(),
        );

        // Process descriptor mode
        let (last_descriptor, send_descriptor, want_descriptor) = match descriptor_mode {
            DescriptorMode::Want => (None, false, true),
            DescriptorMode::Have(ref d) => (Some(d.clone()), false, false),
            DescriptorMode::Send(ref d) => (Some(d.clone()), true, false),
        };

        // Send the transact begin question
        let transact_value_q = RPCOperationTransactBeginQ::new(
            opaque_record_key.clone(),
            if send_descriptor {
                last_descriptor.as_ref().map(|x| x.as_ref().clone())
            } else {
                None
            },
            want_descriptor,
        )?;
        let question = RPCQuestion::new(
            network_result_try!(self.get_destination_respond_to(&dest).await?),
            RPCQuestionDetail::TransactBeginQ(Box::new(transact_value_q)),
        );

        let question_context = QuestionContext::TransactBegin(ValidateTransactBeginContext {
            opaque_record_key: opaque_record_key.clone(),
            descriptor_mode,
        });

        veilid_log!(self debug target: "dht", "{}", debug_string);

        let waitable_reply = network_result_try!(
            self.question(
                dest.clone(),
                question,
                Some(signing_keypair),
                Some(question_context)
            )
            .await?
        );

        // Wait for reply
        let (msg, answer_context) = match self.wait_for_reply(waitable_reply, debug_string).await? {
            TimeoutOr::Timeout => return Ok(NetworkResult::Timeout),
            TimeoutOr::Value(v) => v,
        };

        // Get the right answer type
        let (_, _, kind) = msg.operation.destructure();
        let transact_begin_a = match kind {
            RPCOperationKind::Answer(a) => match a.destructure() {
                RPCAnswerDetail::TransactBeginA(a) => a,
                _ => return Ok(NetworkResult::invalid_message("not a transactbegin answer")),
            },
            _ => return Ok(NetworkResult::invalid_message("not an answer")),
        };

        let (accepted, need_descriptor, opt_descriptor, transaction_id, duration, seqs, peers) =
            transact_begin_a.destructure();

        if debug_target_enabled!("dht") {
            let debug_string_answer = format!(
                "OUT <== TransactBeginA({} {}{}{} @{} peers={}) <= {} (latency={}) seqs:{}",
                opaque_record_key,
                if let Some(xid) = transaction_id {
                    format!("xid={} ", xid)
                } else {
                    "".to_string()
                },
                if accepted { " +accept" } else { "" },
                if need_descriptor { " +needdesc" } else { "" },
                duration,
                peers.len(),
                dest,
                answer_context.latency,
                seqs.to_table_string()
            );

            veilid_log!(self debug target: "dht", "{}", debug_string_answer);

            let peer_ids: Vec<String> = peers
                .iter()
                .filter_map(|p| {
                    p.node_ids()
                        .get(opaque_record_key.kind())
                        .map(|k| k.to_string())
                })
                .collect();
            veilid_log!(self debug target: "dht", "Peers: {:#?}", peer_ids);
        }

        // Validate peers returned are, in fact, closer to the key than the node we sent this to
        let valid = match self.routing_table().verify_peer_infos_closer(
            target_node_id.to_hash_coordinate(),
            opaque_record_key.to_hash_coordinate(),
            &peers,
        ) {
            Ok(v) => v,
            Err(e) => {
                return Ok(NetworkResult::invalid_message(format!(
                    "missing cryptosystem in peers node ids: {}",
                    e
                )));
            }
        };
        if !valid {
            return Ok(NetworkResult::invalid_message("non-closer peers returned"));
        }

        let descriptor_mode = if need_descriptor || !accepted {
            DescriptorMode::Want
        } else if let Some(descriptor) = opt_descriptor {
            DescriptorMode::Send(Arc::new(descriptor))
        } else if !want_descriptor {
            DescriptorMode::Have(last_descriptor.unwrap_or_log())
        } else {
            return Ok(NetworkResult::invalid_message(
                "wanted descriptor but did not get one",
            ));
        };

        // Get expiration timestamp
        // Estimates the duration as calculated at a time halfway through the RPC by the remote node
        let expiration = if duration.is_zero() {
            Timestamp::new(0)
        } else {
            answer_context
                .waitable_reply_context
                .send_ts
                .later(answer_context.latency.div(2))
                .later(duration)
        };

        #[cfg(feature = "verbose-tracing")]
        tracing::Span::current().record("ret.latency", answer_context.latency.as_u64());
        #[cfg(feature = "verbose-tracing")]
        tracing::Span::current().record("ret.accepted", accepted);
        #[cfg(feature = "verbose-tracing")]
        tracing::Span::current().record("ret.peers.len", peers.len());

        Ok(NetworkResult::value(Answer::new(
            answer_context,
            TransactBeginAnswer {
                accepted,
                descriptor_mode,
                transaction_id,
                expiration,
                seqs,
                peers,
            },
        )))
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////

    #[cfg_attr(feature = "instrument", instrument(level = "trace", target = "rpc", skip(self, msg), fields(msg.operation.op_id), ret, err))]
    pub(super) async fn process_transact_begin_q(&self, msg: Message) -> RPCNetworkResult<()> {
        // Ensure this never came over a private route, safety route is okay though
        if msg.header.is_private_routed() {
            return Ok(NetworkResult::invalid_message(
                "not processing transact value request over private route",
            ));
        }
        let routing_table = self.routing_table();
        let routing_domain = msg.header.routing_domain();

        // Ignore if disabled
        let has_cap_dhtv = routing_table
            .get_published_peer_info(msg.header.routing_domain())
            .map(|ppi| ppi.node_info().has_capability(VEILID_CAPABILITY_DHT))
            .unwrap_or(false);
        if !has_cap_dhtv {
            return Ok(NetworkResult::service_unavailable("dht is not available"));
        }

        // Get the question
        let kind = msg.operation.kind().clone();
        let transact_begin_q = match kind {
            RPCOperationKind::Question(q) => match q.destructure() {
                (_, RPCQuestionDetail::TransactBeginQ(q)) => q,
                _ => panic!("not a transactbegin question"),
            },
            _ => panic!("not a question"),
        };

        // Destructure
        let (opaque_record_key, opt_descriptor, want_descriptor) = transact_begin_q.destructure();
        let Some(signer) = &msg.opt_signer else {
            return Ok(NetworkResult::invalid_message(
                "transact begin requires signer",
            ));
        };

        // Extract member id for signer
        let Ok(signing_member_id) = self.storage_manager().generate_member_id(signer) else {
            return Ok(NetworkResult::invalid_message(
                "could not generate member id for signer public key",
            ));
        };

        // Get the nodes that we know about that are closer to the the key than our own node
        let closer_to_key_peers = network_result_try!(routing_table
            .get_reliable_nodes_closer_to_key_peer_info(
                routing_domain,
                opaque_record_key.to_hash_coordinate(),
                vec![VEILID_CAPABILITY_DHT]
            ));

        if debug_target_enabled!("dht") {
            let debug_string = format!(
                "IN <=== TransactBeginQ({}{}{}) <== {} (signer={})",
                opaque_record_key,
                if opt_descriptor.is_some() {
                    " +desc"
                } else {
                    ""
                },
                if want_descriptor { " +wantdesc" } else { "" },
                msg.header.direct_sender_node_id(),
                signer,
            );

            veilid_log!(self debug target: "dht", "{}", debug_string);
        }

        // See if this is within the consensus width
        let consensus_width = self.config().network.dht.consensus_width as usize;

        let (
            accepted,
            need_descriptor,
            opt_descriptor,
            opt_transaction_id,
            opt_expiration,
            transact_begin_seqs,
        ) = if closer_to_key_peers.len() >= consensus_width {
            // Not close enough
            (
                false,
                Default::default(),
                Default::default(),
                Default::default(),
                Default::default(),
                Default::default(),
            )
        } else {
            // Close enough, lets get it

            // See if we have this record ourselves
            let storage_manager = self.storage_manager();
            let inbound_transact_begin_result = network_result_try!(storage_manager
                .inbound_transact_begin(
                    opaque_record_key.clone(),
                    opt_descriptor,
                    want_descriptor,
                    signing_member_id,
                )
                .measure_debug(TimestampDuration::new_ms(100), |dur| {
                    veilid_log!(self debug "inbound_transact_begin: {}", dur);
                })
                .await
                .map_err(RPCError::internal)?);

            match inbound_transact_begin_result {
                InboundTransactBeginResult::Success(transact_begin_result) => (
                    true,
                    false,
                    transact_begin_result
                        .opt_descriptor
                        .as_ref()
                        .map(|x| x.as_ref().clone()),
                    Some(transact_begin_result.transaction_id),
                    Some(transact_begin_result.expiration),
                    transact_begin_result.seqs,
                ),
                InboundTransactBeginResult::NeedDescriptor => (
                    true,
                    true,
                    Default::default(),
                    Default::default(),
                    Default::default(),
                    Default::default(),
                ),
                InboundTransactBeginResult::TransactionUnavailable => (
                    true,
                    false,
                    Default::default(),
                    Default::default(),
                    Default::default(),
                    Default::default(),
                ),
            }
        };

        let duration = if let Some(expiration) = opt_expiration {
            expiration.duration_since(Timestamp::now())
        } else {
            TimestampDuration::new(0)
        };

        if debug_target_enabled!("dht") {
            let debug_string_answer = format!(
                "IN ===> TransactBeginA({}{}{}{} peers={}) ==> {} seqs:{}",
                opaque_record_key,
                if accepted { " +accept" } else { "" },
                if need_descriptor { " +needdesc" } else { "" },
                if let Some(xid) = opt_transaction_id {
                    format!(" xid={}", xid)
                } else {
                    "".to_string()
                },
                closer_to_key_peers.len(),
                msg.header.direct_sender_node_id(),
                transact_begin_seqs.to_table_string(),
            );

            veilid_log!(self debug target: "dht", "{}", debug_string_answer);
        }

        // Make TransactBegin answer
        let transact_begin_a = RPCOperationTransactBeginA::new(
            accepted,
            need_descriptor,
            opt_descriptor.clone(),
            opt_transaction_id.map(|id| id.into()),
            duration,
            transact_begin_seqs,
            closer_to_key_peers,
        )?;

        // Send TransactBegin answer
        Box::pin(self.answer(
            msg,
            RPCAnswer::new(RPCAnswerDetail::TransactBeginA(Box::new(transact_begin_a))),
            None,
        ))
        .measure_debug(TimestampDuration::new_ms(200), |dur| {
            veilid_log!(self debug "process_transact_begin_q answer ({}xid={:?}{}{}): {}",
                if accepted {
                    "+accepted "
                } else {
                    ""
                },
                opt_transaction_id,
                if need_descriptor {
                    " +needdesc"
                } else {
                    ""
                },
                if opt_descriptor.is_some() {
                    " +senddesc"
                } else {
                    ""
                },
                dur
            );
        })
        .await
    }
}