aingle_minimal 0.6.2

Ultra-light AIngle node for IoT devices (<1MB RAM)
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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! The main [`MinimalNode`] implementation.
//!
//! This module ties together all the components of the lightweight node, including
//! storage, networking, cryptography, and the gossip/sync protocols. The [`MinimalNode`]
//! struct is the primary entry point for running an AIngle node on resource-constrained
//! devices.
//!
//! # Architecture
//!
//! The node coordinates several subsystems:
//! - **Storage**: Persistent data storage via SQLite or RocksDB
//! - **Network**: Peer-to-peer communication via CoAP or QUIC
//! - **Gossip**: Data propagation and peer discovery protocol
//! - **Sync**: Consistency maintenance with peers via bloom filters
//! - **Crypto**: Ed25519 signatures for authentication
//!
//! # Examples
//!
//! Basic node creation and operation:
//!
//! ```no_run
//! # use aingle_minimal::{MinimalNode, Config};
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create and start a node
//! let config = Config::iot_mode();
//! let mut node = MinimalNode::new(config)?;
//!
//! // Run the node (blocks until stopped)
//! smol::block_on(async {
//!     node.run().await
//! })?;
//! # Ok(())
//! # }
//! ```

use crate::config::Config;
use crate::crypto::Keypair;
use crate::error::Result;
use crate::gossip::GossipManager;
use crate::network::{Message, Network};
use crate::storage_factory::DynamicStorage;
use crate::storage_trait::StorageBackend;
use crate::sync::SyncManager;
use crate::types::*;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Key used to store known peers in metadata
const PEERS_METADATA_KEY: &str = "known_peers";

/// Interval for auto-saving peers (in seconds)
const PEER_SAVE_INTERVAL_SECS: u64 = 300; // 5 minutes

/// A serializable record of a known peer for persistence.
///
/// This struct captures essential information about a peer that can be
/// saved to storage and restored when the node restarts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerRecord {
    /// The peer's network address (IP:port).
    pub addr: String,
    /// The last known sequence number from this peer.
    pub latest_seq: u32,
    /// Quality score from 0-100 (higher is better).
    pub quality: u8,
    /// Unix timestamp (seconds) of when this peer was last seen.
    pub last_seen_secs: u64,
}

/// A minimal, lightweight AIngle node designed for IoT and resource-constrained devices.
///
/// `MinimalNode` is the core component of the aingle_minimal crate, providing a
/// complete AIngle node that can run on devices with less than 1MB RAM. It manages
/// data storage, network communication, peer synchronization, and cryptographic
/// operations.
///
/// # Features
///
/// - **Sub-second confirmation**: Configurable publish intervals from immediate to minutes
/// - **Zero-fee transactions**: No staking or gas fees required
/// - **Mesh networking**: Support for WiFi, BLE, LoRa, and traditional IP networks
/// - **Power-aware**: Adaptive modes for battery-operated devices
/// - **Auto-discovery**: mDNS-based peer finding on local networks
///
/// # Examples
///
/// ## Basic Usage
///
/// ```
/// # use aingle_minimal::{MinimalNode, Config};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Create a node with IoT configuration
/// let config = Config::iot_mode();
/// let mut node = MinimalNode::new(config)?;
///
/// // Create and store an entry
/// let hash = node.create_entry("Hello, AIngle!")?;
/// println!("Created entry: {}", hash.to_hex());
///
/// // Get node statistics
/// let stats = node.stats()?;
/// println!("Entries: {}, Peers: {}", stats.entries_count, stats.peer_count);
/// # Ok(())
/// # }
/// ```
///
/// ## Running the Node
///
/// ```no_run
/// # use aingle_minimal::{MinimalNode, Config};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut node = MinimalNode::new(Config::iot_mode())?;
///
/// // Add known peers
/// node.add_peer("192.168.1.100:5683".parse()?);
///
/// // Start the node's main loop
/// node.run().await?;
/// # Ok(())
/// # }
/// ```
pub struct MinimalNode {
    config: Config,
    keypair: Keypair,
    storage: DynamicStorage,
    network: Network,
    gossip: GossipManager,
    sync: SyncManager,
    running: Arc<AtomicBool>,
    start_time: Instant,
    /// Timestamp of the last peer save operation
    last_peer_save: Instant,
}

