Skip to content

Commit

Permalink
wip - remote node configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Jan 10, 2024
1 parent f110487 commit b5f9048
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 195 deletions.
40 changes: 24 additions & 16 deletions Servers.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
[[server]]
# if port is not present, port is automatically
# determined by the protocol+network combination
# this means the address is capable of serving
# multiple networks. Supported networks must be
# specified in the network field.
name = "local network agnostic server"
name = "kaspa-ng.org"
location = "EU"
protocol = "wrpc:borsh"
network = ["mainnet","testnet-10"]
address = "127.0.0.1"
protocol = "borsh"
network = ["mainnet"]
address = "wss://kaspa-ng.org:443/mainnet"

[[server]]
# if the port is present, this means the address
# is only capable of serving a single network
# on a dedicated port. The network field should
# contains a single network entry.
name = "local testnet server"
name = "kaspa-ng.org"
location = "EU"
protocol = "wrpc:borsh"
protocol = "borsh"
network = ["testnet-11"]
address = "wss://kaspa-ng.org:443/testnet-11"

[[server]]
name = "localhost"
protocol = "borsh"
network = ["mainnet"]
address = "127.0.0.1"

[[server]]
name = "localhost"
protocol = "borsh"
network = ["testnet-10"]
address = "127.0.0.1"
port = 17210

[[server]]
name = "localhost"
protocol = "borsh"
network = ["testnet-11"]
address = "127.0.0.1"

21 changes: 20 additions & 1 deletion core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use std::borrow::Cow;
use workflow_i18n::*;
use workflow_wasm::callback::CallbackMap;

const MAX_NETWORK_LOAD_SAMPLES: usize = 16;

pub enum Exception {
UtxoIndexNotEnabled { url: Option<String> },
}
Expand Down Expand Up @@ -54,6 +56,7 @@ pub struct Core {
pub debug: bool,
pub window_frame: bool,
callback_map: CallbackMap,
network_load_samples: VecDeque<f32>,
}

