diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 4576b942532..bc71f8f4027 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -812,7 +812,7 @@ mod tests { async fn iroha_should_notify_on_panic() { let notify = Arc::new(Notify::new()); let hook = panic::take_hook(); - >::prepare_panic_hook(Arc::clone(¬ify)); + crate::Iroha::prepare_panic_hook(Arc::clone(¬ify)); let waiters: Vec<_> = repeat(()).take(10).map(|_| Arc::clone(¬ify)).collect(); let handles: Vec<_> = waiters.iter().map(|waiter| waiter.notified()).collect(); thread::spawn(move || { @@ -837,7 +837,7 @@ mod tests { let mut table = toml::Table::new(); iroha_config::base::toml::Writer::new(&mut table) - .write("chain_id", "0") + .write("chain", "0") .write("public_key", pubkey) .write("private_key", ExposedPrivateKey(privkey)) .write(["network", "address"], socket_addr!(127.0.0.1:1337)) diff --git a/config/base/tests/misc.rs b/config/base/tests/misc.rs index 3af08ca8609..110a1e383ae 100644 --- a/config/base/tests/misc.rs +++ b/config/base/tests/misc.rs @@ -72,7 +72,7 @@ pub mod sample_config { .finish_with_origin(); let max_content_len = reader - .read_parameter::(["max_content_length"]) + .read_parameter::(["max_content_len"]) .value_or_else(|| 1024) .finish(); diff --git a/core/src/block.rs b/core/src/block.rs index 19d2fd2487e..03db0f4cc06 100644 --- a/core/src/block.rs +++ b/core/src/block.rs @@ -615,12 +615,15 @@ mod valid { } #[cfg(test)] - pub(crate) fn new_dummy() -> Self { - Self::new_dummy_and_modify_payload(|_| {}) + pub(crate) fn new_dummy(leader_private_key: &PrivateKey) -> Self { + Self::new_dummy_and_modify_payload(leader_private_key, |_| {}) } #[cfg(test)] - pub(crate) fn new_dummy_and_modify_payload(f: impl FnOnce(&mut BlockPayload)) -> Self { + pub(crate) fn new_dummy_and_modify_payload( + leader_private_key: &PrivateKey, + f: impl FnOnce(&mut BlockPayload), + ) -> Self { let mut payload = BlockPayload { header: BlockHeader { height: 2, @@ -638,7 +641,7 @@ mod valid { }; f(&mut payload); BlockBuilder(Chained(payload)) - .sign(iroha_crypto::KeyPair::random().private_key()) + .sign(leader_private_key) .unpack(|_| {}) } } @@ -671,17 +674,25 @@ mod valid { let peers = test_peers![0, 1, 2, 3, 4, 5, 6: key_pairs_iter]; let topology = Topology::new(peers); - let mut block = ValidBlock::new_dummy(); + let mut block = ValidBlock::new_dummy(key_pairs[0].private_key()); let payload = block.0.payload().clone(); key_pairs .iter() - .map(|key_pair| { - BlockSignature(0, SignatureOf::new(key_pair.private_key(), &payload)) + .enumerate() + // Include only peers in validator set + .take(topology.min_votes_for_commit()) + // Skip leader since already singed + .skip(1) + .filter(|(i, _)| *i != 4) // Skip proxy tail + .map(|(i, key_pair)| { + BlockSignature(i as u64, SignatureOf::new(key_pair.private_key(), &payload)) }) .try_for_each(|signature| block.add_signature(signature, &topology)) .expect("Failed to add signatures"); - assert!(block.commit(&topology).unpack(|_| {}).is_ok()); + block.sign(&key_pairs[4], &topology); + + let _ = block.commit(&topology).unpack(|_| {}).unwrap(); } #[test] @@ -693,15 +704,7 @@ mod valid { let peers = test_peers![0,: key_pairs_iter]; let topology = Topology::new(peers); - let mut block = ValidBlock::new_dummy(); - let payload = block.0.payload().clone(); - key_pairs - .iter() - .map(|key_pair| { - BlockSignature(0, SignatureOf::new(key_pair.private_key(), &payload)) - }) - .try_for_each(|signature| block.add_signature(signature, &topology)) - .expect("Failed to add signatures"); + let block = ValidBlock::new_dummy(key_pairs[0].private_key()); assert!(block.commit(&topology).unpack(|_| {}).is_ok()); } @@ -716,18 +719,13 @@ mod valid { let peers = test_peers![0, 1, 2, 3, 4, 5, 6: key_pairs_iter]; let topology = Topology::new(peers); - let mut block = ValidBlock::new_dummy(); - let payload = block.0.payload().clone(); - let proxy_tail_signature = - BlockSignature(0, SignatureOf::new(key_pairs[4].private_key(), &payload)); - block - .add_signature(proxy_tail_signature, &topology) - .expect("Failed to add signature"); + let mut block = ValidBlock::new_dummy(key_pairs[0].private_key()); + block.sign(&key_pairs[4], &topology); assert_eq!( block.commit(&topology).unpack(|_| {}).unwrap_err().1, SignatureVerificationError::NotEnoughSignatures { - votes_count: 1, + votes_count: 2, min_votes_for_commit: topology.min_votes_for_commit(), } .into() @@ -744,14 +742,18 @@ mod valid { let peers = test_peers![0, 1, 2, 3, 4, 5, 6: key_pairs_iter]; let topology = Topology::new(peers); - let mut block = ValidBlock::new_dummy(); + let mut block = ValidBlock::new_dummy(key_pairs[0].private_key()); let payload = block.0.payload().clone(); key_pairs .iter() .enumerate() + // Include only peers in validator set + .take(topology.min_votes_for_commit()) + // Skip leader since already singed + .skip(1) .filter(|(i, _)| *i != 4) // Skip proxy tail - .map(|(_, key_pair)| { - BlockSignature(0, SignatureOf::new(key_pair.private_key(), &payload)) + .map(|(i, key_pair)| { + BlockSignature(i as u64, SignatureOf::new(key_pair.private_key(), &payload)) }) .try_for_each(|signature| block.add_signature(signature, &topology)) .expect("Failed to add signatures"); @@ -916,15 +918,18 @@ mod tests { use super::*; use crate::{ kura::Kura, query::store::LiveQueryStore, smartcontracts::isi::Registrable as _, - state::State, tx::SignatureVerificationFail, + state::State, }; #[test] pub fn committed_and_valid_block_hashes_are_equal() { - let valid_block = ValidBlock::new_dummy(); - let (peer_public_key, _) = KeyPair::random().into_parts(); - let peer_id = PeerId::new("127.0.0.1:8080".parse().unwrap(), peer_public_key); + let peer_key_pair = KeyPair::random(); + let peer_id = PeerId::new( + "127.0.0.1:8080".parse().unwrap(), + peer_key_pair.public_key().clone(), + ); let topology = Topology::new(vec![peer_id]); + let valid_block = ValidBlock::new_dummy(peer_key_pair.private_key()); let committed_block = valid_block .clone() .commit(&topology) @@ -1176,15 +1181,11 @@ mod tests { // Create genesis transaction // Sign with `genesis_wrong_key` as peer which has incorrect genesis key pair + // Bypass `accept_genesis` check to allow signing with wrong key let tx = TransactionBuilder::new(chain_id.clone(), genesis_wrong_account_id.clone()) .with_instructions([isi]) .sign(genesis_wrong_key.private_key()); - let tx = AcceptedTransaction::accept_genesis( - iroha_genesis::GenesisTransaction(tx), - &chain_id, - &SAMPLE_GENESIS_ACCOUNT_ID, - ) - .expect("Valid"); + let tx = AcceptedTransaction(tx); // Create genesis block let transactions = vec![tx]; @@ -1213,8 +1214,8 @@ mod tests { assert!(matches!( error, BlockValidationError::TransactionValidation(TransactionValidationError::Accept( - AcceptTransactionFail::SignatureVerification(SignatureVerificationFail { reason, .. }) - )) if reason == "Signature doesn't correspond to genesis public key" - )); + AcceptTransactionFail::UnexpectedGenesisAccountSignature + )) + )) } } diff --git a/core/src/kura.rs b/core/src/kura.rs index d1335978d64..463aebef02a 100644 --- a/core/src/kura.rs +++ b/core/src/kura.rs @@ -831,6 +831,7 @@ impl AddErrContextExt for Result { #[cfg(test)] mod tests { + use iroha_crypto::KeyPair; use tempfile::TempDir; use super::*; @@ -934,7 +935,7 @@ mod tests { let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked); block_store.create_files_if_they_do_not_exist().unwrap(); - let dummy_block = ValidBlock::new_dummy().into(); + let dummy_block = ValidBlock::new_dummy(KeyPair::random().private_key()).into(); let append_count = 35; for _ in 0..append_count { @@ -950,7 +951,7 @@ mod tests { let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked); block_store.create_files_if_they_do_not_exist().unwrap(); - let dummy_block = ValidBlock::new_dummy().into(); + let dummy_block = ValidBlock::new_dummy(KeyPair::random().private_key()).into(); let append_count = 35; for _ in 0..append_count { @@ -966,7 +967,7 @@ mod tests { let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked); block_store.create_files_if_they_do_not_exist().unwrap(); - let dummy_block = ValidBlock::new_dummy().into(); + let dummy_block = ValidBlock::new_dummy(KeyPair::random().private_key()).into(); let append_count = 35; for _ in 0..append_count { @@ -986,7 +987,7 @@ mod tests { let mut block_store = BlockStore::new(dir.path(), LockStatus::Unlocked); block_store.create_files_if_they_do_not_exist().unwrap(); - let dummy_block = ValidBlock::new_dummy().into(); + let dummy_block = ValidBlock::new_dummy(KeyPair::random().private_key()).into(); let append_count = 35; for _ in 0..append_count { diff --git a/core/src/smartcontracts/isi/query.rs b/core/src/smartcontracts/isi/query.rs index fcd6e87a5f4..2d8a14982b1 100644 --- a/core/src/smartcontracts/isi/query.rs +++ b/core/src/smartcontracts/isi/query.rs @@ -409,12 +409,12 @@ mod tests { let mut transactions = vec![valid_tx; valid_tx_per_block]; transactions.append(&mut vec![invalid_tx; invalid_tx_per_block]); - let (peer_public_key, _) = KeyPair::random().into_parts(); + let (peer_public_key, peer_private_key) = KeyPair::random().into_parts(); let peer_id = PeerId::new("127.0.0.1:8080".parse().unwrap(), peer_public_key); let topology = Topology::new(vec![peer_id]); let first_block = BlockBuilder::new(transactions.clone(), topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(ALICE_KEYPAIR.private_key()) + .sign(&peer_private_key) .unpack(|_| {}) .commit(&topology) .unpack(|_| {}) @@ -426,7 +426,7 @@ mod tests { for _ in 1u64..blocks { let block = BlockBuilder::new(transactions.clone(), topology.clone(), Vec::new()) .chain(0, &mut state_block) - .sign(ALICE_KEYPAIR.private_key()) + .sign(&peer_private_key) .unpack(|_| {}) .commit(&topology) .unpack(|_| {}) @@ -481,7 +481,9 @@ mod tests { let blocks = FindAllBlocks.execute(&state.view())?.collect::>(); assert_eq!(blocks.len() as u64, num_blocks); - assert!(blocks.windows(2).all(|wnd| wnd[0] >= wnd[1])); + assert!(blocks + .windows(2) + .all(|wnd| wnd[0].header() >= wnd[1].header())); Ok(()) } diff --git a/core/src/state.rs b/core/src/state.rs index c77981fd661..e8faf74a6b1 100644 --- a/core/src/state.rs +++ b/core/src/state.rs @@ -1837,11 +1837,11 @@ mod tests { /// Used to inject faulty payload for testing fn new_dummy_block_with_payload(f: impl FnOnce(&mut BlockPayload)) -> CommittedBlock { - let (leader_public_key, _) = iroha_crypto::KeyPair::random().into_parts(); + let (leader_public_key, leader_private_key) = iroha_crypto::KeyPair::random().into_parts(); let peer_id = PeerId::new("127.0.0.1:8080".parse().unwrap(), leader_public_key); let topology = Topology::new(vec![peer_id]); - ValidBlock::new_dummy_and_modify_payload(f) + ValidBlock::new_dummy_and_modify_payload(&leader_private_key, f) .commit(&topology) .unpack(|_| {}) .unwrap() diff --git a/core/src/sumeragi/network_topology.rs b/core/src/sumeragi/network_topology.rs index 4b58d52a5b3..0a55860a7e2 100644 --- a/core/src/sumeragi/network_topology.rs +++ b/core/src/sumeragi/network_topology.rs @@ -360,7 +360,7 @@ mod tests { let peers = test_peers![0, 1, 2, 3, 4, 5, 6: key_pairs_iter]; let topology = Topology::new(peers); - let dummy_block = ValidBlock::new_dummy(); + let dummy_block = ValidBlock::new_dummy(key_pairs[0].private_key()); let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; let dummy_signatures = (0..key_pairs.len()) .map(|i| BlockSignature(i as u64, dummy_signature.clone())) @@ -400,7 +400,7 @@ mod tests { let peers = test_peers![0: key_pairs_iter]; let topology = Topology::new(peers); - let dummy_block = ValidBlock::new_dummy(); + let dummy_block = ValidBlock::new_dummy(key_pairs[0].private_key()); let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; let dummy_signatures = (0..key_pairs.len()) .map(|i| BlockSignature(i as u64, dummy_signature.clone())) @@ -434,7 +434,7 @@ mod tests { let peers = test_peers![0, 1: key_pairs_iter]; let topology = Topology::new(peers); - let dummy_block = ValidBlock::new_dummy(); + let dummy_block = ValidBlock::new_dummy(key_pairs[0].private_key()); let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; let dummy_signatures = (0..key_pairs.len()) .map(|i| BlockSignature(i as u64, dummy_signature.clone())) @@ -470,7 +470,7 @@ mod tests { let peers = test_peers![0, 1, 2: key_pairs_iter]; let topology = Topology::new(peers); - let dummy_block = ValidBlock::new_dummy(); + let dummy_block = ValidBlock::new_dummy(key_pairs[0].private_key()); let dummy_signature = &dummy_block.as_ref().signatures().next().unwrap().1; let dummy_signatures = (0..key_pairs.len()) .map(|i| BlockSignature(i as u64, dummy_signature.clone())) @@ -742,7 +742,7 @@ mod tests { .is_consensus_required() .as_ref() .map(ConsensusTopology::validating_peers), - None, + Some::<&[_]>(&[]), ); } @@ -756,7 +756,7 @@ mod tests { .is_consensus_required() .as_ref() .map(ConsensusTopology::observing_peers), - None, + Some::<&[_]>(&[]), ); } } diff --git a/core/src/tx.rs b/core/src/tx.rs index 1bad2077dd4..a13b81ce40c 100644 --- a/core/src/tx.rs +++ b/core/src/tx.rs @@ -106,6 +106,10 @@ impl AcceptedTransaction { })); } + if *iroha_genesis::GENESIS_DOMAIN_ID == *tx.authority().domain() { + return Err(AcceptTransactionFail::UnexpectedGenesisAccountSignature); + } + match &tx.instructions() { Executable::Instructions(instructions) => { let instruction_count = instructions.len(); diff --git a/crypto/src/signature/mod.rs b/crypto/src/signature/mod.rs index 90a5c75eb51..790e130efff 100644 --- a/crypto/src/signature/mod.rs +++ b/crypto/src/signature/mod.rs @@ -284,7 +284,6 @@ mod tests { #[test] fn signature_serialized_representation() { let input = json!({ - "public_key": "e701210312273E8810581E58948D3FB8F9E8AD53AAA21492EBB8703915BBB565A21B7FCC", "payload": "3A7991AF1ABB77F3FD27CC148404A6AE4439D095A63591B77C788D53F708A02A1509A611AD6D97B01D871E58ED00C8FD7C3917B6CA61A8C2833A19E000AAC2E4" }); diff --git a/schema/gen/src/lib.rs b/schema/gen/src/lib.rs index 27816465c97..313c1c6d0f1 100644 --- a/schema/gen/src/lib.rs +++ b/schema/gen/src/lib.rs @@ -87,7 +87,7 @@ types!( BTreeMap, BTreeMap, BTreeMap, - BTreeMap, + BTreeMap, BTreeMap, BTreeSet, BTreeSet, @@ -244,7 +244,6 @@ types!( Option, Option, Option, - Option>>, Option>, Option>, Option, diff --git a/tools/parity_scale_cli/src/main.rs b/tools/parity_scale_cli/src/main.rs index 0510977d29d..1ff425768d1 100644 --- a/tools/parity_scale_cli/src/main.rs +++ b/tools/parity_scale_cli/src/main.rs @@ -426,7 +426,7 @@ mod tests { is_coloring_supported() ); assert!(try_with("--terminal-colors")?); - assert!(try_with("--terminal-colors=false")?); + assert!(!try_with("--terminal-colors=false")?); assert!(try_with("--terminal-colors=true")?); assert!(try_with("--terminal-colors=random").is_err());