prns-runtime 0.3.4

Runtime-neutral Personal Reticulum node contracts and manifold kernel
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
use core::marker::PhantomData;
use core::mem::MaybeUninit;

use crate::engine::EngineState;
use crate::engine::RatchetPolicy;
use crate::identity::held::HoldIdentityError;
use crate::identity::{Zeroizing, IDENTITY_SECRET_KEY_LEN};
use crate::routing::links::resources::ResourceStrategy;
use crate::routing::request_handlers::RequestHandlerError;
use crate::routing::upstream_app_destinations::RegisterDestinationError;
use crate::routing::{LinkRequestPolicy, ProofStrategy};
use crate::storage::StorageLayout;
use crate::storage::TablePushError;
use crate::units::ByteLimit;
use crate::wire::DestinationHash;

use super::super::request_endpoints::RequestEndpointSet;
use super::super::PrnsEvent;
use super::recipe::{PreConfiguredDestination, PrnsNodeRecipe, ServeMyRequestEndpoints};

pub struct AssembledNode<St, R, F, S>
where
    S: StorageLayout,
{
    pub engine: EngineState<S>,
    pub state: St,
    pub on_event: F,
    pub request_endpoints: PhantomData<R>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigurePreconfiguredDestinationError {
    HoldIdentity(HoldIdentityError),
    Register(RegisterDestinationError),
    RegisterRequestHandler(TablePushError),
    SeedRequester(RequestHandlerError),
    ServesEmptyEndpointSet,
}

struct SingleDestinationConfiguration<'a> {
    app_name: &'a str,
    aspects: &'a [&'a str],
    identity: Zeroizing<[u8; IDENTITY_SECRET_KEY_LEN]>,
    app_data: &'a [u8],
    proof: ProofStrategy,
    link_requests: LinkRequestPolicy,
    ratchet: RatchetPolicy,
    resource_strategy: ResourceStrategy,
    maximum_request_bytes: ByteLimit,
}

pub fn configure_preconfigured_destination<'a, St, R, S>(
    engine: &mut EngineState<S>,
    destination: PreConfiguredDestination<'a>,
) -> Result<DestinationHash, ConfigurePreconfiguredDestinationError>
where
    R: RequestEndpointSet<St>,
    S: StorageLayout,
{
    match destination {
        PreConfiguredDestination::Plain { app_name, aspects } => engine
            .register_plain_destination(app_name, aspects)
            .map_err(ConfigurePreconfiguredDestinationError::Register),
        PreConfiguredDestination::Single {
            app_name,
            aspects,
            identity,
            announce_app_data,
            proof,
            link_requests,
            ratchet,
            resource_strategy,
            maximum_request_bytes,
            request_endpoints,
        } => configure_single_destination::<St, R, S>(
            engine,
            SingleDestinationConfiguration {
                app_name,
                aspects,
                identity,
                app_data: announce_app_data,
                proof,
                link_requests,
                ratchet,
                resource_strategy,
                maximum_request_bytes,
            },
            request_endpoints,
        ),
    }
}

fn configure_single_destination<St, R, S>(
    engine: &mut EngineState<S>,
    configuration: SingleDestinationConfiguration<'_>,
    request_endpoints: ServeMyRequestEndpoints,
) -> Result<DestinationHash, ConfigurePreconfiguredDestinationError>
where
    R: RequestEndpointSet<St>,
    S: StorageLayout,
{
    let SingleDestinationConfiguration {
        app_name,
        aspects,
        identity,
        app_data,
        proof,
        link_requests,
        ratchet,
        resource_strategy,
        maximum_request_bytes,
    } = configuration;
    let held = engine
        .hold_identity(identity)
        .map_err(ConfigurePreconfiguredDestinationError::HoldIdentity)?;
    let destination = engine
        .register_single_destination(
            &held,
            app_name,
            aspects,
            app_data,
            proof,
            link_requests,
            ratchet,
        )
        .map_err(ConfigurePreconfiguredDestinationError::Register)?;
    engine.set_default_resource_strategy(&destination, resource_strategy);
    engine.set_maximum_request_bytes(&destination, maximum_request_bytes);
    if matches!(request_endpoints, ServeMyRequestEndpoints::Yes) {
        register_request_routes_for::<St, R, S>(engine, destination)?;
    }
    Ok(destination)
}

fn register_request_routes_for<St, R, S>(
    engine: &mut EngineState<S>,
    destination: DestinationHash,
) -> Result<(), ConfigurePreconfiguredDestinationError>
where
    R: RequestEndpointSet<St>,
    S: StorageLayout,
{
    for (path, policy) in R::REGISTRATIONS {
        engine
            .register_request_handler(&destination, path, policy.engine_policy())
            .map_err(ConfigurePreconfiguredDestinationError::RegisterRequestHandler)?;
        for seed in policy.seed_list() {
            engine
                .allow_requester(&destination, path, *seed)
                .map_err(ConfigurePreconfiguredDestinationError::SeedRequester)?;
        }
    }
    Ok(())
}

