Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Nov 19, 2023
1 parent 7a3e05c commit bcdcc26
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 32 deletions.
1 change: 0 additions & 1 deletion core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ impl Core {
.expect("Unknown module");

if self.module.type_id() != module.type_id() {

let next = module.clone();

self.stack.push_back(self.module.clone());
Expand Down
46 changes: 36 additions & 10 deletions core/src/modules/block_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ use egui_plot::{
pub struct BlockDag {
#[allow(dead_code)]
runtime: Runtime,
daa_cursor: f64,
last_daa_score : u64,
running : bool,

}

impl BlockDag {
pub fn new(runtime: Runtime) -> Self {
Self { runtime }
Self { runtime, daa_cursor : 0.0, last_daa_score : 0, running : false }
}
}

Expand All @@ -36,20 +40,42 @@ impl ModuleT for BlockDag {
ui.separator();

let current_daa_score = core.state().current_daa_score().unwrap_or_default();
if self.last_daa_score != current_daa_score {

if !self.running {
self.running = true;
self.daa_cursor = current_daa_score as f64;
}

self.last_daa_score = current_daa_score;
}

let delta = 0.005;
let diff = current_daa_score as f64 - self.daa_cursor;
let step = diff * delta;
self.daa_cursor += step;
if diff > 0.01 {
crate::runtime::try_runtime().unwrap().request_repaint();
}

let graph_width = ui.available_width();
let graph_height = ui.available_height();

let plot = Plot::new("block_dag")
.legend(Legend::default())
.width(graph_width)
.height(graph_height)
.include_x(current_daa_score as f64 + 10.)
.include_x(current_daa_score as f64 - 70.)
.include_y(100.)
.include_y(-100.)
.include_x(self.daa_cursor + 8.)
.include_x(self.daa_cursor - 20.)
// .include_x(self.daa_cursor + 30.)
// .include_x(self.daa_cursor - 150.)
.include_y(15.)
.include_y(-15.)
// .include_y(100.)
// .include_y(-100.)
// .auto_bounds_x()
// .auto_bounds_y()
.data_aspect(1.)
.data_aspect(0.3)
.y_axis_width(4)
.show_axes(true)
.show_grid(true)
Expand Down Expand Up @@ -106,10 +132,10 @@ impl ModuleT for BlockDag {

let d = 1.5;
let points: PlotPoints = [
[x+d, y+d],
[x-d, y+d],
[x-d, y-d],
[x+d, y-d],
[x+d*0.3, y+d],
[x-d*0.3, y+d],
[x-d*0.3, y-d],
[x+d*0.3, y-d],
].to_vec().into();

Polygon::new(points)
Expand Down
3 changes: 1 addition & 2 deletions core/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ kaspa_ng_macros::register_modules!(
about,
account_create,
account_manager,
block_dag,
changelog,
deposit,
export,
Expand All @@ -26,7 +25,7 @@ kaspa_ng_macros::register_modules!(
);

#[cfg(not(target_arch = "wasm32"))]
kaspa_ng_macros::register_modules!(register_native_modules, [logs, metrics, node,]);
kaspa_ng_macros::register_modules!(register_native_modules, [logs, metrics, node, block_dag,]);

pub enum ModuleStyle {
Large,
Expand Down
5 changes: 4 additions & 1 deletion core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ impl Runtime {
settings,
));
let metrics_service = Arc::new(MetricsService::new(application_events.clone(), settings));
let block_dag_monitor_service = Arc::new(BlockDagMonitorService::new(application_events.clone(), settings));
let block_dag_monitor_service = Arc::new(BlockDagMonitorService::new(
application_events.clone(),
settings,
));
let plugin_manager_service = Arc::new(PluginManagerService::new(
application_events.clone(),
settings,
Expand Down
42 changes: 25 additions & 17 deletions core/src/runtime/services/blockdag_monitor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::imports::*;
use kaspa_rpc_core::RpcBlock;
use kaspa_rpc_core::notify::connection::{ChannelConnection, ChannelType};
use kaspa_notify::{listener::ListenerId, scope::*};
use kaspa_rpc_core::api::notifications::Notification;
use kaspa_notify::{scope::*, listener::ListenerId};
use kaspa_rpc_core::notify::connection::{ChannelConnection, ChannelType};
use kaspa_rpc_core::RpcBlock;

pub enum BlockDagMonitorEvents {
Exit,
Expand Down Expand Up @@ -35,20 +35,30 @@ impl BlockDagMonitorService {

pub async fn register_notification_listener(&self) -> Result<()> {
if let Some(rpc_api) = self.rpc_api() {
let listener_id =
rpc_api
.register_new_listener(ChannelConnection::new(self.notification_channel.sender.clone(), ChannelType::Persistent));
*self.listener_id.lock().unwrap() = Some(listener_id);
rpc_api.start_notify(listener_id, Scope::BlockAdded(BlockAddedScope { })).await?;
rpc_api.start_notify(listener_id, Scope::VirtualChainChanged(VirtualChainChangedScope { include_accepted_transaction_ids: false })).await?;
let listener_id = rpc_api.register_new_listener(ChannelConnection::new(
self.notification_channel.sender.clone(),
ChannelType::Persistent,
));
*self.listener_id.lock().unwrap() = Some(listener_id);
rpc_api
.start_notify(listener_id, Scope::BlockAdded(BlockAddedScope {}))
.await?;
rpc_api
.start_notify(
listener_id,
Scope::VirtualChainChanged(VirtualChainChangedScope {
include_accepted_transaction_ids: false,
}),
)
.await?;
}

Ok(())
}

pub async fn unregister_notification_listener(&self) -> Result<()> {
if let Some(rpc_api) = self.rpc_api() {
let listener_id = self.listener_id.lock().unwrap().take();
let listener_id = self.listener_id.lock().unwrap().take();
if let Some(id) = listener_id {
// we do not need this as we are unregister the entire listener here...
rpc_api.unregister_listener(id).await?;
Expand All @@ -57,11 +67,9 @@ impl BlockDagMonitorService {
Ok(())
}


pub fn rpc_api(&self) -> Option<Arc<dyn RpcApi>> {
self.rpc_api.lock().unwrap().clone()
}

}

#[async_trait]
Expand All @@ -70,18 +78,18 @@ impl Service for BlockDagMonitorService {
self.rpc_api.lock().unwrap().replace(rpc_api.clone());
Ok(())
}

async fn detach_rpc(self: Arc<Self>) -> Result<()> {
self.rpc_api.lock().unwrap().take();

Ok(())
}

async fn connect_rpc(self: Arc<Self>) -> Result<()> {
self.register_notification_listener().await?;
Ok(())
}

async fn disconnect_rpc(self: Arc<Self>) -> Result<()> {
self.unregister_notification_listener().await?;
Ok(())
Expand All @@ -91,7 +99,7 @@ impl Service for BlockDagMonitorService {
let this = self.clone();
let _application_events_sender = self.application_events.sender.clone();

let mut chain : VecDeque<Arc<RpcBlock>> = Default::default();
let mut chain: VecDeque<Arc<RpcBlock>> = Default::default();

loop {
select! {
Expand Down
2 changes: 1 addition & 1 deletion core/src/runtime/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait Service: Sync + Send {
/// Block until the service is terminated
async fn join(self: Arc<Self>) -> Result<()>;

/// Called when Kaspa RPC API has been created (but node is not
/// Called when Kaspa RPC API has been created (but node is not
/// connected yet, see [`connect_rpc`](Service::connect_rpc))
/// for connectivity signalling.
async fn attach_rpc(self: Arc<Self>, _rpc_api: &Arc<dyn RpcApi>) -> Result<()> {
Expand Down

0 comments on commit bcdcc26

Please sign in to comment.