settings.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. use std::sync::Arc;
  2. use serde::Deserialize;
  3. use structopt::StructOpt;
  4. use structopt_toml::StructOptToml;
  5. use url::Url;
  6. /// Atomic pointer to network settings.
  7. pub type SettingsPtr = Arc<Settings>;
  8. /// Default settings for the network. Can be manually configured.
  9. #[derive(Clone, Debug)]
  10. pub struct Settings {
  11. pub inbound: Option<Url>,
  12. pub outbound_connections: u32,
  13. pub manual_attempt_limit: u32,
  14. pub seed_query_timeout_seconds: u32,
  15. pub connect_timeout_seconds: u32,
  16. pub channel_handshake_seconds: u32,
  17. pub channel_heartbeat_seconds: u32,
  18. pub outbound_retry_seconds: u64,
  19. pub external_addr: Option<Url>,
  20. pub peers: Vec<Url>,
  21. pub seeds: Vec<Url>,
  22. pub node_id: String,
  23. }
  24. impl Default for Settings {
  25. fn default() -> Self {
  26. Self {
  27. inbound: None,
  28. outbound_connections: 0,
  29. manual_attempt_limit: 0,
  30. seed_query_timeout_seconds: 8,
  31. connect_timeout_seconds: 10,
  32. channel_handshake_seconds: 4,
  33. channel_heartbeat_seconds: 10,
  34. outbound_retry_seconds: 20,
  35. external_addr: None,
  36. peers: Vec::new(),
  37. seeds: Vec::new(),
  38. node_id: String::new(),
  39. }
  40. }
  41. }
  42. /// Defines the network settings.
  43. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  44. #[structopt()]
  45. pub struct SettingsOpt {
  46. /// P2P accept address
  47. #[structopt(long = "accept")]
  48. pub inbound: Option<Url>,
  49. /// Connection slots
  50. #[structopt(long = "slots")]
  51. pub outbound_connections: Option<u32>,
  52. /// P2P external address
  53. #[structopt(long)]
  54. pub external_addr: Option<Url>,
  55. /// Peer nodes to connect to
  56. #[serde(default)]
  57. #[structopt(long)]
  58. pub peers: Vec<Url>,
  59. /// Seed nodes to connect to
  60. #[serde(default)]
  61. #[structopt(long)]
  62. pub seeds: Vec<Url>,
  63. #[structopt(skip)]
  64. pub manual_attempt_limit: Option<u32>,
  65. #[structopt(skip)]
  66. pub seed_query_timeout_seconds: Option<u32>,
  67. #[structopt(skip)]
  68. pub connect_timeout_seconds: Option<u32>,
  69. #[structopt(skip)]
  70. pub channel_handshake_seconds: Option<u32>,
  71. #[structopt(skip)]
  72. pub channel_heartbeat_seconds: Option<u32>,
  73. #[structopt(skip)]
  74. pub outbound_retry_seconds: Option<u64>,
  75. #[serde(default)]
  76. #[structopt(skip)]
  77. pub node_id: String,
  78. }
  79. impl From<SettingsOpt> for Settings {
  80. fn from(settings_opt: SettingsOpt) -> Self {
  81. Self {
  82. inbound: settings_opt.inbound,
  83. outbound_connections: settings_opt.outbound_connections.unwrap_or(0),
  84. manual_attempt_limit: settings_opt.manual_attempt_limit.unwrap_or(0),
  85. seed_query_timeout_seconds: settings_opt.seed_query_timeout_seconds.unwrap_or(8),
  86. connect_timeout_seconds: settings_opt.connect_timeout_seconds.unwrap_or(10),
  87. channel_handshake_seconds: settings_opt.channel_handshake_seconds.unwrap_or(4),
  88. channel_heartbeat_seconds: settings_opt.channel_heartbeat_seconds.unwrap_or(10),
  89. outbound_retry_seconds: settings_opt.outbound_retry_seconds.unwrap_or(1200),
  90. external_addr: settings_opt.external_addr,
  91. peers: settings_opt.peers,
  92. seeds: settings_opt.seeds,
  93. node_id: settings_opt.node_id,
  94. }
  95. }
  96. }