holochain_conductor_api 0.7.0-dev.19

Message types for Holochain admin and app interface protocols
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
#![deny(missing_docs)]
//! This module is used to configure the conductor.
//!
//! #### Example minimum conductor config:
//!
//! ```rust
//! let yaml = r#"---
//!
//! ## Configure the keystore to be used.
//! keystore:
//!
//!   ## Use an in-process keystore with default database location.
//!   type: lair_server_in_proc
//!
//! ## Configure an admin WebSocket interface at a specific port.
//! admin_interfaces:
//!   - driver:
//!       type: websocket
//!       port: 1234
//!       allowed_origins: "*"
//!
//! ## Configure the network.
//! network:
//!
//!   ## Use the Holochain-provided dev-test bootstrap server.
//!   bootstrap_url: https://dev-test-bootstrap2.holochain.org
//!
//!   ## Use the Holochain-provided dev-test sbd/signalling server.
//!   signal_url: wss://dev-test-bootstrap2.holochain.org
//!
//!   ## Use the iroh relay server.
//!   relay_url: https://use1-1.relay.n0.iroh-canary.iroh.link./
//!
//!   ## Override the default WebRTC STUN configuration.
//!   ## This is OPTIONAL. If this is not specified, it will default
//!   ## to what you can see here:
//!   webrtc_config: {
//!     "iceServers": [
//!       { "urls": ["stun:stun.l.google.com:19302"] }
//!     ]
//!   }
//! "#;
//!
//!use holochain_conductor_api::conductor::ConductorConfig;
//!
//!let _: ConductorConfig = serde_yaml::from_str(yaml).unwrap();
//! ```

use crate::conductor::process::ERROR_CODE;
use crate::config::conductor::paths::DataRootPath;
use holochain_types::prelude::DbSyncStrategy;
#[cfg(all(feature = "schema", feature = "kitsune2_transport_tx5"))]
use kitsune2_transport_tx5::WebRtcConfig;
use schemars::JsonSchema;
#[cfg(feature = "schema")]
use schemars::Schema;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;
use std::path::Path;

mod admin_interface_config;
#[allow(missing_docs)]
mod error;
mod keystore_config;
/// Defines subdirectories of the config directory.
pub mod paths;
pub mod process;

pub use super::*;
pub use error::*;
pub use keystore_config::KeystoreConfig;

/// All the config information for the conductor
#[derive(Clone, Deserialize, Serialize, Debug, PartialEq, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ConductorConfig {
    /// Override the environment specified tracing config.
    #[serde(default)]
    pub tracing_override: Option<String>,

    /// The path to the data root for this conductor;
    /// This can be `None` while building up the config programatically but MUST
    /// be set by the time the config is used to build a conductor.
    /// The database and compiled wasm directories are derived from this path.
    pub data_root_path: Option<DataRootPath>,

    /// Define how Holochain conductor will connect to a keystore.
    #[serde(default)]
    pub keystore: KeystoreConfig,

    /// Setup admin interfaces to control this conductor through a websocket connection.
    pub admin_interfaces: Option<Vec<AdminInterfaceConfig>>,

    /// Optional config for the network module.
    #[serde(default)]
    pub network: NetworkConfig,

    /// Override the default database synchronous strategy.
    ///
    /// See [sqlite documentation] for information about database sync levels.
    /// See [`DbSyncStrategy`] for details.
    /// This is best left at its default value unless you know what you
    /// are doing.
    ///
    /// [sqlite documentation]: https://www.sqlite.org/pragma.html#pragma_synchronous
    #[serde(default)]
    pub db_sync_strategy: DbSyncStrategy,

    /// Override the default number of read connections available per database.
    ///
    /// The value defaults to twice the number of CPUs, or 8, whichever is greater.
    ///
    /// Note that this configuration is related to the value of `incoming_request_concurrency_limit`.
    /// If one of these is modified, you may want to modify the other to reflect it.
    ///
    /// This is best left at its default value unless you know what you are doing.
    #[serde(default = "default_db_max_readers")]
    pub db_max_readers: u16,

    /// Maximum number of authority responses that can be served in parallel.
    ///
    /// These are responses to requests from other agents of
    /// `get`, `get_links`, `count_links`, `get_agent_activity`, and `must_get_agent_activity`.
    ///
    /// Any additional requests for authority responses beyond this limit are ignored.
    ///
    /// The default value is either 1 or the following calculated value, whichever is greater:
    /// - The default value of `db_max_readers` minus 3, for readers allocated to concurrently running workflows.
    ///
    /// Note that this configuration is related to the value of `db_max_readers`.
    /// If one of these is modified, you may want to modify the other to reflect it.
    ///
    /// This is best left at its default value unless you know what you are doing.
    #[serde(default = "default_incoming_request_concurrency_limit")]
    pub incoming_request_concurrency_limit: u16,

    /// Tuning parameters to adjust the behaviour of the conductor.
    #[serde(default)]
    pub tuning_params: Option<ConductorTuningParams>,

    /// Tracing scope.
    pub tracing_scope: Option<String>,
}

