Skip to content

Commit

Permalink
clippy + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Sep 12, 2024
1 parent 66c5ac8 commit 5b54b54
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions core/resources/i18n/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,7 @@
"ECDSA": "ECDSA",
"Economic": "Economic",
"Enable Market Monitor": "Enable Market Monitor",
"Enable Public wRPC Borsh": "Enable Public wRPC Borsh",
"Enable UPnP": "Enable UPnP",
"Enable custom daemon arguments": "Enable custom daemon arguments",
"Enable experimental features": "Enable experimental features",
Expand Down
4 changes: 2 additions & 2 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct Core {
notifications: Notifications,
pub storage: Storage,
// pub feerate : Option<Arc<RpcFeeEstimate>>,
pub feerate : Option<FeerateEstimate>,
pub feerate: Option<FeerateEstimate>,
}

impl Core {
Expand Down Expand Up @@ -222,7 +222,7 @@ impl Core {
network_pressure: NetworkPressure::default(),
notifications: Notifications::default(),
storage,
feerate : None,
feerate: None,
// daemon_storage_root: Mutex::new(daemon_storage_root),
};

Expand Down
17 changes: 9 additions & 8 deletions core/src/egui/selection_panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl UILayoutExt for Ui {
let mut child_rect = self.available_rect_before_wrap();
child_rect.min.x += indent;

let mut child_ui = self.child_ui_with_id_source(child_rect, *self.layout(), id_source, None);
let mut child_ui =
self.child_ui_with_id_source(child_rect, *self.layout(), id_source, None);
let ret = add_contents(&mut child_ui);

// let left_vline = self.visuals().indent_has_left_vline;
Expand Down Expand Up @@ -149,10 +150,7 @@ impl<Value: PartialEq> SelectionPanel<Value> {
let _res = add_contents(&mut prepared.content_ui);
*min_height = min_height.max(prepared.content_ui.min_rect().height());
prepared.content_ui.set_min_height(*min_height);
let rect = prepared
.frame
.inner_margin
.expand_rect(prepared.content_ui.min_rect());
let rect = prepared.content_ui.min_rect() + prepared.frame.inner_margin;
if !selected && ui.allocate_rect(rect, Sense::hover()).hovered() {
//prepared.frame = prepared.frame.stroke(hover_stroke);
prepared.frame = prepared.frame.fill(selected_bg).stroke(hover_stroke);
Expand Down Expand Up @@ -269,9 +267,12 @@ impl<Value: PartialEq> SelectionPanels<Value> {
//ui.visuals_mut().override_text_color = Some(Color32::WHITE);
{
let available_width = ui.available_width() - indent;
let title =
self.title
.into_galley(ui, Some(TextWrapMode::Wrap), available_width, TextStyle::Heading);
let title = self.title.into_galley(
ui,
Some(TextWrapMode::Wrap),
available_width,
TextStyle::Heading,
);
let text_indent = (available_width - title.size().x) / 2.0;
let rect = ui.cursor().translate(Vec2::new(text_indent, 10.0));
ui.allocate_exact_size(
Expand Down
2 changes: 1 addition & 1 deletion core/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use kaspa_consensus_core::network::{NetworkId, NetworkType};
pub use kaspa_consensus_core::Hash as KaspaHash;
pub use kaspa_metrics_core::MetricsSnapshot;
pub use kaspa_rpc_core::api::rpc::RpcApi;
pub use kaspa_rpc_core::{RpcFeeEstimate,RpcFeerateBucket};
pub use kaspa_rpc_core::{RpcFeeEstimate, RpcFeerateBucket};
pub use kaspa_utils::hex::{FromHex, ToHex};
pub use kaspa_utils::{hashmap::GroupExtension, networking::ContextualNetAddress};
pub use kaspa_wallet_core::prelude::{
Expand Down
7 changes: 3 additions & 4 deletions core/src/runtime/services/feerate_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ impl FeerateMonitorService {
async fn fetch(self: &Arc<Self>) -> Result<()> {
if let Some(rpc_api) = self.rpc_api() {
if let Ok(resp) = rpc_api.get_fee_estimate().await {

// println!("{}",resp.priority_bucket.feerate);
// let feerate = Arc::new(resp);
// self.feerate.lock().unwrap().replace(feerate.clone());
self.application_events
.send(Events::Feerate { feerate : Some(Arc::new(resp)) })
.send(Events::Feerate {
feerate: Some(Arc::new(resp)),
})
.await
.unwrap();
}
Expand All @@ -76,7 +77,6 @@ impl FeerateMonitorService {
.await
.unwrap();
}

}

#[async_trait]
Expand All @@ -97,7 +97,6 @@ impl Service for FeerateMonitorService {
Ok(())
}


async fn spawn(self: Arc<Self>) -> Result<()> {
// let this = self.clone();
// let application_events_sender = self.application_events.sender.clone();
Expand Down
69 changes: 39 additions & 30 deletions core/src/utils/average.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const AVERAGE_ALPHA_HIGH: f64 = 0.8;
const AVERAGE_ALPHA_LOW: f64 = 0.5;

pub struct FeerateEstimate {
pub low : FeerateBucketAverage,
pub economic : FeerateBucketAverage,
pub priority : FeerateBucketAverage,
pub low: FeerateBucketAverage,
pub economic: FeerateBucketAverage,
pub priority: FeerateBucketAverage,
}

impl Default for FeerateEstimate {
Expand All @@ -21,7 +21,7 @@ impl Default for FeerateEstimate {
}

impl FeerateEstimate {
pub fn new(estimate : &RpcFeeEstimate) -> Self {
pub fn new(estimate: &RpcFeeEstimate) -> Self {
let mut feerate = Self {
low: FeerateBucketAverage::default(),
economic: FeerateBucketAverage::default(),
Expand All @@ -32,25 +32,33 @@ impl FeerateEstimate {
}

pub fn insert(&mut self, estimate: &RpcFeeEstimate) {
self.low.insert(estimate.low_buckets.first().map(FeerateBucket::from).unwrap_or_default());
self.economic.insert(estimate.normal_buckets.first().map(FeerateBucket::from).unwrap_or_default());
self.low.insert(
estimate
.low_buckets
.first()
.map(FeerateBucket::from)
.unwrap_or_default(),
);
self.economic.insert(
estimate
.normal_buckets
.first()
.map(FeerateBucket::from)
.unwrap_or_default(),
);
self.priority.insert(estimate.priority_bucket.into());
}

}

#[derive(Debug, Clone, Copy)]
pub struct FeerateBucket {
pub feerate : f64,
pub seconds : f64,
pub feerate: f64,
pub seconds: f64,
}

impl FeerateBucket {
pub fn new(feerate: f64, seconds: f64) -> Self {
Self {
feerate,
seconds,
}
Self { feerate, seconds }
}
}

Expand Down Expand Up @@ -107,14 +115,12 @@ impl From<RpcFeerateBucket> for FeerateBucket {
pub type FeerateBucketAverage = FeerateBucketAverageN<MAX_AVERAGE_SAMPLES>;

#[derive(Default, Debug, Clone)]
pub struct FeerateBucketAverageN<const SAMPLES: usize>
{
pub struct FeerateBucketAverageN<const SAMPLES: usize> {
pub samples: VecDeque<FeerateBucket>,
pub average: FeerateBucket,
}

impl<const SAMPLES: usize> FeerateBucketAverageN<SAMPLES>
{
impl<const SAMPLES: usize> FeerateBucketAverageN<SAMPLES> {
pub fn clear(&mut self) {
self.samples.clear();
self.average = FeerateBucket::default();
Expand All @@ -127,12 +133,16 @@ impl<const SAMPLES: usize> FeerateBucketAverageN<SAMPLES>
let delta = self.average;

if value > self.value() {
let feerate = AVERAGE_ALPHA_HIGH * value.feerate + (1.0 - AVERAGE_ALPHA_HIGH) * delta.feerate;
let seconds = AVERAGE_ALPHA_HIGH * value.seconds + (1.0 - AVERAGE_ALPHA_HIGH) * delta.seconds;
let feerate =
AVERAGE_ALPHA_HIGH * value.feerate + (1.0 - AVERAGE_ALPHA_HIGH) * delta.feerate;
let seconds =
AVERAGE_ALPHA_HIGH * value.seconds + (1.0 - AVERAGE_ALPHA_HIGH) * delta.seconds;
self.samples.push_back(FeerateBucket::new(feerate, seconds));
} else {
let feerate = AVERAGE_ALPHA_LOW * value.feerate + (1.0 - AVERAGE_ALPHA_LOW) * delta.feerate;
let seconds = AVERAGE_ALPHA_LOW * value.seconds + (1.0 - AVERAGE_ALPHA_LOW) * delta.seconds;
let feerate =
AVERAGE_ALPHA_LOW * value.feerate + (1.0 - AVERAGE_ALPHA_LOW) * delta.feerate;
let seconds =
AVERAGE_ALPHA_LOW * value.seconds + (1.0 - AVERAGE_ALPHA_LOW) * delta.seconds;
self.samples.push_back(FeerateBucket::new(feerate, seconds));
}
}
Expand All @@ -141,15 +151,17 @@ impl<const SAMPLES: usize> FeerateBucketAverageN<SAMPLES>
self.samples.pop_front();
}
self.update();

}

pub fn update(&mut self) {
let len = self.samples.len() as f64;
let sum = self.samples.iter().fold(FeerateBucket::default(), |a, b| a + *b);
let sum = self
.samples
.iter()
.fold(FeerateBucket::default(), |a, b| a + *b);
self.average = FeerateBucket {
feerate : sum.feerate / len,
seconds : sum.seconds / len,
feerate: sum.feerate / len,
seconds: sum.seconds / len,
};
}

Expand All @@ -160,7 +172,6 @@ impl<const SAMPLES: usize> FeerateBucketAverageN<SAMPLES>
FeerateBucket::default()
}
}

}

#[cfg(test)]
Expand All @@ -169,8 +180,6 @@ mod test {

#[test]
fn test_average() {


let values = [
248155.3514557079,
215263.2471686213,
Expand Down Expand Up @@ -491,8 +500,8 @@ mod test {

let mut average = FeerateBucketAverageN::<6>::default();
for value in values.iter() {
average.insert(FeerateBucket::new(*value,1.0));
average.insert(FeerateBucket::new(*value, 1.0));
println!("{} -> {:?}", value, average.value());
}
}
}
}
1 change: 0 additions & 1 deletion core/src/utils/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ pub fn precision_from_symbol(symbol: &str) -> usize {
}
}


// /// Format supplied value as a float with 2 decimal places.
// fn format_as_float(f: f64, short: bool) -> String {
// if short {
Expand Down

0 comments on commit 5b54b54

Please sign in to comment.