Skip to content

Commit 620c9d1

Browse files
committed
added better RPC discovery logic
1 parent 6ddb002 commit 620c9d1

File tree

8 files changed

+103
-64
lines changed

8 files changed

+103
-64
lines changed

compute/src/config.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
use dkn_p2p::{
2-
libp2p::{Multiaddr, PeerId},
3-
DriaNetworkType,
4-
};
1+
use dkn_p2p::libp2p::{Multiaddr, PeerId};
52
use dkn_workflows::DriaWorkflowsConfig;
63
use eyre::{eyre, Result};
74
use libsecp256k1::{PublicKey, SecretKey};
85
use std::{env, str::FromStr};
96

107
use dkn_utils::{
118
crypto::{public_key_to_address, secret_to_keypair},
12-
SemanticVersion,
9+
DriaNetwork, SemanticVersion,
1310
};
1411

1512
const DEFAULT_TASK_BATCH_SIZE: usize = 5;
@@ -32,7 +29,7 @@ pub struct DriaComputeNodeConfig {
3229
/// Workflow configurations, e.g. models and providers.
3330
pub workflows: DriaWorkflowsConfig,
3431
/// Network type of the node.
35-
pub network_type: DriaNetworkType,
32+
pub network_type: DriaNetwork,
3633
/// Batch size for batchable tasks (e.g. API-based ones).
3734
///
3835
/// A higher value will help execute more tasks concurrently,
@@ -95,7 +92,7 @@ impl DriaComputeNodeConfig {
9592

9693
// parse network type
9794
let network_type = env::var("DKN_NETWORK")
98-
.map(|s| DriaNetworkType::from(s.as_str()))
95+
.map(|s| DriaNetwork::from(s.as_str()))
9996
.unwrap_or_default();
10097

10198
// parse batch size

compute/src/node/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl DriaComputeNode {
113113
"Connection to RPC {} is lost, geting a new one!",
114114
self.dria_rpc.addr,
115115
);
116-
match DriaRPC::new_for_network(self.dria_rpc.network).await {
116+
match DriaRPC::new_for_network(self.dria_rpc.network, &self.config.version).await {
117117
Ok(new_rpc) => {
118118
self.dria_rpc = new_rpc;
119119

compute/src/node/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl DriaComputeNode {
8282
log::info!("Using initial RPC address: {}", addr);
8383
DriaRPC::new(addr, config.network_type).expect("could not get RPC to connect to")
8484
} else {
85-
DriaRPC::new_for_network(config.network_type)
85+
DriaRPC::new_for_network(config.network_type, &config.version)
8686
.await
8787
.expect("could not get RPC to connect to")
8888
};

compute/src/node/rpc.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use dkn_p2p::libp2p::{multiaddr::Protocol, Multiaddr, PeerId};
2-
use dkn_p2p::DriaNetworkType;
2+
use dkn_utils::{DriaNetwork, SemanticVersion};
33
use eyre::{Context, OptionExt, Result};
44
use std::fmt::Debug;
55

@@ -8,12 +8,12 @@ use std::fmt::Debug;
88
pub struct DriaRPC {
99
pub addr: Multiaddr,
1010
pub peer_id: PeerId,
11-
pub network: DriaNetworkType,
11+
pub network: DriaNetwork,
1212
}
1313

1414
impl DriaRPC {
1515
/// Creates a new RPC target at the given type, along with a network type for refreshing the RPC address.
16-
pub fn new(addr: Multiaddr, network: DriaNetworkType) -> Result<Self> {
16+
pub fn new(addr: Multiaddr, network: DriaNetwork) -> Result<Self> {
1717
let peer_id = addr
1818
.iter()
1919
.find_map(|p| match p {
@@ -29,28 +29,31 @@ impl DriaRPC {
2929
})
3030
}
3131

32-
/// Creates a new RPC target for the given network type.
33-
pub async fn new_for_network(network: DriaNetworkType) -> Result<Self> {
34-
let addr = get_rpc_for_network(&network).await?;
32+
/// Creates a new RPC target for the given network type and version.
33+
pub async fn new_for_network(network: DriaNetwork, version: &SemanticVersion) -> Result<Self> {
34+
let addr = get_rpc_for_network(&network, version).await?;
3535
Self::new(addr, network)
3636
}
3737
}
3838

3939
/// Calls the DKN API to get an RPC address for the given network type.
4040
///
4141
/// The peer id is expected to be within the multi-address.
42-
async fn get_rpc_for_network(network: &DriaNetworkType) -> Result<Multiaddr> {
42+
async fn get_rpc_for_network(
43+
network: &DriaNetwork,
44+
version: &SemanticVersion,
45+
) -> Result<Multiaddr> {
4346
#[derive(serde::Deserialize, Debug)]
4447
struct DriaNodesApiResponse {
4548
pub rpc: Multiaddr,
4649
}
4750

4851
// url to be used is determined by the network type
49-
let url = match network {
50-
DriaNetworkType::Community => "https://dkn.dria.co/v5/available-nodes",
51-
DriaNetworkType::Pro => "https://dkn.dria.co/v5/sdk/available-nodes",
52-
DriaNetworkType::Test => "https://dkn.dria.co/v5/test/available-nodes",
52+
let base_url = match network {
53+
DriaNetwork::Mainnet => "https://dkn.dria.co/available-nodes",
54+
DriaNetwork::Testnet => "https://dkn.dria.co/available-nodes",
5355
};
56+
let url = format!("{}/{}", base_url, version.as_major_minor());
5457

5558
// make the request
5659
let response = reqwest::get(url).await?;
@@ -69,7 +72,9 @@ mod tests {
6972

7073
#[tokio::test]
7174
async fn test_dria_nodes() {
72-
let node = DriaRPC::new_for_network(DriaNetworkType::Community).await;
75+
let node =
76+
DriaRPC::new_for_network(DriaNetwork::Mainnet, &SemanticVersion::from_crate_version())
77+
.await;
7378
assert!(node.is_ok());
7479
}
7580
}

p2p/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ pub use commands::{DriaP2PCommand, DriaP2PCommander};
99
mod protocol;
1010
pub use protocol::DriaP2PProtocol;
1111

12-
mod network;
13-
pub use network::DriaNetworkType;
14-
1512
// re-exports
1613
pub use libp2p;
1714
pub use libp2p_identity;

p2p/src/network.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

utils/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub use csv::split_csv_line;
1212
mod env;
1313
pub use env::safe_read_env;
1414

15+
mod network;
16+
pub use network::DriaNetwork;
17+
1518
mod version;
1619
pub use version::SemanticVersion;
1720

utils/src/network.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use crate::SemanticVersion;
2+
3+
/// Network type, either mainnet or testnet.
4+
#[derive(Default, Debug, Clone, Copy, PartialEq)]
5+
pub enum DriaNetwork {
6+
Mainnet,
7+
#[default]
8+
Testnet,
9+
}
10+
11+
impl From<&str> for DriaNetwork {
12+
fn from(s: &str) -> Self {
13+
match s {
14+
"mainnet" => DriaNetwork::Mainnet,
15+
"testnet" => DriaNetwork::Testnet,
16+
_ => Default::default(),
17+
}
18+
}
19+
}
20+
21+
impl std::fmt::Display for DriaNetwork {
22+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23+
match self {
24+
DriaNetwork::Mainnet => write!(f, "mainnet"),
25+
DriaNetwork::Testnet => write!(f, "testnet"),
26+
}
27+
}
28+
}
29+
30+
impl DriaNetwork {
31+
pub fn protocol_name(&self) -> &str {
32+
match self {
33+
DriaNetwork::Mainnet => "dria",
34+
DriaNetwork::Testnet => "dria-test",
35+
}
36+
}
37+
38+
pub fn discovery_url(&self, version: &SemanticVersion) -> String {
39+
let base_url = match self {
40+
DriaNetwork::Mainnet => "https://mainnet.dkn.dria.co/discovery/available-nodes",
41+
DriaNetwork::Testnet => "https://testnet.dkn.dria.co/discovery/available-nodes",
42+
};
43+
44+
format!("{}/{}", base_url, version.as_major_minor())
45+
}
46+
}
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::*;
51+
52+
#[test]
53+
fn test_dria_network() {
54+
let mainnet = DriaNetwork::Mainnet;
55+
let testnet = DriaNetwork::Testnet;
56+
let version = SemanticVersion {
57+
major: 1,
58+
minor: 0,
59+
patch: 42,
60+
};
61+
62+
assert_eq!(mainnet.to_string(), "mainnet");
63+
assert_eq!(testnet.to_string(), "testnet");
64+
65+
assert_eq!(mainnet.protocol_name(), "dria");
66+
assert_eq!(testnet.protocol_name(), "dria-test");
67+
68+
assert_eq!(
69+
mainnet.discovery_url(&version),
70+
"https://mainnet.dkn.dria.co/discovery/available-nodes/1.0"
71+
);
72+
assert_eq!(
73+
testnet.discovery_url(&version),
74+
"https://testnet.dkn.dria.co/discovery/available-nodes/1.0"
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)