impl Core {
Expand Down Expand Up @@ -183,12 +186,13 @@ impl Core {

release: None,

device: Device::default(),
device: Device::new(window_frame),
market: None,
servers: parse_default_servers().clone(),
debug: false,
window_frame,
callback_map: CallbackMap::default(),
network_load_samples: VecDeque::default(),
};

modules.values().for_each(|module| {
Expand Down Expand Up @@ -635,6 +639,19 @@ impl Core {
Events::Metrics { snapshot } => {
self.metrics = Some(snapshot);
}
Events::MempoolSize { mempool_size } => {
// println!("mempool_size: {} tps: {}", mempool_size,self.settings.node.network.tps());
let load = mempool_size as f32 / self.settings.node.network.tps() as f32;
self.network_load_samples.push_back(load);
if self.network_load_samples.len() > MAX_NETWORK_LOAD_SAMPLES {
self.network_load_samples.pop_front();
}
let network_load = self.network_load_samples.iter().sum::<f32>()
/ self.network_load_samples.len() as f32;
self.state.network_load.replace(network_load);

// println!("network_load: {}", network_load);
}
Events::Exit => {
cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
Expand Down Expand Up @@ -692,7 +709,9 @@ impl Core {
self.state.url = None;
self.state.network_id = None;
self.state.current_daa_score = None;
self.state.network_load = None;
self.metrics = Some(Box::default());
self.network_load_samples.clear();
}
CoreWallet::UtxoIndexNotEnabled { url } => {
self.exception = Some(Exception::UtxoIndexNotEnabled { url });
Expand Down
10 changes: 8 additions & 2 deletions core/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ pub enum Orientation {
Portrait,
}

#[derive(Default, Clone)]
#[derive(Clone)]
pub struct Device {
pub mobile_device: bool,
pub mobile_forced: bool,
pub orientation: Orientation,
pub orientation_forced: Option<Orientation>,
pub screen_size: Vec2,
pub top_offset: f32,
}

impl Device {
pub fn new() -> Self {
pub fn new(window_frame: bool) -> Self {
Self {
mobile_device: false,
mobile_forced: false,
orientation: Orientation::default(),
orientation_forced: None,
screen_size: Vec2::ZERO,
top_offset: if window_frame { 0.0 } else { 32.0 },
}
}

Expand All @@ -39,6 +41,10 @@ impl Device {
self.screen_size = rect.size();
}

pub fn top_offset(&self) -> f32 {
self.top_offset
}

pub fn orientation(&self) -> Orientation {
self.orientation_forced.unwrap_or(self.orientation)
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub enum Error {
#[error("Invalid network type")]
InvalidNetworkType,

#[error("Invalid network '{0}'")]
InvalidNetwork(String),

#[error("Http error: {0}")]
HttpError(#[from] workflow_http::error::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 {
Metrics {
snapshot: Box<MetricsSnapshot>,
},
MempoolSize {
mempool_size: usize,
},
Error(Box<String>),
ServerList {
server_list: Arc<Vec<Server>>,
Expand Down
7 changes: 4 additions & 3 deletions core/src/modules/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ impl ModuleT for Logs {

fn render(
&mut self,
_core: &mut Core,
core: &mut Core,
_ctx: &egui::Context,
_frame: &mut eframe::Frame,
ui: &mut egui::Ui,
) {
use egui_phosphor::light::CLIPBOARD_TEXT;

let available_width = ui.available_width();

#[cfg(not(target_arch = "wasm32"))]
egui::ScrollArea::vertical()
.id_source("node_logs")
Expand All @@ -38,9 +40,8 @@ impl ModuleT for Logs {

let copy_to_clipboard = Button::new(RichText::new(format!(" {CLIPBOARD_TEXT} ")).size(20.));

let screen_rect = ui.ctx().screen_rect();
let button_rect = Rect::from_min_size(
pos2(screen_rect.max.x - 48.0, screen_rect.min.y + 32.0),
pos2(available_width - 48.0, core.device().top_offset() + 32.0),
vec2(38.0, 20.0),
);

Expand Down
2 changes: 1 addition & 1 deletion core/src/modules/overview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Overview {
let screen_rect = ui.ctx().screen_rect();
let logo_size = vec2(648., 994.,) * 0.25;
let left = screen_rect.width() - logo_size.x - 8.;
let top = if core.window_frame { 64. } else { 32. };
let top = core.device().top_offset() + 32.0;
let logo_rect = Rect::from_min_size(Pos2::new(left, top), logo_size);

if screen_rect.width() > 768.0 && !core.device().single_pane() {
Expand Down
127 changes: 104 additions & 23 deletions core/src/modules/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ pub struct Settings {
settings : crate::settings::Settings,
grpc_network_interface : NetworkInterfaceEditor,
reset_settings : bool,


secure_wrpc_only : bool,
}

impl Settings {
pub fn new(runtime: Runtime) -> Self {

cfg_if!{
if #[cfg(target_arch = "wasm32")] {
use workflow_dom::utils::*;
let protocol = window().location().protocol().unwrap();
let secure_wrpc_only = protocol == "https:";
} else {
let secure_wrpc_only = false;
}
}

Self {
runtime,
settings : crate::settings::Settings::default(),
grpc_network_interface : NetworkInterfaceEditor::default(),
reset_settings : false,
secure_wrpc_only,
}
}

Expand Down Expand Up @@ -57,6 +68,68 @@ impl ModuleT for Settings {

impl Settings {


fn render_available_servers(
&mut self,
core: &mut Core,
ui: &mut egui::Ui,
) {

let servers = core.servers.iter().filter(|server| {
if server.network.contains(&self.settings.node.network) {
!(self.secure_wrpc_only && !(server.address.starts_with("wss://") || server.address.starts_with("wrpcs://")))
} else {
false
}
}).collect::<Vec<_>>();
let server_count = servers.len();

CollapsingHeader::new(format!("{} {} {}", i18n("Public p2p Nodes for"), self.settings.node.network, server_count))
.default_open(true)
.show(ui, |ui| {
ui.add_space(4.);

let use_popup = false;

if !use_popup {

for server in servers {
if ui.add(Label::new(format!("• {server}")).sense(Sense::click())).clicked() {
self.settings.node.wrpc_encoding = server.wrpc_encoding();
self.settings.node.wrpc_url = server.address();
}
}

} else {

PopupPanel::new(ui, "server_selector_popup",|ui|{ ui.add(Label::new(format!("{} ⏷",i18n("Select Available Server"))).sense(Sense::click())) }, |ui, _close| {
egui::ScrollArea::vertical()
.id_source("server_selector_popup_scroll")
.auto_shrink([true; 2])
.show(ui, |ui| {
for server in servers {
if ui.add_sized(theme_style().large_button_size,CompositeButton::opt_image_and_text(
None,
Some(server.to_string().into()),
None,
)).clicked() {
self.settings.node.wrpc_encoding = server.wrpc_encoding();
self.settings.node.wrpc_url = server.address();
}
}
});
})
.with_min_width(240.)
.with_max_height(core.device().screen_size.y * 0.5)
.with_close_on_interaction(true)
.build(ui);
}

ui.add_space(4.);
});

}

fn render_node_settings(
&mut self,
core: &mut Core,
Expand All @@ -71,18 +144,17 @@ impl Settings {
.default_open(true)
.show(ui, |ui| {

// if !disable_node_settings {
CollapsingHeader::new("Kaspa Network")
.default_open(true)
.show(ui, |ui| {
ui.horizontal_wrapped(|ui|{
Network::iter().for_each(|network| {
ui.radio_value(&mut self.settings.node.network, *network, network.to_string());
});
CollapsingHeader::new("Kaspa Network")
.default_open(true)
.show(ui, |ui| {
ui.horizontal_wrapped(|ui|{
Network::iter().for_each(|network| {
ui.radio_value(&mut self.settings.node.network, *network, network.to_string());
});
});
});



CollapsingHeader::new("Kaspa Node")
.default_open(true)
.show(ui, |ui| {
Expand Down Expand Up @@ -135,7 +207,7 @@ impl Settings {
}

#[cfg(not(target_arch = "wasm32"))]
if core.settings.developer.custom_daemon_args_enabled() && core.settings.node.node_kind.is_config_capable() {
if core.settings.developer.custom_daemon_args_enabled() && self.settings.node.node_kind.is_config_capable() {
use kaspad_lib::args::Args;
use clap::error::ErrorKind as ClapErrorKind;
use crate::runtime::services::kaspa::Config;
Expand Down Expand Up @@ -202,9 +274,16 @@ impl Settings {
} else {
self.settings.node.grpc_network_interface = self.grpc_network_interface.as_ref().try_into().unwrap(); //NetworkInterfaceConfig::try_from(&self.grpc_network_interface).unwrap();
}

ui.add_space(4.);
});

if self.settings.node.node_kind == KaspadNodeKind::Remote {

self.render_available_servers(core,ui);



CollapsingHeader::new(i18n("Remote p2p Node Configuration"))
.default_open(true)
.show(ui, |ui| {
Expand Down Expand Up @@ -233,14 +312,18 @@ impl Settings {
node_settings_error = Some(i18n("Invalid wRPC URL"));
}

#[cfg(not(target_arch = "wasm32"))]
ui.horizontal_wrapped(|ui|{
// ui.set_max_width(half_width);
ui.label(i18n("Recommended arguments for the remote node: "));
ui.label(RichText::new("kaspad --utxoindex --rpclisten-borsh=0.0.0.0").code().font(FontId::monospace(14.0)).color(theme_color().strong_color));
ui.label(i18n("If you are running locally, use: "));
ui.label(RichText::new("--rpclisten-borsh=127.0.0.1.").code().font(FontId::monospace(14.0)).color(theme_color().strong_color));
});
cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
ui.horizontal_wrapped(|ui|{
ui.label(i18n("Recommended arguments for the remote node: "));
ui.label(RichText::new("kaspad --utxoindex --rpclisten-borsh=0.0.0.0").code().font(FontId::monospace(14.0)).color(theme_color().strong_color));
});
ui.horizontal_wrapped(|ui|{
ui.label(i18n("If you are running locally, use: "));
ui.label(RichText::new("--rpclisten-borsh=127.0.0.1.").code().font(FontId::monospace(14.0)).color(theme_color().strong_color));
});
}
}

});
}
Expand Down Expand Up @@ -330,9 +413,7 @@ impl Settings {
ui: &mut egui::Ui,
) {

if !core.settings.disable_node_settings {
self.render_node_settings(core,ui);
}
self.render_node_settings(core,ui);

CollapsingHeader::new(i18n("Centralized Services"))
.default_open(true)
Expand Down
Loading

0 comments on commit b5f9048

Please sign in to comment.