/// Default value is either 8, or the number of CPU cores multiplied by 2, whichever is greater.
fn default_db_max_readers() -> u16 {
    calculate_default_db_max_readers(num_cpus::get())
}

fn calculate_default_db_max_readers(num_cpus_count: usize) -> u16 {
    let num_cpus_count = u16::try_from(num_cpus_count).unwrap_or(u16::MAX);
    let cpus_x2 = num_cpus_count.saturating_mul(2);
    std::cmp::max(cpus_x2, 8)
}

/// Default value is either 1 or the following calculated value, whichever is greater:
/// - The default value of `db_max_readers` minus 3, for readers allocated to concurrently running workflows.
fn default_incoming_request_concurrency_limit() -> u16 {
    std::cmp::max(default_db_max_readers() - 3, 1)
}

impl Default for ConductorConfig {
    fn default() -> Self {
        Self {
            tracing_override: None,
            data_root_path: None,
            keystore: KeystoreConfig::default(),
            admin_interfaces: None,
            network: NetworkConfig::default(),
            db_sync_strategy: DbSyncStrategy::default(),
            db_max_readers: default_db_max_readers(),
            incoming_request_concurrency_limit: default_incoming_request_concurrency_limit(),
            tuning_params: None,
            tracing_scope: None,
        }
    }
}

/// Helper function to load a config from a YAML string.
fn config_from_yaml<T>(yaml: &str) -> ConductorConfigResult<T>
where
    T: DeserializeOwned,
{
    serde_yaml::from_str(yaml).map_err(ConductorConfigError::SerializationError)
}

impl ConductorConfig {
    /// Create a conductor config from a YAML file path.
    pub fn load_yaml(path: &Path) -> ConductorConfigResult<ConductorConfig> {
        let config_yaml = std::fs::read_to_string(path).map_err(|err| match err {
            e @ std::io::Error { .. } if e.kind() == std::io::ErrorKind::NotFound => {
                ConductorConfigError::ConfigMissing(path.into())
            }
            _ => err.into(),
        })?;
        config_from_yaml(&config_yaml)
    }

    /// Get the tracing scope from the conductor config.
    pub fn tracing_scope(&self) -> Option<String> {
        self.tracing_scope.clone()
    }

    /// Get the data directory for this config or say something nice and die.
    pub fn data_root_path_or_die(&self) -> DataRootPath {
        match &self.data_root_path {
            Some(path) => path.clone(),
            None => {
                println!(
                    "
                    The conductor config does not contain a data_root_path. Please check and fix the
                    config file. Details:

                        Missing field `data_root_path`",
                );
                std::process::exit(ERROR_CODE);
            }
        }
    }

    /// Get the reports directory for this config.
    pub fn reports_path(&self) -> std::path::PathBuf {
        crate::conductor::paths::ReportsRootPath::try_from(self.data_root_path_or_die())
            .expect("can get reports path")
            .0
    }

    /// Get the conductor tuning params for this config (default if not set)
    pub fn conductor_tuning_params(&self) -> ConductorTuningParams {
        self.tuning_params.clone().unwrap_or_default()
    }
}

/// Configure Kitsune2 Reporting.
#[derive(Clone, Default, Deserialize, Serialize, Debug, PartialEq, JsonSchema)]
#[serde(
    rename_all = "snake_case",
    rename_all_fields = "snake_case",
    deny_unknown_fields
)]
pub enum ReportConfig {
    /// Default to no reporting.
    #[default]
    None,

    /// Enable JsonL(ines) reporting.
    JsonLines {
        /// How many days worth of report files to retain.
        days_retained: u32,

        /// How often to report Fetched-Op aggregated data in seconds.
        fetched_op_interval_s: u32,
    },
}

