rings-node 1.0.0-pre.5

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Signed descriptors and DHT registration for intermediate `invoke` symbols (#834 D1′, D2).
//!
//! A node publishes one signed [`OnionInvokeDescriptor`] per `(shape, backend)` pair in `Σ_I × Ω`
//! under [`ONION_INVOKES_TOPIC`], carrying the same `process_epoch` `e_n` as its `relay`
//! registration and the `quota(n, f, ω)` policy ([`OnionInvokePolicy`]). Applications and client
//! constants are never published.

use std::borrow::Borrow;
use std::time::Duration;

use async_trait::async_trait;
use rings_core::delegation::DelegateeKey;
use rings_core::dht::Did;
use rings_core::domain_tag;
use rings_core::ecc::PublicKey;
use rings_core::ecc::VerificationPublicKey;
use rings_core::error::Error as CoreError;
use rings_core::error::Result as CoreResult;
use rings_core::message::DomainTag;
use rings_core::message::MessageSigner;
use rings_core::message::MessageVerification;
use rings_core::utils::SharedClock;
use serde::Deserialize;
use serde::Serialize;

use super::role::OnionInvokeOffer;
use super::signature::OnionBackendName;
use super::signature::OnionInvokeShape;
use super::OnionProcessEpoch;
use super::OnionProcessEpochCell;
use crate::descriptor::heard_elements;
use crate::descriptor::latest_valid_by_key;
use crate::descriptor::sign_descriptor_body;
use crate::descriptor::RegistryElement;
use crate::descriptor::SignedDescriptor;
use crate::descriptor::SignedDescriptorBody;
use crate::error::Error;
use crate::error::Result;
use crate::online::OnlineNodeType;
use crate::registration::DhtRegistrationPublisher;
use crate::registration::LeaseTerms;
use crate::registration::RegistrationContext;
use crate::registration::RegistrationTask;

/// DHT topic used for signed intermediate `invoke` symbol descriptors (#834 D2).
pub const ONION_INVOKES_TOPIC: &str = "onion_invokes";

/// Message family of the onion-invoke descriptor signature.
const ONION_INVOKE_DESCRIPTOR_DOMAIN_TAG: DomainTag =
    domain_tag!("rings-node:onion-invoke-descriptor");

/// Signed `quota(n, f, ω)` policy for one `(node, shape, backend)` registration (#834 D2).
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct OnionInvokePolicy {
    /// Maximum concurrent in-flight invocations of `(shape, backend)` on this node.
    /// `0` means unspecified (clamped to the protocol hard bound).
    pub max_concurrent: u32,
    /// Maximum invocations per minute of `(shape, backend)` on this node.
    /// `0` means unspecified.
    pub max_invocations_per_minute: u64,
}

/// Descriptor fields covered by the onion-invoke signature.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct OnionInvokeDescriptorBody {
    /// DID of the hosting node/account.
    pub did: Did,
    /// Account public key corresponding to `did`.
    pub public_key: VerificationPublicKey,
    /// Delegation public key: the Sphinx key `pk_i` a loop's header blinds the hop's layer to.
    pub delegatee_public_key: PublicKey<33>,
    /// Process epoch `e_n` of the registering process, equal to its `relay` registration's.
    pub process_epoch: OnionProcessEpoch,
    /// Runtime family of this node.
    pub node_type: OnlineNodeType,
    /// Network identifier.
    pub network_id: u32,
    /// Closed `invoke` shape `f ∈ Σ_I`.
    pub shape: OnionInvokeShape,
    /// Canonical open backend name `ω ∈ Ω`.
    pub backend: OnionBackendName,
    /// Signed per-`(node, shape, backend)` quota policy.
    pub policy: OnionInvokePolicy,
    /// Process start timestamp in milliseconds since Unix epoch.
    pub started_at_ms: u128,
    /// Freshness window `T` in milliseconds.
    pub fresh_for_ms: u64,
    /// Lease end `ℓ` in milliseconds since Unix epoch.
    pub expires_at_ms: u128,
    /// Node software version.
    pub version: String,
}

