Skip to content

Commit 3446f90

Browse files
Copilotlarp0
andcommitted
Add SVM network management infrastructure and multi-network support
Co-authored-by: larp0 <[email protected]>
1 parent 57851d8 commit 3446f90

File tree

5 files changed

+325
-10
lines changed

5 files changed

+325
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ spl-token = "4.0"
2020
base64 = "0.21"
2121
bs58 = "0.5"
2222
bincode = "1.3"
23+
reqwest = { version = "0.11", features = ["json"] }

src/config.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
use anyhow::{Result, Context};
2-
use serde::Deserialize;
3-
use std::{env, fs};
2+
use serde::{Deserialize, Serialize};
3+
use std::{env, fs, collections::HashMap};
44
use crate::protocol::LATEST_PROTOCOL_VERSION;
55

6-
#[derive(Debug, Deserialize)]
6+
#[derive(Debug, Deserialize, Serialize, Clone)]
7+
pub struct SvmNetwork {
8+
pub name: String,
9+
pub rpc_url: String,
10+
pub enabled: bool,
11+
}
12+
13+
#[derive(Debug, Deserialize, Serialize, Clone)]
714
pub struct Config {
815
pub rpc_url: String,
916
pub commitment: String,
1017
pub protocol_version: String,
18+
#[serde(default)]
19+
pub svm_networks: HashMap<String, SvmNetwork>,
1120
}
1221

1322
impl Config {
@@ -33,6 +42,15 @@ impl Config {
3342
rpc_url,
3443
commitment,
3544
protocol_version,
45+
svm_networks: HashMap::new(),
3646
})
3747
}
48+
49+
pub fn save(&self) -> Result<()> {
50+
let content = serde_json::to_string_pretty(self)
51+
.context("Failed to serialize config")?;
52+
fs::write("config.json", content)
53+
.context("Failed to write config.json")?;
54+
Ok(())
55+
}
3856
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ pub mod server;
55
pub mod tools;
66
pub mod transport;
77

8-
pub use config::Config;
8+
pub use config::{Config, SvmNetwork};
99
pub use server::start_server;
1010
pub use transport::CustomStdioTransport;

src/server/mod.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ use anyhow::Result;
22
use solana_client::nonblocking::rpc_client::RpcClient;
33
use solana_sdk::commitment_config::CommitmentConfig;
44
use std::sync::Arc;
5+
use std::collections::HashMap;
56
use tokio::sync::RwLock;
67
use crate::transport::{Transport, JsonRpcMessage, JsonRpcNotification, JsonRpcVersion};
78
use crate::{Config, CustomStdioTransport};
89

910
pub struct ServerState {
1011
pub rpc_client: RpcClient,
12+
pub svm_clients: HashMap<String, RpcClient>,
13+
pub config: Config,
1114
pub initialized: bool,
1215
pub protocol_version: String,
1316
}
1417

1518
impl ServerState {
16-
pub fn new(config: &Config) -> Self {
19+
pub fn new(config: Config) -> Self {
1720
let commitment = match config.commitment.as_str() {
1821
"processed" => CommitmentConfig::processed(),
1922
"confirmed" => CommitmentConfig::confirmed(),
@@ -23,15 +26,68 @@ impl ServerState {
2326

2427
let rpc_client = RpcClient::new_with_commitment(
2528
config.rpc_url.clone(),
26-
commitment,
29+
commitment.clone(),
2730
);
2831

32+
// Create RPC clients for enabled SVM networks
33+
let mut svm_clients = HashMap::new();
34+
for (network_id, network) in &config.svm_networks {
35+
if network.enabled {
36+
let client = RpcClient::new_with_commitment(
37+
network.rpc_url.clone(),
38+
commitment.clone(),
39+
);
40+
svm_clients.insert(network_id.clone(), client);
41+
}
42+
}
43+
2944
Self {
3045
rpc_client,
46+
svm_clients,
47+
protocol_version: config.protocol_version.clone(),
48+
config,
3149
initialized: false,
32-
protocol_version: config.protocol_version.clone()
3350
}
3451
}
52+
53+
pub fn update_config(&mut self, new_config: Config) {
54+
let commitment = match new_config.commitment.as_str() {
55+
"processed" => CommitmentConfig::processed(),
56+
"confirmed" => CommitmentConfig::confirmed(),
57+
"finalized" => CommitmentConfig::finalized(),
58+
_ => CommitmentConfig::default(),
59+
};
60+
61+
// Update main RPC client if URL changed
62+
if self.config.rpc_url != new_config.rpc_url {
63+
self.rpc_client = RpcClient::new_with_commitment(
64+
new_config.rpc_url.clone(),
65+
commitment.clone(),
66+
);
67+
}
68+
69+
// Update SVM clients
70+
self.svm_clients.clear();
71+
for (network_id, network) in &new_config.svm_networks {
72+
if network.enabled {
73+
let client = RpcClient::new_with_commitment(
74+
network.rpc_url.clone(),
75+
commitment.clone(),
76+
);
77+
self.svm_clients.insert(network_id.clone(), client);
78+
}
79+
}
80+
81+
self.config = new_config;
82+
}
83+
84+
pub fn get_enabled_networks(&self) -> Vec<&str> {
85+
self.config.svm_networks
86+
.iter()
87+
.filter(|(_, network)| network.enabled)
88+
.map(|(id, _)| id.as_str())
89+
.collect()
90+
}
3591
}
3692

3793
pub async fn start_server() -> Result<()> {
@@ -40,7 +96,7 @@ pub async fn start_server() -> Result<()> {
4096
let config = Config::load()?;
4197
log::info!("Loaded config: RPC URL: {}, Protocol Version: {}", config.rpc_url, config.protocol_version);
4298

43-
let state = Arc::new(RwLock::new(ServerState::new(&config)));
99+
let state = Arc::new(RwLock::new(ServerState::new(config.clone())));
44100

45101
let transport = CustomStdioTransport::new();
46102
transport.open()?;

0 commit comments

Comments
 (0)