impl MinimalNode {
    /// Creates and initializes a new `MinimalNode` with the given configuration.
    ///
    /// This method performs several initialization steps:
    /// 1. Validates the configuration
    /// 2. Generates an Ed25519 keypair for the node's identity
    /// 3. Initializes the storage backend (SQLite, RocksDB, or Memory)
    /// 4. Sets up the network layer with the configured transport
    /// 5. Initializes gossip and sync managers
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Configuration validation fails (see [`Config::validate`])
    /// - Storage backend initialization fails
    /// - Network initialization fails
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Create a node with IoT configuration
    /// let node = MinimalNode::new(Config::iot_mode())?;
    /// println!("Node created with ID: {}", node.public_key().to_hex());
    ///
    /// // Create a node with custom configuration
    /// let mut config = Config::default();
    /// config.memory_limit = 512 * 1024; // 512KB
    /// let node = MinimalNode::new(config)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(config: Config) -> Result<Self> {
        // Validate configuration
        config.validate()?;

        // Generate or load keypair
        let keypair = Keypair::generate();

        // Initialize storage based on configuration
        let storage = DynamicStorage::from_config(config.storage.clone())?;
        log::info!("Using {} storage backend", storage.backend_name());

        // Initialize network
        let node_id = keypair.public_key().to_hex();
        let network = Network::new(config.transport.clone(), config.gossip.clone(), node_id);

        // Initialize gossip manager
        let gossip = GossipManager::new(config.gossip.clone());

        // Initialize sync manager with gossip loop delay as sync interval
        let sync = SyncManager::new(config.gossip.loop_delay * 2);

        let mut node = Self {
            config,
            keypair,
            storage,
            network,
            gossip,
            sync,
            running: Arc::new(AtomicBool::new(false)),
            start_time: Instant::now(),
            last_peer_save: Instant::now(),
        };

        // Load persisted peers from storage
        if let Err(e) = node.load_peers() {
            log::warn!("Failed to load persisted peers: {}", e);
        }

