Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jan 16, 2025
1 parent 6729073 commit ec755ea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
92 changes: 55 additions & 37 deletions applications/minotari_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ use tonic::{Request, Response, Status};
use crate::{
builder::BaseNodeContext,
grpc::{
data_cache::DataCache,
blocks::{block_fees, block_heights, block_size, GET_BLOCKS_MAX_HEIGHTS, GET_BLOCKS_PAGE_SIZE},
data_cache::DataCache,
hash_rate::HashRateMovingAverage,
helpers::{mean, median},
},
Expand Down Expand Up @@ -387,7 +387,9 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
.get_block_reward_at(metadata.best_block_height())
.as_u64();
let constants = self.consensus_rules.consensus_constants(metadata.best_block_height());
let sha3x_estimated_hash_rate = match self.data_cache.get_sha3x_estimated_hash_rate(metadata.best_block_hash())
let sha3x_estimated_hash_rate = match self
.data_cache
.get_sha3x_estimated_hash_rate(metadata.best_block_hash())
.await
{
Some(hash_rate) => hash_rate,
Expand All @@ -406,12 +408,14 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let target_time = constants.pow_target_block_interval(PowAlgorithm::Sha3x);
let estimated_hash_rate = target_difficulty.as_u64() / target_time;
self.data_cache
.set_sha3x_estimated_hash_rate(estimated_hash_rate, metadata.best_block_hash().clone())
.set_sha3x_estimated_hash_rate(estimated_hash_rate, *metadata.best_block_hash())
.await;
estimated_hash_rate
},
};
let randomx_estimated_hash_rate = match self.data_cache.get_randomx_estimated_hash_rate(metadata.best_block_hash())
let randomx_estimated_hash_rate = match self
.data_cache
.get_randomx_estimated_hash_rate(metadata.best_block_hash())
.await
{
Some(hash_rate) => hash_rate,
Expand All @@ -430,7 +434,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
let target_time = constants.pow_target_block_interval(PowAlgorithm::RandomX);
let estimated_hash_rate = target_difficulty.as_u64() / target_time;
self.data_cache
.set_randomx_estimated_hash_rate(estimated_hash_rate, metadata.best_block_hash().clone())
.set_randomx_estimated_hash_rate(estimated_hash_rate, *metadata.best_block_hash())
.await;
estimated_hash_rate
},
Expand Down Expand Up @@ -722,45 +726,59 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
obscure_error_if_true(report_error_flag, Status::internal(e.to_string()))
})?;

let new_template = match algo{
let new_template = match algo {
PowAlgorithm::Sha3x => {
match self.data_cache.get_sha3x_new_block_template(metadata.best_block_hash()).await{
Some(template) => {template},
match self
.data_cache
.get_sha3x_new_block_template(metadata.best_block_hash())
.await
{
Some(template) => template,
None => {
let new_template = handler
.get_new_block_template(algo, request.max_weight)
.await
.map_err(|e| {
warn!(
target: LOG_TARGET,
"Could not get new block template: {}",
e.to_string()
);
obscure_error_if_true(report_error_flag, Status::internal(e.to_string()))
})?;
self.data_cache.set_sha3x_new_block_template(new_template.clone(), metadata.best_block_hash().clone()).await;
let new_template =
handler
.get_new_block_template(algo, request.max_weight)
.await
.map_err(|e| {
warn!(
target: LOG_TARGET,
"Could not get new block template: {}",
e.to_string()
);
obscure_error_if_true(report_error_flag, Status::internal(e.to_string()))
})?;
self.data_cache
.set_sha3x_new_block_template(new_template.clone(), *metadata.best_block_hash())
.await;
new_template
}
},
}
},
},
PowAlgorithm::RandomX => {
match self.data_cache.get_randomx_new_block_template(metadata.best_block_hash()).await{
Some(template) => {template},
match self
.data_cache
.get_randomx_new_block_template(metadata.best_block_hash())
.await
{
Some(template) => template,
None => {
let new_template = handler
.get_new_block_template(algo, request.max_weight)
.await
.map_err(|e| {
warn!(
target: LOG_TARGET,
"Could not get new block template: {}",
e.to_string()
);
obscure_error_if_true(report_error_flag, Status::internal(e.to_string()))
})?;
self.data_cache.set_randomx_new_block_template(new_template.clone(), metadata.best_block_hash().clone()).await;
let new_template =
handler
.get_new_block_template(algo, request.max_weight)
.await
.map_err(|e| {
warn!(
target: LOG_TARGET,
"Could not get new block template: {}",
e.to_string()
);
obscure_error_if_true(report_error_flag, Status::internal(e.to_string()))
})?;
self.data_cache
.set_randomx_new_block_template(new_template.clone(), *metadata.best_block_hash())
.await;
new_template
}
},
}
},
};
Expand Down
14 changes: 7 additions & 7 deletions applications/minotari_node/src/grpc/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::sync::{Arc};
use tokio::sync::RwLock;
use std::sync::Arc;

use tari_common_types::types::FixedHash;
use tari_core::blocks::NewBlockTemplate;
use tokio::sync::RwLock;

pub struct DataCache {
inner_data_cache: Arc<RwLock<InnerDataCache>>,
Expand Down Expand Up @@ -84,6 +84,7 @@ impl DataCache {
pub async fn set_randomx_new_block_template(&self, new_block_template: NewBlockTemplate, current_tip: FixedHash) {
self.inner_data_cache.write().await.randomx_new_block_template = (new_block_template, current_tip);
}

pub async fn set_sha3x_new_block_template(&self, new_block_template: NewBlockTemplate, current_tip: FixedHash) {
self.inner_data_cache.write().await.sha3x_new_block_template = (new_block_template, current_tip);
}
Expand All @@ -92,17 +93,16 @@ impl DataCache {
struct InnerDataCache {
pub randomx_estimated_hash_rate: (u64, FixedHash),
pub sha3x_estimated_hash_rate: (u64, FixedHash),
pub sha3x_new_block_template: (NewBlockTemplate,FixedHash),
pub randomx_new_block_template: (NewBlockTemplate,FixedHash),
pub sha3x_new_block_template: (NewBlockTemplate, FixedHash),
pub randomx_new_block_template: (NewBlockTemplate, FixedHash),
}
impl Default for InnerDataCache {
fn default() -> Self {
Self {
randomx_estimated_hash_rate: (0, FixedHash::default()),
sha3x_estimated_hash_rate: (0, FixedHash::default()),
sha3x_new_block_template: (NewBlockTemplate::empty(),FixedHash::default()),
randomx_new_block_template: (NewBlockTemplate::empty(),FixedHash::default()),
sha3x_new_block_template: (NewBlockTemplate::empty(), FixedHash::default()),
randomx_new_block_template: (NewBlockTemplate::empty(), FixedHash::default()),
}
}
}

0 comments on commit ec755ea

Please sign in to comment.