Skip to content

Commit

Permalink
[refactor] hyperledger#2123: use Algorithm for storing digest function
Browse files Browse the repository at this point in the history
Signed-off-by: Retssaze <Retssaze@GMail.Com>
  • Loading branch information
Retssaze committed Feb 13, 2023
1 parent 407db60 commit 836b3df
Show file tree
Hide file tree
Showing 41 changed files with 2,426 additions and 766 deletions.
296 changes: 271 additions & 25 deletions CHANGELOG.rst

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions Cargo.lock

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

58 changes: 35 additions & 23 deletions cli/src/torii/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// FIXME: This can't be fixed, because one trait in `warp` is private.
#![allow(opaque_hidden_inferred_bound)]

use std::num::TryFromIntError;
use std::{cmp::Ordering, num::TryFromIntError};

use eyre::WrapErr;
use futures::TryStreamExt;
Expand Down Expand Up @@ -170,32 +170,44 @@ pub(crate) async fn handle_queries(
}

fn apply_sorting_and_pagination(
mut vec_of_val: Vec<Value>,
vec_of_val: Vec<Value>,
sorting: &Sorting,
pagination: Pagination,
) -> Vec<Value> {
if let Some(ref key) = sorting.sort_by_metadata_key {
let f = |value1: &Value| {
if let Value::Numeric(NumericValue::U128(num)) = value1 {
*num
} else {
0
}
};

vec_of_val.sort_by_key(|value0| match value0 {
Value::Identifiable(IdentifiableBox::Asset(asset)) => match asset.value() {
AssetValue::Store(store) => store.get(key).map_or(0, f),
_ => 0,
let mut pairs: Vec<(Option<Value>, Value)> = vec_of_val
.into_iter()
.map(|value| {
let key = match &value {
Value::Identifiable(IdentifiableBox::Asset(asset)) => match asset.value() {
AssetValue::Store(store) => store.get(key).cloned(),
_ => None,
},
Value::Identifiable(v) => TryInto::<&dyn HasMetadata>::try_into(v)
.ok()
.and_then(|has_metadata| has_metadata.metadata().get(key))
.cloned(),
_ => None,
};
(key, value)
})
.collect();
pairs.sort_by(
|(left_key, _), (right_key, _)| match (left_key, right_key) {
(Some(l), Some(r)) => l.cmp(r),
(Some(_), None) => Ordering::Less,
(None, Some(_)) => Ordering::Greater,
(None, None) => Ordering::Equal,
},
Value::Identifiable(v) => TryInto::<&dyn HasMetadata>::try_into(v)
.map(|has_metadata| has_metadata.metadata().get(key).map_or(0, f))
.unwrap_or(0),
_ => 0,
});
);
pairs
.into_iter()
.map(|(_, val)| val)
.paginate(pagination)
.collect()
} else {
vec_of_val.into_iter().paginate(pagination).collect()
}

vec_of_val.into_iter().paginate(pagination).collect()
}

#[derive(serde::Serialize)]
Expand Down Expand Up @@ -417,7 +429,7 @@ async fn handle_metrics(sumeragi: Arc<Sumeragi>, _network: Addr<IrohaNetwork>) -
iroha_logger::error!(%error, "Error while calling sumeragi::update_metrics.");
}
sumeragi
.metrics_mutex_access()
.metrics()
.try_to_string()
.map_err(Error::Prometheus)
}
Expand All @@ -429,7 +441,7 @@ async fn handle_status(sumeragi: Arc<Sumeragi>, _network: Addr<IrohaNetwork>) ->
if let Err(error) = sumeragi.update_metrics() {
iroha_logger::error!(%error, "Error while calling `sumeragi::update_metrics`.");
}
let status = Status::from(&sumeragi.metrics_mutex_access());
let status = Status::from(&sumeragi.metrics());
Ok(reply::json(&status))
}

Expand Down
51 changes: 51 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,47 @@ impl Client {
)
}

/// Create a request with sorting and the given filter.
///
/// # Errors
/// Fails if sending request fails
pub fn request_with_sorting_and_filter<R>(
&self,
request: R,
sorting: Sorting,
filter: PredicateBox,
) -> QueryHandlerResult<ClientQueryOutput<R>>
where
R: Query + Into<QueryBox> + Debug,
<R::Output as TryFrom<Value>>::Error: Into<eyre::Error>, // Seems redundant
{
self.request_with_pagination_and_filter_and_sorting(
request,
Pagination::default(),
sorting,
filter,
)
}

/// Query API entry point. Requests quieries from `Iroha` peers with filter.
///
/// Uses default blocking http-client. If you need some custom integration, look at
/// [`Self::prepare_query_request`].
///
/// # Errors
/// Fails if sending request fails
pub fn request_with_filter<R>(
&self,
request: R,
filter: PredicateBox,
) -> QueryHandlerResult<ClientQueryOutput<R>>
where
R: Query + Into<QueryBox> + Debug,
<R::Output as TryFrom<Value>>::Error: Into<eyre::Error>,
{
self.request_with_pagination_and_filter(request, Pagination::default(), filter)
}

/// Query API entry point. Requests queries from `Iroha` peers with pagination.
///
/// Uses default blocking http-client. If you need some custom integration, look at
Expand Down Expand Up @@ -1564,6 +1605,16 @@ pub mod role {
}
}

pub mod parameter {
//! Module with queries for config parameters
use super::*;

/// Construct a query to retrieve all config parameters
pub const fn all() -> FindAllParameters {
FindAllParameters::new()
}
}

#[cfg(test)]
mod tests {
#![allow(clippy::restriction)]
Expand Down
1 change: 1 addition & 0 deletions client/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod queries;
mod query_errors;
mod restart_peer;
mod roles;
mod set_parameter;
mod sorting;
mod transfer_asset;
mod triggers;
Expand Down
39 changes: 39 additions & 0 deletions client/tests/integration/set_parameter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![allow(clippy::restriction)]

use std::str::FromStr;

use eyre::Result;
use iroha_client::client;
use iroha_data_model::prelude::*;
use test_network::*;

#[test]
fn can_change_parameter_value() -> Result<()> {
let (_rt, _peer, test_client) = <PeerBuilder>::new().with_port(10_800).start_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

let account_id: AccountId = "alice@wonderland".parse().expect("Valid");
let parameter = Parameter::from_str("?BlockSyncGossipPeriod=20000")?;
let parameter_id = ParameterId::from_str("BlockSyncGossipPeriod")?;
let param_box = SetParameterBox::new(parameter, account_id);

let old_params = test_client.request(client::parameter::all())?;
let param_val_old = old_params
.iter()
.find(|param| param.id() == &parameter_id)
.expect("Parameter should exist")
.val();

test_client.submit_blocking(param_box)?;

let new_params = test_client.request(client::parameter::all())?;

let param_val_new = new_params
.iter()
.find(|param| param.id() == &parameter_id)
.expect("Parameter should exist")
.val();

assert_ne!(param_val_old, param_val_new);
Ok(())
}
Loading

0 comments on commit 836b3df

Please sign in to comment.