/// All the network config information for the conductor.
#[derive(Clone, Deserialize, Serialize, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct NetworkConfig {
    /// Authentication material if required by the bootstrap service.
    ///
    /// This material should be specified as a base64 url-safe, with no padding, string.
    #[serde(default)]
    pub base64_auth_material_bootstrap: Option<String>,

    /// Authentication material if required by the relay service.
    ///
    /// This material should be specified as a base64 url-safe, with no padding, string.
    #[serde(default)]
    pub base64_auth_material_relay: Option<String>,

    /// The Kitsune2 bootstrap server to use for WAN discovery.
    #[schemars(schema_with = "holochain_util::jsonschema::url2_schema")]
    pub bootstrap_url: url2::Url2,

    /// The Kitsune2 signaling server for WebRTC connections to use.
    #[schemars(schema_with = "holochain_util::jsonschema::url2_schema")]
    pub signal_url: url2::Url2,

    /// The iroh relay server address used with the iroh transport.
    #[schemars(schema_with = "holochain_util::jsonschema::url2_schema")]
    pub relay_url: url2::Url2,

    /// The amount of time, in seconds, to elapse before a request-response roundtrip times out.
    ///
    /// This value defaults to 60 seconds.
    ///
    /// This additionally sets two related timestamps:
    /// - The time to elapse before a single transport message times out (i.e. a request or response). Set to the floor of 1/2 of this value, so defaults to 30 seconds.
    /// - The time to elapse while attempting to establish a webrtc connection, before falling back to a relay connection. Set to the floor of 3/8 of this value, so defaults to 22 seconds.
    #[serde(default = "default_request_timeout_s")]
    pub request_timeout_s: u64,

    /// The Kitsune2 webrtc_config to use for connecting to peers.
    #[cfg_attr(
        all(feature = "schema", feature = "kitsune2_transport_tx5"),
        schemars(schema_with = "webrtc_config_schema")
    )]
    pub webrtc_config: Option<serde_json::Value>,

    /// The target arc factor to apply when receiving hints from kitsune2.
    /// In normal operation, leave this as the default 1.
    /// For leacher nodes that do not contribute to gossip, set to zero.
    #[serde(default = "default_target_arc_factor")]
    pub target_arc_factor: u32,

    /// Configure Kitsune2 Reporting.
    #[serde(default)]
    pub report: ReportConfig,

    /// Use this advanced field to directly configure kitsune2.
    ///
    /// The above options actually just set specific values in this config.
    /// Use only if you know what you are doing!
    #[cfg_attr(feature = "schema", schemars(schema_with = "kitsune2_config_schema"))]
    pub advanced: Option<serde_json::Value>,

    /// Disable the bootstrap module.
    #[cfg(feature = "test-utils")]
    #[serde(default)]
    pub disable_bootstrap: bool,

    /// Disable Kitsune publish.
    #[cfg(feature = "test-utils")]
    #[serde(default)]
    pub disable_publish: bool,

    /// Disable Kitsune gossip.
    #[cfg(feature = "test-utils")]
    #[serde(default)]
    pub disable_gossip: bool,
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            base64_auth_material_bootstrap: None,
            base64_auth_material_relay: None,
            bootstrap_url: url2::Url2::parse("https://dev-test-bootstrap2.holochain.org"),
            signal_url: url2::Url2::parse("wss://dev-test-bootstrap2.holochain.org"),
            relay_url: url2::Url2::parse("https://use1-1.relay.n0.iroh-canary.iroh.link./"),
            request_timeout_s: default_request_timeout_s(),
            webrtc_config: None,
            target_arc_factor: default_target_arc_factor(),
            report: Default::default(),
            advanced: None,
            #[cfg(feature = "test-utils")]
            disable_bootstrap: false,
            #[cfg(feature = "test-utils")]
            disable_publish: false,
            #[cfg(feature = "test-utils")]
            disable_gossip: false,
        }
    }
}

const fn default_request_timeout_s() -> u64 {
    60
}

const fn default_target_arc_factor() -> u32 {
    1
}

impl std::fmt::Debug for NetworkConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("NetworkConfig");
        s.field(
            "base64_auth_material_bootstrap",
            &self
                .base64_auth_material_bootstrap
                .as_ref()
                .map(|_| "<redacted>"),
        );
        s.field(
            "base64_auth_material_relay",
            &self
                .base64_auth_material_relay
                .as_ref()
                .map(|_| "<redacted>"),
        );
        s.field("bootstrap_url", &self.bootstrap_url);
        s.field("signal_url", &self.signal_url);
        s.field("relay_url", &self.relay_url);
        s.field("request_timeout_s", &self.request_timeout_s);
        s.field("webrtc_config", &self.webrtc_config);
        s.field("target_arc_factor", &self.target_arc_factor);
        s.field("report", &self.report);
        s.field("advanced", &self.advanced);
        #[cfg(feature = "test-utils")]
        {
            s.field("disable_bootstrap", &self.disable_bootstrap);
            s.field("disable_publish", &self.disable_publish);
            s.field("disable_gossip", &self.disable_gossip);
        }
        s.finish()
    }
}

impl NetworkConfig {
    /// Set the gossip interval.
    #[cfg(feature = "test-utils")]
    pub fn with_gossip_initiate_interval_ms(mut self, initiate_interval_ms: u32) -> Self {
        self.insert_into_config(|module_config| {
            Self::insert_module_config(
                module_config,
                "k2Gossip",
                "initiateIntervalMs",
                serde_json::Value::Number(serde_json::Number::from(initiate_interval_ms)),
            )?;

            Ok(())
        })
        .unwrap();

        self
    }

    /// Set the gossip initiate jitter.
    #[cfg(feature = "test-utils")]
    pub fn with_gossip_initiate_jitter_ms(mut self, initiate_jitter_ms: u32) -> Self {
        self.insert_into_config(|module_config| {
            Self::insert_module_config(
                module_config,
                "k2Gossip",
                "initiateJitterMs",
                serde_json::Value::Number(serde_json::Number::from(initiate_jitter_ms)),
            )?;

            Ok(())
        })
        .unwrap();

        self
    }

