Skip to content

Commit 9acae3b

Browse files
committed
better DriaNetwork logic
1 parent 10aa9ad commit 9acae3b

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

compute/src/config.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ impl DriaComputeNodeConfig {
9292

9393
// parse network type
9494
let network_type = env::var("DKN_NETWORK")
95-
.map(|s| DriaNetwork::from(s.as_str()))
96-
.unwrap_or_default();
95+
// if there is an explicit value, default to testnet on error
96+
.map(|s| DriaNetwork::try_from(s.as_str()).unwrap_or(DriaNetwork::Testnet))
97+
// if there is no explicit value, default to mainnet
98+
.unwrap_or(DriaNetwork::Mainnet);
9799
if network_type == DriaNetwork::Testnet {
98-
log::warn!("Using testnet!");
100+
log::warn!("Using testnet network!");
99101
}
100102

101103
// parse batch size

utils/src/network.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
use crate::SemanticVersion;
22

33
/// Network type, either mainnet or testnet.
4-
#[derive(Default, Debug, Clone, Copy, PartialEq)]
4+
#[derive(Debug, Clone, Copy, PartialEq)]
55
pub enum DriaNetwork {
66
Mainnet,
7-
#[default]
87
Testnet,
98
}
109

11-
impl From<&str> for DriaNetwork {
12-
fn from(s: &str) -> Self {
10+
impl TryFrom<&str> for DriaNetwork {
11+
type Error = ();
12+
13+
/// Converts a string to a `DriaNetwork`, using the same name as in:
14+
///
15+
/// - "mainnet" for `DriaNetwork::Mainnet`
16+
/// - "testnet" for `DriaNetwork::Testnet`
17+
fn try_from(s: &str) -> Result<Self, Self::Error> {
1318
match s {
14-
"mainnet" => DriaNetwork::Mainnet,
15-
"testnet" => DriaNetwork::Testnet,
16-
_ => Default::default(),
19+
"mainnet" => Ok(DriaNetwork::Mainnet),
20+
"testnet" => Ok(DriaNetwork::Testnet),
21+
_ => Err(()),
1722
}
1823
}
1924
}
@@ -28,13 +33,17 @@ impl std::fmt::Display for DriaNetwork {
2833
}
2934

3035
impl DriaNetwork {
36+
/// Returns the protocol name for the given network, which can be used by
37+
/// libp2p `identify` protocol.
3138
pub fn protocol_name(&self) -> &str {
3239
match self {
3340
DriaNetwork::Mainnet => "dria",
3441
DriaNetwork::Testnet => "dria-test",
3542
}
3643
}
3744

45+
/// Returns the discovery URL for the given version, where the
46+
/// major.minor version is appended to the URL as a path variable.
3847
pub fn discovery_url(&self, version: &SemanticVersion) -> String {
3948
let base_url = match self {
4049
DriaNetwork::Mainnet => "https://mainnet.dkn.dria.co/discovery/v0/available-nodes",

0 commit comments

Comments
 (0)