impl OnionInvokeDescriptorBody {
    fn body_ref(&self) -> OnionInvokeDescriptorBodyRef<'_> {
        OnionInvokeDescriptorBodyRef {
            did: self.did,
            public_key: &self.public_key,
            delegatee_public_key: &self.delegatee_public_key,
            process_epoch: self.process_epoch,
            node_type: &self.node_type,
            network_id: self.network_id,
            shape: &self.shape,
            backend: &self.backend,
            policy: &self.policy,
            started_at_ms: self.started_at_ms,
            fresh_for_ms: self.fresh_for_ms,
            expires_at_ms: self.expires_at_ms,
            version: self.version.as_str(),
        }
    }

    fn signing_data(&self) -> CoreResult<Vec<u8>> {
        self.body_ref().signing_data()
    }
}

impl SignedDescriptorBody for OnionInvokeDescriptorBody {
    type Descriptor = OnionInvokeDescriptor;

    const DOMAIN_TAG: DomainTag = ONION_INVOKE_DESCRIPTOR_DOMAIN_TAG;

    fn body_did(&self) -> Did {
        self.did
    }

    fn body_public_key(&self) -> &VerificationPublicKey {
        &self.public_key
    }

    fn body_network_id(&self) -> u32 {
        self.network_id
    }

    fn body_signing_data(&self) -> CoreResult<Vec<u8>> {
        self.signing_data()
    }

    fn into_signed_descriptor(self, signature: MessageVerification) -> Self::Descriptor {
        OnionInvokeDescriptor {
            did: self.did,
            public_key: self.public_key,
            delegatee_public_key: self.delegatee_public_key,
            process_epoch: self.process_epoch,
            node_type: self.node_type,
            network_id: self.network_id,
            shape: self.shape,
            backend: self.backend,
            policy: self.policy,
            started_at_ms: self.started_at_ms,
            heartbeat_at_ms: 0,
            fresh_for_ms: self.fresh_for_ms,
            expires_at_ms: self.expires_at_ms,
            version: self.version,
            signature,
        }
    }
}

#[derive(Serialize)]
struct OnionInvokeDescriptorBodyRef<'a> {
    did: Did,
    public_key: &'a VerificationPublicKey,
    delegatee_public_key: &'a PublicKey<33>,
    process_epoch: OnionProcessEpoch,
    node_type: &'a OnlineNodeType,
    network_id: u32,
    shape: &'a OnionInvokeShape,
    backend: &'a OnionBackendName,
    policy: &'a OnionInvokePolicy,
    started_at_ms: u128,
    fresh_for_ms: u64,
    expires_at_ms: u128,
    version: &'a str,
}

impl OnionInvokeDescriptorBodyRef<'_> {
    fn signing_data(&self) -> CoreResult<Vec<u8>> {
        rings_codec::serialize(self).map_err(CoreError::CodecSerialize)
    }
}

/// Signed descriptor published for one `(shape, backend)` `invoke` registration (#834 D2).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct OnionInvokeDescriptor {
    /// DID of the hosting node/account.
    pub did: Did,
    /// Account public key corresponding to `did`.
    pub public_key: VerificationPublicKey,
    /// Delegation public key: the Sphinx key `pk_i` a loop's header blinds the hop's layer to.
    pub delegatee_public_key: PublicKey<33>,
    /// Process epoch `e_n` of the registering process, equal to its `relay` registration's.
    pub process_epoch: OnionProcessEpoch,
    /// Runtime family of this node.
    pub node_type: OnlineNodeType,
    /// Network identifier.
    pub network_id: u32,
    /// Closed `invoke` shape `f ∈ Σ_I`.
    pub shape: OnionInvokeShape,
    /// Canonical open backend name `ω ∈ Ω`.
    pub backend: OnionBackendName,
    /// Signed per-`(node, shape, backend)` quota policy.
    pub policy: OnionInvokePolicy,
    /// Process start timestamp in milliseconds since Unix epoch.
    pub started_at_ms: u128,
    /// When the reader last heard this descriptor, in milliseconds since Unix epoch (`0` until heard).
    #[serde(skip)]
    pub heartbeat_at_ms: u128,
    /// Freshness window `T` in milliseconds.
    pub fresh_for_ms: u64,
    /// Lease end `ℓ` in milliseconds since Unix epoch.
    pub expires_at_ms: u128,
    /// Node software version.
    pub version: String,
    /// Signature covering every field above except `heartbeat_at_ms`.
    pub signature: MessageVerification,
}

