Files
Meshtastic-Android/core/database
James Rich e6b08fff65 fix: Address P3 + P4 code-review findings — pre-existing bugs + perf regressions
P3 — Pre-existing bugs surfaced when files were moved:

- MeshUser.hwModelString: the unconditional replace('p', '.') mangles any
  hardware enum name containing a literal 'p' (HELTEC_WIRELESS_PAPER had
  become 'heltec-wireless-.a.er'). Use a regex bounded by digits so version
  markers like RAK4631_V1P0 → rak4631-v1.0 still work and other 'p's are
  preserved.
- EnvironmentMetrics.relativeHumidity: document the special-case
  `it != 0.0f` filter. It's intentional — firmware reports 0%RH when the
  humidity sensor isn't fitted and a real outdoor 0%RH reading is
  physically implausible. Other fields don't get this guard because their
  zero values (0V, 0A, 0°C) are meaningful sensor data.

P4 — Performance regressions introduced by the AIDL refactor:

- NodeManagerImpl: restore the O(1) byId index. The refactor dropped the
  secondary nodeDBbyID map and made getNodeById a linear scan over the
  whole node DB. getNodeById is called per-incoming-packet from
  MeshDataHandlerImpl (three times — shouldFilterMessage,
  handleNotificationMessage, getSenderName) and per-outbound send from
  CommandSenderImpl.resolveNodeNum.ById. The new NodeIndex holder maintains
  byNum and byId together in a single atomic ref so concurrent readers
  never see an inconsistent snapshot.
- NodeWithRelations.toModel: inline directly to a single Node(...)
  constructor call. The previous form did `node.toModel().copy(metadata=…,
  manuallyVerified=…)` which allocated the Node twice per DB row — wasteful
  on every nodeDBbyNum emission.
- NodeRepositoryImpl.getUser: replace two NodeAddress.fromString(userId)
  calls with a single string equality check against NodeAddress.ID_LOCAL.
  getUser is on the hot path for paged contacts and message arrival.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-28 07:52:01 -05:00
..

:core:database

This module provides the local Room database persistence layer for the application using Room Kotlin Multiplatform (KMP).

Key Components

  • MeshtasticDatabase: The main Room database class, defined in commonMain.
  • DAOs (Data Access Objects):
    • NodeInfoDao: Manages storage and retrieval of node information (NodeEntity). Contains critical logic for handling Public Key Conflict (PKC) resolution and preventing identity wiping attacks.
    • PacketDao: Handles storage of mesh packets, including text messages, waypoints, and reactions.
  • Entities:
    • NodeEntity: Represents a node on the mesh.
    • Packet: Represents a stored packet.
    • ReactionEntity: Represents emoji reactions to packets.

Security Considerations

Public Key Conflict (PKC) Handling

The NodeInfoDao implements specific logic to protect against impersonation and "wipe" attacks:

  • Wipe Protection: Receiving an is_licensed=true packet (which normally clears the public key for compliance) will not clear an existing valid public key if one is already known. This prevents attackers from sending fake licensed packets to wipe keys from the DB.
  • Conflict Detection: If a new key arrives for an existing node ID that conflicts with a known valid key, the key is set to ERROR_BYTE_STRING to flag the potential impersonation.

Dependency Graph

graph TB
  :core:database[database]:::kmp-library
  :core:database --> :core:common
  :core:database --> :core:model
  :core:database -.-> :core:di
  :core:database -.-> :core:proto
  :core:database -.-> :core:resources
  :core:database -.-> :core:testing

classDef android-application fill:#CAFFBF,stroke:#000,stroke-width:2px,color:#000;
classDef android-application-compose fill:#CAFFBF,stroke:#000,stroke-width:2px,color:#000;
classDef compose-desktop-application fill:#CAFFBF,stroke:#000,stroke-width:2px,color:#000;
classDef android-feature fill:#FFD6A5,stroke:#000,stroke-width:2px,color:#000;
classDef android-library fill:#9BF6FF,stroke:#000,stroke-width:2px,color:#000;
classDef android-library-compose fill:#9BF6FF,stroke:#000,stroke-width:2px,color:#000;
classDef android-test fill:#A0C4FF,stroke:#000,stroke-width:2px,color:#000;
classDef jvm-library fill:#BDB2FF,stroke:#000,stroke-width:2px,color:#000;
classDef kmp-feature fill:#FFD6A5,stroke:#000,stroke-width:2px,color:#000;
classDef kmp-library-compose fill:#FFC1CC,stroke:#000,stroke-width:2px,color:#000;
classDef kmp-library fill:#FFC1CC,stroke:#000,stroke-width:2px,color:#000;
classDef unknown fill:#FFADAD,stroke:#000,stroke-width:2px,color:#000;