-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stats): added miner and p2pool block stats (#26)
Description --- We need to know whether how many blocks we sent to both p2pool and the target network. Motivation and Context --- How Has This Been Tested? --- - Normally start mining with p2pool - Check stats at http://127.0.0.1:19000/stats Example output: ```json { "connected": true, "connected_since": 1722856949, "num_of_miners": 2, "last_block_won": { "hash": "fbdbf4a36888c6f5ad92c67f40987ab9cc1313809ea98859bc42eda68d54c832", "height": 5403, "timestamp": 1722857161, "miner_wallet_address": "f27mEvFXUZJCNFQM1MbkRwBnNJHwsK6JBxzrwVyYNm6bgGHpL1xDj7Gjevwj9caFrp23iLGUcysK6decdDL87sQCtL2" }, "share_chain_height": 5406, "pool_hash_rate": [4235994], "pool_total_earnings": 55525928731, "pool_total_estimated_earnings": { "1min": 12123240, "1h": 727394400, "1d": 17457465600, "1w": 122202259200, "30d": 523723968000 }, "total_earnings": { "f2CrtWaTZE3xWSxCkR1mR9Bszx321Ar2S1fPcBLNSNWiLBVwqkeankFjeTmdxYLuyeHg8oM4vSgsV1tjL4GSKEuy9pk": 9716846540, "f27mEvFXUZJCNFQM1MbkRwBnNJHwsK6JBxzrwVyYNm6bgGHpL1xDj7Gjevwj9caFrp23iLGUcysK6decdDL87sQCtL2": 58426310963 }, "estimated_earnings": { "f2CrtWaTZE3xWSxCkR1mR9Bszx321Ar2S1fPcBLNSNWiLBVwqkeankFjeTmdxYLuyeHg8oM4vSgsV1tjL4GSKEuy9pk": { "1min": 1728660, "1h": 103719600, "1d": 2489270400, "1w": 17424892800, "30d": 74678112000 }, "f27mEvFXUZJCNFQM1MbkRwBnNJHwsK6JBxzrwVyYNm6bgGHpL1xDj7Gjevwj9caFrp23iLGUcysK6decdDL87sQCtL2": { "1min": 10394520, "1h": 623671200, "1d": 14968108800, "1w": 104776761600, "30d": 449043264000 } }, "miner_block_stats": { "accepted": 154, "rejected": 0, "submitted": 154 }, "p2pool_block_stats": { "accepted": 2, "rejected": 77, "submitted": 79 } } ``` What process can a PR reviewer use to test or verify this change? --- Check previous section. Breaking Changes --- - [x] None - [ ] Requires data directory on base node to be deleted - [ ] Requires hard fork - [ ] Other - Please specify
- Loading branch information
Showing
8 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
pub const MINER_STAT_ACCEPTED_BLOCKS_COUNT: &str = "miner_accepted_blocks_count"; | ||
pub const MINER_STAT_REJECTED_BLOCKS_COUNT: &str = "miner_rejected_blocks_count"; | ||
pub const P2POOL_STAT_ACCEPTED_BLOCKS_COUNT: &str = "p2pool_accepted_blocks_count"; | ||
pub const P2POOL_STAT_REJECTED_BLOCKS_COUNT: &str = "p2pool_rejected_blocks_count"; | ||
|
||
pub mod handlers; | ||
pub mod models; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ mod server; | |
pub mod grpc; | ||
pub mod http; | ||
pub mod p2p; | ||
pub mod stats_store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
use std::collections::HashMap; | ||
|
||
use tokio::sync::RwLock; | ||
|
||
pub struct StatsStore { | ||
stats: RwLock<HashMap<String, u64>>, | ||
} | ||
|
||
impl StatsStore { | ||
pub fn new() -> Self { | ||
Self { | ||
stats: RwLock::new(HashMap::new()), | ||
} | ||
} | ||
|
||
/// Returns one stat by [`key`]. | ||
pub async fn get(&self, key: &String) -> u64 { | ||
let read_lock = self.stats.read().await; | ||
read_lock.get(key).copied().unwrap_or(0) | ||
} | ||
|
||
/// Increments stat with given key. | ||
/// If the value is not found by key, simply create new value. | ||
pub async fn inc(&self, key: &String, by: u64) { | ||
let mut write_lock = self.stats.write().await; | ||
match write_lock.get(key) { | ||
Some(stat) => { | ||
let value = stat + by; | ||
write_lock.insert(key.clone(), value); | ||
}, | ||
None => { | ||
write_lock.insert(key.clone(), by); | ||
}, | ||
} | ||
} | ||
} |