Skip to content

Commit

Permalink
[feature] hyperledger#2085: Add Address
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Petrosyan <a-p-petrosyan@yandex.ru>
  • Loading branch information
appetrosyan committed Aug 25, 2022
1 parent 274b96c commit 1d81e1d
Show file tree
Hide file tree
Showing 78 changed files with 2,251 additions and 1,613 deletions.
2 changes: 1 addition & 1 deletion cli/src/torii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum Error {
pub(crate) const fn query_status_code(query_error: &query::Error) -> StatusCode {
use query::Error::*;
match query_error {
Decode(_) | Evaluate(_) | Conversion(_) => StatusCode::BAD_REQUEST,
BareAccount | Decode(_) | Evaluate(_) | Conversion(_) => StatusCode::BAD_REQUEST,
Signature(_) | Unauthorized => StatusCode::UNAUTHORIZED,
Permission(_) => StatusCode::FORBIDDEN,
Find(_) => StatusCode::NOT_FOUND,
Expand Down
20 changes: 10 additions & 10 deletions cli/src/torii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use iroha_core::{
tx::TransactionValidator,
wsv::World,
};
use iroha_data_model::{account::GENESIS_ACCOUNT_NAME, predicate::PredicateBox, prelude::*};
use iroha_data_model::{account::genesis::GENESIS_ACCOUNT_NAME, predicate::PredicateBox, prelude::*};
use iroha_version::prelude::*;
use test_network::{prepare_test_for_nextest, unique_port};
use test_network::*;
use tokio::time;
use warp::test::WsClient;

Expand All @@ -35,7 +35,7 @@ async fn create_torii() -> (Torii, KeyPair) {
config.torii.p2p_addr = format!("127.0.0.1:{}", unique_port::get_unique_free_port().unwrap());
config.torii.api_url = format!("127.0.0.1:{}", unique_port::get_unique_free_port().unwrap());
config.torii.telemetry_url =
format!("127.0.0.1:{}", unique_port::get_unique_free_port().unwrap());
format!("127.0.0.1:{}", unique_port::get_unique_free_port().expect("Valid"));
let (events, _) = tokio::sync::broadcast::channel(100);
let wsv = Arc::new(WorldStateView::new(World::with(
('a'..='z')
Expand All @@ -49,7 +49,7 @@ async fn create_torii() -> (Torii, KeyPair) {
assert!(domain
.add_account(
Account::new(
AccountId::from_str("alice@wonderland").expect("Valid"),
Alias::from_str("alice@wonderland").expect("Valid"),
[keys.public_key().clone()],
)
.build()
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn torii_pagination() {
let get_domains = |start, limit| {
let query: VerifiedQueryRequest = QueryRequest::new(
QueryBox::FindAllDomains(Default::default()),
AccountId::from_str("alice@wonderland").expect("Valid"),
Alias::from_str("alice@wonderland").expect("Valid").alice_key(),
PredicateBox::default(),
)
.sign(keys.clone())
Expand Down Expand Up @@ -185,7 +185,7 @@ impl QuerySet {
torii.query_judge = Arc::new(DenyAll::new());
}

let authority = AccountId::from_str("alice@wonderland").expect("Valid");
let authority = Alias::from_str("alice@wonderland").expect("Valid").alice_key();
for instruction in self.instructions {
instruction
.execute(authority.clone(), &torii.wsv)
Expand All @@ -199,9 +199,9 @@ impl QuerySet {
self.account.unwrap_or(authority),
PredicateBox::default(),
)
.sign(self.keys.unwrap_or(keys))
.expect("Failed to sign query with keys")
.into();
.sign(self.keys.unwrap_or(keys))
.expect("Failed to sign query with keys")
.into();

let response = warp::test::request()
.method("POST")
Expand Down Expand Up @@ -289,7 +289,7 @@ fn register_domain() -> Instruction {
fn register_account(name: &str) -> Instruction {
let (public_key, _) = KeyPair::generate().unwrap().into();
RegisterBox::new(Account::new(
AccountId::new(name.parse().expect("Valid"), DOMAIN.parse().expect("Valid")),
Alias::new(name.parse().expect("Valid"), DOMAIN.parse().expect("Valid")),
[public_key],
))
.into()
Expand Down
10 changes: 6 additions & 4 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ fn query_requests(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("query-requests");
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain = RegisterBox::new(Domain::new(domain_id.clone()));
let account_id = AccountId::new("account".parse().expect("Valid"), domain_id.clone());
let (public_key, _) = KeyPair::generate()
.expect("Failed to generate KeyPair")
.into();
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let account_alias = Alias::new("account".parse().expect("Valid"), domain_id.clone());
let account_id = AccountId::new(public_key.clone(), account_alias);
let create_account = RegisterBox::new(Account::from_id(account_id.clone()));
let asset_definition_id = AssetDefinitionId::new("xor".parse().expect("Valid"), domain_id);
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
let quantity: u32 = 200;
Expand Down Expand Up @@ -143,11 +144,12 @@ fn instruction_submits(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("instruction-requests");
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain = RegisterBox::new(Domain::new(domain_id.clone()));
let account_id = AccountId::new("account".parse().expect("Valid"), domain_id.clone());
let alias = Alias::new("account".parse().expect("Valid"), domain_id.clone());
let (public_key, _) = KeyPair::generate()
.expect("Failed to generate Key-pair.")
.into();
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let account_id = AccountId::new(public_key.clone(), alias);
let create_account = RegisterBox::new(Account::from_id(account_id.clone()));
let asset_definition_id = AssetDefinitionId::new("xor".parse().expect("Valid"), domain_id);
let mut client_config = iroha_client::samples::get_client_config(&get_key_pair());
client_config.torii_api_url = SmallStr::from_string(peer.api_address.clone());
Expand Down
21 changes: 14 additions & 7 deletions client/benches/tps/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{fmt, fs::File, io::BufReader, path::Path, str::FromStr as _, sync::mps

use eyre::{Result, WrapErr};
use iroha_client::client::Client;
use iroha_crypto::KeyPair;
use iroha_data_model::prelude::*;
use iroha_permissions_validators::public_blockchain::{
burn::CanBurnUserAssets, transfer::CanTransferUserAssets,
Expand Down Expand Up @@ -139,13 +140,19 @@ impl MeasurerUnit {
let keypair =
iroha_core::prelude::KeyPair::generate().expect("Failed to generate KeyPair.");

let account_id = account_id(self.name);
let alice_id = <Account as Identifiable>::Id::from_str("alice@wonderland")?;
let account_id = {
let alias = account_id(self.name);
AccountId::new(keypair.public_key().clone(), alias)
};
let alice_id = {
let alias = Alias::from_str("alice@wonderland")?;
let (public_key, _) = KeyPair::generate().expect("Valid").into();
AccountId::new(public_key, alias)
};
let asset_id = asset_id(self.name);

let register_me = RegisterBox::new(Account::new(
account_id.clone(),
[keypair.public_key().clone()],
let register_me = RegisterBox::new(Account::from_id(
account_id.clone() ,
));
self.client.submit_blocking(register_me)?;

Expand Down Expand Up @@ -241,11 +248,11 @@ impl MeasurerUnit {
fn asset_id(account_name: UnitName) -> AssetId {
AssetId::new(
"rose#wonderland".parse().expect("Valid"),
account_id(account_name),
account_id(account_name).fresh_key(),
)
}

#[allow(clippy::expect_used)]
fn account_id(name: UnitName) -> AccountId {
fn account_id(name: UnitName) -> Alias {
format!("{}@wonderland", name).parse().expect("Valid")
}
12 changes: 7 additions & 5 deletions client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use iroha::samples::get_config;
use iroha_core::genesis::{
GenesisNetwork, GenesisNetworkTrait, RawGenesisBlock, RawGenesisBlockBuilder,
};
use iroha_crypto::KeyPair;
use iroha_data_model::prelude::*;
use test_network::{
get_key_pair, wait_for_genesis_committed, Peer as TestPeer, PeerBuilder, TestRuntime,
Expand Down Expand Up @@ -65,12 +66,13 @@ fn create_million_accounts_directly() {
wait_for_genesis_committed(&vec![test_client.clone()], 0);
for i in 0_u32..1_000_000_u32 {
let domain_id: DomainId = format!("wonderland-{}", i).parse().expect("Valid");
let normal_account_id = AccountId::new(
format!("bob-{}", i).parse().expect("Valid"),
domain_id.clone(),
);
let normal_account_id = {
let alias = Alias::new(format!("bob-{}", i).parse().expect("Valid"), domain_id.clone());
let (public_key, _) = KeyPair::generate().expect("Valid").into();
AccountId::new(public_key, alias)
};
let create_domain = RegisterBox::new(Domain::new(domain_id));
let create_account = RegisterBox::new(Account::new(normal_account_id.clone(), []));
let create_account = RegisterBox::new(Account::from_id(normal_account_id.clone()));
if test_client
.submit_all([create_domain.into(), create_account.into()].to_vec())
.is_err()
Expand Down
7 changes: 6 additions & 1 deletion client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,11 +1161,16 @@ pub mod account {
FindAllAccounts::new()
}

/// Construct a query to get account by id
/// Construct a query to get account by id. The alias is ignored.
pub fn by_id(account_id: impl Into<EvaluatesTo<AccountId>>) -> FindAccountById {
FindAccountById::new(account_id)
}

/// Construct a query to get account by alias.
pub fn by_alias(alias: impl Into<EvaluatesTo<Alias>>) -> FindAccountIdByAlias {
FindAccountIdByAlias::new(alias)
}

/// Construct a query to get all accounts containing specified asset
pub fn all_with_asset(
asset_definition_id: impl Into<EvaluatesTo<AssetDefinitionId>>,
Expand Down
5 changes: 3 additions & 2 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod http_default;
/// Module containing sample configurations for tests and benchmarks.
pub mod samples {
use iroha_config::torii::uri;
use iroha_core::tx::AccountId;
use iroha_crypto::KeyPair;

use super::Configuration;
Expand All @@ -20,9 +21,9 @@ pub mod samples {
pub fn get_client_config(key_pair: &KeyPair) -> Configuration {
let (public_key, private_key) = key_pair.clone().into();
Configuration {
public_key,
public_key: public_key.clone(),
private_key,
account_id: "alice@wonderland".parse().expect("Should not fail."),
account_id: AccountId::new(public_key, "alice@wonderland".parse().expect("Should not fail.")),
torii_api_url: iroha_primitives::small::SmallStr::from_str(uri::DEFAULT_API_URL),
..Configuration::default()
}
Expand Down
21 changes: 11 additions & 10 deletions client/tests/integration/add_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ fn client_add_account_with_name_length_more_than_limit_should_not_commit_transac

let pipeline_time = super::Configuration::pipeline_time();

let normal_account_id: AccountId = "bob@wonderland".parse().expect("Valid");
let create_account = RegisterBox::new(Account::new(normal_account_id.clone(), []));
let normal_account_id = "bob@wonderland".parse::<Alias>().expect("Valid").fresh_key();
let create_account = RegisterBox::new(Account::from_id(normal_account_id.clone()));
test_client.submit(create_account)?;

let too_long_account_name = "0".repeat(2_usize.pow(14));
let incorrect_account_id: AccountId = (too_long_account_name + "@wonderland")
.parse()
.expect("Valid");
let create_account = RegisterBox::new(Account::new(incorrect_account_id.clone(), []));
let incorrect_account_id = (too_long_account_name + "@wonderland")
.parse::<Alias>()
.expect("Valid")
.fresh_key();
let create_account = RegisterBox::new(Account::from_id(incorrect_account_id.clone()));
test_client.submit(create_account)?;

thread::sleep(pipeline_time * 2);

assert!(test_client
.request(client::account::by_id(normal_account_id))
.is_ok());
.request(client::account::by_id(normal_account_id))
.is_ok());
assert!(test_client
.request(client::account::by_id(incorrect_account_id))
.is_err());
.request(client::account::by_id(incorrect_account_id))
.is_err());

Ok(())
}
48 changes: 24 additions & 24 deletions client/tests/integration/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn client_register_asset_should_add_asset_once_but_not_twice() -> Result<()> {
wait_for_genesis_committed(&vec![test_client.clone()], 0);

// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let account_id = Alias::from_str("alice@wonderland").expect("Valid").alice_key();

let asset_definition_id = AssetDefinitionId::from_str("test_asset#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
Expand Down Expand Up @@ -48,7 +48,7 @@ fn unregister_asset_should_remove_asset_from_account() -> Result<()> {
wait_for_genesis_committed(&vec![test_client.clone()], 0);

// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let account_id = Alias::from_str("alice@wonderland").expect("Valid").alice_key();

let asset_definition_id = AssetDefinitionId::from_str("test_asset#wonderland").expect("Valid");
let asset_id = AssetId::new(asset_definition_id.clone(), account_id.clone());
Expand Down Expand Up @@ -84,7 +84,7 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() ->
wait_for_genesis_committed(&vec![test_client.clone()], 0);

// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let account_id = Alias::from_str("alice@wonderland").expect("Valid").alice_key();
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();
Expand Down Expand Up @@ -116,7 +116,7 @@ fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount(
wait_for_genesis_committed(&vec![test_client.clone()], 0);

// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let account_id = Alias::from_str("alice@wonderland").expect("Valid").alice_key();
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset = RegisterBox::new(AssetDefinition::big_quantity(asset_definition_id.clone()));
let metadata = iroha_data_model::metadata::UnlimitedMetadata::default();
Expand Down Expand Up @@ -147,7 +147,7 @@ fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> {
let (_rt, _peer, mut test_client) = <PeerBuilder>::new().start_with_runtime();

// Given
let account_id = AccountId::from_str("alice@wonderland").expect("Valid");
let account_id = Alias::from_str("alice@wonderland").expect("Valid").alice_key();
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let identifiable_box = AssetDefinition::fixed(asset_definition_id.clone());
let create_asset = RegisterBox::new(identifiable_box);
Expand All @@ -164,25 +164,25 @@ fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> {
);
let instructions: Vec<Instruction> = vec![create_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions.into(), metadata)?;
test_client.submit_transaction(tx)?;
test_client.poll_request(client::asset::by_account_id(account_id.clone()), |result| {
result.iter().any(|asset| {
asset.id().definition_id == asset_definition_id
&& *asset.value() == AssetValue::Fixed(quantity)
})
})?;

// Add some fractional part
let quantity2: Fixed = Fixed::try_from(0.55_f64).unwrap();
let mint = MintBox::new(
Value::Fixed(quantity2),
IdBox::AssetId(AssetId::new(
asset_definition_id.clone(),
account_id.clone(),
)),
);
// and check that it is added without errors
let sum = quantity
test_client.submit_transaction(tx)?;
test_client.poll_request(client::asset::by_account_id(account_id.clone()), |result| {
result.iter().any(|asset| {
asset.id().definition_id == asset_definition_id
&& *asset.value() == AssetValue::Fixed(quantity)
})
})?;

// Add some fractional part
let quantity2: Fixed = Fixed::try_from(0.55_f64).unwrap();
let mint = MintBox::new(
Value::Fixed(quantity2),
IdBox::AssetId(AssetId::new(
asset_definition_id.clone(),
account_id.clone(),
)),
);
// and check that it is added without errors
let sum = quantity
.checked_add(quantity2)
.map_err(|e| eyre::eyre!("{}", e))?;
test_client.submit_till(mint, client::asset::by_account_id(account_id), |result| {
Expand Down
6 changes: 2 additions & 4 deletions client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{str::FromStr as _, thread};

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

Expand All @@ -20,9 +19,8 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
let pipeline_time = Configuration::pipeline_time();

let create_domain = RegisterBox::new(Domain::new(DomainId::from_str("domain")?));
let account_id = AccountId::from_str("account@domain")?;
let (public_key, _) = KeyPair::generate()?.into();
let create_account = RegisterBox::new(Account::new(account_id.clone(), [public_key]));
let account_id = Alias::from_str("account@domain")?.fresh_key();
let create_account = RegisterBox::new(Account::from_id(account_id.clone()));
let asset_definition_id = AssetDefinitionId::from_str("xor#domain")?;
let create_asset = RegisterBox::new(AssetDefinition::quantity(asset_definition_id.clone()));
iroha_client.submit_all(vec![
Expand Down
Loading

0 comments on commit 1d81e1d

Please sign in to comment.