Skip to content

Commit

Permalink
fix: fixed http stats caching (#42)
Browse files Browse the repository at this point in the history
Description
---
By mistake SHA3 stats were returned for both RandomX, this is fixed in
this PR.

Motivation and Context
---
Http stats are cached in memory (to avoid too much stress on in memory
share chain).

How Has This Been Tested?
---
- Start P2Pool
- Wait for syncing
- Check stats whether they are the same or not (should not)

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


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify
  • Loading branch information
ksrichard authored Sep 11, 2024
1 parent 14c65f2 commit 1a4a365
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 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.5"
version = "0.1.5-pre.6"
edition = "2021"

[dependencies]
Expand Down
25 changes: 18 additions & 7 deletions src/server/http/stats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::server::http::stats::models::Stats;
use std::sync::Arc;
use std::time::Duration;
use tari_core::proof_of_work::PowAlgorithm;
use tokio::sync::RwLock;
use tokio::time::Instant;

Expand All @@ -21,7 +22,8 @@ impl CachedStats {

pub struct StatsCache {
ttl: Duration,
stats: Arc<RwLock<Option<CachedStats>>>,
stats_sha3x: Arc<RwLock<Option<CachedStats>>>,
stats_randomx: Arc<RwLock<Option<CachedStats>>>,
}

impl Default for StatsCache {
Expand All @@ -34,12 +36,17 @@ impl StatsCache {
pub fn new(ttl: Duration) -> Self {
Self {
ttl,
stats: Arc::new(RwLock::new(None)),
stats_sha3x: Arc::new(RwLock::new(None)),
stats_randomx: Arc::new(RwLock::new(None)),
}
}

pub async fn update(&self, stats: Stats) {
let mut stats_lock = self.stats.write().await;
pub async fn update(&self, stats: Stats, pow_algo: PowAlgorithm) {
let stats_lock = match pow_algo {
PowAlgorithm::RandomX => self.stats_randomx.clone(),
PowAlgorithm::Sha3x => self.stats_sha3x.clone(),
};
let mut stats_lock = stats_lock.write().await;
match &mut *stats_lock {
Some(curr_stats) => {
curr_stats.stats = stats;
Expand All @@ -51,12 +58,16 @@ impl StatsCache {
}
}

pub async fn stats(&self) -> Option<Stats> {
let lock = self.stats.read().await;
pub async fn stats(&self, pow_algo: PowAlgorithm) -> Option<Stats> {
let stats_lock = match pow_algo {
PowAlgorithm::RandomX => self.stats_randomx.clone(),
PowAlgorithm::Sha3x => self.stats_sha3x.clone(),
};
let lock = stats_lock.read().await;
let last_update = lock.as_ref()?.last_update;
if lock.is_some() && Instant::now().duration_since(last_update) > self.ttl {
drop(lock);
let mut lock = self.stats.write().await;
let mut lock = stats_lock.write().await;
*lock = None;
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/http/stats/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn handle_get_stats(State(state): State<AppState>) -> Result<Json<Hash
async fn get_stats(state: AppState, algo: PowAlgorithm) -> Result<Stats, StatusCode> {
// return from cache if possible
let stats_cache = state.stats_cache.clone();
if let Some(stats) = stats_cache.stats().await {
if let Some(stats) = stats_cache.stats(algo).await {
return Ok(stats);
}

Expand Down Expand Up @@ -171,7 +171,7 @@ async fn get_stats(state: AppState, algo: PowAlgorithm) -> Result<Stats, StatusC
tribe: TribeDetails::new(state.tribe.to_string(), state.tribe.formatted()),
};

stats_cache.update(result.clone()).await;
stats_cache.update(result.clone(), algo).await;

Ok(result)
}
Expand Down

0 comments on commit 1a4a365

Please sign in to comment.