#[allow(clippy::expect_used)]
pub fn assemble_node<'a, D, St, R, F, I, S, P>(
    recipe: PrnsNodeRecipe<D, St, R, F, I, S, P>,
) -> (AssembledNode<St, R, F, S>, I, P)
where
    D: IntoIterator<Item = PreConfiguredDestination<'a>>,
    R: RequestEndpointSet<St>,
    F: FnMut(PrnsEvent<'_>, &St),
    S: StorageLayout,
{
    let PrnsNodeRecipe {
        transport_identity,
        pre_configured_destinations,
        app_state,
        storage: _,
        request_endpoints: _,
        interfaces,
        persistence,
        on_event,
    } = recipe;

    let mut node = AssembledNode {
        engine: EngineState::<S>::default(),
        state: app_state,
        on_event,
        request_endpoints: PhantomData,
    };
    configure_assembled_node(&mut node, pre_configured_destinations, transport_identity);
    (node, interfaces, persistence)
}

#[expect(
    unsafe_code,
    clippy::undocumented_unsafe_blocks,
    reason = "every AssembledNode field is initialized before the slot is exposed"
)]
pub fn assemble_node_in_place<'a, 'slot, D, St, R, F, I, S, P>(
    slot: &'slot mut MaybeUninit<AssembledNode<St, R, F, S>>,
    recipe: PrnsNodeRecipe<D, St, R, F, I, S, P>,
) -> (&'slot mut AssembledNode<St, R, F, S>, I, P)
where
    D: IntoIterator<Item = PreConfiguredDestination<'a>>,
    R: RequestEndpointSet<St>,
    F: FnMut(PrnsEvent<'_>, &St),
    S: StorageLayout,
{
    let PrnsNodeRecipe {
        transport_identity,
        pre_configured_destinations,
        app_state,
        storage: _,
        request_endpoints: _,
        interfaces,
        persistence,
        on_event,
    } = recipe;
    let node = slot.as_mut_ptr();
    unsafe {
        let engine =
            &mut *core::ptr::addr_of_mut!((*node).engine).cast::<MaybeUninit<EngineState<S>>>();
        EngineState::init_in_place(engine);
        core::ptr::addr_of_mut!((*node).state).write(app_state);
        core::ptr::addr_of_mut!((*node).on_event).write(on_event);
        core::ptr::addr_of_mut!((*node).request_endpoints).write(PhantomData);
    }
    let node = unsafe { slot.assume_init_mut() };
    configure_assembled_node(node, pre_configured_destinations, transport_identity);
    (node, interfaces, persistence)
}

#[allow(clippy::expect_used)]
fn configure_assembled_node<'a, D, St, R, F, S>(
    node: &mut AssembledNode<St, R, F, S>,
    pre_configured_destinations: D,
    transport_identity: Option<Zeroizing<[u8; IDENTITY_SECRET_KEY_LEN]>>,
) where
    D: IntoIterator<Item = PreConfiguredDestination<'a>>,
    R: RequestEndpointSet<St>,
    F: FnMut(PrnsEvent<'_>, &St),
    S: StorageLayout,
{
    let mut any_destination_declared = false;
    let mut any_destination_serves = false;
    for destination in pre_configured_destinations {
        any_destination_declared = true;
        any_destination_serves |= matches!(
            destination,
            PreConfiguredDestination::Single {
                request_endpoints: ServeMyRequestEndpoints::Yes,
                ..
            }
        );
        configure_preconfigured_destination::<St, R, S>(&mut node.engine, destination)
            .expect("recipe destination is valid and fits the store");
    }
    assert!(
        R::REGISTRATIONS.is_empty() || any_destination_serves || !any_destination_declared,
        "the recipe declares request endpoints but no destination serves them; set request_endpoints: ServeMyRequestEndpoints::Yes on a destination"
    );

    if let Some(secret) = transport_identity {
        let identity = node
            .engine
            .hold_identity(secret)
            .expect("the transport identity fits the held-identity store");
        node.engine
            .set_transport_identity(&identity)
            .expect("the transport identity was just held");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::identity::IdentityHash;
    use crate::routing::request_handlers::RequestPathHash;
    use crate::runtime::request_endpoints::{Decline, RequestContext, RequestEndpointPolicy};
    use crate::runtime::{ManuallyAttached, NoPersistence};
    use crate::storage::TestFixedStorage;

    type Storage = TestFixedStorage<4, 4, 128, 4, 4, 4, 2, 2, 2, 2, 2, 2>;

    struct Routes;

    impl RequestEndpointSet<()> for Routes {
        const REGISTRATIONS: &'static [(&'static str, RequestEndpointPolicy)] =
            &[("/test", RequestEndpointPolicy::AllowList(&[]))];

        async fn dispatch(
            _cx: RequestContext<'_, ()>,
            _path_hash: RequestPathHash,
        ) -> Result<(), Decline> {
            Err(Decline::Ignore)
        }
    }

    fn configured_engine(
        request_endpoints: ServeMyRequestEndpoints,
        maximum_request_bytes: ByteLimit,
    ) -> (EngineState<Storage>, DestinationHash) {
        let mut engine = EngineState::<Storage>::default();
        let destination = configure_preconfigured_destination::<(), Routes, Storage>(
            &mut engine,
            PreConfiguredDestination::Single {
                app_name: "test",
                aspects: &["requests"],
                identity: Zeroizing::new([0x11; IDENTITY_SECRET_KEY_LEN]),
                announce_app_data: &[],
                proof: ProofStrategy::ProveAll,
                link_requests: LinkRequestPolicy::AcceptAll,
                ratchet: RatchetPolicy::NoRatchets,
                resource_strategy: ResourceStrategy::AcceptNone,
                maximum_request_bytes,
                request_endpoints,
            },
        )
        .expect("the test destination fits fixed storage");
        (engine, destination)
    }

    #[test]
    fn node_route_set_attaches_routes_to_the_destination() {
        let (mut engine, destination) =
            configured_engine(ServeMyRequestEndpoints::Yes, ByteLimit::Unlimited);

        assert_eq!(
            engine.allow_requester(&destination, "/test", IdentityHash::new([0x22; 16])),
            Ok(())
        );
    }

    #[test]
    fn none_leaves_routes_unattached_from_the_destination() {
        let (mut engine, destination) =
            configured_engine(ServeMyRequestEndpoints::No, ByteLimit::Unlimited);

        assert_eq!(
            engine.allow_requester(&destination, "/test", IdentityHash::new([0x22; 16])),
            Err(RequestHandlerError::NoSuchHandler)
        );
    }

    #[test]
    fn recipe_request_limit_reaches_the_registered_destination() {
        let (engine, destination) =
            configured_engine(ServeMyRequestEndpoints::No, ByteLimit::Maximum(1_024));

        assert_eq!(
            engine
                .upstream_app_destinations()
                .find(|registered| registered.destination == destination)
                .and_then(|registered| match registered.kind {
                    crate::routing::upstream_app_destinations::UpstreamAppDestinationKind::Single {
                        maximum_request_bytes,
                        ..
                    } => Some(maximum_request_bytes),
                    crate::routing::upstream_app_destinations::UpstreamAppDestinationKind::Plain
                    | crate::routing::upstream_app_destinations::UpstreamAppDestinationKind::Group => None,
                }),
            Some(ByteLimit::Maximum(1_024)),
        );
    }

    #[test]
    fn in_place_assembly_initializes_and_configures_the_node() {
        let mut slot = MaybeUninit::uninit();
        let storage: Storage = TestFixedStorage;
        let (node, ManuallyAttached, NoPersistence) = assemble_node_in_place(
            &mut slot,
            PrnsNodeRecipe {
                transport_identity: Some(Zeroizing::new([0x33; IDENTITY_SECRET_KEY_LEN])),
                pre_configured_destinations: [PreConfiguredDestination::Plain {
                    app_name: "test",
                    aspects: &["plain"],
                }],
                app_state: (),
                storage,
                request_endpoints: (),
                interfaces: ManuallyAttached,
                persistence: NoPersistence,
                on_event: |_, _| {},
            },
        );

        assert!(node.engine.network_transport_enabled());
        assert_eq!(node.engine.held_identity_hashes().len(), 1);
        assert_eq!(node.engine.upstream_app_destinations().count(), 1);
    }

    #[test]
    #[should_panic(expected = "no destination serves them")]
    fn declared_endpoints_with_no_serving_destination_fail_loudly() {
        let mut slot = MaybeUninit::uninit();
        let storage: Storage = TestFixedStorage;
        let (_node, ManuallyAttached, NoPersistence) = assemble_node_in_place(
            &mut slot,
            PrnsNodeRecipe {
                transport_identity: None,
                pre_configured_destinations: [PreConfiguredDestination::Plain {
                    app_name: "test",
                    aspects: &["plain"],
                }],
                app_state: (),
                storage,
                request_endpoints: Routes,
                interfaces: ManuallyAttached,
                persistence: NoPersistence,
                on_event: |_, _| {},
            },
        );
    }
}