prns-core 0.3.4

Pure Reticulum engine and wire contract for Personal Reticulum
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
use core::marker::PhantomData;

use allocator_api2::alloc::{Allocator, Global};

use crate::crypto::ratchets::FixedSelfRatchetTable;
use crate::identity::destination_identity::{
    NoDestinationIdentityAppData, NoDestinationIdentityTable,
};
use crate::identity::held::FixedHeldIdentityTable;
use crate::interfaces::EMBEDDED_MAX_LINK_MTU;
#[cfg(feature = "flash")]
use crate::persistence::{
    flash_journal_record_storage_len, maximum_route_upsert_payload_len, self_ratchets_snapshot_len,
};
use crate::routing::announce::defaults::MAX_ANNOUNCE_IDS_PER_DESTINATION;
use crate::routing::announce::destination_announce_limit::{
    destination_announce_limit_index_buckets, FixedHeapDestinationAnnounceLimitTable,
};
use crate::routing::announce::held::FixedHeapHeldAnnounceTable;
use crate::routing::announce::interface_announce_limit::FixedInterfaceAnnounceLimitTable;
use crate::routing::announce::schedule::FixedHeapScheduledAnnounceQueue;
use crate::routing::announce::stored::{
    FixedHeapAnnounceIdHistory, FixedHeapAnnounceRecordTable, FixedHeapPackedAppDataArena,
};
use crate::routing::blackhole::{blackhole_index_buckets, FixedHeapBlackholeTable};
use crate::routing::dedup::FixedPacketHashHistory;
use crate::routing::delivery::receipts::FixedReceiptTable;
use crate::routing::group_keys::FixedGroupKeyTable;
use crate::routing::links::channel::channel_mdu;
use crate::routing::links::channel::receive::WINDOW_MAX_MESSAGES;
use crate::routing::links::channel::table::impls::FixedHeapChannelTable;
use crate::routing::links::resources::assembly::{
    FixedIncomingAssemblyTable, FixedStaticOutgoingAssemblyTable,
};
use crate::routing::links::resources::table::{
    FixedHeapResourceTable, IncomingResourceState, OutgoingResourceState,
};
use crate::routing::links::resources::{
    max_outgoing_resource_reaction_frames, max_part_count, sealed_transfer_bytes,
};
use crate::routing::links::table::FixedHeapLinkTable;
use crate::routing::links::transported::FixedHeapTransportedLinkTable;
use crate::routing::path_requests::interface_path_request_limit::FixedInterfacePathRequestLimitTable;
use crate::routing::path_requests::pending::FixedPendingPathRequestTable;
use crate::routing::path_requests::recent::FixedRecentPathRequestTable;
use crate::routing::path_requests::recursive::FixedRecursivePathRequestTable;
use crate::routing::path_requests::seen::FixedSeenPathRequestTable;
use crate::routing::request_handlers::FixedRequestHandlerTable;
use crate::routing::reverse_routes::FixedReverseRouteTable;
use crate::routing::route_expiry::LinearRouteExpiryIndex;
use crate::routing::routes::{route_index_buckets, FixedHeapRouteTable};
use crate::routing::tunnel::FixedTunnelTable;
use crate::routing::upstream_app_destinations::FixedUpstreamAppDestinationTable;
use crate::routing::warmth::FixedDepartedInterfaceTable;
use crate::storage::{DisplayedStorageLimits, StorageCapacity, StorageLayout};

const MAX_TRACKED_DESTINATIONS: usize = 512;
const MAX_UPSTREAM_APP_DESTINATIONS: usize = 2;
const MAX_HELD_IDENTITIES: usize = 1;
const MAX_LINK_SESSIONS: usize = 512;
const MAX_TRANSPORTED_LINKS: usize = 32;
const MAX_OUTSTANDING_RECEIPTS: usize = 8;
const MAX_PACKET_HASHES: usize = 48;
const MAX_BLACKHOLED_IDENTITIES: usize = 32;
const BLACKHOLE_REASON_BYTES: usize = 64;
const MAX_REVERSE_ROUTES: usize = 32;
const MAX_PENDING_PATH_REQUESTS: usize = 8;
const MAX_HELD_ANNOUNCES: usize = 512;
const RETAINED_RATCHETS_PER_DESTINATION: usize = 8;
/// Cheap: an open channel costs a metadata row; the bulk payloads live in [`CHANNEL_WINDOW_POOL`].
const MAX_CONCURRENT_CHANNELS: usize = 8;
const MAX_RESOURCE_ASSEMBLIES: usize = 1;
/// The real PSRAM dial; a channel that finds the pool dry cannot grow its window until another drains a slot.
const CHANNEL_WINDOW_POOL: usize = 192;
/// Incoming resources retain the existing conservative PSRAM bound.
const MAX_INCOMING_RESOURCE_TRANSFER_BYTES: usize = 8192;
/// Source-capable S3 nodes reuse one outgoing plaintext window for each archive segment.
#[cfg(feature = "large-static-responses")]
const MAX_OUTGOING_RESOURCE_PLAINTEXT_BYTES: usize = 256 * 1024;
#[cfg(not(feature = "large-static-responses"))]
const MAX_OUTGOING_RESOURCE_PLAINTEXT_BYTES: usize = 8192;
const MAX_OUTGOING_RESOURCE_TRANSFER_BYTES: usize =
    sealed_transfer_bytes(MAX_OUTGOING_RESOURCE_PLAINTEXT_BYTES);