impl OnionInvokeDescriptor {
    /// Create and sign an onion-invoke descriptor.
    pub fn new_signed(
        body: OnionInvokeDescriptorBody,
        signer: MessageSigner<impl Borrow<DelegateeKey>>,
    ) -> CoreResult<Self> {
        sign_descriptor_body(
            body,
            signer,
            "onion invoke descriptor DID/public key/session mismatch",
        )
    }

    fn body_ref(&self) -> OnionInvokeDescriptorBodyRef<'_> {
        OnionInvokeDescriptorBodyRef {
            did: self.did,
            public_key: &self.public_key,
            delegatee_public_key: &self.delegatee_public_key,
            process_epoch: self.process_epoch,
            node_type: &self.node_type,
            network_id: self.network_id,
            shape: &self.shape,
            backend: &self.backend,
            policy: &self.policy,
            started_at_ms: self.started_at_ms,
            fresh_for_ms: self.fresh_for_ms,
            expires_at_ms: self.expires_at_ms,
            version: self.version.as_str(),
        }
    }

    fn signing_data(&self) -> CoreResult<Vec<u8>> {
        self.body_ref().signing_data()
    }

    /// Verify the descriptor signature and DID/public-key binding.
    pub fn verify_signature(&self, network_id: u32) -> bool {
        self.descriptor_verify_signature(network_id)
    }

    /// Returns whether this descriptor is expired at `now_ms`.
    pub fn is_expired_at(&self, now_ms: u128) -> bool {
        self.descriptor_is_expired_at(now_ms)
    }

    /// Returns whether a reader lists this descriptor as live at `now_ms`.
    pub fn is_live_at(&self, now_ms: u128, network_id: u32) -> bool {
        self.descriptor_is_live_at(now_ms, network_id)
    }

    /// Select the newest valid onion-invoke descriptor per `(DID, shape, backend)` (#834 D2).
    pub fn latest_valid_by_symbol_did(
        descriptors: impl IntoIterator<Item = Self>,
        now_ms: u128,
        network_id: u32,
        include_expired: bool,
    ) -> Vec<Self> {
        latest_valid_by_key(
            descriptors,
            Self::registration_key,
            now_ms,
            network_id,
            include_expired,
        )
        .into_values()
        .collect()
    }

    /// Return the registration key `(DID, shape, backend)`.
    pub(crate) fn registration_key(&self) -> (Did, OnionInvokeShape, OnionBackendName) {
        (self.did, self.shape, self.backend.clone())
    }
}

impl SignedDescriptor for OnionInvokeDescriptor {
    type Body = OnionInvokeDescriptorBody;

    fn descriptor_did(&self) -> Did {
        self.did
    }

    fn descriptor_public_key(&self) -> &VerificationPublicKey {
        &self.public_key
    }

    fn descriptor_network_id(&self) -> u32 {
        self.network_id
    }

    fn descriptor_signature(&self) -> &MessageVerification {
        &self.signature
    }

    fn descriptor_heartbeat_at_ms(&self) -> u128 {
        self.heartbeat_at_ms
    }

    fn descriptor_expires_at_ms(&self) -> u128 {
        self.expires_at_ms
    }

    fn descriptor_fresh_for_ms(&self) -> u64 {
        self.fresh_for_ms
    }

    fn descriptor_signing_data(&self) -> CoreResult<Vec<u8>> {
        self.signing_data()
    }

    fn heard_at(self, heartbeat_at_ms: u128) -> Self {
        Self {
            heartbeat_at_ms,
            ..self
        }
    }
}

impl RegistryElement for OnionInvokeDescriptor {}

/// Result of decoding one onion-invoke registry entry.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct OnionInvokeDescriptorDecodeReport {
    /// Descriptors that decoded successfully under the closed signature and backend grammar.
    pub descriptors: Vec<OnionInvokeDescriptor>,
    /// Number of registry values explicitly rejected at the decode boundary.
    pub rejected_values: usize,
}

impl OnionInvokeDescriptorDecodeReport {
    /// Return whether the entry contained values this node could not decode.
    pub const fn has_rejections(&self) -> bool {
        self.rejected_values > 0
    }
}

