Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,694 changes: 3,333 additions & 361 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ serde = { version = "1", features = ["derive"] }
toml = "0.8"
unicode-width = "0.2"
p2poolv2_config = { git = "https://github.com/p2poolv2/p2poolv2", package = "p2poolv2_config" }
p2poolv2_api = { git = "https://github.com/p2poolv2/p2poolv2", package = "p2poolv2_api" }
bitcoin = "0.32.5"
toml_edit = "0.22"
reqwest = { version = "0.12", features = ["json", "rustls-tls"] }
serde_json = "1.0.135"
tokio = { version = "1.0", features = ["full"] }
tokio-tungstenite = "0.23"
futures-util = "0.3"
url = "2"
chrono = "0.4.44"
base64 = "0.22.1"

[dev-dependencies]
insta = "1.44.3"
Expand Down
5 changes: 5 additions & 0 deletions config/pdm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[api]
host = "127.0.0.1"
port = 46884
auth_user = "p2pool"
auth_pass = "p2pool"
24 changes: 24 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use crate::bitcoin_config::ConfigEntry as BitcoinEntry;
use crate::components::bitcoin_config_view::BitcoinConfigView;
use crate::components::file_explorer::FileExplorer;
use crate::components::p2pool_client::{ChainInfo, PeerInfo, ShareInfo};
use crate::components::p2pool_config_view::P2PoolConfigView;
use crate::components::p2pool_log_parser::ParsedP2PoolState;
use crate::components::settings_view::SettingsView;
use crate::p2poolv2_config::P2PoolConfigEntry as P2PoolEntry;
use crate::settings::Settings;
use p2poolv2_config::Config as P2PoolConfig;
use std::path::PathBuf;
Expand All @@ -31,6 +34,11 @@ pub const BITCOIN_STATUS_TABS: &[&str] = &["Chain Info", "System", "Logs", "Peer

pub const MAX_BITCOIN_STATUS_TAB: usize = BITCOIN_STATUS_TABS.len() - 1;

/// Tab labels for the P2Pool Status view
pub const P2POOL_STATUS_TABS: &[&str] = &["Chain Info", "System", "Logs", "Peers", "Shares"];

pub const MAX_P2POOL_STATUS_TAB: usize = P2POOL_STATUS_TABS.len() - 1;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CurrentScreen {
Home,
Expand Down Expand Up @@ -102,6 +110,14 @@ pub struct App {
/// Cached result of `settings::config_dir()`, used to display the default
/// settings storage path without repeated env-var lookups during rendering.
pub config_dir: PathBuf,
pub p2pool_data: Vec<P2PoolEntry>,
pub p2pool_status_tab: usize,
pub chain_info: Option<ChainInfo>,
pub peers: Vec<PeerInfo>,
pub recent_shares: Vec<ShareInfo>,
pub p2pool_logs: Vec<String>,
pub p2pool_state: ParsedP2PoolState,
pub api_status: Option<String>,
}

impl App {
Expand All @@ -123,6 +139,14 @@ impl App {
settings: Settings::default(),
home_dir: std::env::var("HOME").unwrap_or_default(),
config_dir: crate::settings::config_dir().unwrap_or_default(),
p2pool_data: Vec::new(),
p2pool_status_tab: 0,
chain_info: None,
peers: Vec::new(),
recent_shares: Vec::new(),
p2pool_logs: Vec::new(),
p2pool_state: ParsedP2PoolState::new(),
api_status: None,
}
}

Expand Down
79 changes: 79 additions & 0 deletions src/components/difficulty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: 2024 PDM Authors
//
// SPDX-License-Identifier: AGPL-3.0-or-later

pub fn difficulty_from_bits(bits_str: &str) -> String {
let bits = match u32::from_str_radix(bits_str.trim_start_matches("0x"), 16) {
Ok(v) => v,
Err(_) => return "?".to_string(),
};

let exponent = (bits >> 24) as i32;
let mantissa = (bits & 0x007f_ffff) as f64;

let shift = 8.0 * (exponent as f64 - 3.0);

// Guard against absurdly large/small shifts
if shift >= 1024.0 || mantissa == 0.0 {
return "0".to_string();
}

let target = mantissa * 2f64.powf(shift);
if target == 0.0 {
return "0".to_string();
}

// diff1_target = 0xffff << 208 (same constant as the JS dashboard)
// Using f64: precision is fine for a display string.
let diff1 = 65535.0_f64 * 2f64.powf(208.0);
let difficulty = diff1 / target;

format_difficulty(difficulty)
}

fn format_difficulty(value: f64) -> String {
const SUFFIXES: &[&str] = &["", "K", "M", "G", "T", "P", "E"];

if value < 10_000.0 {
return format!("{:.0}", value);
}

let mut scaled = value;
let mut tier = 0usize;

while scaled >= 1_000.0 && tier < SUFFIXES.len() - 1 {
scaled /= 1_000.0;
tier += 1;
}

if scaled >= 100.0 {
format!("{:.0}{}", scaled, SUFFIXES[tier])
} else if scaled >= 10.0 {
format!("{:.1}{}", scaled, SUFFIXES[tier])
} else {
format!("{:.2}{}", scaled, SUFFIXES[tier])
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn difficulty_1d00ffff_is_1() {
// Genesis block bits — difficulty 1
let d = difficulty_from_bits("1d00ffff");
assert_eq!(d, "1");
}

#[test]
fn bad_bits_returns_question_mark() {
assert_eq!(difficulty_from_bits("nothex"), "?");
}

#[test]
fn zero_mantissa_returns_zero() {
// exponent=0x1d, mantissa=0 → target = 0 → "0"
assert_eq!(difficulty_from_bits("1d000000"), "0");
}
}
4 changes: 4 additions & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

pub mod bitcoin_config_view;
pub mod bitcoin_status_view;
pub mod difficulty;
pub mod file_explorer;
pub mod home_view;
pub mod ln_config_view;
pub mod ln_status_view;
pub mod p2pool_client;
pub mod p2pool_config_view;
pub mod p2pool_log_parser;
pub mod p2pool_status_view;
pub mod p2pool_websockets;
pub mod settings_view;
pub mod shares_market_view;
pub mod status_bar;
Loading
Loading