const RETAINED_ANNOUNCE_APP_DATA_BYTES: usize = 40 * 1024;
const ROUTE_INDEX_BUCKETS: usize = route_index_buckets(MAX_TRACKED_DESTINATIONS);
const DESTINATION_ANNOUNCE_LIMIT_INDEX_BUCKETS: usize =
    destination_announce_limit_index_buckets(MAX_TRACKED_DESTINATIONS);
const MAX_OUTGOING_RESOURCE_PARTS: usize = max_part_count(MAX_OUTGOING_RESOURCE_TRANSFER_BYTES);
const MAX_INCOMING_RESOURCE_PARTS: usize = max_part_count(MAX_INCOMING_RESOURCE_TRANSFER_BYTES);
const CHANNEL_REORDER_DEPTH: usize = WINDOW_MAX_MESSAGES as usize;
const CHANNEL_MESSAGE_BYTES: usize = channel_mdu(EMBEDDED_MAX_LINK_MTU);

/// The PSRAM-backed ESP32-S3 storage profile.
///
/// `MAX_REQUEST_HANDLERS` is independent from the upstream-application destination capacity:
/// applications commonly register several request paths on one destination. It defaults to the
/// historical two rows, while application recipes with more routes should supply their exact
/// registration count.
pub struct Esp32S3<
    A: Allocator = Global,
    const MAX_REQUEST_HANDLERS: usize = MAX_UPSTREAM_APP_DESTINATIONS,
>(PhantomData<A>);

impl<A: Allocator, const MAX_REQUEST_HANDLERS: usize> Esp32S3<A, MAX_REQUEST_HANDLERS> {
    pub const TRACKED_DESTINATIONS: usize = MAX_TRACKED_DESTINATIONS;

    /// Cheap retained sessions outnumber every configured auto-interface fleet member, leaving
    /// admission room without multiplying resource or channel workspaces.
    pub const LINK_SESSIONS: usize = MAX_LINK_SESSIONS;

    /// The most frames one resource request can synchronously emit for this storage recipe.
    ///
    /// A request names at most `WINDOW_MAX` existing parts, and a response can append one hashmap
    /// update. Compact layouts cannot materialize a full 75-part resource, so deriving the bound
    /// from their outgoing store avoids reserving unreachable transport backlog on every lane.
    pub const MAX_OUTGOING_RESOURCE_REACTION_FRAMES: usize =
        max_outgoing_resource_reaction_frames(MAX_OUTGOING_RESOURCE_TRANSFER_BYTES);
}

#[cfg(feature = "flash")]
impl<A: Allocator, const MAX_REQUEST_HANDLERS: usize> Esp32S3<A, MAX_REQUEST_HANDLERS> {
    pub const MAX_CRITICAL_FLASH_JOURNAL_BYTES: usize = MAX_UPSTREAM_APP_DESTINATIONS
        * flash_journal_record_storage_len(
            crate::wire::TRUNCATED_HASH_BYTE_LEN
                + self_ratchets_snapshot_len(RETAINED_RATCHETS_PER_DESTINATION),
            4,
        );

    pub const MAX_COMPACTED_FLASH_JOURNAL_BYTES: usize = MAX_TRACKED_DESTINATIONS
        * (flash_journal_record_storage_len(maximum_route_upsert_payload_len(0, 0), 4) + 3)
        + RETAINED_ANNOUNCE_APP_DATA_BYTES
        + MAX_UPSTREAM_APP_DESTINATIONS
            * flash_journal_record_storage_len(
                crate::wire::TRUNCATED_HASH_BYTE_LEN
                    + self_ratchets_snapshot_len(RETAINED_RATCHETS_PER_DESTINATION),
                4,
            )
        + flash_journal_record_storage_len(0, 4);
}

