Skip to content

Commit

Permalink
preemptive disconnect for RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Jan 12, 2024
1 parent 67db586 commit aaa683f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 17 deletions.
14 changes: 12 additions & 2 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Core {
pub fn new(
cc: &eframe::CreationContext<'_>,
runtime: crate::runtime::Runtime,
mut settings: Settings,
#[allow(unused_mut)] mut settings: Settings,
window_frame: bool,
) -> Self {
crate::fonts::init_fonts(cc);
Expand Down Expand Up @@ -136,11 +136,13 @@ impl Core {
}
};

#[allow(unused_mut)]
let mut module = modules
.get(&TypeId::of::<modules::Overview>())
.unwrap()
.clone();

#[cfg(not(target_arch = "wasm32"))]
if settings.version != env!("CARGO_PKG_VERSION") {
settings.version = env!("CARGO_PKG_VERSION").to_string();
settings.store_sync().unwrap();
Expand Down Expand Up @@ -609,7 +611,15 @@ impl Core {
_frame: &mut eframe::Frame,
) -> Result<()> {
match event {
Events::VisibilityChange(_state) => {}
Events::VisibilityChange(state) => match state {
VisibilityState::Visible => {
self.module.clone().show(self);
}
VisibilityState::Hidden => {
self.module.clone().hide(self);
}
_ => {}
},
Events::Market(update) => {
if self.market.is_none() {
self.market = Some(Market::default());
Expand Down
16 changes: 12 additions & 4 deletions core/src/modules/block_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ impl BlockDag {
self.block_scale = preset.block_scale;
}

fn reset_state(&mut self) {
self.running = false;
self.daa_cursor = 0.0;
self.last_daa_score = 0;
}

}

impl ModuleT for BlockDag {
Expand Down Expand Up @@ -477,10 +483,12 @@ impl ModuleT for BlockDag {
}

fn disconnect(&mut self, _core: &mut Core) {
self.running = false;
self.daa_cursor = 0.0;
self.last_daa_score = 0;
}
self.reset_state();
}

fn show(&mut self, _core: &mut Core) {
self.reset_state();
}

}

10 changes: 10 additions & 0 deletions core/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub trait ModuleT: Downcast {
fn reset(&mut self, _core: &mut Core) {}
fn connect(&mut self, _core: &mut Core) {}
fn disconnect(&mut self, _core: &mut Core) {}
fn hide(&mut self, _core: &mut Core) {}
fn show(&mut self, _core: &mut Core) {}

fn init(&mut self, _core: &mut Core) {}

Expand Down Expand Up @@ -118,6 +120,14 @@ impl Module {
self.inner.module.borrow_mut().disconnect(core)
}

pub fn hide(&self, core: &mut Core) {
self.inner.module.borrow_mut().hide(core)
}

pub fn show(&self, core: &mut Core) {
self.inner.module.borrow_mut().show(core)
}

pub fn status_bar(&self, core: &mut Core, ui: &mut Ui) {
self.inner.module.borrow_mut().status_bar(core, ui)
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/primitives/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ impl DaaBucket {
.sort_by(|a, b| a.dst_y.partial_cmp(&b.dst_y).unwrap());
let y_distance = settings.y_dist;
let len = self.blocks.len();

#[allow(clippy::collapsible_else_if)]
if let Some(mut vspc_idx) = self.blocks.iter().position(|block| block.vspc) {
if settings.center_vspc && len > 2 {
let mid = len / 2;
Expand Down
44 changes: 35 additions & 9 deletions core/src/runtime/services/kaspa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use kaspa_wallet_core::rpc::{
ConnectOptions, ConnectStrategy, NotificationMode, Rpc, RpcCtl, WrpcEncoding,
};

const ENABLE_PREEMPTIVE_DISCONNECT: bool = true;

cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -233,11 +235,42 @@ impl KaspaService {
}
}

fn is_wrpc_client(&self) -> bool {
self.wallet.has_rpc()
&& self
.wallet()
.rpc_api()
.clone()
.downcast_arc::<KaspaRpcClient>()
.is_ok()
}

async fn disconnect_rpc(&self) -> Result<()> {
if let Ok(wrpc_client) = self
.wallet()
.rpc_api()
.clone()
.downcast_arc::<KaspaRpcClient>()
{
wrpc_client.disconnect().await?;
} else {
self.wallet().rpc_ctl().signal_close().await?;
}

Ok(())
}

pub async fn stop_all_services(&self) -> Result<()> {
if !self.wallet().has_rpc() {
return Ok(());
}

let preemptive_disconnect = ENABLE_PREEMPTIVE_DISCONNECT && self.is_wrpc_client();

if preemptive_disconnect {
self.disconnect_rpc().await?;
}

for service in crate::runtime::runtime().services().into_iter() {
let instant = Instant::now();
service.clone().detach_rpc().await?;
Expand All @@ -250,15 +283,8 @@ impl KaspaService {
}
}

if let Ok(wrpc_client) = self
.wallet()
.rpc_api()
.clone()
.downcast_arc::<KaspaRpcClient>()
{
wrpc_client.disconnect().await?;
} else {
self.wallet().rpc_ctl().signal_close().await?;
if !preemptive_disconnect {
self.disconnect_rpc().await?;
}

self.wallet().stop().await.expect("Unable to stop wallet");
Expand Down
3 changes: 1 addition & 2 deletions core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ pub struct NodeSettings {

impl Default for NodeSettings {
fn default() -> Self {

Self {
connection_config_kind: NodeConnectionConfigKind::default(),
public_servers: HashMap::default(),
Expand Down Expand Up @@ -477,7 +476,7 @@ impl Default for Settings {
Self {
initialized: false,
revision: SETTINGS_REVISION.to_string(),

splash_screen: true,
version: "0.0.0".to_string(),
developer: DeveloperSettings::default(),
Expand Down

0 comments on commit aaa683f

Please sign in to comment.