Skip to content

Commit

Permalink
feat(http-stats): added /miners endpoint to http server (#43)
Browse files Browse the repository at this point in the history
Description
---
Added new `/miners` endpoint to http server to show how many shares
particular miners have.

Motivation and Context
---

How Has This Been Tested?
---
- Start P2Pool
- Query http://127.0.0.1:19000/miners

Example response:
```json
{
  "sha3": {
    "327atsnkv9bFARzE6fayMGWmjj13mArRHNAb2U2nNZXUc4vPnkfHkAjiYaBD7x49HeNTzw46Zjrkrqt4sB4M9PMTjBf": 35,
    "322SDoFr5ZqC4mpLtxoEow2zH4zdkZGpWgsVFzYQSgvZoSfgcGmrg3zmyyBAkGPDcoqe7pimicfz3wdhNDHrdaoJ7VD": 60,
    "324wcQjgDRE6Uz647Kjh6Zp1dNX8scYbLE3nXk2x65X5qyvoU8NjbLCSmzxSfUuNvhBbTiauJFRLhP4pepkLst7Q4Dt": 60
  },
  "randomx": {
    "322SDoFr5ZqC4mpLtxoEow2zH4zdkZGpWgsVFzYQSgvZoSfgcGmrg3zmyyBAkGPDcoqe7pimicfz3wdhNDHrdaoJ7VD": 1,
    "324wcQjgDRE6Uz647Kjh6Zp1dNX8scYbLE3nXk2x65X5qyvoU8NjbLCSmzxSfUuNvhBbTiauJFRLhP4pepkLst7Q4Dt": 3
  }
}
```

What process can a PR reviewer use to test or verify this change?
---

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
ksrichard authored Sep 12, 2024
1 parent 1a4a365 commit 75eb9f9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sha_p2pool"
version = "0.1.5-pre.6"
version = "0.1.5-pre.7"
edition = "2021"

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/server/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ where
pub fn routes(&self) -> Router {
Router::new()
.route("/stats", get(handlers::handle_get_stats))
.route("/miners", get(handlers::handle_miners_with_shares))
.route("/health", get(health::handle_health))
.route("/version", get(version::handle_version))
.with_state(AppState {
Expand Down
22 changes: 22 additions & 0 deletions src/server/http/stats/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ use crate::sharechain::SHARE_COUNT;

const LOG_TARGET: &str = "p2pool::server::stats::get";

pub async fn handle_miners_with_shares(
State(state): State<AppState>,
) -> Result<Json<HashMap<String, HashMap<String, u64>>>, StatusCode> {
let mut result = HashMap::with_capacity(2);
result.insert(
PowAlgorithm::Sha3x.to_string().to_lowercase(),
state.share_chain_sha3x.miners_with_shares().await.map_err(|error| {
error!(target: LOG_TARGET, "Failed to get Sha3x miners with shares: {error:?}");
StatusCode::INTERNAL_SERVER_ERROR
})?,
);
result.insert(
PowAlgorithm::RandomX.to_string().to_lowercase(),
state.share_chain_random_x.miners_with_shares().await.map_err(|error| {
error!(target: LOG_TARGET, "Failed to get RandomX miners with shares: {error:?}");
StatusCode::INTERNAL_SERVER_ERROR
})?,
);

Ok(Json(result))
}

pub async fn handle_get_stats(State(state): State<AppState>) -> Result<Json<HashMap<String, Stats>>, StatusCode> {
let mut result = HashMap::with_capacity(2);
result.insert(
Expand Down
7 changes: 7 additions & 0 deletions src/sharechain/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,13 @@ impl ShareChain for InMemoryShareChain {

Ok(hash_rates_sum.div(hash_rates_count))
}

async fn miners_with_shares(&self) -> ShareChainResult<HashMap<String, u64>> {
let levels_lock = self.block_levels.read().await;
let chain = self.chain(levels_lock.iter());
let chain = chain.iter().tail(BLOCKS_WINDOW).collect_vec();
Ok(self.miners_with_shares(chain))
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/sharechain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::sharechain::{block::Block, error::Error};
use async_trait::async_trait;
use minotari_app_grpc::tari_rpc::{NewBlockCoinbase, SubmitBlockRequest};
use num::BigUint;
use std::collections::HashMap;
use tari_common_types::types::FixedHash;
use tari_core::consensus::ConsensusManager;
use tari_core::proof_of_work::randomx_factory::RandomXFactory;
Expand Down Expand Up @@ -107,4 +108,7 @@ pub trait ShareChain: Send + Sync + 'static {
/// (including all blocks and not just strongest chain).
/// Returning number is the result in hash/second.
async fn hash_rate(&self) -> ShareChainResult<BigUint>;

/// Returns the current miners with all the current shares in the current blocks window.
async fn miners_with_shares(&self) -> ShareChainResult<HashMap<String, u64>>;
}

0 comments on commit 75eb9f9

Please sign in to comment.