Skip to content

Commit

Permalink
[refactor] #4277: Infallible key pair generation (#4283)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Polyakov <[email protected]>
  • Loading branch information
Arjentix authored Feb 16, 2024
1 parent 960f657 commit 0f83d26
Show file tree
Hide file tree
Showing 40 changed files with 153 additions and 183 deletions.
10 changes: 5 additions & 5 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ mod tests {

use super::*;

fn config_factory() -> Result<ConfigurationProxy> {
let key_pair = KeyPair::generate()?;
fn config_factory() -> ConfigurationProxy {
let key_pair = KeyPair::generate();

let mut base = ConfigurationProxy {
chain_id: Some(ChainId::new("0")),
Expand All @@ -663,7 +663,7 @@ mod tests {
torii.p2p_addr = Some(socket_addr!(127.0.0.1:1337));
torii.api_url = Some(socket_addr!(127.0.0.1:1337));

Ok(base)
base
}

#[test]
Expand All @@ -675,7 +675,7 @@ mod tests {
.build();

let config = {
let mut cfg = config_factory()?;
let mut cfg = config_factory();
cfg.genesis.as_mut().unwrap().file = Some(Some("./genesis/gen.json".into()));
cfg.kura.as_mut().unwrap().block_store_path = Some("../storage".into());
cfg.snapshot.as_mut().unwrap().dir_path = Some("../snapshots".into());
Expand Down Expand Up @@ -729,7 +729,7 @@ mod tests {
.build();

let config = {
let mut cfg = config_factory()?;
let mut cfg = config_factory();
cfg.genesis.as_mut().unwrap().file = Some(Some("./genesis.json".into()));
cfg
};
Expand Down
4 changes: 1 addition & 3 deletions cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ pub fn get_config_proxy(
) -> ConfigurationProxy {
let chain_id = chain_id.unwrap_or_else(|| ChainId::new("0"));

let (public_key, private_key) = key_pair
.unwrap_or_else(|| KeyPair::generate().expect("Key pair generation failed"))
.into();
let (public_key, private_key) = key_pair.unwrap_or_else(KeyPair::generate).into();
iroha_logger::info!(%public_key);
ConfigurationProxy {
chain_id: Some(chain_id),
Expand Down
8 changes: 2 additions & 6 deletions client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ fn query_requests(criterion: &mut Criterion) {
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain = Register::domain(Domain::new(domain_id.clone()));
let account_id = AccountId::new(domain_id.clone(), "account".parse().expect("Valid"));
let (public_key, _) = KeyPair::generate()
.expect("Failed to generate KeyPair")
.into();
let (public_key, _) = KeyPair::generate().into();
let create_account = Register::account(Account::new(account_id.clone(), [public_key]));
let asset_definition_id = AssetDefinitionId::new(domain_id, "xor".parse().expect("Valid"));
let create_asset =
Expand Down Expand Up @@ -169,9 +167,7 @@ fn instruction_submits(criterion: &mut Criterion) {
let domain_id: DomainId = "domain".parse().expect("Valid");
let create_domain: InstructionBox = Register::domain(Domain::new(domain_id.clone())).into();
let account_id = AccountId::new(domain_id.clone(), "account".parse().expect("Valid"));
let (public_key, _) = KeyPair::generate()
.expect("Failed to generate Key-pair.")
.into();
let (public_key, _) = KeyPair::generate().into();
let create_account = Register::account(Account::new(account_id.clone(), [public_key])).into();
let asset_definition_id = AssetDefinitionId::new(domain_id, "xor".parse().expect("Valid"));
let mut client_config =
Expand Down
3 changes: 1 addition & 2 deletions client/benches/tps/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ impl MeasurerUnit {

/// Submit initial transactions for measurement
fn ready(self) -> Result<Self> {
let keypair =
iroha_client::crypto::KeyPair::generate().expect("Failed to generate KeyPair.");
let keypair = iroha_client::crypto::KeyPair::generate();

let account_id = account_id(self.name);
let asset_id = asset_id(self.name);
Expand Down
4 changes: 1 addition & 3 deletions client/examples/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ fn account_registration_test(config: &Configuration) -> Result<(), Error> {

// TODO: consider getting a key from white_rabbit
// Generate a new public key for a new account
let (public_key, _) = KeyPair::generate()
.expect("Failed to generate KeyPair")
.into();
let (public_key, _) = KeyPair::generate().into();

// #region register_account_generate
// Generate a new account
Expand Down
2 changes: 1 addition & 1 deletion client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ mod tests {

#[test]
fn txs_same_except_for_nonce_have_different_hashes() {
let (public_key, private_key) = KeyPair::generate().unwrap().into();
let (public_key, private_key) = KeyPair::generate().into();

let cfg = ConfigurationProxy {
chain_id: Some(ChainId::new("0")),
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ fn find_rate_and_make_exchange_isi_should_succeed() {
let seller_btc: AssetId = "btc#crypto#seller@company".parse().expect("Valid.");
let buyer_eth: AssetId = "eth#crypto#buyer@company".parse().expect("Valid.");

let seller_keypair = KeyPair::generate().expect("Failed to generate seller KeyPair.");
let buyer_keypair = KeyPair::generate().expect("Failed to generate seller KeyPair.");
let seller_keypair = KeyPair::generate();
let buyer_keypair = KeyPair::generate();

let register_account = |account_id: AccountId, signature: PublicKey| {
Register::account(Account::new(account_id, [signature]))
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
let create_domain: InstructionBox =
Register::domain(Domain::new(DomainId::from_str("domain")?)).into();
let account_id = AccountId::from_str("account@domain")?;
let (public_key, _) = KeyPair::generate()?.into();
let (public_key, _) = KeyPair::generate().into();
let create_account = Register::account(Account::new(account_id.clone(), [public_key])).into();
let asset_definition_id = AssetDefinitionId::from_str("xor#domain")?;
let create_asset =
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration/burn_public_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn public_keys_cannot_be_burned_to_nothing() {
let (_rt, _peer, client) = <PeerBuilder>::new().with_port(10_045).start_with_runtime();
wait_for_genesis_committed(&vec![client.clone()], 0);

let charlie_initial_keypair = KeyPair::generate().unwrap();
let charlie_initial_keypair = KeyPair::generate();
let register_charlie = Register::account(Account::new(
charlie_id.clone(),
[charlie_initial_keypair.public_key().clone()],
Expand All @@ -62,7 +62,7 @@ fn public_keys_cannot_be_burned_to_nothing() {
assert_eq!(keys_count, 1);

let mint_keys = (0..KEYS_COUNT - 1).map(|_| {
let (public_key, _) = KeyPair::generate().unwrap().into();
let (public_key, _) = KeyPair::generate().into();
Mint::account_public_key(public_key, charlie_id.clone())
});

Expand Down
14 changes: 7 additions & 7 deletions client/tests/integration/domain_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn domain_owner_domain_permissions() -> Result<()> {
let kingdom = Domain::new(kingdom_id.clone());
test_client.submit_blocking(Register::domain(kingdom))?;

let bob_keypair = KeyPair::generate()?;
let bob_keypair = KeyPair::generate();
let bob = Account::new(bob_id.clone(), [bob_keypair.public_key().clone()]);
test_client.submit_blocking(Register::account(bob))?;

Expand Down Expand Up @@ -93,15 +93,15 @@ fn domain_owner_account_permissions() -> Result<()> {
let kingdom = Domain::new(kingdom_id);
test_client.submit_blocking(Register::domain(kingdom))?;

let mad_hatter_keypair = KeyPair::generate()?;
let mad_hatter_keypair = KeyPair::generate();
let mad_hatter = Account::new(
mad_hatter_id.clone(),
[mad_hatter_keypair.public_key().clone()],
);
test_client.submit_blocking(Register::account(mad_hatter))?;

// check that "alice@wonderland" as owner of domain can burn and mint public keys for accounts in her domain
let mad_hatter_new_keypair = KeyPair::generate()?;
let mad_hatter_new_keypair = KeyPair::generate();
test_client.submit_blocking(Mint::account_public_key(
mad_hatter_new_keypair.public_key().clone(),
mad_hatter_id.clone(),
Expand Down Expand Up @@ -157,7 +157,7 @@ fn domain_owner_asset_definition_permissions() -> Result<()> {
let kingdom = Domain::new(kingdom_id.clone());
test_client.submit_blocking(Register::domain(kingdom))?;

let bob_keypair = KeyPair::generate()?;
let bob_keypair = KeyPair::generate();
let bob = Account::new(bob_id.clone(), [bob_keypair.public_key().clone()]);
test_client.submit_blocking(Register::account(bob))?;

Expand Down Expand Up @@ -227,7 +227,7 @@ fn domain_owner_asset_permissions() -> Result<()> {
let kingdom = Domain::new(kingdom_id.clone());
test_client.submit_blocking(Register::domain(kingdom))?;

let bob_keypair = KeyPair::generate()?;
let bob_keypair = KeyPair::generate();
let bob = Account::new(bob_id.clone(), [bob_keypair.public_key().clone()]);
test_client.submit_blocking(Register::account(bob))?;

Expand Down Expand Up @@ -292,7 +292,7 @@ fn domain_owner_trigger_permissions() -> Result<()> {
let kingdom = Domain::new(kingdom_id);
test_client.submit_blocking(Register::domain(kingdom))?;

let bob_keypair = KeyPair::generate()?;
let bob_keypair = KeyPair::generate();
let bob = Account::new(bob_id.clone(), [bob_keypair.public_key().clone()]);
test_client.submit_blocking(Register::account(bob))?;

Expand Down Expand Up @@ -353,7 +353,7 @@ fn domain_owner_transfer() -> Result<()> {
let kingdom = Domain::new(kingdom_id.clone());
test_client.submit_blocking(Register::domain(kingdom))?;

let bob_keypair = KeyPair::generate()?;
let bob_keypair = KeyPair::generate();
let bob = Account::new(bob_id.clone(), [bob_keypair.public_key().clone()]);
test_client.submit_blocking(Register::account(bob))?;

Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/multiple_blocks_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn long_multiple_blocks_created() -> Result<()> {

let create_domain: InstructionBox = Register::domain(Domain::new("domain".parse()?)).into();
let account_id: AccountId = "account@domain".parse()?;
let (public_key, _) = KeyPair::generate()?.into();
let (public_key, _) = KeyPair::generate().into();
let create_account = Register::account(Account::new(account_id.clone(), [public_key])).into();
let asset_definition_id: AssetDefinitionId = "xor#domain".parse()?;
let create_asset =
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/multisignature_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn transaction_signed_by_new_signatory_of_account_should_pass() -> Result<()> {
let asset_definition_id: AssetDefinitionId = "xor#wonderland".parse().expect("Valid");
let create_asset =
Register::asset_definition(AssetDefinition::quantity(asset_definition_id.clone()));
let key_pair = KeyPair::generate()?;
let key_pair = KeyPair::generate();
let add_signatory = Mint::account_public_key(key_pair.public_key().clone(), account_id.clone());

let instructions: [InstructionBox; 2] = [create_asset.into(), add_signatory.into()];
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/multisignature_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn multisignature_transactions_should_wait_for_all_signatures() -> Result<()> {

let alice_id = AccountId::from_str("alice@wonderland")?;
let alice_key_pair = get_key_pair();
let key_pair_2 = KeyPair::generate()?;
let key_pair_2 = KeyPair::generate();
let asset_definition_id = AssetDefinitionId::from_str("camomile#wonderland")?;
let create_asset =
Register::asset_definition(AssetDefinition::quantity(asset_definition_id.clone()));
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/offline_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn register_offline_peer() -> Result<()> {
check_status(&peer_clients, 1);

let address = socket_addr!(128.0.0.2:8085);
let key_pair = KeyPair::generate().unwrap();
let key_pair = KeyPair::generate();
let public_key = key_pair.public_key().clone();
let peer_id = PeerId::new(address, public_key);
let register_peer = Register::peer(DataModelPeer::new(peer_id));
Expand Down
8 changes: 4 additions & 4 deletions client/tests/integration/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn permissions_disallow_asset_transfer() {
let asset_definition_id: AssetDefinitionId = "xor#wonderland".parse().expect("Valid");
let create_asset =
Register::asset_definition(AssetDefinition::quantity(asset_definition_id.clone()));
let mouse_keypair = KeyPair::generate().expect("Failed to generate KeyPair.");
let mouse_keypair = KeyPair::generate();

let alice_start_assets = get_assets(&iroha_client, &alice_id);
iroha_client
Expand Down Expand Up @@ -130,7 +130,7 @@ fn permissions_disallow_asset_burn() {
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").expect("Valid");
let create_asset =
Register::asset_definition(AssetDefinition::quantity(asset_definition_id.clone()));
let mouse_keypair = KeyPair::generate().expect("Failed to generate KeyPair.");
let mouse_keypair = KeyPair::generate();

let alice_start_assets = get_assets(&iroha_client, &alice_id);

Expand Down Expand Up @@ -201,7 +201,7 @@ fn permissions_differ_not_only_by_names() {

let alice_id: AccountId = "alice@wonderland".parse().expect("Valid");
let mouse_id: AccountId = "mouse@outfit".parse().expect("Valid");
let mouse_keypair = KeyPair::generate().expect("Failed to generate KeyPair.");
let mouse_keypair = KeyPair::generate();

// Registering mouse
let outfit_domain: DomainId = "outfit".parse().unwrap();
Expand Down Expand Up @@ -305,7 +305,7 @@ fn stored_vs_granted_token_payload() -> Result<()> {
let create_asset =
Register::asset_definition(AssetDefinition::store(asset_definition_id.clone()));
let mouse_id: AccountId = "mouse@wonderland".parse().expect("Valid");
let mouse_keypair = KeyPair::generate().expect("Failed to generate KeyPair.");
let mouse_keypair = KeyPair::generate();
let new_mouse_account = Account::new(mouse_id.clone(), [mouse_keypair.public_key().clone()]);
let instructions: [InstructionBox; 2] = [
Register::account(new_mouse_account).into(),
Expand Down
7 changes: 3 additions & 4 deletions client/tests/integration/queries/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ fn find_asset_total_quantity() -> Result<()> {
"white_rabbit@looking_glass".parse()?,
];

let keys =
core::iter::repeat_with(|| KeyPair::generate().expect("Failed to generate `KeyPair`"))
.take(accounts.len() - 1)
.collect::<Vec<_>>();
let keys = core::iter::repeat_with(KeyPair::generate)
.take(accounts.len() - 1)
.collect::<Vec<_>>();

// Registering accounts
let register_accounts = accounts
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn register_and_grant_role_for_metadata_access() -> Result<()> {
let mouse_id = AccountId::from_str("mouse@wonderland")?;

// Registering Mouse
let mouse_key_pair = KeyPair::generate()?;
let mouse_key_pair = KeyPair::generate();
let register_mouse = Register::account(Account::new(
mouse_id.clone(),
[mouse_key_pair.public_key().clone()],
Expand Down
4 changes: 1 addition & 3 deletions client/tests/integration/transfer_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ fn generate_two_ids() -> (AccountId, AccountId) {
}

fn create_mouse(mouse_id: AccountId) -> Register<Account> {
let (mouse_public_key, _) = KeyPair::generate()
.expect("Failed to generate KeyPair")
.into();
let (mouse_public_key, _) = KeyPair::generate().into();
Register::account(Account::new(mouse_id, [mouse_public_key]))
}
2 changes: 1 addition & 1 deletion client/tests/integration/unregister_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn init() -> Result<(
.into_set_parameters();
let create_domain = Register::domain(Domain::new("domain".parse()?));
let account_id: AccountId = "account@domain".parse()?;
let (public_key, _) = KeyPair::generate()?.into();
let (public_key, _) = KeyPair::generate().into();
let create_account = Register::account(Account::new(account_id.clone(), [public_key]));
let asset_definition_id: AssetDefinitionId = "xor#domain".parse()?;
let create_asset =
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn executor_upgrade_should_work() -> Result<()> {
client.submit_blocking(register_admin_domain)?;

let admin_id: AccountId = "admin@admin".parse()?;
let admin_keypair = KeyPair::generate()?;
let admin_keypair = KeyPair::generate();
let admin_account = Account::new(admin_id.clone(), [admin_keypair.public_key().clone()]);
let register_admin_account = Register::account(admin_account);
client.submit_blocking(register_admin_account)?;
Expand Down
2 changes: 1 addition & 1 deletion core/benches/blocks/apply_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl WsvApplyBlocks {
let accounts_per_domain = 1000;
let assets_per_domain = 1000;
let account_id: AccountId = "alice@wonderland".parse()?;
let key_pair = KeyPair::generate()?;
let key_pair = KeyPair::generate();
let wsv = build_wsv(rt, &account_id, &key_pair);

let nth = 100;
Expand Down
2 changes: 1 addition & 1 deletion core/benches/blocks/validate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl WsvValidateBlocks {
let accounts_per_domain = 1000;
let assets_per_domain = 1000;
let account_id: AccountId = "alice@wonderland".parse()?;
let key_pair = KeyPair::generate()?;
let key_pair = KeyPair::generate();
let wsv = build_wsv(rt, &account_id, &key_pair);

let nth = 100;
Expand Down
6 changes: 3 additions & 3 deletions core/benches/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn measure_block_size_for_n_executors(n_executors: u32) {
let xor_id = AssetDefinitionId::from_str("xor#test").expect("tested");
let alice_xor_id = AssetId::new(xor_id, alice_id);
let transfer = Transfer::asset_quantity(alice_xor_id, 10_u32, bob_id);
let keypair = KeyPair::generate().expect("Failed to generate KeyPair.");
let keypair = KeyPair::generate();
let tx = TransactionBuilder::new(
chain_id.clone(),
AccountId::from_str("alice@wonderland").expect("checked"),
Expand All @@ -53,10 +53,10 @@ async fn measure_block_size_for_n_executors(n_executors: u32) {
let topology = Topology::new(UniqueVec::new());
let mut block = BlockBuilder::new(vec![tx], topology, Vec::new())
.chain(0, &mut wsv)
.sign(&KeyPair::generate().unwrap());
.sign(&KeyPair::generate());

for _ in 1..n_executors {
block = block.sign(&KeyPair::generate().unwrap());
block = block.sign(&KeyPair::generate());
}
let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked);
block_store.create_files_if_they_do_not_exist().unwrap();
Expand Down
Loading

0 comments on commit 0f83d26

Please sign in to comment.