-
Notifications
You must be signed in to change notification settings - Fork 980
feat: expose stratum mining stats via Owner API #3897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,12 @@ use crate::handlers::server_api::StatusHandler; | |
| use crate::p2p::types::PeerInfoDisplay; | ||
| use crate::p2p::{self, PeerData}; | ||
| use crate::rest::*; | ||
| use crate::types::Status; | ||
| use crate::types::{MiningStatus, Status}; | ||
| use std::net::SocketAddr; | ||
| use std::sync::Weak; | ||
| use std::sync::{Arc, Weak}; | ||
|
|
||
| /// Callback that returns a snapshot of stratum mining stats for the Owner API. | ||
| pub type MiningStatsProvider = Arc<dyn Fn() -> MiningStatus + Send + Sync>; | ||
|
|
||
| /// Main interface into all node API functions. | ||
| /// Node APIs are split into two separate blocks of functionality | ||
|
|
@@ -37,6 +40,8 @@ pub struct Owner { | |
| pub chain: Weak<Chain>, | ||
| pub peers: Weak<p2p::Peers>, | ||
| pub sync_state: Weak<SyncState>, | ||
| /// Optional provider of live stratum mining stats (set by the server process). | ||
| pub mining_stats: Option<MiningStatsProvider>, | ||
| } | ||
|
|
||
| impl Owner { | ||
|
|
@@ -58,6 +63,22 @@ impl Owner { | |
| chain, | ||
| peers, | ||
| sync_state, | ||
| mining_stats: None, | ||
| } | ||
| } | ||
|
|
||
| /// Create a new Owner API instance with a mining stats provider. | ||
| pub fn with_mining_stats( | ||
| chain: Weak<Chain>, | ||
| peers: Weak<p2p::Peers>, | ||
| sync_state: Weak<SyncState>, | ||
| mining_stats: Option<MiningStatsProvider>, | ||
| ) -> Self { | ||
| Owner { | ||
| chain, | ||
| peers, | ||
| sync_state, | ||
| mining_stats, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -78,6 +99,23 @@ impl Owner { | |
| status_handler.get_status() | ||
| } | ||
|
|
||
| /// Returns current stratum mining statistics, mirroring the TUI mining tab. | ||
| /// | ||
| /// # Returns | ||
| /// * Result Containing: | ||
| /// * A [`MiningStatus`](types/struct.MiningStatus.html) | ||
| /// * or [`Error`](struct.Error.html) if no mining stats provider is configured. | ||
| /// | ||
|
|
||
| pub fn get_mining_status(&self) -> Result<MiningStatus, Error> { | ||
|
wiesche89 marked this conversation as resolved.
|
||
| match &self.mining_stats { | ||
| Some(provider) => Ok(provider()), | ||
| None => Err(Error::Internal( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fixed now. Could you also update the PR description? It still says a missing provider returns a disabled default. |
||
| "no mining stats provider configured".to_string(), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| /// Trigger a validation of the chain state. | ||
| /// | ||
| /// # Arguments | ||
|
|
@@ -201,3 +239,45 @@ impl Owner { | |
| peer_handler.unban_peer(addr) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
| use crate::types::{MiningStatus, WorkerInfo}; | ||
| use std::sync::Arc; | ||
|
|
||
| #[test] | ||
| fn get_mining_status_without_provider_returns_error() { | ||
| let owner = Owner::new(Weak::new(), Weak::new(), Weak::new()); | ||
| assert!(owner.get_mining_status().is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_mining_status_with_provider() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we test this once through the generated JSON RPC handler? The current tests only call Owner directly, so the wire response is not covered. |
||
| let expected = MiningStatus { | ||
| is_enabled: true, | ||
| is_running: true, | ||
| num_workers: 2, | ||
| block_height: 50, | ||
| network_difficulty: 10, | ||
| edge_bits: 29, | ||
| blocks_found: 1, | ||
| network_hashrate: 0.5, | ||
| minimum_share_difficulty: 1, | ||
| worker_stats: vec![WorkerInfo { | ||
| id: "w0".into(), | ||
| last_seen: 100, | ||
| initial_block_height: 40, | ||
| pow_difficulty: 1, | ||
| num_accepted: 3, | ||
| num_rejected: 0, | ||
| num_stale: 0, | ||
| num_blocks_found: 0, | ||
| }], | ||
| }; | ||
| let expected_clone = expected.clone(); | ||
| let provider: MiningStatsProvider = Arc::new(move || expected_clone.clone()); | ||
| let owner = Owner::with_mining_stats(Weak::new(), Weak::new(), Weak::new(), Some(provider)); | ||
| assert_eq!(owner.get_mining_status().unwrap(), expected); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.