    /// Set the gossip min initiate interval.
    #[cfg(feature = "test-utils")]
    pub fn with_gossip_min_initiate_interval_ms(mut self, min_initiate_interval_ms: u32) -> Self {
        self.insert_into_config(|module_config| {
            Self::insert_module_config(
                module_config,
                "k2Gossip",
                "minInitiateIntervalMs",
                serde_json::Value::Number(serde_json::Number::from(min_initiate_interval_ms)),
            )?;

            Ok(())
        })
        .unwrap();

        self
    }

    /// Set the gossip round timeout.
    #[cfg(feature = "test-utils")]
    pub fn with_gossip_round_timeout_ms(mut self, round_timeout_ms: u32) -> Self {
        self.insert_into_config(|module_config| {
            Self::insert_module_config(
                module_config,
                "k2Gossip",
                "roundTimeoutMs",
                serde_json::Value::Number(serde_json::Number::from(round_timeout_ms)),
            )?;

            Ok(())
        })
        .unwrap();

        self
    }

    /// Convert the network config to a K2 config object.
    ///
    /// Values that are set directly on the network config are merged into the [`NetworkConfig::advanced`] field.
    pub fn to_k2_config(&self) -> ConductorConfigResult<serde_json::Value> {
        let mut working = self
            .advanced
            .clone()
            .unwrap_or_else(|| serde_json::Value::Object(Default::default()));

        if let Some(module_config) = working.as_object_mut() {
            Self::insert_module_config(
                module_config,
                "coreBootstrap",
                "serverUrl",
                serde_json::Value::String(self.bootstrap_url.as_str().into()),
            )?;
            Self::insert_module_config(
                module_config,
                "tx5Transport",
                "serverUrl",
                serde_json::Value::String(self.signal_url.as_str().into()),
            )?;

            // timeoutS is set to the floor of 1/2 of the request_timeout_s.
            let timeout_s: serde_json::Number = (self.request_timeout_s / 2).into();
            Self::insert_module_config(
                module_config,
                "tx5Transport",
                "timeoutS",
                serde_json::Value::Number(timeout_s),
            )?;

            // webrtcConnectTimeoutS is set to the floor of 3/8 of the request_timeout_s.
            let webrtc_connect_timeout_s: serde_json::Number =
                ((self.request_timeout_s * 3) / 8).into();
            Self::insert_module_config(
                module_config,
                "tx5Transport",
                "webrtcConnectTimeoutS",
                serde_json::Value::Number(webrtc_connect_timeout_s),
            )?;

            if let Some(webrtc_config) = &self.webrtc_config {
                Self::insert_module_config(
                    module_config,
                    "tx5Transport",
                    "webrtcConfig",
                    webrtc_config.clone(),
                )?;
            }

            if tracing::enabled!(target: "NETAUDIT", tracing::Level::WARN) {
                tracing::info!(
                    "The NETAUDIT target is enabled, turning on network backend tracing"
                );
                Self::insert_module_config(
                    module_config,
                    "tx5Transport",
                    "tracingEnabled",
                    serde_json::Value::Bool(true),
                )?;
            }

            Self::insert_module_config(
                module_config,
                "irohTransport",
                "relayUrl",
                serde_json::Value::String(self.relay_url.as_str().into()),
            )?;
        } else {
            return Err(ConductorConfigError::InvalidNetworkConfig(
                "advanced field must be an object".to_string(),
            ));
        }

        Ok(working)
    }

    #[cfg(feature = "test-utils")]
    fn insert_into_config(
        &mut self,
        mutator: impl Fn(&mut serde_json::Map<String, serde_json::Value>) -> ConductorConfigResult<()>,
    ) -> ConductorConfigResult<()> {
        if self.advanced.is_none() {
            self.advanced = Some(serde_json::Value::Object(Default::default()));
        }

        if let Some(module_config) = self
            .advanced
            .as_mut()
            .expect("Just checked")
            .as_object_mut()
        {
            mutator(module_config)?;
        }

        Ok(())
    }

    // Helper function for injecting a key-value pair into a module's configuration
    fn insert_module_config(
        module_config: &mut serde_json::Map<String, serde_json::Value>,
        module: &str,
        key: &str,
        value: serde_json::Value,
    ) -> ConductorConfigResult<()> {
        if let Some(module_config) = module_config.get_mut(module) {
            if let Some(module_config) = module_config.as_object_mut() {
                if module_config.contains_key(key) {
                    tracing::warn!("The {} module configuration contains a '{}' field, which is being overwritten", module, key);
                }

                // The config for this module exists and is an object, insert the key-value pair
                module_config.insert(key.into(), value);
            } else {
                // The configuration for this module exists, but isn't an object
                return Err(ConductorConfigError::InvalidNetworkConfig(format!(
                    "advanced.{module} field must be an object"
                )));
            }
        } else {
            // The config for this module isn't set at all, so we need to insert it
            module_config.insert(
                module.into(),
                serde_json::json!({
                    key: value,
                }),
            );
        }

        Ok(())
    }
}