impl<A: Allocator, const MAX_REQUEST_HANDLERS: usize> Default for Esp32S3<A, MAX_REQUEST_HANDLERS> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<A: Allocator + Default, const MAX_REQUEST_HANDLERS: usize> StorageLayout
    for Esp32S3<A, MAX_REQUEST_HANDLERS>
{
    const LIMITS: DisplayedStorageLimits = DisplayedStorageLimits {
        tracked_destinations: StorageCapacity::Fixed(MAX_TRACKED_DESTINATIONS),
        destination_identities: StorageCapacity::Fixed(0),
        announce_records: StorageCapacity::Fixed(MAX_TRACKED_DESTINATIONS),
        upstream_app_destinations: StorageCapacity::Fixed(MAX_UPSTREAM_APP_DESTINATIONS),
        held_identities: StorageCapacity::Fixed(MAX_HELD_IDENTITIES),
        links: StorageCapacity::Fixed(MAX_LINK_SESSIONS),
        channels: StorageCapacity::Fixed(MAX_CONCURRENT_CHANNELS),
        channel_window_pool: Some(CHANNEL_WINDOW_POOL),
        channel_reorder_depth: StorageCapacity::Fixed(CHANNEL_REORDER_DEPTH),
        link_mtu: StorageCapacity::Fixed(EMBEDDED_MAX_LINK_MTU),
        resource_transfer_bytes: StorageCapacity::Fixed(MAX_INCOMING_RESOURCE_TRANSFER_BYTES),
        receipts: StorageCapacity::Fixed(MAX_OUTSTANDING_RECEIPTS),
        packet_hashes: StorageCapacity::Fixed(MAX_PACKET_HASHES),
        blackholed_identities: StorageCapacity::Fixed(MAX_BLACKHOLED_IDENTITIES),
        blackhole_reason_bytes: StorageCapacity::Fixed(BLACKHOLE_REASON_BYTES),
        reverse_routes: StorageCapacity::Fixed(MAX_REVERSE_ROUTES),
        pending_path_requests: StorageCapacity::Fixed(MAX_PENDING_PATH_REQUESTS),
        held_announces: StorageCapacity::Fixed(MAX_HELD_ANNOUNCES),
        ratchets_per_destination: StorageCapacity::Fixed(RETAINED_RATCHETS_PER_DESTINATION),
    };

    type Routes = FixedHeapRouteTable<MAX_TRACKED_DESTINATIONS, ROUTE_INDEX_BUCKETS, A>;
    type RouteExpiries = LinearRouteExpiryIndex;
    type DestinationIdentities = NoDestinationIdentityTable;
    type DestinationIdentityAppData = NoDestinationIdentityAppData;
    type Announces = FixedHeapAnnounceRecordTable<MAX_TRACKED_DESTINATIONS, A>;
    type History =
        FixedHeapAnnounceIdHistory<MAX_TRACKED_DESTINATIONS, MAX_ANNOUNCE_IDS_PER_DESTINATION, A>;
    type AppData =
        FixedHeapPackedAppDataArena<RETAINED_ANNOUNCE_APP_DATA_BYTES, MAX_TRACKED_DESTINATIONS, A>;
    type ScheduledAnnounces = FixedHeapScheduledAnnounceQueue<MAX_TRACKED_DESTINATIONS, A>;
    type UpstreamAppDestinations = FixedUpstreamAppDestinationTable<MAX_UPSTREAM_APP_DESTINATIONS>;
    type HeldIdentities = FixedHeldIdentityTable<MAX_HELD_IDENTITIES>;
    type SelfRatchets =
        FixedSelfRatchetTable<MAX_UPSTREAM_APP_DESTINATIONS, RETAINED_RATCHETS_PER_DESTINATION>;
    type Receipts = FixedReceiptTable<MAX_OUTSTANDING_RECEIPTS>;
    type PacketHashes = FixedPacketHashHistory<MAX_PACKET_HASHES>;
    type Blackholes = FixedHeapBlackholeTable<
        MAX_BLACKHOLED_IDENTITIES,
        { blackhole_index_buckets(MAX_BLACKHOLED_IDENTITIES) },
        BLACKHOLE_REASON_BYTES,
        A,
    >;
    type ReverseRoutes = FixedReverseRouteTable<MAX_REVERSE_ROUTES>;
    type DepartedInterfaces = FixedDepartedInterfaceTable<16>;
    type PendingPathRequests = FixedPendingPathRequestTable<MAX_PENDING_PATH_REQUESTS>;
    type RecentPathRequests = FixedRecentPathRequestTable<8>;
    type SeenPathRequests = FixedSeenPathRequestTable<8>;
    type Tunnels = FixedTunnelTable<0>;
    type RecursivePathRequests = FixedRecursivePathRequestTable<8>;
    type InterfacePathRequestLimits = FixedInterfacePathRequestLimitTable<8>;
    type InterfaceAnnounceLimits = FixedInterfaceAnnounceLimitTable<8>;
    type DirtyInterfaces = heapless::Vec<crate::interfaces::InterfaceId, 8>;
    type HeldAnnounces = FixedHeapHeldAnnounceTable<MAX_HELD_ANNOUNCES, A>;
    type HeldAnnounceAppData = FixedHeapPackedAppDataArena<65536, MAX_HELD_ANNOUNCES, A>;
    type DestinationAnnounceLimits = FixedHeapDestinationAnnounceLimitTable<
        MAX_TRACKED_DESTINATIONS,
        DESTINATION_ANNOUNCE_LIMIT_INDEX_BUCKETS,
        A,
    >;
    type GroupKeys = FixedGroupKeyTable<MAX_UPSTREAM_APP_DESTINATIONS>;
    type RequestHandlers = FixedRequestHandlerTable<MAX_REQUEST_HANDLERS>;
    type TransportedLinks = FixedHeapTransportedLinkTable<MAX_TRANSPORTED_LINKS, A>;
    type Links = FixedHeapLinkTable<MAX_LINK_SESSIONS, A>;
    type OutgoingResources = FixedHeapResourceTable<
        OutgoingResourceState,
        1,
        MAX_OUTGOING_RESOURCE_TRANSFER_BYTES,
        MAX_OUTGOING_RESOURCE_PARTS,
        A,
    >;
    type IncomingResources = FixedHeapResourceTable<
        IncomingResourceState,
        1,
        MAX_INCOMING_RESOURCE_TRANSFER_BYTES,
        MAX_INCOMING_RESOURCE_PARTS,
        A,
    >;
    type IncomingAssemblies = FixedIncomingAssemblyTable<MAX_RESOURCE_ASSEMBLIES>;
    type OutgoingAssemblies = FixedStaticOutgoingAssemblyTable<MAX_RESOURCE_ASSEMBLIES>;
    type Channels = FixedHeapChannelTable<
        MAX_CONCURRENT_CHANNELS,
        CHANNEL_REORDER_DEPTH,
        CHANNEL_MESSAGE_BYTES,
        CHANNEL_WINDOW_POOL,
        A,
    >;
}

