Skip to content

Commit

Permalink
wip servers
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Dec 21, 2023
1 parent ce4ed82 commit 8cb738c
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ slug = "0.1.5"
sysinfo = "0.29.10"
thiserror = "1.0.50"
tokio = { version = "1", features = ["sync", "rt-multi-thread", "process"] }
toml = "0.8.8"
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3.64", features = ['Window'] }
Expand Down
15 changes: 15 additions & 0 deletions Servers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[server]]
name = "aspectron alpha"
location = "EU"
protocol = "wrpc:borsh"
network = ["testnet-10"]
address = "1.2.3.4"

[[server]]
name = "community alpha"
location = "EU"
protocol = "wrpc:json"
network = ["testnet-10"]
address = "4.5.6.7"
port = 3030

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ serde_json.workspace = true
serde.workspace = true
slug.workspace = true
thiserror.workspace = true
toml.workspace = true
wasm-bindgen.workspace = true
zeroize.workspace = true

Expand Down
5 changes: 5 additions & 0 deletions core/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::result::Result;
use crate::servers::parse_default_servers;
use cfg_if::cfg_if;
use kaspa_ng_core::runtime;
use kaspa_ng_core::settings::Settings;
Expand Down Expand Up @@ -187,6 +188,8 @@ cfg_if! {

set_log_level(LevelFilter::Info);

parse_default_servers();

let mut settings = if reset_settings {
println!("Resetting kaspa-ng settings on user request...");
Settings::default().store_sync()?.clone()
Expand Down Expand Up @@ -275,6 +278,8 @@ cfg_if! {

kaspa_core::log::set_log_level(kaspa_core::log::LevelFilter::Info);

parse_default_servers();

let settings = Settings::load().await.unwrap_or_else(|err| {
log_error!("Unable to load settings: {err}");
Settings::default()
Expand Down
20 changes: 20 additions & 0 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct Core {

pub device: Device,
pub market: Option<Market>,
pub servers : Option<Arc<Vec<Server>>>,
pub debug: bool,
}

Expand Down Expand Up @@ -178,13 +179,15 @@ impl Core {

device: Device::default(),
market: None,
servers : None,
debug: false,
};

modules.values().for_each(|module| {
module.init(&mut this);
});

this.update_servers();
this.wallet_update_list();

this
Expand Down Expand Up @@ -580,6 +583,9 @@ impl Core {
_frame: &mut eframe::Frame,
) -> Result<()> {
match event {
Events::ServerList { server_list } => {
self.servers = Some(server_list);
}
Events::Market(update) => {
if self.market.is_none() {
self.market = Some(Market::default());
Expand Down Expand Up @@ -1016,4 +1022,18 @@ impl Core {
pub fn apply_default_style(&self, ui: &mut Ui) {
ui.style_mut().text_styles = self.default_style.text_styles.clone();
}

pub fn update_servers(&self) {
let runtime = self.runtime.clone();
spawn(async move {
let server_list = load_servers().await?;
runtime
.send(Events::ServerList {
server_list,
})
.await?;
Ok(())
});
}

}
3 changes: 3 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ pub enum Error {

#[error("Account creation error")]
AccountCreationError,

#[error(transparent)]
Toml(#[from] toml::de::Error),
}

impl Error {
Expand Down
3 changes: 3 additions & 0 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub enum Events {
snapshot: Box<MetricsSnapshot>,
},
Error(Box<String>),
ServerList {
server_list : Arc<Vec<Server>>,
},
WalletList {
wallet_list: Arc<Vec<WalletDescriptor>>,
},
Expand Down
1 change: 1 addition & 0 deletions core/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub use crate::primitives::{
};
pub use crate::result::Result;
pub use crate::runtime::{runtime, spawn, spawn_with_result, Payload, Runtime, Service};
pub use crate::servers::{Server,load_servers};
pub use crate::settings::{
KaspadNodeKind, NetworkInterfaceConfig, NetworkInterfaceKind, NodeSettings, RpcConfig,
Settings, UserInterfaceSettings,
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ pub mod runtime;
pub mod settings;
pub mod state;
pub mod status;
pub mod servers;
pub mod sync;
pub mod utils;
2 changes: 1 addition & 1 deletion core/src/modules/overview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Overview {
});
});
} else {
CollapsingHeader::new(RichText::new(format!("{} {}",i18n("Update Available to version"), release.version)).color(theme_color().alert_color).strong())
CollapsingHeader::new(RichText::new(format!("{} {}",i18n("Update Available to version"), release.version)).color(theme_color().warning_color).strong())
.id_source("redistributables-update")
.default_open(true)
.show(ui, |ui| {
Expand Down
2 changes: 2 additions & 0 deletions core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::imports::*;
pub enum Network {
#[default]
Mainnet,
#[serde(alias = "testnet-10")]
Testnet10,
#[serde(alias = "testnet-11")]
Testnet11,
}

Expand Down
95 changes: 95 additions & 0 deletions core/src/servers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::imports::*;
use serde_json::Value;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Server {
pub name : Option<String>,
pub location : Option<String>,
pub protocol : String,
pub network : Vec<Network>,
pub port : Option<u16>,
pub address : String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServerConfig {
server : Vec<Server>,
}

// #[derive(Clone, Debug, Serialize, Deserialize)]
// pub struct Record {
// pub name : Option<String>,
// pub location : Option<String>,
// pub protocol : String,
// pub network : Network,
// pub port : Option<u16>,
// }

// impl From<(String, Record)> for Server {
// fn from((address, record) : (String, Record)) -> Self {
// Self {
// name : record.name,
// location : record.location,
// protocol : record.protocol,
// network : record.network,
// port : record.port,
// address,
// // uri : record.uri,
// }
// }
// }

// impl Server {
// pub fn new(name : &str, location : &str, protocol : &str, network : &Network, uri : &str) -> Self {
// Self {
// name : Some(name.to_string()),
// location : Some(location.to_string()),
// protocol : protocol.to_string(),
// network : network.clone(),
// // uri : uri.to_string(),
// }
// }
// }

fn parse_servers_impl(toml : &str) -> Result<Arc<Vec<Server>>> {
// let table: Value = toml::from_str(&toml)?;

// println!("table: {:?}", table);
// panic!();

Ok(toml::from_str::<ServerConfig>(toml)?.server.into())


}


fn parse_servers(toml : &str) -> Arc<Vec<Server>> {
match parse_servers_impl(toml) {
Ok(servers) => servers,
Err(e) => {
cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
println!();
panic!("Error parsing Servers.toml: {}", e);
} else {
log_error!("Error parsing Servers.toml: {}", e);
vec![].into()
}
}
}
}
}


pub fn parse_default_servers() -> &'static Arc<Vec<Server>> {
static EMBEDDED_SERVERS: OnceLock<Arc<Vec<Server>>> = OnceLock::new();
EMBEDDED_SERVERS.get_or_init(|| {
parse_servers(include_str!("../../Servers.toml"))
})
}

pub async fn load_servers() -> Result<Arc<Vec<Server>>> {
let servers = parse_default_servers();
println!("servers: {:?}", servers);
Ok(servers.clone())
}

0 comments on commit 8cb738c

Please sign in to comment.