Skip to content

Commit 60178eb

Browse files
committed
Address review feedback
- Filter mining status worker_stats to currently connected workers (StratumStats retains disconnected workers for the node lifetime) - Return an internal error from get_mining_status when no stats provider is wired, instead of masking it as disabled - Restore the public node_apis signature; add node_apis_with_mining_stats for provider wiring - Document is_running as set-once at startup - Trim get_mining_status doc comment to match neighboring methods
1 parent ead8310 commit 60178eb

8 files changed

Lines changed: 59 additions & 20 deletions

File tree

api/src/handlers.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ pub fn node_apis<B, P>(
6262
tls_config: Option<TLSConfig>,
6363
api_chan: (mpsc::Sender<()>, mpsc::Receiver<()>),
6464
stop_state: Arc<StopState>,
65+
) -> Result<(), Error>
66+
where
67+
B: BlockChain + 'static,
68+
P: PoolAdapter + 'static,
69+
{
70+
node_apis_with_mining_stats(
71+
addr,
72+
chain,
73+
tx_pool,
74+
peers,
75+
sync_state,
76+
api_secret,
77+
foreign_api_secret,
78+
tls_config,
79+
api_chan,
80+
stop_state,
81+
None,
82+
)
83+
}
84+
85+
/// Same as [`node_apis`] but additionally wires a provider of live stratum
86+
/// mining stats into the Owner API `get_mining_status` method.
87+
pub fn node_apis_with_mining_stats<B, P>(
88+
addr: &str,
89+
chain: Arc<Chain>,
90+
tx_pool: Arc<RwLock<pool::TransactionPool<B, P>>>,
91+
peers: Arc<p2p::Peers>,
92+
sync_state: Arc<SyncState>,
93+
api_secret: Option<String>,
94+
foreign_api_secret: Option<String>,
95+
tls_config: Option<TLSConfig>,
96+
api_chan: (mpsc::Sender<()>, mpsc::Receiver<()>),
97+
stop_state: Arc<StopState>,
6598
mining_stats: Option<MiningStatsProvider>,
6699
) -> Result<(), Error>
67100
where

api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub use crate::auth::{
4747
};
4848
pub use crate::foreign::Foreign;
4949
pub use crate::foreign_rpc::ForeignRpc;
50-
pub use crate::handlers::node_apis;
50+
pub use crate::handlers::{node_apis, node_apis_with_mining_stats};
5151
pub use crate::owner::{MiningStatsProvider, Owner};
5252
pub use crate::owner_rpc::OwnerRpc;
5353
pub use crate::rest::*;

api/src/owner.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,20 @@ impl Owner {
9999
status_handler.get_status()
100100
}
101101

102-
/// Returns current stratum mining statistics (enabled/running, workers, difficulty, etc.).
103-
/// Useful when running headless without the TUI mining tab.
104-
///
105-
/// When stratum is disabled or no stats provider is wired up, returns a default
106-
/// (disabled) [`MiningStatus`](types/struct.MiningStatus.html).
102+
/// Returns current stratum mining statistics, mirroring the TUI mining tab.
107103
///
108104
/// # Returns
109105
/// * Result Containing:
110106
/// * A [`MiningStatus`](types/struct.MiningStatus.html)
111-
/// * or [`Error`](struct.Error.html) if an error is encountered.
107+
/// * or [`Error`](struct.Error.html) if no mining stats provider is configured.
112108
///
113109
114110
pub fn get_mining_status(&self) -> Result<MiningStatus, Error> {
115111
match &self.mining_stats {
116112
Some(provider) => Ok(provider()),
117-
None => Ok(MiningStatus::default()),
113+
None => Err(Error::Internal(
114+
"no mining stats provider configured".to_string(),
115+
)),
118116
}
119117
}
120118

@@ -249,10 +247,9 @@ mod test {
249247
use std::sync::Arc;
250248

251249
#[test]
252-
fn get_mining_status_without_provider_returns_default() {
250+
fn get_mining_status_without_provider_returns_error() {
253251
let owner = Owner::new(Weak::new(), Weak::new(), Weak::new());
254-
let status = owner.get_mining_status().unwrap();
255-
assert_eq!(status, MiningStatus::default());
252+
assert!(owner.get_mining_status().is_err());
256253
}
257254

258255
#[test]

api/src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ pub struct WorkerInfo {
133133
pub struct MiningStatus {
134134
/// Whether the stratum server is enabled in config
135135
pub is_enabled: bool,
136-
/// Whether the stratum server is currently running
136+
/// Whether the stratum server has been started. Set once at startup and
137+
/// not cleared on listener failure or shutdown.
137138
pub is_running: bool,
138139
/// Number of currently connected workers
139140
pub num_workers: usize,
@@ -149,7 +150,7 @@ pub struct MiningStatus {
149150
pub network_hashrate: f64,
150151
/// Minimum share difficulty requested from miners
151152
pub minimum_share_difficulty: u64,
152-
/// Per-worker status
153+
/// Per-worker status for currently connected workers
153154
pub worker_stats: Vec<WorkerInfo>,
154155
}
155156

doc/api/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Or from the CLI:
3030
grin client miningstatus
3131
```
3232

33-
The response includes whether stratum is enabled/running, connected worker count, current block height and network difficulty, blocks found, network hashrate estimate, and per-worker share stats.
33+
The response includes whether stratum is enabled/running, connected worker count, current block height and network difficulty, blocks found, network hashrate estimate, and per-worker share stats for currently connected workers.
3434

3535
## Node API v1
3636

servers/src/common/stats.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,14 @@ impl From<&StratumStats> for MiningStatus {
327327
blocks_found: stats.blocks_found,
328328
network_hashrate: stats.network_hashrate,
329329
minimum_share_difficulty: stats.minimum_share_difficulty,
330-
worker_stats: stats.worker_stats.iter().map(WorkerInfo::from).collect(),
330+
// worker_stats retains every worker that ever connected; only expose
331+
// currently connected ones so the response does not grow unbounded.
332+
worker_stats: stats
333+
.worker_stats
334+
.iter()
335+
.filter(|w| w.is_connected)
336+
.map(WorkerInfo::from)
337+
.collect(),
331338
}
332339
}
333340
}
@@ -350,6 +357,10 @@ mod test {
350357
worker.num_stale = 2;
351358
worker.num_blocks_found = 1;
352359

360+
let mut disconnected = WorkerStats::default();
361+
disconnected.id = "gone".into();
362+
disconnected.is_connected = false;
363+
353364
let stats = StratumStats {
354365
is_enabled: true,
355366
is_running: true,
@@ -360,7 +371,7 @@ mod test {
360371
blocks_found: 4,
361372
network_hashrate: 12.5,
362373
minimum_share_difficulty: 3,
363-
worker_stats: vec![worker],
374+
worker_stats: vec![worker, disconnected],
364375
};
365376

366377
let mining = MiningStatus::from(&stats);

servers/src/grin/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl Server {
316316
api::types::MiningStatus::from(&*stats)
317317
});
318318

319-
api::node_apis(
319+
api::node_apis_with_mining_stats(
320320
&config.api_http_addr,
321321
shared_chain.clone(),
322322
tx_pool.clone(),

src/bin/cmd/client.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ impl HTTPNodeClient {
178178
if !status.worker_stats.is_empty() {
179179
writeln!(e, "Workers:").unwrap();
180180
for worker in status.worker_stats {
181-
if !worker.is_connected {
182-
continue;
183-
}
184181
writeln!(
185182
e,
186183
" id={} accepted={} rejected={} stale={} blocks={} difficulty={}",

0 commit comments

Comments
 (0)