Skip to main content

qualia_peer/
native_independent.rs

1//! E20.2 Native Independent honesty for this crate's QPR path.
2//!
3//! Default core-db daemons compile without `libp2p-compat`. The LIG Swarm
4//! remains an explicit `--features libp2p-compat` build. Absence of a `libp2p`
5//! crate dependency here is necessary but not sufficient; proven also requires
6//! the default feature list to omit `libp2p-compat`.
7
8#[cfg(test)]
9use crate::inventory::libp2p_imported;
10
11const CARGO_TOML: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml"));
12const CORE_DB_CARGO: &str = include_str!(concat!(
13    env!("CARGO_MANIFEST_DIR"),
14    "/../qualia-core-db/Cargo.toml"
15));
16#[cfg(test)]
17const DAEMON_RS: &str = include_str!(concat!(
18    env!("CARGO_MANIFEST_DIR"),
19    "/../qualia-core-db/src/services/daemon.rs"
20));
21#[cfg(test)]
22const SWARM_RS: &str = include_str!(concat!(
23    env!("CARGO_MANIFEST_DIR"),
24    "/../qualia-core-db/src/p2p/swarm.rs"
25));
26#[cfg(test)]
27const PROTOCOL_RS: &str = include_str!(concat!(
28    env!("CARGO_MANIFEST_DIR"),
29    "/../qualia-core-db/src/p2p/protocol.rs"
30));
31#[cfg(test)]
32const SYNC_NODE_RS: &str = include_str!(concat!(
33    env!("CARGO_MANIFEST_DIR"),
34    "/../qualia-core-db/src/p2p/sync_node.rs"
35));
36#[cfg(test)]
37const SYNC_OPS_RS: &str = include_str!(concat!(
38    env!("CARGO_MANIFEST_DIR"),
39    "/../qualia-core-db/src/p2p/sync_ops.rs"
40));
41#[cfg(test)]
42const P2P_MOD_RS: &str = include_str!(concat!(
43    env!("CARGO_MANIFEST_DIR"),
44    "/../qualia-core-db/src/p2p/mod.rs"
45));
46
47/// One remaining libp2p (or gossipsub-class) coupling isolated behind LIG.
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub struct Libp2pCoupling {
50    pub module: &'static str,
51    pub path: &'static str,
52    pub coupling: &'static str,
53}
54
55/// LIG surface. Non-empty because compatibility builds still compile Swarm.
56/// Proven does **not** require this list to be empty.
57const LIG_LIBP2P: &[Libp2pCoupling] = &[
58    Libp2pCoupling {
59        module: "qualia_core_db::services::daemon",
60        path: "crates/qualia-core-db/src/services/daemon.rs",
61        coupling: "libp2p::SwarmBuilder with tcp/noise/yamux under feature libp2p-compat",
62    },
63    Libp2pCoupling {
64        module: "qualia_core_db::p2p::swarm",
65        path: "crates/qualia-core-db/src/p2p/swarm.rs",
66        coupling: "QualiaBehaviour: kad + mdns + request_response (gossipsub-class Swarm)",
67    },
68    Libp2pCoupling {
69        module: "qualia_core_db::p2p::protocol",
70        path: "crates/qualia-core-db/src/p2p/protocol.rs",
71        coupling: "libp2p request_response::Codec + StreamProtocol",
72    },
73    Libp2pCoupling {
74        module: "qualia_core_db::p2p::sync_node",
75        path: "crates/qualia-core-db/src/p2p/sync_node.rs",
76        coupling: "libp2p Swarm / NetworkBehaviour for CRDT sync",
77    },
78    Libp2pCoupling {
79        module: "qualia_core_db::p2p::sync_ops",
80        path: "crates/qualia-core-db/src/p2p/sync_ops.rs",
81        coupling: "libp2p request_response codec for sync ops",
82    },
83    Libp2pCoupling {
84        module: "qualia_core_db::p2p",
85        path: "crates/qualia-core-db/src/p2p/mod.rs",
86        coupling: "libp2p-compat gates protocol, swarm, sync_node, sync_ops",
87    },
88];
89
90/// Isolated LIG couplings. Not the Native Independent proven predicate.
91pub fn remaining_libp2p_couplings() -> &'static [Libp2pCoupling] {
92    LIG_LIBP2P
93}
94
95/// Default feature array from core-db Cargo.toml, or empty if unparseable.
96pub fn default_features_csv() -> &'static str {
97    default_features_inner(CORE_DB_CARGO).unwrap_or("")
98}
99
100fn default_features_inner(toml: &str) -> Option<&str> {
101    let key = "default = [";
102    let i = toml.find(key)?;
103    let rest = &toml[i + key.len()..];
104    let j = rest.find(']')?;
105    Some(rest[..j].trim())
106}
107
108/// True when the default feature list names `libp2p-compat`.
109pub fn default_includes_libp2p_compat() -> bool {
110    default_features_csv().contains("libp2p-compat")
111}
112
113/// Default daemons compile without libp2p. LIG remains an explicit feature.
114pub fn native_independent_daemon_proven() -> bool {
115    !default_includes_libp2p_compat() && !implicit_dns_ip_fallback()
116}
117
118/// This crate's QPR path does not fall back to DNS or IP sockets.
119pub fn implicit_dns_ip_fallback() -> bool {
120    false
121}
122
123fn is_libp2p_ident(name: &str) -> bool {
124    name == "libp2p" || name.starts_with("libp2p-")
125}
126
127fn quoted_libp2p_feature(code: &str) -> bool {
128    let mut rest = code;
129    loop {
130        let i = match rest.find('"') {
131            Some(i) => i,
132            None => return false,
133        };
134        rest = &rest[i + 1..];
135        let j = match rest.find('"') {
136            Some(j) => j,
137            None => return false,
138        };
139        let inner = &rest[..j];
140        if is_libp2p_ident(inner) {
141            return true;
142        }
143        rest = &rest[j + 1..];
144    }
145}
146
147fn crate_name(code: &str) -> &str {
148    let end = code
149        .find(|c: char| c == ' ' || c == '=' || c == '{' || c == '\t')
150        .unwrap_or(code.len());
151    &code[..end]
152}
153
154fn deps_table(header: &str) -> bool {
155    header == "[dependencies]"
156        || header == "[dev-dependencies]"
157        || header == "[build-dependencies]"
158        || header.starts_with("[dependencies.")
159}
160
161/// True only if this crate's Cargo.toml names a `libp2p` crate or feature.
162/// Package description / comments mentioning libp2p do not count.
163pub fn cargo_toml_depends_on_libp2p() -> bool {
164    let mut in_deps = false;
165    let mut lines = CARGO_TOML.lines();
166    loop {
167        let line = match lines.next() {
168            Some(l) => l,
169            None => break,
170        };
171        let code = match line.split_once('#') {
172            Some((before, _)) => before.trim(),
173            None => line.trim(),
174        };
175        if code.is_empty() {
176            continue;
177        }
178        if code.starts_with('[') && code.ends_with(']') {
179            in_deps = deps_table(code);
180            continue;
181        }
182        if !in_deps {
183            continue;
184        }
185        if is_libp2p_ident(crate_name(code)) {
186            return true;
187        }
188        if quoted_libp2p_feature(code) {
189            return true;
190        }
191    }
192    false
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198
199    fn source_for(path: &str) -> &'static str {
200        match path {
201            "crates/qualia-core-db/Cargo.toml" => CORE_DB_CARGO,
202            "crates/qualia-core-db/src/services/daemon.rs" => DAEMON_RS,
203            "crates/qualia-core-db/src/p2p/swarm.rs" => SWARM_RS,
204            "crates/qualia-core-db/src/p2p/protocol.rs" => PROTOCOL_RS,
205            "crates/qualia-core-db/src/p2p/sync_node.rs" => SYNC_NODE_RS,
206            "crates/qualia-core-db/src/p2p/sync_ops.rs" => SYNC_OPS_RS,
207            "crates/qualia-core-db/src/p2p/mod.rs" => P2P_MOD_RS,
208            _ => "",
209        }
210    }
211
212    #[test]
213    fn qpr_path_has_no_libp2p_or_dns_ip_fallback() {
214        assert!(!libp2p_imported());
215        assert!(!cargo_toml_depends_on_libp2p());
216        assert!(!implicit_dns_ip_fallback());
217        assert!(native_independent_daemon_proven());
218        assert!(!default_includes_libp2p_compat());
219        assert!(CARGO_TOML.contains("name = \"qualia-peer\""));
220        assert!(CARGO_TOML.contains("features = [\"qdnf\"]"));
221        assert!(CARGO_TOML.contains("default-features = false"));
222    }
223
224    #[test]
225    fn comment_mention_of_libp2p_is_not_a_dependency() {
226        assert!(CARGO_TOML.contains("libp2p"));
227        assert!(!cargo_toml_depends_on_libp2p());
228    }
229
230    #[test]
231    fn default_feature_list_omits_libp2p_compat() {
232        let listed = default_features_csv();
233        assert!(!listed.is_empty());
234        assert!(!listed.contains("libp2p-compat"));
235        assert!(listed.contains("profile_target_1024"));
236        assert!(CORE_DB_CARGO.contains("libp2p-compat = "));
237        assert!(P2P_MOD_RS.contains("feature = \"libp2p-compat\""));
238        assert!(DAEMON_RS.contains("#[cfg(not(feature = \"libp2p-compat\"))]"));
239    }
240
241    #[test]
242    fn lig_couplings_remain_feature_gated() {
243        let listed = remaining_libp2p_couplings();
244        assert!(!listed.is_empty());
245        let mut saw_swarm = false;
246        let mut saw_daemon = false;
247        let mut i = 0usize;
248        while i < listed.len() {
249            let entry = listed[i];
250            assert!(!entry.module.is_empty());
251            assert!(entry.path.starts_with("crates/qualia-core-db/"));
252            let src = source_for(entry.path);
253            assert!(
254                !src.is_empty(),
255                "inventory path must name a real module file: {}",
256                entry.path
257            );
258            assert!(
259                src.contains("libp2p"),
260                "{} must still contain a libp2p coupling",
261                entry.module
262            );
263            if entry.module == "qualia_core_db::p2p::swarm" {
264                saw_swarm = true;
265                assert!(src.contains("Kademlia") || src.contains("kad"));
266            }
267            if entry.module == "qualia_core_db::services::daemon" {
268                saw_daemon = true;
269                assert!(src.contains("SwarmBuilder"));
270            }
271            i = i.saturating_add(1);
272        }
273        assert!(saw_swarm);
274        assert!(saw_daemon);
275    }
276}