#[cfg(test)]
mod tests {
    use super::Esp32S3;
    use crate::engine::EngineState;
    use crate::routing::links::resources::assembly::{
        IncomingAssemblyTable, OutgoingAssemblyTable,
    };
    use crate::routing::links::table::LinkTable;
    use crate::routing::links::transported::TransportedLinkTable;
    use crate::routing::request_handlers::RequestHandlerTable;
    use crate::routing::routes::RouteTable;
    use crate::storage::{StorageCapacity, StorageLayout};

    #[test]
    fn outgoing_resource_reaction_bound_matches_the_storage_recipe() {
        type Layout = Esp32S3<allocator_api2::alloc::Global>;
        #[cfg(not(feature = "large-static-responses"))]
        assert_eq!(Layout::MAX_OUTGOING_RESOURCE_REACTION_FRAMES, 19);
        #[cfg(feature = "large-static-responses")]
        assert_eq!(Layout::MAX_OUTGOING_RESOURCE_REACTION_FRAMES, 76);
    }

    #[test]
    fn limits_report_the_storage_constants() {
        type L = Esp32S3;
        assert_eq!(
            <L as StorageLayout>::LIMITS.tracked_destinations,
            StorageCapacity::Fixed(512)
        );
        assert_eq!(
            <L as StorageLayout>::LIMITS.upstream_app_destinations,
            StorageCapacity::Fixed(super::MAX_UPSTREAM_APP_DESTINATIONS)
        );
        assert_eq!(
            <L as StorageLayout>::LIMITS.held_identities,
            StorageCapacity::Fixed(super::MAX_HELD_IDENTITIES)
        );
        assert_eq!(
            <L as StorageLayout>::LIMITS.links,
            StorageCapacity::Fixed(512)
        );
        assert_eq!(
            <L as StorageLayout>::LIMITS.channels,
            StorageCapacity::Fixed(8)
        );
        assert_eq!(<L as StorageLayout>::LIMITS.channel_window_pool, Some(192));
        assert_eq!(
            <<L as StorageLayout>::Routes as Default>::default().capacity(),
            512
        );
        assert_eq!(
            <<L as StorageLayout>::Links as Default>::default().capacity(),
            512
        );
        assert_eq!(
            <<L as StorageLayout>::TransportedLinks as Default>::default().capacity(),
            32
        );
        assert_eq!(
            <<L as StorageLayout>::IncomingAssemblies as Default>::default().capacity(),
            1
        );
        assert_eq!(
            <<L as StorageLayout>::OutgoingAssemblies as Default>::default().capacity(),
            1
        );
        let engine = EngineState::<L>::default();
        assert_eq!(
            engine.incoming_resources.transfer_capacity(),
            super::MAX_INCOMING_RESOURCE_TRANSFER_BYTES,
            "source serving must not raise the incoming resource limit"
        );
        assert_eq!(
            engine.outgoing_resources.transfer_capacity(),
            super::MAX_OUTGOING_RESOURCE_TRANSFER_BYTES
        );
        assert!(engine.outgoing_assemblies.supports_static_continuations());
        #[cfg(feature = "large-static-responses")]
        assert_eq!(super::MAX_OUTGOING_RESOURCE_PLAINTEXT_BYTES, 256 * 1024);
        #[cfg(not(feature = "large-static-responses"))]
        assert_eq!(
            super::MAX_OUTGOING_RESOURCE_PLAINTEXT_BYTES,
            super::MAX_INCOMING_RESOURCE_TRANSFER_BYTES
        );
    }

