Skip to content

Commit

Permalink
[refactor] #4080: Export data model through iroha_client
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic committed Nov 24, 2023
1 parent 302f599 commit b1455a5
Show file tree
Hide file tree
Showing 57 changed files with 286 additions and 226 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::thread;

use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use iroha::samples::{construct_executor, get_config};
use iroha_client::client::{asset, Client};
use iroha_client::{
client::{asset, Client},
data_model::prelude::*,
};
use iroha_config::base::runtime_upgrades::Reload;
use iroha_crypto::KeyPair;
use iroha_data_model::prelude::*;
use iroha_genesis::{GenesisNetwork, RawGenesisBlockBuilder};
use iroha_primitives::unique_vec;
use iroha_version::Encode;
Expand Down Expand Up @@ -45,7 +47,7 @@ fn query_requests(criterion: &mut Criterion) {
configuration
.logger
.max_log_level
.reload(iroha_data_model::Level::ERROR)
.reload(iroha_client::data_model::Level::ERROR)
.expect("Should not fail");
let mut group = criterion.benchmark_group("query-requests");
let domain_id: DomainId = "domain".parse().expect("Valid");
Expand Down
10 changes: 6 additions & 4 deletions client/benches/tps/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use std::{
};

use eyre::{Result, WrapErr};
use iroha_client::client::Client;
use iroha_data_model::{
parameter::{default::MAX_TRANSACTIONS_IN_BLOCK, ParametersBuilder},
prelude::*,
use iroha_client::{
client::Client,
data_model::{
parameter::{default::MAX_TRANSACTIONS_IN_BLOCK, ParametersBuilder},
prelude::*,
},
};
use serde::Deserialize;
use serde_json::json;
Expand Down
2 changes: 1 addition & 1 deletion client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{thread, time::Duration};

use iroha::samples::{construct_executor, get_config};
use iroha_data_model::prelude::*;
use iroha_client::data_model::prelude::*;
use iroha_genesis::{GenesisNetwork, RawGenesisBlock, RawGenesisBlockBuilder};
use iroha_primitives::unique_vec;
use test_network::{
Expand Down
56 changes: 33 additions & 23 deletions client/examples/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use std::fs::File;

use eyre::{Error, WrapErr};
use iroha_client::data_model::TryToValue;
// #region rust_config_crates
use iroha_config::client::Configuration;
use iroha_data_model::TryToValue;
// #endregion rust_config_crates

fn main() {
Expand Down Expand Up @@ -47,10 +47,12 @@ fn json_config_client_test(config: &Configuration) -> Result<(), Error> {

fn domain_registration_test(config: &Configuration) -> Result<(), Error> {
// #region domain_register_example_crates
use iroha_client::client::Client;
use iroha_data_model::{
metadata::UnlimitedMetadata,
prelude::{Domain, DomainId, InstructionExpr, RegisterExpr},
use iroha_client::{
client::Client,
data_model::{
metadata::UnlimitedMetadata,
prelude::{Domain, DomainId, InstructionExpr, RegisterExpr},
},
};
// #endregion domain_register_example_crates

Expand Down Expand Up @@ -91,9 +93,9 @@ fn domain_registration_test(config: &Configuration) -> Result<(), Error> {

fn account_definition_test() -> Result<(), Error> {
// #region account_definition_comparison
use iroha_data_model::prelude::AccountId;
use iroha_client::data_model::prelude::AccountId;

// Create an `iroha_data_model::AccountId` instance
// Create an `iroha_client::data_model::AccountId` instance
// with a DomainId instance and a Domain ID for an account
let longhand_account_id = AccountId::new("white_rabbit".parse()?, "looking_glass".parse()?);
let account_id: AccountId = "white_rabbit@looking_glass"
Expand All @@ -111,12 +113,14 @@ fn account_definition_test() -> Result<(), Error> {

fn account_registration_test(config: &Configuration) -> Result<(), Error> {
// #region register_account_crates
use iroha_client::client::Client;
use iroha_crypto::KeyPair;
use iroha_data_model::{
metadata::UnlimitedMetadata,
prelude::{Account, AccountId, InstructionExpr, RegisterExpr},
use iroha_client::{
client::Client,
data_model::{
metadata::UnlimitedMetadata,
prelude::{Account, AccountId, InstructionExpr, RegisterExpr},
},
};
use iroha_crypto::KeyPair;
// #endregion register_account_crates

// Create an Iroha client
Expand Down Expand Up @@ -161,9 +165,11 @@ fn asset_registration_test(config: &Configuration) -> Result<(), Error> {
// #region register_asset_crates
use std::str::FromStr as _;

use iroha_client::client::Client;
use iroha_data_model::prelude::{
AccountId, AssetDefinition, AssetDefinitionId, AssetId, IdBox, MintExpr, RegisterExpr,
use iroha_client::{
client::Client,
data_model::prelude::{
AccountId, AssetDefinition, AssetDefinitionId, AssetId, IdBox, MintExpr, RegisterExpr,
},
};
// #endregion register_asset_crates

Expand Down Expand Up @@ -209,10 +215,12 @@ fn asset_minting_test(config: &Configuration) -> Result<(), Error> {
// #region mint_asset_crates
use std::str::FromStr;

use iroha_client::client::Client;
use iroha_data_model::{
prelude::{AccountId, AssetDefinitionId, AssetId, MintExpr, ToValue},
IdBox,
use iroha_client::{
client::Client,
data_model::{
prelude::{AccountId, AssetDefinitionId, AssetId, MintExpr, ToValue},
IdBox,
},
};
// #endregion mint_asset_crates

Expand Down Expand Up @@ -267,10 +275,12 @@ fn asset_burning_test(config: &Configuration) -> Result<(), Error> {
// #region burn_asset_crates
use std::str::FromStr;

use iroha_client::client::Client;
use iroha_data_model::{
prelude::{AccountId, AssetDefinitionId, AssetId, BurnExpr, ToValue},
IdBox,
use iroha_client::{
client::Client,
data_model::{
prelude::{AccountId, AssetDefinitionId, AssetId, BurnExpr, ToValue},
IdBox,
},
};
// #endregion burn_asset_crates

Expand Down
75 changes: 37 additions & 38 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ use futures_util::StreamExt;
use http_default::{AsyncWebSocketStream, WebSocketStream};
use iroha_config::{client::Configuration, torii::uri, GetConfiguration, PostConfiguration};
use iroha_crypto::{HashOf, KeyPair};
use iroha_data_model::{
block::SignedBlock,
isi::Instruction,
predicate::PredicateBox,
prelude::*,
query::{Pagination, Query, Sorting},
transaction::TransactionPayload,
BatchedResponse, ValidationFail,
};
use iroha_logger::prelude::*;
use iroha_telemetry::metrics::Status;
use iroha_version::prelude::*;
Expand All @@ -34,6 +25,15 @@ use url::Url;

use self::{blocks_api::AsyncBlockStream, events_api::AsyncEventStream};
use crate::{
data_model::{
block::SignedBlock,
isi::Instruction,
predicate::PredicateBox,
prelude::*,
query::{Pagination, Query, Sorting},
transaction::TransactionPayload,
BatchedResponse, ValidationFail,
},
http::{Method as HttpMethod, RequestBuilder, Response, StatusCode},
http_default::{self, DefaultRequestBuilder, WebSocketError, WebSocketMessage},
query_builder::QueryRequestBuilder,
Expand Down Expand Up @@ -144,7 +144,7 @@ where
.map_err(Into::into)
.wrap_err("Unexpected type")?;

self.query_request.request = iroha_data_model::query::QueryRequest::Cursor(cursor);
self.query_request.request = crate::data_model::query::QueryRequest::Cursor(cursor);
Ok(value)
}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ where

fn next(&mut self) -> Option<Self::Item> {
if self.client_cursor >= self.iter.len() {
let iroha_data_model::query::QueryRequest::Cursor(cursor) =
let crate::data_model::query::QueryRequest::Cursor(cursor) =
&self.query_handler.query_request.request
else {
return None;
Expand Down Expand Up @@ -323,18 +323,18 @@ macro_rules! impl_query_output {
}
impl_query_output! {
bool,
iroha_data_model::Value,
iroha_data_model::numeric::NumericValue,
iroha_data_model::role::Role,
iroha_data_model::asset::Asset,
iroha_data_model::asset::AssetDefinition,
iroha_data_model::account::Account,
iroha_data_model::domain::Domain,
iroha_data_model::block::BlockHeader,
iroha_data_model::query::MetadataValue,
iroha_data_model::query::TransactionQueryOutput,
iroha_data_model::permission::PermissionTokenSchema,
iroha_data_model::trigger::Trigger<iroha_data_model::events::TriggeringFilterBox>,
crate::data_model::Value,
crate::data_model::numeric::NumericValue,
crate::data_model::role::Role,
crate::data_model::asset::Asset,
crate::data_model::asset::AssetDefinition,
crate::data_model::account::Account,
crate::data_model::domain::Domain,
crate::data_model::block::BlockHeader,
crate::data_model::query::MetadataValue,
crate::data_model::query::TransactionQueryOutput,
crate::data_model::permission::PermissionTokenSchema,
crate::data_model::trigger::Trigger<crate::data_model::events::TriggeringFilterBox>,
}

/// Iroha client
Expand Down Expand Up @@ -367,7 +367,7 @@ pub struct Client {
pub struct QueryRequest {
torii_url: Url,
headers: HashMap<String, String>,
request: iroha_data_model::query::QueryRequest<Vec<u8>>,
request: crate::data_model::query::QueryRequest<Vec<u8>>,
}

impl QueryRequest {
Expand All @@ -378,8 +378,8 @@ impl QueryRequest {
Self {
torii_url: format!("http://{torii_url}").parse().unwrap(),
headers: HashMap::new(),
request: iroha_data_model::query::QueryRequest::Query(
iroha_data_model::query::QueryWithParameters {
request: crate::data_model::query::QueryRequest::Query(
crate::data_model::query::QueryWithParameters {
query: Vec::default(),
sorting: Sorting::default(),
pagination: Pagination::default(),
Expand All @@ -397,12 +397,12 @@ impl QueryRequest {
.headers(self.headers);

match self.request {
iroha_data_model::query::QueryRequest::Query(query_with_params) => builder
crate::data_model::query::QueryRequest::Query(query_with_params) => builder
.params(query_with_params.sorting().clone().into_query_parameters())
.params(query_with_params.pagination().into_query_parameters())
.params(query_with_params.fetch_size().into_query_parameters())
.body(query_with_params.query().clone()),
iroha_data_model::query::QueryRequest::Cursor(cursor) => {
crate::data_model::query::QueryRequest::Cursor(cursor) => {
builder.params(Vec::from(cursor))
}
}
Expand Down Expand Up @@ -754,10 +754,10 @@ impl Client {
/// ```ignore
/// use eyre::Result;
/// use iroha_client::{
/// data_model::{predicate::PredicateBox, prelude::{Account, FindAllAccounts, Pagination}},
/// client::Client,
/// http::{RequestBuilder, Response, Method},
/// };
/// use iroha_data_model::{predicate::PredicateBox, prelude::{Account, FindAllAccounts, Pagination}};
///
/// struct YourAsyncRequest;
///
Expand Down Expand Up @@ -821,8 +821,8 @@ impl Client {
let query_request = QueryRequest {
torii_url: self.torii_url.clone(),
headers: self.headers.clone(),
request: iroha_data_model::query::QueryRequest::Query(
iroha_data_model::query::QueryWithParameters::new(
request: crate::data_model::query::QueryRequest::Query(
crate::data_model::query::QueryWithParameters::new(
request, sorting, pagination, fetch_size,
),
),
Expand Down Expand Up @@ -883,7 +883,7 @@ impl Client {
#[cfg(debug_assertions)]
pub fn request_with_cursor<O>(
&self,
cursor: iroha_data_model::query::cursor::ForwardCursor,
cursor: crate::data_model::query::cursor::ForwardCursor,
) -> QueryResult<O::Target>
where
O: QueryOutput,
Expand All @@ -892,7 +892,7 @@ impl Client {
let request = QueryRequest {
torii_url: self.torii_url.clone(),
headers: self.headers.clone(),
request: iroha_data_model::query::QueryRequest::Cursor(cursor),
request: crate::data_model::query::QueryRequest::Cursor(cursor),
};
let response = request.clone().assemble().build()?.send()?;

Expand Down Expand Up @@ -1386,7 +1386,7 @@ pub mod events_api {
pub struct Events;

impl FlowEvents for Events {
type Event = iroha_data_model::prelude::Event;
type Event = crate::data_model::prelude::Event;

fn message(&self, message: Vec<u8>) -> Result<Self::Event> {
let event_socket_message = EventMessage::decode_all(&mut message.as_slice())?;
Expand All @@ -1413,9 +1413,8 @@ mod blocks_api {
pub mod flow {
use std::num::NonZeroU64;

use iroha_data_model::block::stream::*;

use super::*;
use crate::data_model::block::stream::*;

/// Initialization struct for Blocks API flow.
pub struct Init {
Expand Down Expand Up @@ -1466,7 +1465,7 @@ mod blocks_api {
pub struct Events;

impl FlowEvents for Events {
type Event = iroha_data_model::block::SignedBlock;
type Event = crate::data_model::block::SignedBlock;

fn message(&self, message: Vec<u8>) -> Result<Self::Event> {
Ok(BlockMessage::decode_all(&mut message.as_slice()).map(Into::into)?)
Expand Down Expand Up @@ -1758,9 +1757,9 @@ mod tests {
#[cfg(test)]
mod query_errors_handling {
use http::Response;
use iroha_data_model::{asset::Asset, query::error::QueryExecutionFail, ValidationFail};

use super::*;
use crate::data_model::{asset::Asset, query::error::QueryExecutionFail, ValidationFail};

#[test]
fn certain_errors() -> Result<()> {
Expand Down
Loading

0 comments on commit b1455a5

Please sign in to comment.