/// Tuning parameters to adjust the behaviour of the conductor.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ConductorTuningParams {
    /// The delay between retries of sys validation when there are missing dependencies waiting to be found on the DHT.
    ///
    /// Default: 10 seconds
    pub sys_validation_retry_delay: Option<std::time::Duration>,
    /// The delay between retries attempts at resolving failed countersigning sessions.
    ///
    /// This is potentially a very heavy operation because it has to gather information from the network,
    /// so it is recommended not to set this too low.
    ///
    /// Default: 5 minutes
    pub countersigning_resolution_retry_delay: Option<std::time::Duration>,
    /// The maximum number of times that Holochain should attempt to resolve a failed countersigning session.
    ///
    /// Note that this *only* applies to sessions that fail through a timeout. Sessions that fail because
    /// of a conductor crash or otherwise will not be limited by this value. This is a safety measure to
    /// make it less likely that timeout leads to a wrong decision because of a temporary network issue.
    ///
    /// Holochain will always try once, whatever value you set. The possible values for this setting are:
    /// - `None`: Not set, then Holochain will just make a single attempt and then consider the session failed
    ///   if it can't make a decision.
    /// - `Some(0)`: Holochain will treat this the same as a session that failed after a crash. It will retry
    ///   until it can make a decision or until the user forces a decision.
    /// - `Some(n)`, n > 0: Holochain will retry `n` times, including the required first attempt. If
    ///   it can't make a decision after `n` retries, it will consider the session failed.
    pub countersigning_resolution_retry_limit: Option<usize>,
    /// Only publish a DhtOp once during this interval. This allows for triggering the publish workflow
    /// frequently without flooding the network with spurious publishes.
    ///
    /// Default: 5 minutes
    pub min_publish_interval: Option<std::time::Duration>,
    /// How often the publish workflow should be triggered.
    ///
    /// This should only be set in tests and will not be respected in production.
    ///
    /// Default: None
    pub publish_trigger_interval: Option<std::time::Duration>,
    /// Disable self-validation of authored ops.
    ///
    /// This is intended *ONLY* for testing. Disabling self-validation means that you lose the
    /// protection of checking your own ops before publishing them to the DHT. This is useful
    /// when testing warrants, where you want to intentionally author invalid ops.
    pub disable_self_validation: bool,
    /// Prevent issuance of warrants. Useful for testing whether warrants are gossiped
    /// and published.
    ///
    /// Default: false
    #[cfg(feature = "test-utils")]
    pub disable_warrant_issuance: bool,
}

impl ConductorTuningParams {
    /// Create a new [`ConductorTuningParams`] with all values missing, which will cause the defaults to be used.
    pub fn new() -> Self {
        Self {
            sys_validation_retry_delay: None,
            countersigning_resolution_retry_delay: None,
            countersigning_resolution_retry_limit: None,
            min_publish_interval: None,
            publish_trigger_interval: None,
            disable_self_validation: false,
            #[cfg(feature = "test-utils")]
            disable_warrant_issuance: false,
        }
    }

    /// Get the current value of `sys_validation_retry_delay` or its default value.
    pub fn sys_validation_retry_delay(&self) -> std::time::Duration {
        self.sys_validation_retry_delay
            .unwrap_or_else(|| std::time::Duration::from_secs(10))
    }

    /// Get the current value of `countersigning_resolution_retry_delay` or its default value.
    pub fn countersigning_resolution_retry_delay(&self) -> std::time::Duration {
        self.countersigning_resolution_retry_delay
            .unwrap_or_else(|| std::time::Duration::from_secs(60 * 5))
    }

    /// Get the current value of `min_publish_interval` or its default value.
    pub fn min_publish_interval(&self) -> std::time::Duration {
        self.min_publish_interval
            .unwrap_or_else(|| std::time::Duration::from_secs(60 * 5))
    }
}

impl Default for ConductorTuningParams {
    fn default() -> Self {
        let empty = Self::new();
        Self {
            sys_validation_retry_delay: Some(empty.sys_validation_retry_delay()),
            countersigning_resolution_retry_delay: Some(
                empty.countersigning_resolution_retry_delay(),
            ),
            countersigning_resolution_retry_limit: None,
            publish_trigger_interval: None,
            min_publish_interval: None,
            disable_self_validation: false,
            #[cfg(feature = "test-utils")]
            disable_warrant_issuance: false,
        }
    }
}

#[cfg(all(feature = "schema", feature = "kitsune2_transport_tx5"))]
fn webrtc_config_schema(_: &mut schemars::SchemaGenerator) -> Schema {
    let schema = schemars::schema_for!(Option<WebRtcConfig>);

    // Note that the definitions for this type are not being copied. This type is embedded in the
    // K2 config, so the definitions are already present in the schema.

    schema
}

