diff --git a/src/Identity.cpp b/src/Identity.cpp index 8bb9c91..bded207 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -420,6 +420,39 @@ Binary format for known_destinations persistence: } } + // Normalize timestamps to current boot epoch. + // OS::time() on Arduino returns seconds since boot, not epoch time. + // Persisted entries carry timestamps from the previous boot session + // (e.g., 5000.0 = device had been up 5000s when saved). On a fresh + // boot at uptime 30s, a new announce would get timestamp 30.0 — which + // the LRU cull considers "older" than the persisted 5000.0, causing + // the NEW entry to be culled immediately. Fix: remap all loaded + // timestamps into [1.0, now-1] preserving their relative order, so + // any fresh announce at OS::time() is always "newest". + if (loaded > 0) { + double now = OS::time(); + double max_ts = 0, min_ts = 1e18; + for (const auto& [key, entry] : _known_destinations) { + if (entry._timestamp > max_ts) max_ts = entry._timestamp; + if (entry._timestamp < min_ts) min_ts = entry._timestamp; + } + double range = max_ts - min_ts; + // Map [min_ts, max_ts] → [1.0, now - 1.0] + // All loaded entries end up before "now", preserving relative order + double target_max = (now > 2.0) ? now - 1.0 : 1.0; + double target_min = 1.0; + for (auto& [key, entry] : _known_destinations) { + if (range > 0.001) { + double normalized = (entry._timestamp - min_ts) / range; + entry._timestamp = target_min + normalized * (target_max - target_min); + } else { + entry._timestamp = target_min; + } + } + VERBOSEF("Normalized %zu destination timestamps: [%.1f,%.1f] -> [%.1f,%.1f]", + loaded, min_ts, max_ts, target_min, target_max); + } + cull_known_destinations(); VERBOSEF("Loaded %zu known destinations from storage", loaded); } diff --git a/src/Transport.cpp b/src/Transport.cpp index 099b43f..392a0de 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -872,6 +872,11 @@ Transport::DestinationEntry empty_destination_entry; TRACE("Transport::outbound: Path to destination is unknown"); TRACEF("[DIAG] OUTBOUND: NO PATH for dest=%.16s, broadcasting H1", packet.destination_hash().toHex().c_str()); + if (packet.packet_type() == Type::Packet::PROOF) { + VERBOSEF("[DIAG-PROOF] Proof entering broadcast: dest=%s iface_count=%d attached=%s", + packet.destination_hash().toHex().c_str(), (int)_interfaces.size(), + packet.attached_interface() ? packet.attached_interface().toString().c_str() : "NONE"); + } bool stored_hash = false; for (auto& [hash, interface] : _interfaces) { TRACEF("Transport::outbound: Checking interface %s", interface.toString().c_str()); @@ -1061,6 +1066,10 @@ Transport::DestinationEntry empty_destination_entry; if (should_transmit) { TRACE("Transport::outbound: Packet transmission allowed"); + if (packet.packet_type() == Type::Packet::PROOF) { + VERBOSEF("[DIAG-PROOF] TRANSMITTING proof on %s (%d bytes)", + interface.toString().c_str(), (int)packet.raw().size()); + } if (!stored_hash) { // CBA ACCUMULATES _packet_hashlist.insert(packet.packet_hash()); @@ -1079,6 +1088,10 @@ Transport::DestinationEntry empty_destination_entry; } else { TRACE("Transport::outbound: Packet transmission refused"); + if (packet.packet_type() == Type::Packet::PROOF) { + VERBOSEF("[DIAG-PROOF] REFUSED proof on %s (should_transmit=false)", + interface.toString().c_str()); + } } } } @@ -1870,10 +1883,8 @@ Transport::DestinationEntry empty_destination_entry; ); // CBA ACCUMULATES _announce_table.insert({packet.destination_hash(), announce_entry}); - // Defer cull until 10% over capacity - if (_announce_table.size() > _announce_table_maxsize + (_announce_table_maxsize / 10)) { - cull_announce_table(); - } + // CBA IMMEDIATE CULL + cull_announce_table(); } } // TODO: Check from_local_client once and store result @@ -1902,10 +1913,8 @@ Transport::DestinationEntry empty_destination_entry; ); // CBA ACCUMULATES _announce_table.insert({packet.destination_hash(), announce_entry}); - // Defer cull until 10% over capacity - if (_announce_table.size() > _announce_table_maxsize + (_announce_table_maxsize / 10)) { - cull_announce_table(); - } + // CBA IMMEDIATE CULL + cull_announce_table(); } } @@ -1999,13 +2008,11 @@ Transport::DestinationEntry empty_destination_entry; new_announce.send(); } - // Cache announce packet for path persistence (transport nodes only) - // Endpoints reconnect to TCP on reboot and get fresh announces - if (Reticulum::transport_enabled()) { - TRACEF("Caching packet %s", packet.get_hash().toHex().c_str()); - if (RNS::Transport::cache_packet(packet, true)) { - packet.cached(true); - } + // Cache announce packet for path persistence (all nodes, not just transport) + // Endpoints need cached announces so path table entries survive reboot + TRACEF("Caching packet %s", packet.get_hash().toHex().c_str()); + if (RNS::Transport::cache_packet(packet, true)) { + packet.cached(true); } //TRACEF("Adding packet %s to packet table", packet.get_hash().toHex().c_str()); //PacketEntry packet_entry(packet); @@ -2031,10 +2038,8 @@ Transport::DestinationEntry empty_destination_entry; if (_path_table.insert({packet.destination_hash(), destination_table_entry}).second) { TRACEF("Added destination %s to path table!", packet.destination_hash().toHex().c_str()); ++_destinations_added; - // Defer cull until 10% over capacity - if (_path_table.size() > _path_table_maxsize + (_path_table_maxsize / 10)) { - cull_path_table(); - } + // CBA IMMEDIATE CULL + cull_path_table(); } else { ERRORF("Failed to add destination %s to path table!", packet.destination_hash().toHex().c_str()); @@ -3773,34 +3778,31 @@ TRACEF("Transport::write_path_table: buffer size %zu bytes", Persistence::_buffe TRACE("Transport::clean_caches()"); #if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS) - // Endpoints don't cache announce packets — just wipe any stale files - if (!Reticulum::transport_enabled()) { - OS::list_directory(Reticulum::_cachepath, [](const char* file_name) { - char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE]; - snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, file_name); - OS::remove_file(packet_cache_path); - OS::run_loop(); - }); - cleaning_caches = false; - return; - } - - // Transport nodes: build hash set from path table for O(1) lookups - std::set valid_hashes; - for (auto& [destination_hash, destination_entry] : _path_table) { - valid_hashes.insert(destination_entry.announce_packet_hash().toHex()); - } - - // Remove cached packets no longer referenced by any path - OS::list_directory(Reticulum::_cachepath, [&valid_hashes](const char* file_name) { - if (valid_hashes.find(file_name) == valid_hashes.end()) { - TRACEF("Transport::clean_caches: No matching path found, removing cached packet %s", file_name); - char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE]; - snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, file_name); - OS::remove_file(packet_cache_path); + // CBA Remove cached packets no longer in path list + std::list remove_list; + OS::list_directory(Reticulum::_cachepath, [&remove_list](const char* file_name) { + TRACEF("Transport::clean_caches: Checking for use of cached packet %s", file_name); + bool found = false; + for (auto& [destination_hash, destination_entry] : _path_table) { + if (strcasecmp(file_name, destination_entry.announce_packet_hash().toHex().c_str()) == 0) { + found = true; + break; + } } + if (!found) { + remove_list.push_back(file_name); + } + //OS::reset_watchdog(); OS::run_loop(); }); + for (auto& file_name : remove_list) { + TRACEF("Transport::clean_caches: No matching path found, removing cached packet %s", file_name.c_str()); + char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE]; + snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, file_name.c_str()); + OS::remove_file(packet_cache_path); + //OS::reset_watchdog(); + OS::run_loop(); + } #endif cleaning_caches = false;