    #[test]
    fn request_handler_capacity_is_an_application_owned_dimension() {
        type L = Esp32S3<allocator_api2::alloc::Global, 5>;
        let handlers = <L as StorageLayout>::RequestHandlers::default();
        assert_eq!(handlers.capacity(), 5);
        assert_eq!(
            <L as StorageLayout>::LIMITS.upstream_app_destinations,
            StorageCapacity::Fixed(super::MAX_UPSTREAM_APP_DESTINATIONS)
        );
    }

    #[test]
    fn print_footprint() {
        type L = Esp32S3;
        println!(
            "EngineState<Esp32S3<Global>> = {} bytes SRAM-resident (host 64-bit; the cold bulk lives behind boxes in PSRAM and is not counted here)",
            core::mem::size_of::<EngineState<L>>()
        );
        println!(
            "  Routes        {:>6} B (index inline; rows boxed)",
            core::mem::size_of::<<L as StorageLayout>::Routes>()
        );
        println!(
            "  Announces     {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::Announces>()
        );
        println!(
            "  History       {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::History>()
        );
        println!(
            "  AppData       {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::AppData>()
        );
        println!(
            "  ScheduledAnn  {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::ScheduledAnnounces>()
        );
        println!(
            "  DestinationAnnounceLimits {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::DestinationAnnounceLimits>()
        );
        println!(
            "  ReverseRoutes {:>6} B (inline)",
            core::mem::size_of::<<L as StorageLayout>::ReverseRoutes>()
        );
        println!(
            "  PacketHashes  {:>6} B (inline)",
            core::mem::size_of::<<L as StorageLayout>::PacketHashes>()
        );
        println!(
            "  Blackholes    {:>6} B (index inline; rows allocated)",
            core::mem::size_of::<<L as StorageLayout>::Blackholes>()
        );
        println!(
            "  UpstreamApps  {:>6} B (inline)",
            core::mem::size_of::<<L as StorageLayout>::UpstreamAppDestinations>()
        );
        println!(
            "  HeldIds       {:>6} B (inline)",
            core::mem::size_of::<<L as StorageLayout>::HeldIdentities>()
        );
        println!(
            "  Receipts      {:>6} B (inline)",
            core::mem::size_of::<<L as StorageLayout>::Receipts>()
        );
        println!(
            "  Links         {:>6} B (inline)",
            core::mem::size_of::<<L as StorageLayout>::Links>()
        );
        println!(
            "  OutResources  {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::OutgoingResources>()
        );
        println!(
            "  InResources   {:>6} B (boxed)",
            core::mem::size_of::<<L as StorageLayout>::IncomingResources>()
        );
        println!(
            "  Channels      {:>6} B (metadata inline; payload pools boxed)",
            core::mem::size_of::<<L as StorageLayout>::Channels>()
        );
    }
}