#[cfg(feature = "schema")]
fn kitsune2_config_schema(generator: &mut schemars::SchemaGenerator) -> Schema {
    #[allow(dead_code)]
    #[derive(JsonSchema)]
    #[schemars(rename_all = "camelCase")]
    struct K2Config {
        #[serde(flatten)]
        core_bootstrap: Option<kitsune2_core::factories::CoreBootstrapModConfig>,
        #[serde(flatten)]
        core_fetch: Option<kitsune2_core::factories::CoreFetchModConfig>,
        #[serde(flatten)]
        core_publish: Option<kitsune2_core::factories::CorePublishModConfig>,
        #[serde(flatten)]
        core_space: Option<kitsune2_core::factories::CoreSpaceModConfig>,
        #[serde(flatten)]
        mem_peer_store: Option<kitsune2_core::factories::MemPeerStoreModConfig>,
        #[serde(flatten)]
        k2_gossip: Option<kitsune2_gossip::K2GossipModConfig>,
        #[cfg(feature = "kitsune2_transport_tx5")]
        #[serde(flatten)]
        tx5_transport: Option<kitsune2_transport_tx5::Tx5TransportModConfig>,
        #[serde(flatten)]
        iroh_transport: Option<kitsune2_transport_iroh::IrohTransportModConfig>,
    }

    let schema = schemars::schema_for!(Option<K2Config>);

    for (k, v) in schema
        .get("$defs")
        .and_then(|d| d.as_object())
        .expect("No definitions")
    {
        if generator
            .definitions_mut()
            .insert(k.clone(), v.clone())
            .is_some()
        {
            tracing::warn!("Conflicting definition for {k} in K2Config");
        }
    }

    schema
}

#[cfg(test)]
mod tests {
    use super::*;
    use holochain_types::websocket::AllowedOrigins;
    use matches::assert_matches;
    use std::path::Path;
    use std::path::PathBuf;

    #[test]
    fn config_load_yaml() {
        let bad_path = Path::new("fake");
        let result = ConductorConfig::load_yaml(bad_path);
        assert_eq!(
            "Err(ConfigMissing(\"fake\"))".to_string(),
            format!("{result:?}")
        );

        // successful load test in conductor/interactive
    }

    #[test]
    fn config_bad_yaml() {
        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml("this isn't yaml");
        assert_matches!(result, Err(ConductorConfigError::SerializationError(_)));
    }

    #[test]
    fn config_complete_minimal_config() {
        let yaml = r#"---
    data_root_path: /path/to/env
    keystore:
      type: danger_test_keystore
    "#;
        let result: ConductorConfig = config_from_yaml(yaml).unwrap();
        pretty_assertions::assert_eq!(
            result,
            ConductorConfig {
                tracing_override: None,
                data_root_path: Some(PathBuf::from("/path/to/env").into()),
                network: NetworkConfig::default(),
                keystore: KeystoreConfig::DangerTestKeystore,
                admin_interfaces: None,
                db_sync_strategy: DbSyncStrategy::default(),
                db_max_readers: default_db_max_readers(),
                tuning_params: None,
                tracing_scope: None,
                incoming_request_concurrency_limit: default_incoming_request_concurrency_limit(),
            }
        );
    }

    #[test]
    fn config_rejects_unrecognized_fields() {
        // Test unrecognized field at top level
        let yaml = r#"---
data_root_path: /path/to/env
keystore:
  type: danger_test_keystore
unknown_field: some_value
   "#;
        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml(yaml);
        assert_matches!(result, Err(ConductorConfigError::SerializationError(_)));
        if let Err(ConductorConfigError::SerializationError(e)) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("unknown_field"),
                "Error message should mention the unknown field name: {}",
                error_msg
            );
        }

        // Test unrecognized field in keystore config
        let yaml = r#"---
data_root_path: /path/to/env
keystore:
  type: lair_server
  unknown_keystore_field: true
   "#;
        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml(yaml);
        assert_matches!(result, Err(ConductorConfigError::SerializationError(_)));
        if let Err(ConductorConfigError::SerializationError(e)) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("unknown_keystore_field"),
                "Error message should mention the unknown field name: {}",
                error_msg
            );
        }
    }

    #[test]
    fn admin_interface_rejects_unrecognized_fields() {
        // Test unrecognized field in admin interface
        let yaml = r#"---
data_root_path: /path/to/env
keystore:
  type: danger_test_keystore
admin_interfaces:
  - driver:
      type: websocket
      port: 12345
      allowed_origins: "*"
      unknown_driver_field: test
   "#;
        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml(yaml);
        assert_matches!(result, Err(ConductorConfigError::SerializationError(_)));
        if let Err(ConductorConfigError::SerializationError(e)) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("unknown_driver_field"),
                "Error message should mention the unknown field name: {}",
                error_msg
            );
        }

        // Test unrecognized field at admin interface level
        let yaml = r#"---
data_root_path: /path/to/env
keystore:
  type: danger_test_keystore