        Ok(node)
    }

    /// Returns the node's public key, which serves as its permanent identity on the network.
    ///
    /// The public key is derived from the node's Ed25519 keypair and uniquely identifies
    /// this agent across the AIngle network. It's used for signing actions and verifying
    /// the node's identity to peers.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let node = MinimalNode::new(Config::test_mode())?;
    /// let pubkey = node.public_key();
    /// println!("Node identity: {}", pubkey.to_hex());
    /// # Ok(())
    /// # }
    /// ```
    pub fn public_key(&self) -> AgentPubKey {
        self.keypair.public_key()
    }

    /// Returns statistics about the node's current state and performance.
    ///
    /// This provides real-time metrics useful for monitoring node health and resource usage.
    ///
    /// # Errors
    ///
    /// Returns an error if storage statistics cannot be retrieved.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Create some test data
    /// node.create_entry("test")?;
    ///
    /// // Get statistics
    /// let stats = node.stats()?;
    /// println!("Entries: {}", stats.entries_count);
    /// println!("Actions: {}", stats.actions_count);
    /// println!("Storage used: {} bytes", stats.storage_used);
    /// println!("Connected peers: {}", stats.peer_count);
    /// println!("Uptime: {} seconds", stats.uptime_secs);
    /// # Ok(())
    /// # }
    /// ```
    pub fn stats(&self) -> Result<NodeStats> {
        let storage_stats = self.storage.stats()?;

        Ok(NodeStats {
            entries_count: storage_stats.entry_count,
            actions_count: storage_stats.action_count,
            memory_used: 0, // Would need system call to measure
            storage_used: storage_stats.db_size,
            peer_count: self.network.peer_count(),
            uptime_secs: self.start_time.elapsed().as_secs(),
        })
    }

    /// Creates a new application entry, signs it, and stores it on the node's source chain.
    ///
    /// This is the primary method for publishing data to the AIngle network. The entry
    /// is automatically:
    /// 1. Serialized to JSON
    /// 2. Wrapped in a signed [`Action`]
    /// 3. Stored locally in the database
    /// 4. Queued for gossip to peers
    /// 5. Published immediately if `publish_interval` is zero
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Content cannot be serialized
    /// - Storage operation fails
    /// - Network publishing fails (when immediate publishing is enabled)
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # use serde::{Serialize, Deserialize};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Create a simple text entry
    /// let hash = node.create_entry("Hello, world!")?;
    /// println!("Created entry: {}", hash.to_hex());
    ///
    /// // Create a structured entry
    /// #[derive(Serialize, Deserialize)]
    /// struct SensorReading {
    ///     temperature: f64,
    ///     humidity: f64,
    ///     timestamp: u64,
    /// }
    ///
    /// let reading = SensorReading {
    ///     temperature: 23.5,
    ///     humidity: 65.2,
    ///     timestamp: 1234567890,
    /// };
    ///
    /// let hash = node.create_entry(reading)?;
    /// println!("Created sensor reading: {}", hash.to_hex());
    /// # Ok(())
    /// # }
    /// ```
    pub fn create_entry<T: serde::Serialize>(&mut self, content: T) -> Result<Hash> {
        // Create entry
        let entry = Entry::app(content)?;
        let entry_hash = entry.hash();

        // Get previous action
        let seq = self.storage.get_latest_seq()? + 1;
        let prev_action = if seq > 1 {
            // Would need to get actual previous action hash
            None
        } else {
            None
        };

        // Create action
        let action = Action {
            action_type: ActionType::Create,
            author: self.keypair.public_key(),
            timestamp: Timestamp::now(),
            seq,
            prev_action,
            entry_hash: Some(entry_hash.clone()),
            signature: self.sign_action_data(seq, &entry_hash),
        };

        // Store record
        let record = Record {
            action,
            entry: Some(entry),
        };

        let action_hash = self.storage.put_record(&record)?;

        // Queue for gossip
        self.gossip.announce(action_hash.clone());

        // Track in sync manager
        self.sync.add_local_hash(action_hash.clone());

        // Immediate publish if configured
        if self.config.publish_interval == Duration::ZERO {
            self.publish_pending()?;
        }

        Ok(action_hash)
    }

    /// Creates multiple entries in a single optimized batch operation.
    ///
    /// This is significantly faster than calling [`Self::create_entry`] multiple times
    /// because it:
    /// - Gets the latest sequence number once
    /// - Uses a single database transaction
    /// - Announces all entries for gossip at once
    ///
    /// # Performance
    ///
    /// For 100 entries, batch creation is typically 3-5x faster than individual
    /// creates due to reduced transaction overhead.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # use serde_json::json;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::iot_mode())?;
    ///
    /// // Batch create sensor readings
    /// let readings: Vec<_> = (0..10)
    ///     .map(|i| json!({"sensor": "temp", "value": 20.0 + i as f64}))
    ///     .collect();
    ///
    /// let hashes = node.create_entries_batch(&readings)?;
    /// println!("Created {} entries", hashes.len());
    /// # Ok(())
    /// # }
    /// ```
    pub fn create_entries_batch<T: serde::Serialize>(
        &mut self,
        contents: &[T],
    ) -> Result<Vec<Hash>> {
        if contents.is_empty() {
            return Ok(Vec::new());
        }

        // Get base sequence number once
        let base_seq = self.storage.get_latest_seq()? + 1;
        let author = self.keypair.public_key();
        let timestamp = Timestamp::now();

        // Build all records
        let mut records = Vec::with_capacity(contents.len());

        for (i, content) in contents.iter().enumerate() {
            let entry = Entry::app(content)?;
            let entry_hash = entry.hash();
            let seq = base_seq + i as u32;

            let action = Action {
                action_type: ActionType::Create,
                author: author.clone(),
                timestamp,
                seq,
                prev_action: None,
                entry_hash: Some(entry_hash.clone()),
                signature: self.sign_action_data(seq, &entry_hash),
            };

            records.push(Record {
                action,
                entry: Some(entry),
            });
        }

        // Store all records in single transaction
        let hashes = self.storage.put_records_batch(&records)?;

        // Announce all for gossip
        for hash in &hashes {
            self.gossip.announce(hash.clone());
            self.sync.add_local_hash(hash.clone());
        }

        // Immediate publish if configured
        if self.config.publish_interval == Duration::ZERO {
            self.publish_pending()?;
        }

        Ok(hashes)
    }

    /// Retrieves an [`Entry`] from storage by its content hash.
    ///
    /// This method looks up an entry in the local database. It only returns entries
    /// that have been stored locally, either created by this node or received via gossip.
    ///
    /// # Errors
    ///
    /// Returns an error if the storage backend fails to perform the lookup.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Create an entry
    /// let hash = node.create_entry("test data")?;
    ///
    /// // Retrieve it
    /// if let Some(entry) = node.get_entry(&hash)? {
    ///     println!("Found entry with {} bytes", entry.size());
    /// } else {
    ///     println!("Entry not found");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_entry(&self, hash: &Hash) -> Result<Option<Entry>> {
        self.storage.get_entry(hash)
    }

    /// Signs the essential data of an action.
    fn sign_action_data(&self, seq: u32, entry_hash: &Hash) -> Signature {
        let mut data = Vec::new();
        data.extend_from_slice(&seq.to_be_bytes());
        data.extend_from_slice(entry_hash.as_bytes());
        self.keypair.sign(&data)
    }

    /// Publishes pending announcements to the network via gossip.
    fn publish_pending(&mut self) -> Result<()> {
        let announcements = self.gossip.take_announcements(50);
        for hash in announcements {
            let message = Message::NewRecord { hash };
            smol::block_on(self.network.broadcast(&message))?;
        }
        Ok(())
    }

    /// Runs the node's main event loop.
    ///
    /// This async function starts the network, begins peer discovery, and enters the
    /// main loop to handle gossip and data synchronization. It will run continuously
    /// until [`stop()`](Self::stop) is called from another thread.
    ///
    /// The main loop performs these operations:
    /// - Syncs with discovered mDNS peers
    /// - Runs periodic gossip rounds with known peers
    /// - Publishes pending data at configured intervals
    /// - Handles network messages
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Network startup fails
    /// - mDNS discovery fails to initialize
    /// - Network operations fail during the main loop
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::iot_mode())?;
    ///
    /// // Add some known peers
    /// node.add_peer("192.168.1.100:5683".parse()?);
    /// node.add_peer("192.168.1.101:5683".parse()?);
    ///
    /// // Start the node's main loop (blocks until stopped)
    /// node.run().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn run(&mut self) -> Result<()> {
        self.running.store(true, Ordering::SeqCst);
        log::info!(
            "Starting minimal node: {}",
            self.keypair.public_key().to_hex()
        );

        // Start network
        self.network.start().await?;

        // Start mDNS discovery if enabled
        if self.config.enable_mdns {
            let port = match &self.config.transport {
                crate::config::TransportConfig::Coap { port, .. } => *port,
                crate::config::TransportConfig::Quic { port, .. } => *port,
                #[cfg(feature = "webrtc")]
                crate::config::TransportConfig::WebRtc { signaling_port, .. } => *signaling_port,
                _ => 5683,
            };
            if let Err(e) = self.network.start_discovery(port) {
                log::warn!("Failed to start mDNS discovery: {}", e);
            }
        }

        let mut discovery_sync_counter = 0u32;

        // Main loop
        while self.running.load(Ordering::SeqCst) {
            // Sync discovered peers periodically (every 100 iterations = ~1 second)
            discovery_sync_counter = discovery_sync_counter.wrapping_add(1);
            if discovery_sync_counter % 100 == 0 {
                self.network.sync_discovered_peers();
            }

            // Check gossip timing
            if self.gossip.should_gossip() {
                self.run_gossip_round().await;
            }

            // Publish pending if interval passed
            if self.config.publish_interval > Duration::ZERO {
                self.publish_pending()?;
            }

            // Periodically save peers to storage
            if self.last_peer_save.elapsed().as_secs() >= PEER_SAVE_INTERVAL_SECS {
                if let Err(e) = self.save_peers() {
                    log::warn!("Failed to save peers: {}", e);
                }
            }

            // Sleep to avoid busy loop
            smol::Timer::after(Duration::from_millis(10)).await;
        }

        // Cleanup: Save peers before stopping
        if let Err(e) = self.save_peers() {
            log::warn!("Failed to save peers on shutdown: {}", e);
        }

        self.network.stop().await?;
        log::info!("Node stopped");

        Ok(())
    }

    /// Runs a single round of gossip and synchronization with a set of peers.
    async fn run_gossip_round(&mut self) {
        let peers = self.network.gossip_peers();
        let mut success_count = 0;

        for addr in peers {
            let latest_seq = self.storage.get_latest_seq().unwrap_or(0);

            // Try to sync with this peer
            match self
                .sync
                .sync_with_peer(&addr, &mut self.network, &self.storage, &mut self.gossip)
                .await
            {
                Ok(result) => {
                    self.network.update_peer(addr, latest_seq);
                    success_count += 1;
                    log::debug!(
                        "Sync with {} complete: sent_filter={}, records_sent={}, records_received={}",
                        addr,
                        result.sent_filter,
                        result.records_sent,
                        result.records_received
                    );
                }
                Err(e) => {
                    log::warn!("Sync with {} failed: {}", addr, e);
                    self.network.mark_peer_failed(&addr);
                }
            }
        }

        self.gossip.gossip_complete(success_count > 0);
    }

    /// Stops the node's main event loop gracefully.
    ///
    /// This method signals the node to stop its main loop and shut down. It can be
    /// called from any thread and is safe to call multiple times.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # use std::sync::Arc;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::iot_mode())?;
    ///
    /// // In another task/thread, stop the node after some work
    /// let stop_signal = Arc::new(node);
    /// let stop_handle = stop_signal.clone();
    ///
    /// // Start the node
    /// let task = smol::spawn(async move {
    ///     // node.run().await
    /// });
    ///
    /// // Later, stop it
    /// stop_handle.stop();
    /// # Ok(())
    /// # }
    /// ```
    pub fn stop(&self) {
        self.running.store(false, Ordering::SeqCst);
    }

    /// Returns `true` if the node's main loop is currently running.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Node is not running until run() is called
    /// assert!(!node.is_running());
    /// # Ok(())
    /// # }
    /// ```
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::SeqCst)
    }

    /// Manually adds a peer to the node's peer list.
    ///
    /// This is useful for bootstrapping connections to known peers when mDNS
    /// discovery is disabled or when connecting to peers outside the local network.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Add known peers
    /// node.add_peer("192.168.1.100:5683".parse()?);
    /// node.add_peer("10.0.0.50:5683".parse()?);
    ///
    /// // Now the node will attempt to gossip with these peers
    /// # Ok(())
    /// # }
    /// ```
    pub fn add_peer(&mut self, addr: std::net::SocketAddr) {
        self.network.add_peer(addr);
    }

    /// Saves the current list of known peers to persistent storage.
    ///
    /// This method serializes all active peers to JSON and stores them in the
    /// database's metadata table. Peers are automatically saved periodically
    /// during the main loop, but this method can be called manually for
    /// immediate persistence.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or storage operations fail.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Add some peers
    /// node.add_peer("192.168.1.100:5683".parse()?);
    /// node.add_peer("192.168.1.101:5683".parse()?);
    ///
    /// // Save peers to storage
    /// node.save_peers()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn save_peers(&mut self) -> Result<()> {
        let peers = self.network.active_peers();
        if peers.is_empty() {
            log::debug!("No active peers to save");
            return Ok(());
        }

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let peer_records: Vec<PeerRecord> = peers
            .iter()
            .map(|p| {
                // Estimate last_seen as now minus elapsed time
                let elapsed_secs = p.last_seen.elapsed().as_secs();
                PeerRecord {
                    addr: p.addr.to_string(),
                    latest_seq: p.latest_seq,
                    quality: p.quality,
                    last_seen_secs: now.saturating_sub(elapsed_secs),
                }
            })
            .collect();

        let json = serde_json::to_string(&peer_records)
            .map_err(|e| crate::error::Error::Serialization(e.to_string()))?;

        self.storage.set_metadata(PEERS_METADATA_KEY, &json)?;
        self.last_peer_save = Instant::now();

        log::info!("Saved {} peers to storage", peer_records.len());
        Ok(())
    }

    /// Loads previously saved peers from persistent storage.
    ///
    /// This method is automatically called during node initialization to restore
    /// known peers from the previous session. Peers with a quality score above
    /// a threshold are re-added to the network.
    ///
    /// # Errors
    ///
    /// Returns an error if storage read or deserialization fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    ///
    /// // Peers are automatically loaded during new()
    /// // But can also be called manually:
    /// node.load_peers()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn load_peers(&mut self) -> Result<()> {
        let json = match self.storage.get_metadata(PEERS_METADATA_KEY)? {
            Some(data) => data,
            None => {
                log::debug!("No persisted peers found");
                return Ok(());
            }
        };

        let peer_records: Vec<PeerRecord> = serde_json::from_str(&json)
            .map_err(|e| crate::error::Error::Serialization(e.to_string()))?;

        let mut loaded_count = 0;
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        for record in peer_records {
            // Skip peers with very low quality or that haven't been seen in > 24 hours
            if record.quality < 10 {
                log::debug!(
                    "Skipping low-quality peer: {} (quality={})",
                    record.addr,
                    record.quality
                );
                continue;
            }

            let age_secs = now.saturating_sub(record.last_seen_secs);
            if age_secs > 24 * 60 * 60 {
                log::debug!(
                    "Skipping stale peer: {} (last seen {} hours ago)",
                    record.addr,
                    age_secs / 3600
                );
                continue;
            }

            if let Ok(addr) = record.addr.parse::<SocketAddr>() {
                self.network.add_peer(addr);
                // Restore quality by simulating successful interactions
                let quality_boosts = record.quality.saturating_sub(50) / 5;
                for _ in 0..quality_boosts {
                    self.network.update_peer(addr, record.latest_seq);
                }
                loaded_count += 1;
            } else {
                log::warn!("Invalid peer address in storage: {}", record.addr);
            }
        }

        log::info!("Loaded {} peers from storage", loaded_count);
        Ok(())
    }

    /// Returns the list of known peers as serializable records.
    ///
    /// This is useful for exporting peer information or debugging.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut node = MinimalNode::new(Config::test_mode())?;
    /// node.add_peer("192.168.1.100:5683".parse()?);
    ///
    /// let peers = node.get_known_peers();
    /// for peer in peers {
    ///     println!("Peer: {} (quality: {})", peer.addr, peer.quality);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_known_peers(&self) -> Vec<PeerRecord> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        self.network
            .active_peers()
            .iter()
            .map(|p| {
                let elapsed_secs = p.last_seen.elapsed().as_secs();
                PeerRecord {
                    addr: p.addr.to_string(),
                    latest_seq: p.latest_seq,
                    quality: p.quality,
                    last_seen_secs: now.saturating_sub(elapsed_secs),
                }
            })
            .collect()
    }

    /// Returns statistics from the sync manager.
    ///
    /// These statistics provide insights into data synchronization performance,
    /// including sync attempts, successes, failures, and data transfer metrics.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let node = MinimalNode::new(Config::test_mode())?;
    ///
    /// let stats = node.sync_stats();
    /// println!("Successful syncs: {}", stats.total_successful_syncs);
    /// println!("Failed syncs: {}", stats.total_failed_syncs);
    /// # Ok(())
    /// # }
    /// ```
    pub fn sync_stats(&self) -> crate::sync::SyncStats {
        self.sync.stats()
    }

    /// Returns statistics from the gossip manager.
    ///
    /// These statistics provide insights into gossip protocol performance,
    /// including gossip rounds, peer interactions, and data propagation metrics.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aingle_minimal::{MinimalNode, Config};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let node = MinimalNode::new(Config::test_mode())?;
    ///
    /// let stats = node.gossip_stats();
    /// println!("Gossip round: {}", stats.round);
    /// println!("Queue length: {}", stats.queue_length);
    /// # Ok(())
    /// # }
    /// ```
    pub fn gossip_stats(&self) -> crate::gossip::GossipStats {
        self.gossip.stats()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_node_creation() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config);
        assert!(node.is_ok());
    }

    #[test]
    fn test_create_entry() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        let content = serde_json::json!({
            "sensor_id": "temp_001",
            "value": 23.5,
            "unit": "celsius"
        });

        let hash = node.create_entry(content);
        assert!(hash.is_ok());
    }

    #[test]
    fn test_node_stats() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        let stats = node.stats();
        assert!(stats.is_ok());
        assert_eq!(stats.unwrap().entries_count, 0);
    }

    #[test]
    fn test_public_key() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        let pubkey = node.public_key();
        // Public key should be 32 bytes
        assert_eq!(pubkey.0.len(), 32);
        // Hex representation should be 64 characters
        assert_eq!(pubkey.to_hex().len(), 64);
    }

    #[test]
    fn test_get_entry() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Create an entry
        let content = "test content";
        let hash = node.create_entry(content).unwrap();

        // Get the entry back
        let entry = node.get_entry(&hash);
        assert!(entry.is_ok());
        // Note: entry may be None depending on storage implementation
    }

    #[test]
    fn test_get_entry_not_found() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        // Try to get non-existent entry
        let hash = Hash([0u8; 32]);
        let entry = node.get_entry(&hash);
        assert!(entry.is_ok());
        assert!(entry.unwrap().is_none());
    }

    #[test]
    fn test_stop_and_is_running() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        // Node should not be running initially
        assert!(!node.is_running());

        // Stop should work even when not running
        node.stop();
        assert!(!node.is_running());
    }

    #[test]
    fn test_add_peer() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Add a peer
        let addr: std::net::SocketAddr = "192.168.1.100:5683".parse().unwrap();
        node.add_peer(addr);

        // Verify peer was added via stats
        let stats = node.stats().unwrap();
        assert!(stats.peer_count >= 0); // At least no error
    }

    #[test]
    fn test_add_multiple_peers() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Add multiple peers
        node.add_peer("192.168.1.100:5683".parse().unwrap());
        node.add_peer("192.168.1.101:5683".parse().unwrap());
        node.add_peer("192.168.1.102:5683".parse().unwrap());

        // Stats should work
        let stats = node.stats().unwrap();
        assert!(stats.peer_count >= 0);
    }

    #[test]
    fn test_sync_stats() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        let sync_stats = node.sync_stats();
        // Initial stats should be zero
        assert_eq!(sync_stats.total_successful_syncs, 0);
        assert_eq!(sync_stats.total_failed_syncs, 0);
        assert_eq!(sync_stats.peer_count, 0);
    }

    #[test]
    fn test_gossip_stats() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        let gossip_stats = node.gossip_stats();
        // Initial stats should be zero
        assert_eq!(gossip_stats.round, 0);
        assert_eq!(gossip_stats.pending_announcements, 0);
        assert_eq!(gossip_stats.queue_length, 0);
    }

    #[test]
    fn test_create_multiple_entries() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Create multiple entries
        let hash1 = node.create_entry("entry 1").unwrap();
        let hash2 = node.create_entry("entry 2").unwrap();
        let hash3 = node.create_entry("entry 3").unwrap();

        // All hashes should be different
        assert_ne!(hash1, hash2);
        assert_ne!(hash2, hash3);
        assert_ne!(hash1, hash3);

        // Stats should reflect the entries
        let stats = node.stats().unwrap();
        assert!(stats.actions_count >= 3);
    }

    #[test]
    fn test_node_stats_after_entries() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Initial stats
        let initial_stats = node.stats().unwrap();
        let initial_actions = initial_stats.actions_count;

        // Create an entry
        node.create_entry("test").unwrap();

        // Stats should update
        let updated_stats = node.stats().unwrap();
        assert!(updated_stats.actions_count > initial_actions);
    }

    #[test]
    fn test_node_uptime() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        // Sleep a bit to ensure uptime is measurable
        std::thread::sleep(std::time::Duration::from_millis(10));

        let stats = node.stats().unwrap();
        // Uptime should be at least 0 (could be 0 if very fast)
        assert!(stats.uptime_secs >= 0);
    }

    #[test]
    fn test_node_with_iot_config() {
        let config = Config::iot_mode();
        let node = MinimalNode::new(config);
        assert!(node.is_ok());

        let node = node.unwrap();
        assert!(!node.is_running());
    }

    #[test]
    fn test_create_entry_with_struct() {
        use serde::{Deserialize, Serialize};

        #[derive(Serialize, Deserialize)]
        struct SensorReading {
            temperature: f64,
            humidity: f64,
            timestamp: u64,
        }

        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        let reading = SensorReading {
            temperature: 23.5,
            humidity: 65.2,
            timestamp: 1234567890,
        };

        let hash = node.create_entry(reading);
        assert!(hash.is_ok());
    }

    #[test]
    fn test_create_entry_with_nested_struct() {
        use serde::{Deserialize, Serialize};

        #[derive(Serialize, Deserialize)]
        struct Location {
            latitude: f64,
            longitude: f64,
        }

        #[derive(Serialize, Deserialize)]
        struct DeviceData {
            device_id: String,
            location: Location,
            readings: Vec<f64>,
        }

        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        let data = DeviceData {
            device_id: "device_001".to_string(),
            location: Location {
                latitude: 40.7128,
                longitude: -74.0060,
            },
            readings: vec![1.0, 2.0, 3.0, 4.0, 5.0],
        };

        let hash = node.create_entry(data);
        assert!(hash.is_ok());
    }

    // ==================== Peer Persistence Tests ====================

    #[test]
    fn test_peer_record_serialization() {
        let record = PeerRecord {
            addr: "192.168.1.100:5683".to_string(),
            latest_seq: 42,
            quality: 75,
            last_seen_secs: 1234567890,
        };

        let json = serde_json::to_string(&record).unwrap();
        assert!(json.contains("192.168.1.100:5683"));
        assert!(json.contains("42"));
        assert!(json.contains("75"));

        let parsed: PeerRecord = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.addr, record.addr);
        assert_eq!(parsed.latest_seq, record.latest_seq);
        assert_eq!(parsed.quality, record.quality);
        assert_eq!(parsed.last_seen_secs, record.last_seen_secs);
    }

    #[test]
    fn test_peer_record_vec_serialization() {
        let records = vec![
            PeerRecord {
                addr: "192.168.1.100:5683".to_string(),
                latest_seq: 10,
                quality: 80,
                last_seen_secs: 1000,
            },
            PeerRecord {
                addr: "10.0.0.50:5683".to_string(),
                latest_seq: 20,
                quality: 60,
                last_seen_secs: 2000,
            },
        ];

        let json = serde_json::to_string(&records).unwrap();
        let parsed: Vec<PeerRecord> = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0].addr, "192.168.1.100:5683");
        assert_eq!(parsed[1].addr, "10.0.0.50:5683");
    }

    #[test]
    fn test_save_peers_empty() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // No peers to save - should succeed silently
        let result = node.save_peers();
        assert!(result.is_ok());
    }

    #[test]
    fn test_save_peers_with_peers() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Add some peers
        node.add_peer("192.168.1.100:5683".parse().unwrap());
        node.add_peer("192.168.1.101:5683".parse().unwrap());

        // Save should succeed
        let result = node.save_peers();
        assert!(result.is_ok());
    }

    #[test]
    fn test_load_peers_empty_storage() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Load when nothing is stored - should succeed
        let result = node.load_peers();
        assert!(result.is_ok());
    }

    #[test]
    fn test_save_and_load_peers_roundtrip() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Add peers
        node.add_peer("192.168.1.100:5683".parse().unwrap());
        node.add_peer("10.0.0.50:5683".parse().unwrap());

        // Save peers
        node.save_peers().unwrap();

        // Create a new node with the same storage
        // (In test_mode, storage is in-memory, so we need to load in same instance)
        let peer_count_before = node.stats().unwrap().peer_count;

        // Load should work (peers already in network, so no change expected)
        node.load_peers().unwrap();

        let peer_count_after = node.stats().unwrap().peer_count;
        assert!(peer_count_after >= peer_count_before);
    }

    #[test]
    fn test_get_known_peers_empty() {
        let config = Config::test_mode();
        let node = MinimalNode::new(config).unwrap();

        let peers = node.get_known_peers();
        assert!(peers.is_empty());
    }

    #[test]
    fn test_get_known_peers_with_peers() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Add peers
        node.add_peer("192.168.1.100:5683".parse().unwrap());
        node.add_peer("192.168.1.101:5683".parse().unwrap());

        let peers = node.get_known_peers();
        assert_eq!(peers.len(), 2);

        // Check addresses are correct
        let addrs: Vec<&str> = peers.iter().map(|p| p.addr.as_str()).collect();
        assert!(addrs.contains(&"192.168.1.100:5683"));
        assert!(addrs.contains(&"192.168.1.101:5683"));

        // Check quality is initialized
        for peer in &peers {
            assert!(peer.quality > 0);
        }
    }

    #[test]
    fn test_peer_record_debug() {
        let record = PeerRecord {
            addr: "127.0.0.1:5683".to_string(),
            latest_seq: 0,
            quality: 50,
            last_seen_secs: 0,
        };

        let debug_str = format!("{:?}", record);
        assert!(debug_str.contains("PeerRecord"));
        assert!(debug_str.contains("127.0.0.1:5683"));
    }

    #[test]
    fn test_peer_record_clone() {
        let record1 = PeerRecord {
            addr: "192.168.1.1:5683".to_string(),
            latest_seq: 100,
            quality: 90,
            last_seen_secs: 999999,
        };

        let record2 = record1.clone();
        assert_eq!(record1.addr, record2.addr);
        assert_eq!(record1.latest_seq, record2.latest_seq);
        assert_eq!(record1.quality, record2.quality);
        assert_eq!(record1.last_seen_secs, record2.last_seen_secs);
    }

    #[test]
    fn test_peer_persistence_skips_low_quality() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Manually set metadata with a low-quality peer
        let records = vec![PeerRecord {
            addr: "192.168.1.100:5683".to_string(),
            latest_seq: 0,
            quality: 5, // Very low quality
            last_seen_secs: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        }];

        let json = serde_json::to_string(&records).unwrap();
        node.storage
            .set_metadata(PEERS_METADATA_KEY, &json)
            .unwrap();

        // Load peers - should skip the low-quality peer
        node.load_peers().unwrap();

        let peers = node.get_known_peers();
        // The low-quality peer should be skipped
        assert!(peers.is_empty());
    }

    #[test]
    fn test_peer_persistence_skips_stale() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Manually set metadata with a stale peer (>24 hours old)
        let old_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            .saturating_sub(25 * 60 * 60); // 25 hours ago

        let records = vec![PeerRecord {
            addr: "192.168.1.100:5683".to_string(),
            latest_seq: 100,
            quality: 80, // Good quality but stale
            last_seen_secs: old_time,
        }];

        let json = serde_json::to_string(&records).unwrap();
        node.storage
            .set_metadata(PEERS_METADATA_KEY, &json)
            .unwrap();

        // Load peers - should skip the stale peer
        node.load_peers().unwrap();

        let peers = node.get_known_peers();
        // The stale peer should be skipped
        assert!(peers.is_empty());
    }

    #[test]
    fn test_peer_persistence_accepts_recent_quality_peer() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Manually set metadata with a good, recent peer
        let recent_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let records = vec![PeerRecord {
            addr: "192.168.1.100:5683".to_string(),
            latest_seq: 50,
            quality: 60, // Good quality
            last_seen_secs: recent_time,
        }];

        let json = serde_json::to_string(&records).unwrap();
        node.storage
            .set_metadata(PEERS_METADATA_KEY, &json)
            .unwrap();

        // Load peers - should accept the good peer
        node.load_peers().unwrap();

        let peers = node.get_known_peers();
        assert_eq!(peers.len(), 1);
        assert_eq!(peers[0].addr, "192.168.1.100:5683");
    }

    #[test]
    fn test_peer_persistence_invalid_address() {
        let config = Config::test_mode();
        let mut node = MinimalNode::new(config).unwrap();

        // Manually set metadata with an invalid address
        let recent_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let records = vec![PeerRecord {
            addr: "not-a-valid-address".to_string(), // Invalid
            latest_seq: 50,
            quality: 80,
            last_seen_secs: recent_time,
        }];

        let json = serde_json::to_string(&records).unwrap();
        node.storage
            .set_metadata(PEERS_METADATA_KEY, &json)
            .unwrap();

        // Load peers - should succeed but skip invalid address
        let result = node.load_peers();
        assert!(result.is_ok());

        let peers = node.get_known_peers();
        assert!(peers.is_empty());
    }
}