diff --git a/client/src/client.rs b/client/src/client.rs index e9b7ba80130..638c19a5305 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -63,17 +63,17 @@ pub type QueryResult = core::result::Result; /// Trait for signing transactions pub trait Sign { /// Sign transaction with provided key pair. - fn sign(self, key_pair: crate::crypto::KeyPair) -> SignedTransaction; + fn sign(self, key_pair: &crate::crypto::KeyPair) -> SignedTransaction; } impl Sign for TransactionBuilder { - fn sign(self, key_pair: crate::crypto::KeyPair) -> SignedTransaction { + fn sign(self, key_pair: &crate::crypto::KeyPair) -> SignedTransaction { self.sign(key_pair) } } impl Sign for SignedTransaction { - fn sign(self, key_pair: crate::crypto::KeyPair) -> SignedTransaction { + fn sign(self, key_pair: &crate::crypto::KeyPair) -> SignedTransaction { self.sign(key_pair) } } @@ -471,9 +471,7 @@ impl Client { tx_builder.set_nonce(nonce); }; - tx_builder - .with_metadata(metadata) - .sign(self.key_pair.clone()) + tx_builder.with_metadata(metadata).sign(&self.key_pair) } /// Signs transaction @@ -481,7 +479,7 @@ impl Client { /// # Errors /// Fails if signature generation fails pub fn sign_transaction(&self, transaction: Tx) -> SignedTransaction { - transaction.sign(self.key_pair.clone()) + transaction.sign(&self.key_pair) } /// Signs query @@ -489,7 +487,7 @@ impl Client { /// # Errors /// Fails if signature generation fails pub fn sign_query(&self, query: QueryBuilder) -> SignedQuery { - query.sign(self.key_pair.clone()) + query.sign(&self.key_pair) } /// Instructions API entry point. Submits one Iroha Special Instruction to `Iroha` peers. diff --git a/client/tests/integration/asset.rs b/client/tests/integration/asset.rs index 9b4dc11ec24..2758c751962 100644 --- a/client/tests/integration/asset.rs +++ b/client/tests/integration/asset.rs @@ -281,7 +281,7 @@ fn find_rate_and_make_exchange_isi_should_succeed() { let grant_asset_transfer_tx = TransactionBuilder::new(chain_id, asset_id.account_id().clone()) .with_instructions([allow_alice_to_transfer_asset]) - .sign(owner_keypair); + .sign(&owner_keypair); test_client .submit_transaction_blocking(&grant_asset_transfer_tx) diff --git a/client/tests/integration/burn_public_keys.rs b/client/tests/integration/burn_public_keys.rs index 3cbed2c79dc..83552134f18 100644 --- a/client/tests/integration/burn_public_keys.rs +++ b/client/tests/integration/burn_public_keys.rs @@ -18,7 +18,7 @@ fn submit( let tx = if let Some((account_id, keypair)) = submitter { TransactionBuilder::new(chain_id, account_id) .with_instructions(instructions) - .sign(keypair) + .sign(&keypair) } else { let tx = client.build_transaction(instructions, UnlimitedMetadata::default()); client.sign_transaction(tx) diff --git a/client/tests/integration/domain_owner.rs b/client/tests/integration/domain_owner.rs index 46817d1b8ae..da184e675b7 100644 --- a/client/tests/integration/domain_owner.rs +++ b/client/tests/integration/domain_owner.rs @@ -29,7 +29,7 @@ fn domain_owner_domain_permissions() -> Result<()> { // Asset definitions can't be registered by "bob@kingdom" by default let transaction = TransactionBuilder::new(chain_id.clone(), bob_id.clone()) .with_instructions([Register::asset_definition(coin.clone())]) - .sign(bob_keypair.clone()); + .sign(&bob_keypair); let err = test_client .submit_transaction_blocking(&transaction) .expect_err("Tx should fail due to permissions"); @@ -57,7 +57,7 @@ fn domain_owner_domain_permissions() -> Result<()> { test_client.submit_blocking(Grant::permission(token.clone(), bob_id.clone()))?; let transaction = TransactionBuilder::new(chain_id, bob_id.clone()) .with_instructions([Register::asset_definition(coin)]) - .sign(bob_keypair); + .sign(&bob_keypair); test_client.submit_transaction_blocking(&transaction)?; test_client.submit_blocking(Revoke::permission(token, bob_id.clone()))?; @@ -175,7 +175,7 @@ fn domain_owner_asset_definition_permissions() -> Result<()> { let coin = AssetDefinition::quantity(coin_id.clone()); let transaction = TransactionBuilder::new(chain_id, bob_id.clone()) .with_instructions([Register::asset_definition(coin)]) - .sign(bob_keypair); + .sign(&bob_keypair); test_client.submit_transaction_blocking(&transaction)?; // check that "alice@wonderland" as owner of domain can transfer asset definitions in her domain @@ -246,7 +246,7 @@ fn domain_owner_asset_permissions() -> Result<()> { Register::asset_definition(coin), Register::asset_definition(store), ]) - .sign(bob_keypair); + .sign(&bob_keypair); test_client.submit_transaction_blocking(&transaction)?; // check that "alice@wonderland" as owner of domain can register and unregister assets in her domain diff --git a/client/tests/integration/permissions.rs b/client/tests/integration/permissions.rs index 02df38624ac..c6ccc522f29 100644 --- a/client/tests/integration/permissions.rs +++ b/client/tests/integration/permissions.rs @@ -99,7 +99,7 @@ fn permissions_disallow_asset_transfer() { ); let transfer_tx = TransactionBuilder::new(chain_id, mouse_id) .with_instructions([transfer_asset]) - .sign(mouse_keypair); + .sign(&mouse_keypair); let err = iroha_client .submit_transaction_blocking(&transfer_tx) .expect_err("Transaction was not rejected."); @@ -150,7 +150,7 @@ fn permissions_disallow_asset_burn() { ); let burn_tx = TransactionBuilder::new(chain_id, mouse_id) .with_instructions([burn_asset]) - .sign(mouse_keypair); + .sign(&mouse_keypair); let err = iroha_client .submit_transaction_blocking(&burn_tx) @@ -241,7 +241,7 @@ fn permissions_differ_not_only_by_names() { let grant_hats_access_tx = TransactionBuilder::new(chain_id.clone(), mouse_id.clone()) .with_instructions([allow_alice_to_set_key_value_in_hats]) - .sign(mouse_keypair.clone()); + .sign(&mouse_keypair); client .submit_transaction_blocking(&grant_hats_access_tx) .expect("Failed grant permission to modify Mouse's hats"); @@ -277,7 +277,7 @@ fn permissions_differ_not_only_by_names() { let grant_shoes_access_tx = TransactionBuilder::new(chain_id, mouse_id) .with_instructions([allow_alice_to_set_key_value_in_shoes]) - .sign(mouse_keypair); + .sign(&mouse_keypair); client .submit_transaction_blocking(&grant_shoes_access_tx) @@ -328,7 +328,7 @@ fn stored_vs_granted_token_payload() -> Result<()> { let transaction = TransactionBuilder::new(chain_id, mouse_id) .with_instructions([allow_alice_to_set_key_value_in_mouse_asset]) - .sign(mouse_keypair); + .sign(&mouse_keypair); iroha_client .submit_transaction_blocking(&transaction) .expect("Failed to grant permission to alice."); diff --git a/client/tests/integration/roles.rs b/client/tests/integration/roles.rs index 9d84393cffa..a9102dc718d 100644 --- a/client/tests/integration/roles.rs +++ b/client/tests/integration/roles.rs @@ -80,7 +80,7 @@ fn register_and_grant_role_for_metadata_access() -> Result<()> { let grant_role = Grant::role(role_id.clone(), alice_id.clone()); let grant_role_tx = TransactionBuilder::new(chain_id, mouse_id.clone()) .with_instructions([grant_role]) - .sign(mouse_key_pair); + .sign(&mouse_key_pair); test_client.submit_transaction_blocking(&grant_role_tx)?; // Alice modifies Mouse's metadata diff --git a/client/tests/integration/upgrade.rs b/client/tests/integration/upgrade.rs index aad3386b01a..42ee2f623de 100644 --- a/client/tests/integration/upgrade.rs +++ b/client/tests/integration/upgrade.rs @@ -34,7 +34,7 @@ fn executor_upgrade_should_work() -> Result<()> { let transfer_alice_rose = Transfer::asset_quantity(alice_rose, 1_u32, admin_rose); let transfer_rose_tx = TransactionBuilder::new(chain_id.clone(), admin_id.clone()) .with_instructions([transfer_alice_rose.clone()]) - .sign(admin_keypair.clone()); + .sign(&admin_keypair); let _ = client .submit_transaction_blocking(&transfer_rose_tx) .expect_err("Should fail"); @@ -48,7 +48,7 @@ fn executor_upgrade_should_work() -> Result<()> { // Creating new transaction instead of cloning, because we need to update it's creation time let transfer_rose_tx = TransactionBuilder::new(chain_id, admin_id) .with_instructions([transfer_alice_rose]) - .sign(admin_keypair); + .sign(&admin_keypair); client .submit_transaction_blocking(&transfer_rose_tx) .expect("Should succeed"); diff --git a/configs/peer/executor.wasm b/configs/peer/executor.wasm index 3087a631be1..dbac1ecc7cf 100644 Binary files a/configs/peer/executor.wasm and b/configs/peer/executor.wasm differ diff --git a/core/benches/blocks/apply_blocks.rs b/core/benches/blocks/apply_blocks.rs index 112a9229d21..5a2e48b7e82 100644 --- a/core/benches/blocks/apply_blocks.rs +++ b/core/benches/blocks/apply_blocks.rs @@ -40,8 +40,7 @@ impl WsvApplyBlocks { instructions .into_iter() .map(|instructions| { - let block = - create_block(&mut wsv, instructions, account_id.clone(), key_pair.clone()); + let block = create_block(&mut wsv, instructions, account_id.clone(), &key_pair); wsv.apply_without_execution(&block).map(|()| block) }) .collect::, _>>()? diff --git a/core/benches/blocks/common.rs b/core/benches/blocks/common.rs index 1a80c8a9a81..0c2ec273287 100644 --- a/core/benches/blocks/common.rs +++ b/core/benches/blocks/common.rs @@ -25,13 +25,13 @@ pub fn create_block( wsv: &mut WorldStateView, instructions: Vec, account_id: AccountId, - key_pair: KeyPair, + key_pair: &KeyPair, ) -> CommittedBlock { let chain_id = ChainId::new("0"); let transaction = TransactionBuilder::new(chain_id.clone(), account_id) .with_instructions(instructions) - .sign(key_pair.clone()); + .sign(key_pair); let limits = wsv.transaction_executor().transaction_limits; let topology = Topology::new(UniqueVec::new()); diff --git a/core/benches/blocks/validate_blocks.rs b/core/benches/blocks/validate_blocks.rs index c5ce48dee56..9b59b9f7ed7 100644 --- a/core/benches/blocks/validate_blocks.rs +++ b/core/benches/blocks/validate_blocks.rs @@ -69,7 +69,7 @@ impl WsvValidateBlocks { assert_eq!(wsv.height(), 0); for (instructions, i) in instructions.into_iter().zip(1..) { finalized_wsv = wsv.clone(); - let block = create_block(&mut wsv, instructions, account_id.clone(), key_pair.clone()); + let block = create_block(&mut wsv, instructions, account_id.clone(), &key_pair); wsv.apply_without_execution(&block)?; assert_eq!(wsv.height(), i); assert_eq!(wsv.height(), finalized_wsv.height() + 1); diff --git a/core/benches/kura.rs b/core/benches/kura.rs index 9054d5301c7..3c9e9b103e2 100644 --- a/core/benches/kura.rs +++ b/core/benches/kura.rs @@ -32,7 +32,7 @@ async fn measure_block_size_for_n_executors(n_executors: u32) { AccountId::from_str("alice@wonderland").expect("checked"), ) .with_instructions([transfer]) - .sign(keypair.clone()); + .sign(&keypair); let transaction_limits = TransactionLimits { max_instruction_number: 4096, max_wasm_size_bytes: 0, @@ -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().unwrap()); for _ in 1..n_executors { - block = block.sign(KeyPair::generate().unwrap()); + block = block.sign(&KeyPair::generate().unwrap()); } let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked); block_store.create_files_if_they_do_not_exist().unwrap(); diff --git a/core/benches/validation.rs b/core/benches/validation.rs index 044096885f1..970bc80571c 100644 --- a/core/benches/validation.rs +++ b/core/benches/validation.rs @@ -23,7 +23,7 @@ const TRANSACTION_LIMITS: TransactionLimits = TransactionLimits { max_wasm_size_bytes: 0, }; -fn build_test_transaction(keys: KeyPair, chain_id: ChainId) -> SignedTransaction { +fn build_test_transaction(keys: &KeyPair, chain_id: ChainId) -> SignedTransaction { let domain_name = "domain"; let domain_id = DomainId::from_str(domain_name).expect("does not panic"); let create_domain: InstructionBox = Register::domain(Domain::new(domain_id)).into(); @@ -98,7 +98,7 @@ fn accept_transaction(criterion: &mut Criterion) { let chain_id = ChainId::new("0"); let keys = KeyPair::generate().expect("Failed to generate keys"); - let transaction = build_test_transaction(keys, chain_id.clone()); + let transaction = build_test_transaction(&keys, chain_id.clone()); let mut success_count = 0; let mut failures_count = 0; let _ = criterion.bench_function("accept", |b| { @@ -116,14 +116,14 @@ fn sign_transaction(criterion: &mut Criterion) { let chain_id = ChainId::new("0"); let keys = KeyPair::generate().expect("Failed to generate keys"); - let transaction = build_test_transaction(keys, chain_id); + let transaction = build_test_transaction(&keys, chain_id); let key_pair = KeyPair::generate().expect("Failed to generate KeyPair."); let mut count = 0; let _ = criterion.bench_function("sign", |b| { b.iter_batched( || transaction.clone(), |transaction| { - let _: SignedTransaction = transaction.sign(key_pair.clone()); + let _: SignedTransaction = transaction.sign(&key_pair); count += 1; }, BatchSize::SmallInput, @@ -137,7 +137,7 @@ fn validate_transaction(criterion: &mut Criterion) { let keys = KeyPair::generate().expect("Failed to generate keys"); let transaction = AcceptedTransaction::accept( - build_test_transaction(keys.clone(), chain_id.clone()), + build_test_transaction(&keys, chain_id.clone()), &chain_id, &TRANSACTION_LIMITS, ) @@ -163,7 +163,7 @@ fn sign_blocks(criterion: &mut Criterion) { let keys = KeyPair::generate().expect("Failed to generate keys"); let transaction = AcceptedTransaction::accept( - build_test_transaction(keys, chain_id.clone()), + build_test_transaction(&keys, chain_id.clone()), &chain_id, &TRANSACTION_LIMITS, ) @@ -182,7 +182,7 @@ fn sign_blocks(criterion: &mut Criterion) { b.iter_batched( || block.clone(), |block| { - let _: ValidBlock = block.sign(key_pair.clone()); + let _: ValidBlock = block.sign(&key_pair); count += 1; }, BatchSize::SmallInput, diff --git a/core/src/block.rs b/core/src/block.rs index 81908e6a76d..ea695adfb1e 100644 --- a/core/src/block.rs +++ b/core/src/block.rs @@ -216,7 +216,7 @@ mod chained { impl BlockBuilder { /// Sign this block and get [`SignedBlock`]. - pub fn sign(self, key_pair: KeyPair) -> ValidBlock { + pub fn sign(self, key_pair: &KeyPair) -> ValidBlock { let signature = SignatureOf::new(key_pair, &self.0 .0); ValidBlock( @@ -427,7 +427,7 @@ mod valid { /// Add additional signatures for [`Self`]. #[must_use] - pub fn sign(self, key_pair: KeyPair) -> Self { + pub fn sign(self, key_pair: &KeyPair) -> Self { ValidBlock(self.0.sign(key_pair)) } @@ -458,7 +458,7 @@ mod valid { commit_topology: UniqueVec::new(), event_recommendations: Vec::new(), })) - .sign(KeyPair::generate().unwrap()) + .sign(&KeyPair::generate().unwrap()) } /// Check if block's signatures meet requirements for given topology. @@ -532,7 +532,7 @@ mod valid { let payload = block.payload().clone(); key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair.clone(), &payload)) + .map(|key_pair| SignatureOf::new(key_pair, &payload)) .try_for_each(|signature| block.add_signature(signature)) .expect("Failed to add signatures"); @@ -555,7 +555,7 @@ mod valid { key_pairs .iter() .enumerate() - .map(|(_, key_pair)| SignatureOf::new(key_pair.clone(), &payload)) + .map(|(_, key_pair)| SignatureOf::new(key_pair, &payload)) .try_for_each(|signature| block.add_signature(signature)) .expect("Failed to add signatures"); @@ -576,7 +576,7 @@ mod valid { let mut block = ValidBlock::new_dummy(); let payload = block.payload().clone(); - let proxy_tail_signature = SignatureOf::new(key_pairs[4].clone(), &payload); + let proxy_tail_signature = SignatureOf::new(&key_pairs[4], &payload); block .add_signature(proxy_tail_signature) .expect("Failed to add signature"); @@ -608,7 +608,7 @@ mod valid { .iter() .enumerate() .filter(|(i, _)| *i != 4) // Skip proxy tail - .map(|(_, key_pair)| SignatureOf::new(key_pair.clone(), &payload)) + .map(|(_, key_pair)| SignatureOf::new(key_pair, &payload)) .try_for_each(|signature| block.add_signature(signature)) .expect("Failed to add signatures"); @@ -735,7 +735,7 @@ mod tests { let transaction_limits = &wsv.transaction_executor().transaction_limits; let tx = TransactionBuilder::new(chain_id.clone(), alice_id) .with_instructions([create_asset_definition]) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx = AcceptedTransaction::accept(tx, &chain_id, transaction_limits).expect("Valid"); // Creating a block of two identical transactions and validating it @@ -743,7 +743,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology, Vec::new()) .chain(0, &mut wsv) - .sign(alice_keys); + .sign(&alice_keys); // The first transaction should be confirmed assert!(valid_block.payload().transactions[0].error.is_none()); @@ -778,7 +778,7 @@ mod tests { let transaction_limits = &wsv.transaction_executor().transaction_limits; let tx = TransactionBuilder::new(chain_id.clone(), alice_id.clone()) .with_instructions([create_asset_definition]) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx = AcceptedTransaction::accept(tx, &chain_id, transaction_limits).expect("Valid"); let quantity: u32 = 200; @@ -796,12 +796,12 @@ mod tests { let tx0 = TransactionBuilder::new(chain_id.clone(), alice_id.clone()) .with_instructions([fail_mint]) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx0 = AcceptedTransaction::accept(tx0, &chain_id, transaction_limits).expect("Valid"); let tx2 = TransactionBuilder::new(chain_id.clone(), alice_id) .with_instructions([succeed_mint]) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx2 = AcceptedTransaction::accept(tx2, &chain_id, transaction_limits).expect("Valid"); // Creating a block of two identical transactions and validating it @@ -809,7 +809,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology, Vec::new()) .chain(0, &mut wsv) - .sign(alice_keys); + .sign(&alice_keys); // The first transaction should fail assert!(valid_block.payload().transactions[0].error.is_some()); @@ -851,12 +851,12 @@ mod tests { let instructions_accept: [InstructionBox; 2] = [create_domain.into(), create_asset.into()]; let tx_fail = TransactionBuilder::new(chain_id.clone(), alice_id.clone()) .with_instructions(instructions_fail) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx_fail = AcceptedTransaction::accept(tx_fail, &chain_id, transaction_limits).expect("Valid"); let tx_accept = TransactionBuilder::new(chain_id.clone(), alice_id) .with_instructions(instructions_accept) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx_accept = AcceptedTransaction::accept(tx_accept, &chain_id, transaction_limits).expect("Valid"); @@ -865,7 +865,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let valid_block = BlockBuilder::new(transactions, topology, Vec::new()) .chain(0, &mut wsv) - .sign(alice_keys); + .sign(&alice_keys); // The first transaction should be rejected assert!( diff --git a/core/src/queue.rs b/core/src/queue.rs index d55bd2eb94d..3da000e068d 100644 --- a/core/src/queue.rs +++ b/core/src/queue.rs @@ -391,7 +391,7 @@ mod tests { wsv::World, PeersIds, }; - fn accepted_tx(account_id: &str, key: KeyPair) -> AcceptedTransaction { + fn accepted_tx(account_id: &str, key: &KeyPair) -> AcceptedTransaction { let chain_id = ChainId::new("0"); let message = std::iter::repeat_with(rand::random::) @@ -442,7 +442,7 @@ mod tests { }); queue - .push(accepted_tx("alice@wonderland", key_pair), &wsv) + .push(accepted_tx("alice@wonderland", &key_pair), &wsv) .expect("Failed to push tx into queue"); } @@ -469,13 +469,13 @@ mod tests { for _ in 0..max_txs_in_queue { queue - .push(accepted_tx("alice@wonderland", key_pair.clone()), &wsv) + .push(accepted_tx("alice@wonderland", &key_pair), &wsv) .expect("Failed to push tx into queue"); thread::sleep(Duration::from_millis(10)); } assert!(matches!( - queue.push(accepted_tx("alice@wonderland", key_pair), &wsv), + queue.push(accepted_tx("alice@wonderland", &key_pair), &wsv), Err(Failure { err: Error::Full, .. @@ -525,9 +525,9 @@ mod tests { max_wasm_size_bytes: 0, }; let fully_signed_tx: AcceptedTransaction = { - let mut signed_tx = tx.clone().sign(key_pairs[0].clone()); + let mut signed_tx = tx.clone().sign(&key_pairs[0]); for key_pair in &key_pairs[1..] { - signed_tx = signed_tx.sign(key_pair.clone()); + signed_tx = signed_tx.sign(key_pair); } AcceptedTransaction::accept(signed_tx, &chain_id, &tx_limits) .expect("Failed to accept Transaction.") @@ -539,7 +539,7 @@ mod tests { )); let get_tx = |key_pair| { - AcceptedTransaction::accept(tx.clone().sign(key_pair), &chain_id, &tx_limits) + AcceptedTransaction::accept(tx.clone().sign(&key_pair), &chain_id, &tx_limits) .expect("Failed to accept Transaction.") }; for key_pair in key_pairs { @@ -587,7 +587,7 @@ mod tests { }); for _ in 0..5 { queue - .push(accepted_tx("alice@wonderland", alice_key.clone()), &wsv) + .push(accepted_tx("alice@wonderland", &alice_key), &wsv) .expect("Failed to push tx into queue"); thread::sleep(Duration::from_millis(10)); } @@ -606,7 +606,7 @@ mod tests { kura, query_handle, ); - let tx = accepted_tx("alice@wonderland", alice_key); + let tx = accepted_tx("alice@wonderland", &alice_key); wsv.transactions.insert(tx.hash(), 1); let queue = Queue::from_configuration(&Configuration { transaction_time_to_live_ms: 100_000, @@ -636,7 +636,7 @@ mod tests { kura, query_handle, ); - let tx = accepted_tx("alice@wonderland", alice_key); + let tx = accepted_tx("alice@wonderland", &alice_key); let queue = Queue::from_configuration(&Configuration { transaction_time_to_live_ms: 100_000, max_transactions_in_queue: 100, @@ -675,13 +675,13 @@ mod tests { }); for _ in 0..(max_txs_in_block - 1) { queue - .push(accepted_tx("alice@wonderland", alice_key.clone()), &wsv) + .push(accepted_tx("alice@wonderland", &alice_key), &wsv) .expect("Failed to push tx into queue"); thread::sleep(Duration::from_millis(100)); } queue - .push(accepted_tx("alice@wonderland", alice_key.clone()), &wsv) + .push(accepted_tx("alice@wonderland", &alice_key), &wsv) .expect("Failed to push tx into queue"); std::thread::sleep(Duration::from_millis(101)); assert_eq!( @@ -692,7 +692,7 @@ mod tests { ); queue - .push(accepted_tx("alice@wonderland", alice_key), &wsv) + .push(accepted_tx("alice@wonderland", &alice_key), &wsv) .expect("Failed to push tx into queue"); std::thread::sleep(Duration::from_millis(210)); assert_eq!( @@ -724,7 +724,7 @@ mod tests { .expect("Default queue config should always build") }); queue - .push(accepted_tx("alice@wonderland", alice_key), &wsv) + .push(accepted_tx("alice@wonderland", &alice_key), &wsv) .expect("Failed to push tx into queue"); let a = queue @@ -771,7 +771,7 @@ mod tests { .with_instructions(instructions); tx.set_ttl(Duration::from_millis(10)); let now = std::time::Instant::now(); - let tx = tx.sign(alice_key); + let tx = tx.sign(&alice_key); println!("Signing time: {}ms", now.elapsed().as_millis()); let limits = TransactionLimits { max_instruction_number: 4096, @@ -821,7 +821,7 @@ mod tests { // Spawn a thread where we push transactions thread::spawn(move || { while start_time.elapsed() < run_for { - let tx = accepted_tx("alice@wonderland", alice_key.clone()); + let tx = accepted_tx("alice@wonderland", &alice_key); match queue_arc_clone.push(tx, &wsv_clone) { Ok(()) | Err(Failure { @@ -885,7 +885,7 @@ mod tests { .expect("Default queue config should always build") }); - let tx = accepted_tx(alice_id, alice_key.clone()); + let tx = accepted_tx(alice_id, &alice_key); assert!(queue.push(tx.clone(), &wsv).is_ok()); // create the same tx but with timestamp in the future let tx = { @@ -898,7 +898,7 @@ mod tests { new_tx.set_creation_time(tx.0.payload().creation_time_ms + 2 * future_threshold_ms); - let new_tx = new_tx.sign(alice_key); + let new_tx = new_tx.sign(&alice_key); let limits = TransactionLimits { max_instruction_number: 4096, max_wasm_size_bytes: 0, @@ -952,17 +952,11 @@ mod tests { // First push by Alice should be fine queue - .push( - accepted_tx("alice@wonderland", alice_key_pair.clone()), - &wsv, - ) + .push(accepted_tx("alice@wonderland", &alice_key_pair), &wsv) .expect("Failed to push tx into queue"); // Second push by Alice excide limit and will be rejected - let result = queue.push( - accepted_tx("alice@wonderland", alice_key_pair.clone()), - &wsv, - ); + let result = queue.push(accepted_tx("alice@wonderland", &alice_key_pair), &wsv); assert!( matches!( result, @@ -976,7 +970,7 @@ mod tests { // First push by Bob should be fine despite previous Alice error queue - .push(accepted_tx("bob@wonderland", bob_key_pair.clone()), &wsv) + .push(accepted_tx("bob@wonderland", &bob_key_pair), &wsv) .expect("Failed to push tx into queue"); let transactions = queue.collect_transactions_for_block(&wsv, 10); @@ -991,11 +985,11 @@ mod tests { // After cleanup Alice and Bob pushes should work fine queue - .push(accepted_tx("alice@wonderland", alice_key_pair), &wsv) + .push(accepted_tx("alice@wonderland", &alice_key_pair), &wsv) .expect("Failed to push tx into queue"); queue - .push(accepted_tx("bob@wonderland", bob_key_pair), &wsv) + .push(accepted_tx("bob@wonderland", &bob_key_pair), &wsv) .expect("Failed to push tx into queue"); } } diff --git a/core/src/smartcontracts/isi/query.rs b/core/src/smartcontracts/isi/query.rs index c1c2c082cc4..ca8d4a90f35 100644 --- a/core/src/smartcontracts/isi/query.rs +++ b/core/src/smartcontracts/isi/query.rs @@ -270,14 +270,14 @@ mod tests { let instructions: [InstructionBox; 0] = []; let tx = TransactionBuilder::new(chain_id.clone(), ALICE_ID.clone()) .with_instructions(instructions) - .sign(ALICE_KEYS.clone()); + .sign(&ALICE_KEYS); AcceptedTransaction::accept(tx, &chain_id, &limits)? }; let invalid_tx = { let isi = Fail::new("fail".to_owned()); let tx = TransactionBuilder::new(chain_id.clone(), ALICE_ID.clone()) .with_instructions([isi.clone(), isi]) - .sign(ALICE_KEYS.clone()); + .sign(&ALICE_KEYS); AcceptedTransaction::accept(tx, &chain_id, &huge_limits)? }; @@ -287,7 +287,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let first_block = BlockBuilder::new(transactions.clone(), topology.clone(), Vec::new()) .chain(0, &mut wsv) - .sign(ALICE_KEYS.clone()) + .sign(&ALICE_KEYS) .commit(&topology) .expect("Block is valid"); @@ -297,7 +297,7 @@ mod tests { for _ in 1u64..blocks { let block = BlockBuilder::new(transactions.clone(), topology.clone(), Vec::new()) .chain(0, &mut wsv) - .sign(ALICE_KEYS.clone()) + .sign(&ALICE_KEYS) .commit(&topology) .expect("Block is valid"); @@ -420,7 +420,7 @@ mod tests { let instructions: [InstructionBox; 0] = []; let tx = TransactionBuilder::new(chain_id.clone(), ALICE_ID.clone()) .with_instructions(instructions) - .sign(ALICE_KEYS.clone()); + .sign(&ALICE_KEYS); let tx_limits = &wsv.transaction_executor().transaction_limits; let va_tx = AcceptedTransaction::accept(tx, &chain_id, tx_limits)?; @@ -428,7 +428,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let vcb = BlockBuilder::new(vec![va_tx.clone()], topology.clone(), Vec::new()) .chain(0, &mut wsv) - .sign(ALICE_KEYS.clone()) + .sign(&ALICE_KEYS) .commit(&topology) .expect("Block is valid"); @@ -437,7 +437,7 @@ mod tests { let unapplied_tx = TransactionBuilder::new(chain_id, ALICE_ID.clone()) .with_instructions([Unregister::account("account@domain".parse().unwrap())]) - .sign(ALICE_KEYS.clone()); + .sign(&ALICE_KEYS); let wrong_hash = unapplied_tx.hash(); let not_found = FindTransactionByHash::new(wrong_hash).execute(&wsv); assert!(matches!( diff --git a/core/src/sumeragi/main_loop.rs b/core/src/sumeragi/main_loop.rs index 8f63901f980..7007948b92e 100644 --- a/core/src/sumeragi/main_loop.rs +++ b/core/src/sumeragi/main_loop.rs @@ -292,7 +292,7 @@ impl Sumeragi { let mut new_wsv = self.wsv.clone(); let genesis = BlockBuilder::new(transactions, self.current_topology.clone(), vec![]) .chain(0, &mut new_wsv) - .sign(self.key_pair.clone()); + .sign(&self.key_pair); let genesis_msg = BlockCreated::from(genesis.clone()).into(); @@ -422,7 +422,7 @@ impl Sumeragi { } }; - let signed_block = block.sign(self.key_pair.clone()); + let signed_block = block.sign(&self.key_pair); Some(VotingBlock::new(signed_block, new_wsv)) } @@ -648,7 +648,7 @@ impl Sumeragi { event_recommendations, ) .chain(current_view_change_index, &mut new_wsv) - .sign(self.key_pair.clone()); + .sign(&self.key_pair); if let Some(current_topology) = current_topology.is_consensus_required() { info!(%addr, block_payload_hash=%new_block.payload().hash(), "Block created"); @@ -927,7 +927,7 @@ pub(crate) fn run( let suspect_proof = ProofBuilder::new(sumeragi.wsv.latest_block_hash(), current_view_change_index) - .sign(sumeragi.key_pair.clone()); + .sign(&sumeragi.key_pair); view_change_proof_chain .insert_proof( @@ -1182,7 +1182,7 @@ mod tests { fn create_data_for_test( chain_id: &ChainId, topology: &Topology, - leader_key_pair: KeyPair, + leader_key_pair: &KeyPair, ) -> (WorldStateView, Arc, SignedBlock) { // Predefined world state let alice_id: AccountId = "alice@wonderland".parse().expect("Valid"); @@ -1204,7 +1204,7 @@ mod tests { // Making two transactions that have the same instruction let tx = TransactionBuilder::new(chain_id.clone(), alice_id.clone()) .with_instructions([fail_box]) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx = AcceptedTransaction::accept( tx, chain_id, @@ -1215,7 +1215,7 @@ mod tests { // Creating a block of two identical transactions and validating it let block = BlockBuilder::new(vec![tx.clone(), tx], topology.clone(), Vec::new()) .chain(0, &mut wsv) - .sign(leader_key_pair.clone()); + .sign(leader_key_pair); let genesis = block.commit(topology).expect("Block is valid"); wsv.apply(&genesis).expect("Failed to apply block"); @@ -1231,7 +1231,7 @@ mod tests { let tx1 = TransactionBuilder::new(chain_id.clone(), alice_id.clone()) .with_instructions([create_asset_definition1]) - .sign(alice_keys.clone()); + .sign(&alice_keys); let tx1 = AcceptedTransaction::accept( tx1, chain_id, @@ -1241,7 +1241,7 @@ mod tests { .expect("Valid"); let tx2 = TransactionBuilder::new(chain_id.clone(), alice_id) .with_instructions([create_asset_definition2]) - .sign(alice_keys); + .sign(&alice_keys); let tx2 = AcceptedTransaction::accept( tx2, chain_id, @@ -1269,7 +1269,7 @@ mod tests { leader_key_pair.public_key().clone(), )]); let (finalized_wsv, _, mut block) = - create_data_for_test(&chain_id, &topology, leader_key_pair); + create_data_for_test(&chain_id, &topology, &leader_key_pair); let wsv = finalized_wsv.clone(); // Malform block to make it invalid @@ -1289,7 +1289,7 @@ mod tests { leader_key_pair.public_key().clone(), )]); let (finalized_wsv, kura, mut block) = - create_data_for_test(&chain_id, &topology, leader_key_pair); + create_data_for_test(&chain_id, &topology, &leader_key_pair); let mut wsv = finalized_wsv.clone(); let validated_block = @@ -1317,7 +1317,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let leader_key_pair = KeyPair::generate().unwrap(); let (finalized_wsv, _, mut block) = - create_data_for_test(&chain_id, &topology, leader_key_pair); + create_data_for_test(&chain_id, &topology, &leader_key_pair); let wsv = finalized_wsv.clone(); // Change block height @@ -1346,7 +1346,8 @@ mod tests { "127.0.0.1:8080".parse().unwrap(), leader_key_pair.public_key().clone(), )]); - let (finalized_wsv, _, block) = create_data_for_test(&chain_id, &topology, leader_key_pair); + let (finalized_wsv, _, block) = + create_data_for_test(&chain_id, &topology, &leader_key_pair); let wsv = finalized_wsv.clone(); let result = handle_block_sync(&chain_id, block, &wsv, &finalized_wsv); assert!(matches!(result, Ok(BlockSyncOk::CommitBlock(_, _)))) @@ -1362,7 +1363,7 @@ mod tests { leader_key_pair.public_key().clone(), )]); let (finalized_wsv, kura, mut block) = - create_data_for_test(&chain_id, &topology, leader_key_pair); + create_data_for_test(&chain_id, &topology, &leader_key_pair); let mut wsv = finalized_wsv.clone(); let validated_block = @@ -1390,7 +1391,7 @@ mod tests { leader_key_pair.public_key().clone(), )]); let (finalized_wsv, kura, mut block) = - create_data_for_test(&chain_id, &topology, leader_key_pair); + create_data_for_test(&chain_id, &topology, &leader_key_pair); let mut wsv = finalized_wsv.clone(); // Increase block view change index @@ -1428,7 +1429,7 @@ mod tests { let topology = Topology::new(UniqueVec::new()); let leader_key_pair = KeyPair::generate().unwrap(); let (finalized_wsv, _, mut block) = - create_data_for_test(&chain_id, &topology, leader_key_pair); + create_data_for_test(&chain_id, &topology, &leader_key_pair); let wsv = finalized_wsv.clone(); // Change block height and view change index diff --git a/core/src/sumeragi/network_topology.rs b/core/src/sumeragi/network_topology.rs index 1d5a0b57665..eb7c10a16df 100644 --- a/core/src/sumeragi/network_topology.rs +++ b/core/src/sumeragi/network_topology.rs @@ -344,7 +344,7 @@ mod tests { let dummy = "value to sign"; let signatures = key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair.clone(), &dummy)) + .map(|key_pair| SignatureOf::new(key_pair, &dummy)) .collect::>>(); let leader_signatures = @@ -386,7 +386,7 @@ mod tests { let dummy = "value to sign"; let signatures = key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair.clone(), &dummy)) + .map(|key_pair| SignatureOf::new(key_pair, &dummy)) .collect::>>(); let leader_signatures = @@ -419,7 +419,7 @@ mod tests { let dummy = "value to sign"; let signatures = key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair.clone(), &dummy)) + .map(|key_pair| SignatureOf::new(key_pair, &dummy)) .collect::>>(); let leader_signatures = @@ -453,7 +453,7 @@ mod tests { let dummy = "value to sign"; let signatures = key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair.clone(), &dummy)) + .map(|key_pair| SignatureOf::new(key_pair, &dummy)) .collect::>>(); let leader_signatures = @@ -488,7 +488,7 @@ mod tests { let dummy = "value to sign"; let signatures = key_pairs .iter() - .map(|key_pair| SignatureOf::new(key_pair.clone(), &dummy)) + .map(|key_pair| SignatureOf::new(key_pair, &dummy)) .collect::>>(); let leader_signatures = diff --git a/core/src/sumeragi/view_change.rs b/core/src/sumeragi/view_change.rs index ef9dbd891e0..9a24f0ece33 100644 --- a/core/src/sumeragi/view_change.rs +++ b/core/src/sumeragi/view_change.rs @@ -54,7 +54,7 @@ impl ProofBuilder { } /// Sign this message with the peer's public and private key. - pub fn sign(mut self, key_pair: KeyPair) -> SignedProof { + pub fn sign(mut self, key_pair: &KeyPair) -> SignedProof { let signature = SignatureOf::new(key_pair, &self.0.payload); self.0.signatures.insert(signature); self.0 diff --git a/crypto/src/signature/mod.rs b/crypto/src/signature/mod.rs index c26fbaef23e..acdf50d12f8 100644 --- a/crypto/src/signature/mod.rs +++ b/crypto/src/signature/mod.rs @@ -56,17 +56,15 @@ impl Signature { /// /// # Errors /// Fails if signing fails - pub fn new(key_pair: KeyPair, payload: &[u8]) -> Self { - let (public_key, private_key) = key_pair.into(); - - let signature = match private_key { - crate::PrivateKey::Ed25519(sk) => ed25519::Ed25519Sha512::sign(payload, &sk), - crate::PrivateKey::Secp256k1(sk) => secp256k1::EcdsaSecp256k1Sha256::sign(payload, &sk), - crate::PrivateKey::BlsSmall(sk) => bls::BlsSmall::sign(payload, &sk), - crate::PrivateKey::BlsNormal(sk) => bls::BlsNormal::sign(payload, &sk), + pub fn new(key_pair: &KeyPair, payload: &[u8]) -> Self { + let signature = match &key_pair.private_key { + crate::PrivateKey::Ed25519(sk) => ed25519::Ed25519Sha512::sign(payload, sk), + crate::PrivateKey::Secp256k1(sk) => secp256k1::EcdsaSecp256k1Sha256::sign(payload, sk), + crate::PrivateKey::BlsSmall(sk) => bls::BlsSmall::sign(payload, sk), + crate::PrivateKey::BlsNormal(sk) => bls::BlsNormal::sign(payload, sk), }; Self { - public_key, + public_key: key_pair.public_key.clone(), payload: ConstVec::new(signature), } } @@ -197,7 +195,7 @@ impl SignatureOf { /// # Errors /// Fails if signing fails #[inline] - fn from_hash(key_pair: KeyPair, hash: HashOf) -> Self { + fn from_hash(key_pair: &KeyPair, hash: HashOf) -> Self { Self(Signature::new(key_pair, hash.as_ref()), PhantomData) } @@ -219,7 +217,7 @@ impl SignatureOf { /// # Errors /// Fails if signing fails #[inline] - pub fn new(key_pair: KeyPair, value: &T) -> Self { + pub fn new(key_pair: &KeyPair, value: &T) -> Self { Self::from_hash(key_pair, HashOf::new(value)) } @@ -472,7 +470,7 @@ impl SignaturesOf { /// # Errors /// Forwards [`SignatureOf::new`] errors #[inline] - pub fn new(key_pair: KeyPair, value: &T) -> Self { + pub fn new(key_pair: &KeyPair, value: &T) -> Self { SignatureOf::new(key_pair, value).into() } @@ -533,7 +531,7 @@ mod tests { ) .expect("Failed to generate key pair."); let message = b"Test message to sign."; - let signature = Signature::new(key_pair.clone(), message); + let signature = Signature::new(&key_pair, message); assert!(*signature.public_key() == *key_pair.public_key()); signature.verify(message).unwrap(); } @@ -546,7 +544,7 @@ mod tests { ) .expect("Failed to generate key pair."); let message = b"Test message to sign."; - let signature = Signature::new(key_pair.clone(), message); + let signature = Signature::new(&key_pair, message); assert!(*signature.public_key() == *key_pair.public_key()); signature.verify(message).unwrap(); } @@ -559,7 +557,7 @@ mod tests { ) .expect("Failed to generate key pair."); let message = b"Test message to sign."; - let signature = Signature::new(key_pair.clone(), message); + let signature = Signature::new(&key_pair, message); assert!(*signature.public_key() == *key_pair.public_key()); signature.verify(message).unwrap(); } @@ -572,7 +570,7 @@ mod tests { ) .expect("Failed to generate key pair."); let message = b"Test message to sign."; - let signature = Signature::new(key_pair.clone(), message); + let signature = Signature::new(&key_pair, message); assert!(*signature.public_key() == *key_pair.public_key()); signature.verify(message).unwrap(); } @@ -582,9 +580,9 @@ mod tests { fn signatures_of_deduplication_by_public_key() { let key_pair = KeyPair::generate().expect("Failed to generate keys"); let signatures = [ - SignatureOf::new(key_pair.clone(), &1), - SignatureOf::new(key_pair.clone(), &2), - SignatureOf::new(key_pair, &3), + SignatureOf::new(&key_pair, &1), + SignatureOf::new(&key_pair, &2), + SignatureOf::new(&key_pair, &3), ] .into_iter() .collect::>(); @@ -605,7 +603,7 @@ mod tests { .flat_map(|key| { core::iter::repeat_with(move || key.clone()) .zip(0..) - .map(|(key, i)| SignatureOf::new(key, &i)) + .map(|(key, i)| SignatureOf::new(&key, &i)) .take(signatures_per_key) }) .map(SignatureWrapperOf) diff --git a/data_model/src/block.rs b/data_model/src/block.rs index fc184015382..24d8a470e6e 100644 --- a/data_model/src/block.rs +++ b/data_model/src/block.rs @@ -201,7 +201,7 @@ impl SignedBlock { #[cfg(feature = "std")] #[cfg(feature = "transparent_api")] #[must_use] - pub fn sign(mut self, key_pair: KeyPair) -> Self { + pub fn sign(mut self, key_pair: &KeyPair) -> Self { let signature = iroha_crypto::SignatureOf::new(key_pair, self.payload()); let SignedBlock::V1(block) = &mut self; block.signatures.insert(signature); diff --git a/data_model/src/query/mod.rs b/data_model/src/query/mod.rs index 915ca0dd026..a28e2c59a20 100644 --- a/data_model/src/query/mod.rs +++ b/data_model/src/query/mod.rs @@ -1231,7 +1231,7 @@ pub mod http { /// # Errors /// Fails if signature creation fails. #[inline] - pub fn sign(self, key_pair: iroha_crypto::KeyPair) -> SignedQuery { + pub fn sign(self, key_pair: &iroha_crypto::KeyPair) -> SignedQuery { SignedQueryV1 { signature: SignatureOf::new(key_pair, &self.payload), payload: self.payload, diff --git a/data_model/src/transaction.rs b/data_model/src/transaction.rs index ab8b6f77e77..649fb9838b4 100644 --- a/data_model/src/transaction.rs +++ b/data_model/src/transaction.rs @@ -285,7 +285,7 @@ impl SignedTransaction { /// Fails if signature creation fails #[cfg(feature = "std")] #[must_use] - pub fn sign(self, key_pair: iroha_crypto::KeyPair) -> SignedTransaction { + pub fn sign(self, key_pair: &iroha_crypto::KeyPair) -> SignedTransaction { let SignedTransaction::V1(mut tx) = self; let signature = iroha_crypto::SignatureOf::new(key_pair, &tx.payload); tx.signatures.insert(signature); @@ -748,7 +748,7 @@ mod http { /// /// Fails if signature creation fails #[cfg(feature = "std")] - pub fn sign(self, key_pair: iroha_crypto::KeyPair) -> SignedTransaction { + pub fn sign(self, key_pair: &iroha_crypto::KeyPair) -> SignedTransaction { let signatures = SignaturesOf::new(key_pair, &self.payload); SignedTransactionV1 { diff --git a/genesis/src/lib.rs b/genesis/src/lib.rs index 811f608687f..e4861426467 100644 --- a/genesis/src/lib.rs +++ b/genesis/src/lib.rs @@ -63,13 +63,7 @@ impl GenesisNetwork { .chain(raw_block.transactions); let transactions = transactions_iter - .map(|raw_transaction| { - raw_transaction - // FIXME: fix underlying chain of `.sign` so that it doesn't - // consume the key pair unnecessarily. It might be costly to clone - // the key pair for a large genesis. - .sign(chain_id.clone(), genesis_key_pair.clone()) - }) + .map(|raw_transaction| raw_transaction.sign(chain_id.clone(), genesis_key_pair)) .map(GenesisTransaction) .collect(); @@ -191,7 +185,7 @@ impl GenesisTransactionBuilder { /// /// # Errors /// Fails if signing or accepting fails. - fn sign(self, chain_id: ChainId, genesis_key_pair: KeyPair) -> SignedTransaction { + fn sign(self, chain_id: ChainId, genesis_key_pair: &KeyPair) -> SignedTransaction { TransactionBuilder::new(chain_id, GENESIS_ACCOUNT_ID.clone()) .with_instructions(self.isi) .sign(genesis_key_pair) diff --git a/p2p/src/peer.rs b/p2p/src/peer.rs index 1f12dc44d72..b7720d5661a 100644 --- a/p2p/src/peer.rs +++ b/p2p/src/peer.rs @@ -513,7 +513,7 @@ mod state { let write_half = &mut connection.write; let payload = create_payload(&kx_local_pk, &kx_remote_pk); - let signature = Signature::new(key_pair, &payload); + let signature = Signature::new(&key_pair, &payload); let data = signature.encode(); let data = &cryptographer.encrypt(data.as_slice())?;