admin_interfaces:
  - driver:
      type: websocket
      port: 12345
      allowed_origins: "*"
    unknown_admin_field: value
   "#;
        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml(yaml);
        assert_matches!(result, Err(ConductorConfigError::SerializationError(_)));
        if let Err(ConductorConfigError::SerializationError(e)) = result {
            let error_msg = e.to_string();
            assert!(
                error_msg.contains("unknown_admin_field"),
                "Error message should mention the unknown field name: {}",
                error_msg
            );
        }
    }

    #[test]
    fn empty_config_uses_default_values() {
        let result: ConductorConfig = config_from_yaml("").unwrap();
        pretty_assertions::assert_eq!(result, ConductorConfig::default());
    }

    #[test]
    #[allow(clippy::field_reassign_with_default)]
    fn config_complete_config() {
        holochain_trace::test_run();

        let yaml = r#"---
    data_root_path: /path/to/env

    keystore:
      type: lair_server_in_proc

    admin_interfaces:
      - driver:
          type: websocket
          port: 1234
          allowed_origins: "*"

    network:
      bootstrap_url: https://test-boot.tld
      signal_url: wss://test-sig.tld
      relay_url: https://relay.tld
      webrtc_config: {
        "iceServers": [
          { "urls": ["stun:test-stun.tld:443"] },
        ]
      }
      request_timeout_s: 70
      advanced: {
        "my": {
          "totally": {
            "random": {
              "advanced": {
                "config": true
              }
            }
          }
        }
      }

    db_sync_strategy: Fast
    db_max_readers: 100
    incoming_request_concurrency_limit: 100
    "#;

        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml(yaml);
        let mut network_config = NetworkConfig::default();
        network_config.bootstrap_url = url2::url2!("https://test-boot.tld");
        network_config.signal_url = url2::url2!("wss://test-sig.tld");
        network_config.relay_url = url2::url2!("https://relay.tld");
        network_config.request_timeout_s = 70;
        network_config.webrtc_config = Some(serde_json::json!({
            "iceServers": [
                { "urls": ["stun:test-stun.tld:443"] },
            ]
        }));
        network_config.advanced = Some(serde_json::json!({
            "my": {
                "totally": {
                    "random": {
                        "advanced": {
                            "config": true,
                        }
                    }
                }
            }
        }));

        pretty_assertions::assert_eq!(
            result.unwrap(),
            ConductorConfig {
                tracing_override: None,
                data_root_path: Some(PathBuf::from("/path/to/env").into()),
                keystore: KeystoreConfig::LairServerInProc { lair_root: None },
                admin_interfaces: Some(vec![AdminInterfaceConfig {
                    driver: InterfaceDriver::Websocket {
                        port: 1234,
                        danger_bind_addr: None,
                        allowed_origins: AllowedOrigins::Any
                    }
                }]),
                network: network_config,
                db_sync_strategy: DbSyncStrategy::Fast,
                db_max_readers: 100,
                incoming_request_concurrency_limit: 100,
                tuning_params: None,
                tracing_scope: None,
            }
        );
    }

    #[test]
    fn config_new_lair_keystore() {
        let yaml = r#"---
    data_root_path: /path/to/env
    keystore:
      type: lair_server
      connection_url: "unix:///var/run/lair-keystore/socket?k=EcRDnP3xDIZ9Rk_1E-egPE0mGZi5CcszeRxVkb2QXXQ"
    "#;
        let result: ConductorConfigResult<ConductorConfig> = config_from_yaml(yaml);
        pretty_assertions::assert_eq!(
            result.unwrap(),
            ConductorConfig {
                tracing_override: None,
                data_root_path: Some(PathBuf::from("/path/to/env").into()),
                network: NetworkConfig::default(),
                keystore: KeystoreConfig::LairServer {
                    connection_url: url2::url2!("unix:///var/run/lair-keystore/socket?k=EcRDnP3xDIZ9Rk_1E-egPE0mGZi5CcszeRxVkb2QXXQ"),
                },
                admin_interfaces: None,
                db_sync_strategy: DbSyncStrategy::Resilient,
                db_max_readers: default_db_max_readers(),
                tuning_params: None,
                tracing_scope: None,
                incoming_request_concurrency_limit: default_incoming_request_concurrency_limit(),
            }
        );
    }

    #[test]
    fn default_network_config_accepted_by_k2() {
        let network_config = NetworkConfig::default();
        let k2_config = network_config.to_k2_config().unwrap();

        let builder = kitsune2_core::default_test_builder()
            .with_default_config()
            .unwrap();
        builder.config.set_module_config(&k2_config).unwrap();
        builder.validate_config().unwrap();
    }

    #[test]
    fn network_config_preserves_advanced_overrides() {
        let network_config = NetworkConfig {
            advanced: Some(serde_json::json!({
                "coreBootstrap": {
                    "backoffMinMs": "3500",
                },
                "tx5Transport": {
                    "signalAllowPlainText": "true"
                },
                "irohTransport": {
                    "relayAllowPlainText": "true"
                },
                "coreSpace": {
                    "reSignFreqMs": "1000",
                }
            })),
            ..Default::default()
        };

        let k2_config = network_config.to_k2_config().unwrap();

        let builder = kitsune2_core::default_test_builder()
            .with_default_config()
            .unwrap();
        builder.config.set_module_config(&k2_config).unwrap();
        builder.validate_config().unwrap();

        assert_eq!(
            k2_config,
            serde_json::json!({
                "coreBootstrap": {
                    "serverUrl": "https://dev-test-bootstrap2.holochain.org/",
                    "backoffMinMs": "3500",
                },
                "tx5Transport": {
                    "serverUrl": "wss://dev-test-bootstrap2.holochain.org/",
                    "timeoutS": 30,
                    "webrtcConnectTimeoutS": 22,
                    "signalAllowPlainText": "true"
                },
                "irohTransport": {
                    "relayUrl": "https://use1-1.relay.n0.iroh-canary.iroh.link./",
                    "relayAllowPlainText": "true"
                },
                "coreSpace": {
                    "reSignFreqMs": "1000",
                }
            })
        );
    }

    #[test]
    fn network_config_overrides_conflicting_advanced_fields() {
        let network_config = NetworkConfig {
            advanced: Some(serde_json::json!({
                "coreBootstrap": {
                    "serverUrl": "https://something-else.net",
                },
                "tx5Transport": {
                    "serverUrl": "wss://sbd.nowhere.net",
                    "timeoutS": 10,
                    "webrtcConnectTimeoutS": 10
                },
                "irohTransport": {
                    "relayUrl": "https://iroh.nowhere.net",
                },
            })),
            ..Default::default()
        };

        let k2_config = network_config.to_k2_config().unwrap();

        let builder = kitsune2_core::default_test_builder()
            .with_default_config()
            .unwrap();
        builder.config.set_module_config(&k2_config).unwrap();
        builder.validate_config().unwrap();

        assert_eq!(
            k2_config,
            serde_json::json!({
                "coreBootstrap": {
                    "serverUrl": "https://dev-test-bootstrap2.holochain.org/",
                },
                "tx5Transport": {
                    "serverUrl": "wss://dev-test-bootstrap2.holochain.org/",
                    "timeoutS": 30,
                    "webrtcConnectTimeoutS": 22
                },
                "irohTransport": {
                    "relayUrl": "https://use1-1.relay.n0.iroh-canary.iroh.link./",
                },
            })
        );
    }

    #[test]
    fn tune_kitsune_params_for_testing() {
        let network_config = NetworkConfig::default()
            .with_gossip_round_timeout_ms(100)
            .with_gossip_initiate_interval_ms(200)
            .with_gossip_initiate_jitter_ms(50)
            .with_gossip_min_initiate_interval_ms(300);

        let k2_config = network_config.to_k2_config().unwrap();

        let builder = kitsune2_core::default_test_builder()
            .with_default_config()
            .unwrap();
        builder.config.set_module_config(&k2_config).unwrap();
        builder.validate_config().unwrap();

        assert_eq!(
            k2_config,
            serde_json::json!({
                "coreBootstrap": {
                    "serverUrl": "https://dev-test-bootstrap2.holochain.org/",
                },
                "tx5Transport": {
                    "serverUrl": "wss://dev-test-bootstrap2.holochain.org/",
                    "timeoutS": 30,
                    "webrtcConnectTimeoutS": 22
                },
                "irohTransport": {
                    "relayUrl": "https://use1-1.relay.n0.iroh-canary.iroh.link./",
                },
                "k2Gossip": {
                    "roundTimeoutMs": 100,
                    "initiateIntervalMs": 200,
                    "initiateJitterMs": 50,
                    "minInitiateIntervalMs": 300,
                }
            })
        );
    }

    #[test]
    fn default_db_max_readers_calculation() {
        let config = ConductorConfig::default();
        assert_eq!(config.db_max_readers, default_db_max_readers());

        // On systems with <= 4 CPUs, db_max_readers should default to 8.
        let cpu_count = 4;
        assert_eq!(calculate_default_db_max_readers(cpu_count), 8);

        let cpu_count = 3;
        assert_eq!(calculate_default_db_max_readers(cpu_count), 8);

        // On systems with > 4 CPUs, db_max_readers should default to 2x CPU count
        let cpu_count = 5;
        assert_eq!(calculate_default_db_max_readers(cpu_count), 10);

        // db_max_readers won't overflow
        let cpu_count = (u16::MAX as usize / 2) + 1;
        assert_eq!(calculate_default_db_max_readers(cpu_count), u16::MAX);

        let cpu_count = u32::MAX as usize;
        assert_eq!(calculate_default_db_max_readers(cpu_count), u16::MAX);
    }

    #[cfg(feature = "schema")]
    #[test]
    fn schema_generation() {
        let schema = schemars::schema_for!(ConductorConfig);
        let schema_json = serde_json::to_value(&schema).unwrap();

        let default_config = ConductorConfig::default();
        let default_config_json = serde_json::to_value(&default_config).unwrap();

        jsonschema::validate(&schema_json, &default_config_json).unwrap();
    }
}