/// Periodic node-layer DHT registration for offered `(shape, backend)` pairs.
#[derive(Clone, Debug)]
pub(crate) struct OnionInvokeRegistration {
    heartbeat_interval: Duration,
    node_type: OnlineNodeType,
    process_epoch: OnionProcessEpochCell,
    started_at_ms: u128,
    offer: OnionInvokeOffer,
    clock: SharedClock,
    publisher: DhtRegistrationPublisher<Vec<OnionInvokeDescriptor>>,
}

impl OnionInvokeRegistration {
    pub(crate) fn new_with_clock(
        heartbeat_interval: Duration,
        ttl: Duration,
        node_type: OnlineNodeType,
        offer: OnionInvokeOffer,
        process_epoch: OnionProcessEpochCell,
        clock: SharedClock,
    ) -> Self {
        let started_at_ms = clock.now_ms();
        Self {
            heartbeat_interval,
            node_type,
            process_epoch,
            started_at_ms,
            offer,
            clock,
            publisher: DhtRegistrationPublisher::new(ONION_INVOKES_TOPIC, heartbeat_interval, ttl),
        }
    }

    /// Build this node's signed `invoke` descriptors under `terms`.
    pub fn descriptors_at(
        &self,
        context: &RegistrationContext<'_>,
        terms: LeaseTerms,
    ) -> Result<Vec<OnionInvokeDescriptor>> {
        self.offer
            .entries()
            .map(|((shape, backend), policy)| {
                OnionInvokeDescriptor::new_signed(
                    OnionInvokeDescriptorBody {
                        did: context.did(),
                        public_key: context.delegator_verification_pubkey()?,
                        delegatee_public_key: context.delegatee_key().delegatee_public_key(),
                        process_epoch: self.process_epoch.get(),
                        node_type: self.node_type.clone(),
                        network_id: context.network_id(),
                        shape: *shape,
                        backend: backend.clone(),
                        policy: *policy,
                        started_at_ms: self.started_at_ms,
                        fresh_for_ms: terms.fresh_for_ms,
                        expires_at_ms: terms.expires_at_ms,
                        version: crate::util::build_version(),
                    },
                    context.message_signer(),
                )
                .map_err(Error::CoreError)
            })
            .collect()
    }

    /// Publish one heartbeat of this node's signed `invoke` descriptors.
    pub async fn publish_descriptors(
        &self,
        context: &RegistrationContext<'_>,
    ) -> Result<Vec<OnionInvokeDescriptor>> {
        self.publisher
            .publish_lease(context, self.clock.now_ms(), |terms| {
                self.descriptors_at(context, terms)
            })
            .await
    }

    /// Decode `invoke` descriptors from a DHT entry, counting schema rejections.
    pub fn decode_descriptors_from_entry(
        entry: &rings_core::dht::entry::Entry,
    ) -> OnionInvokeDescriptorDecodeReport {
        let mut report = OnionInvokeDescriptorDecodeReport::default();
        for (value, heard_at_ms) in heard_elements(entry) {
            match OnionInvokeDescriptor::from_element(value) {
                Ok(descriptor) => report.descriptors.push(descriptor.heard_at(heard_at_ms)),
                Err(error) => {
                    report.rejected_values = report.rejected_values.saturating_add(1);
                    tracing::debug!(
                        "rejected onion-invoke descriptor registry value at schema boundary: {error}"
                    );
                }
            }
        }
        report
    }

    /// Decode `invoke` descriptors from a DHT entry, dropping rejected values.
    pub fn descriptors_from_entry(
        entry: &rings_core::dht::entry::Entry,
    ) -> Vec<OnionInvokeDescriptor> {
        let report = Self::decode_descriptors_from_entry(entry);
        if report.has_rejections() {
            tracing::warn!(
                rejected_values = report.rejected_values,
                "ignored unsupported onion-invoke descriptor registry values"
            );
        }
        report.descriptors
    }
}

#[cfg_attr(rings_browser, async_trait(?Send))]
#[cfg_attr(rings_native, async_trait)]
impl RegistrationTask for OnionInvokeRegistration {
    fn name(&self) -> &'static str {
        "onion-invoke"
    }

    fn interval(&self) -> Duration {
        self.heartbeat_interval
    }

    async fn register_once(&self, context: &RegistrationContext<'_>) -> Result<()> {
        self.publish_descriptors(context).await.map(|_| ())
    }
}