Skip to content

Commit

Permalink
fix review comments, add proper defaults for key pairs
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Apr 14, 2022
1 parent 221dc65 commit 4905e4e
Show file tree
Hide file tree
Showing 29 changed files with 300 additions and 216 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

12 changes: 10 additions & 2 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iroha_core::{
sumeragi::config::SumeragiConfiguration,
wsv::config::Configuration as WorldStateViewConfiguration,
};
use iroha_crypto::{PrivateKey, PublicKey};
use iroha_crypto::prelude::*;
use iroha_data_model::prelude::*;
use iroha_logger::Configuration as LoggerConfiguration;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -63,8 +63,16 @@ pub struct Configuration {
}

impl Default for Configuration {
#[allow(clippy::expect_used)]
fn default() -> Self {
let (public_key, private_key) = iroha_crypto::KeyPair::default().into();
let public_key =
r#"ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10"#
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"1B038DDD 463090FC B30CFA9A 24341679 20BC90CD D7C045BC 64FBCB51 49135100 954C83A4 220FAFFB 2C1D23FC 5225B3E7 952D53AC BB2A065F F30C631E 5E1D6B10"
).expect("Private key not hex encoded");

Self {
public_key,
Expand Down
13 changes: 10 additions & 3 deletions client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, fs::File, io::BufReader, path::Path, str::FromStr};

use eyre::{eyre, Result, WrapErr};
use iroha_config::derive::Configurable;
use iroha_crypto::{PrivateKey, PublicKey};
use iroha_crypto::prelude::*;
use iroha_data_model::{prelude::*, transaction};
use iroha_logger::Configuration as LoggerConfiguration;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -97,10 +97,17 @@ pub struct Configuration {
}

impl Default for Configuration {
#[allow(clippy::expect_used)]
fn default() -> Self {
let (public_key, private_key) = iroha_crypto::KeyPair::default().into();
let public_key =
r#"ed01201c61faf8fe94e253b93114240394f79a607b7fa55f9e5a41ebec74b88055768b"#
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"282ED9F3 CF92811C 3818DBC4 AE594ED5 9DC1A2F7 8E4241E3 1924E101 D6B1FB83 1C61FAF8 FE94E253 B9311424 0394F79A 607B7FA5 5F9E5A41 EBEC74B8 8055768B"
).expect("Private key not hex encoded");

#[allow(clippy::expect_used)]
Self {
public_key,
private_key,
Expand Down
7 changes: 2 additions & 5 deletions client/tests/integration/roles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::restriction)]

use std::{collections::BTreeMap, str::FromStr as _, time::Duration};
use std::{str::FromStr as _, time::Duration};

use eyre::{eyre, Result};
use iroha_client::client::{self, Client};
Expand Down Expand Up @@ -113,10 +113,7 @@ fn register_role_with_empty_token_params() -> Result<()> {

let role_id = iroha_data_model::role::Id::new("root".parse::<Name>().expect("Valid"));
let mut permissions = Permissions::new();
permissions.insert(PermissionToken {
name: "token".parse().expect("Valid"),
params: BTreeMap::new(),
});
permissions.insert(PermissionToken::new("token".parse().expect("Valid")));
let register_role = RegisterBox::new(Role::new(role_id, permissions));

test_client.submit(register_role)?;
Expand Down
4 changes: 2 additions & 2 deletions core/benches/sumeragi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use criterion::{criterion_group, criterion_main, Criterion};
use iroha_core::sumeragi::network_topology;
use iroha_crypto::{Hash, KeyPair};
use iroha_crypto::{Hash, HashOf, KeyPair};
use iroha_data_model::prelude::*;

const N_PEERS: usize = 255;
Expand All @@ -22,7 +22,7 @@ fn get_n_peers(n: usize) -> Vec<PeerId> {
fn sort_peers(criterion: &mut Criterion) {
let peers = get_n_peers(N_PEERS);
let _ = criterion.bench_function("sort_peers", |b| {
let hash = Hash::new([1_u8; Hash::LENGTH]).into();
let hash = HashOf::new(&[1_u8; Hash::LENGTH]).transmute();
b.iter(|| network_topology::sort_peers_by_hash(peers.clone(), &hash));
});
}
Expand Down
20 changes: 9 additions & 11 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use std::{collections::BTreeSet, error::Error, iter, marker::PhantomData};

use dashmap::{mapref::one::Ref as MapRef, DashMap};
use eyre::{eyre, Context, Result};
use iroha_crypto::{Hash, HashOf, KeyPair, SignatureOf, SignaturesOf};
use iroha_data_model::{
current_time, events::prelude::*, merkle::MerkleTree, transaction::prelude::*,
};
use iroha_crypto::{merkle::MerkleTree, HashOf, KeyPair, SignatureOf, SignaturesOf};
use iroha_data_model::{current_time, events::prelude::*, transaction::prelude::*};
use iroha_schema::IntoSchema;
use iroha_version::{declare_versioned_with_scale, version_with_scale};
use parity_scale_codec::{Decode, Encode};
Expand Down Expand Up @@ -49,7 +47,7 @@ impl<T> Default for EmptyChainHash<T> {

impl<T> From<EmptyChainHash<T>> for HashOf<T> {
fn from(EmptyChainHash(PhantomData): EmptyChainHash<T>) -> Self {
Hash::zeroed().into()
HashOf::zeroed()
}
}

Expand Down Expand Up @@ -220,8 +218,8 @@ impl PendingBlock {
consensus_estimation: DEFAULT_CONSENSUS_ESTIMATION_MS,
height: height + 1,
previous_block_hash,
transactions_hash: Hash::zeroed().into(),
rejected_transactions_hash: Hash::zeroed().into(),
transactions_hash: HashOf::zeroed(),
rejected_transactions_hash: HashOf::zeroed(),
view_change_proofs,
invalidated_blocks_hashes,
genesis_topology: None,
Expand All @@ -239,8 +237,8 @@ impl PendingBlock {
consensus_estimation: DEFAULT_CONSENSUS_ESTIMATION_MS,
height: 1,
previous_block_hash: EmptyChainHash::default().into(),
transactions_hash: Hash::zeroed().into(),
rejected_transactions_hash: Hash::zeroed().into(),
transactions_hash: HashOf::zeroed(),
rejected_transactions_hash: HashOf::zeroed(),
view_change_proofs: ViewChangeProofs::empty(),
invalidated_blocks_hashes: Vec::new(),
genesis_topology: Some(genesis_topology),
Expand All @@ -258,8 +256,8 @@ impl PendingBlock {
consensus_estimation: DEFAULT_CONSENSUS_ESTIMATION_MS,
height: 1,
previous_block_hash: EmptyChainHash::default().into(),
transactions_hash: Hash::zeroed().into(),
rejected_transactions_hash: Hash::zeroed().into(),
transactions_hash: HashOf::zeroed(),
rejected_transactions_hash: HashOf::zeroed(),
view_change_proofs: ViewChangeProofs::empty(),
invalidated_blocks_hashes: Vec::new(),
genesis_topology: None,
Expand Down
3 changes: 1 addition & 2 deletions core/src/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use std::{
use async_trait::async_trait;
use futures::{Stream, StreamExt, TryStreamExt};
use iroha_actor::{broker::*, prelude::*};
use iroha_crypto::HashOf;
use iroha_data_model::merkle::MerkleTree;
use iroha_crypto::{merkle::MerkleTree, HashOf};
use iroha_logger::prelude::*;
use iroha_version::scale::{DecodeVersioned, EncodeVersioned};
use pin_project::pin_project;
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub mod prelude {
//! Re-exports important traits and types. Meant to be glob imported when using `Iroha`.
#[doc(inline)]
pub use iroha_crypto::{Hash, KeyPair, PrivateKey, PublicKey};
pub use iroha_crypto::{Algorithm, Hash, KeyPair, PrivateKey, PublicKey};

#[doc(inline)]
pub use crate::{
Expand Down
4 changes: 2 additions & 2 deletions core/src/smartcontracts/isi/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod tests {

use std::{str::FromStr, sync::Arc};

use iroha_crypto::{Hash, KeyPair};
use iroha_crypto::{Hash, HashOf, KeyPair};
use iroha_data_model::transaction::TransactionLimits;
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -351,7 +351,7 @@ mod tests {
.commit();
wsv.apply(vcb).await?;

let wrong_hash = Hash::new([2_u8]);
let wrong_hash: Hash = HashOf::new(&2_u8).into();
let not_found = FindTransactionByHash::new(wrong_hash).execute(&wsv);
assert!(matches!(not_found, Err(_)));

Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<W: WorldTrait> ValidQuery<W> for FindTransactionByHash {
.evaluate(wsv, &Context::default())
.wrap_err("Failed to get hash")
.map_err(|e| query::Error::Evaluate(e.to_string()))?;
let hash = hash.into();
let hash = hash.typed();
if !wsv.has_transaction(&hash) {
return Err(FindError::Transaction(hash).into());
};
Expand Down
17 changes: 13 additions & 4 deletions core/src/sumeragi/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,24 @@ pub struct SumeragiConfiguration {
}

impl Default for SumeragiConfiguration {
#[allow(clippy::expect_used)]
fn default() -> Self {
let key_pair = iroha_crypto::KeyPair::default();
let public_key: PublicKey =
r#"ed0120e6ba36a2f2442152cf0a691b2a238f8de69affb6016aec44493627784543d4b6"#
.parse()
.expect("Public key not in mulithash format");
let private_key = PrivateKey::from_hex(
Algorithm::Ed25519,
"077CC547 30A0C341 5837DB47 C169FAFA 93B6E2E6 35D5DD73 49B0CDF6 CC73997F E6BA36A2 F2442152 CF0A691B 2A238F8D E69AFFB6 016AEC44 49362778 4543D4B6"
).expect("Private key not hex encoded");

let peer_id = PeerId {
address: "localhost".to_owned(),
public_key: key_pair.public_key().clone(),
address: "127.0.0.1".to_owned(),
public_key: public_key.clone(),
};

Self {
key_pair,
key_pair: KeyPair::new(public_key, private_key),
trusted_peers: TrustedPeers::default(),
peer_id,
block_time_ms: DEFAULT_BLOCK_TIME_MS,
Expand Down
2 changes: 1 addition & 1 deletion core/src/sumeragi/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ impl<G: GenesisNetworkTrait, K: KuraTrait, W: WorldTrait, F: FaultInjection> Con
info!("Starting Sumeragi");
self.connect_peers().await;

if height != 0 && *last_block != Hash::zeroed() {
if height != 0 && last_block != HashOf::zeroed() {
self.init(last_block, height);
} else if let Some(genesis_network) = self.genesis_network.take() {
let addr = self.network.clone();
Expand Down
40 changes: 21 additions & 19 deletions core/src/sumeragi/network_topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn sort_peers_by_hash(
}

/// Sorts peers based on the `hash` and `counter` combined as a seed.
pub fn sort_peers_by_hash_and_counter(
fn sort_peers_by_hash_and_counter(
mut peers: Vec<PeerId>,
hash: &HashOf<VersionedCommittedBlock>,
counter: u64,
Expand Down Expand Up @@ -531,45 +531,47 @@ mod tests {
.collect()
}

fn get_hash(seed: u8) -> HashOf<VersionedCommittedBlock> {
HashOf::new(&seed).transmute()
}

#[test]
fn different_order() {
let hash1 = get_hash(1);
let hash2 = get_hash(2);
let peers: Vec<_> = topology_test_peers().into_iter().collect();
let peers_1 = sort_peers_by_hash(peers.clone(), &Hash::new([1_u8; Hash::LENGTH]).into());
let peers_2 = sort_peers_by_hash(peers, &Hash::new([2_u8; Hash::LENGTH]).into());
let peers_1 = sort_peers_by_hash(peers.clone(), &hash1);
let peers_2 = sort_peers_by_hash(peers, &hash2);
assert_ne!(peers_1, peers_2);
}

#[test]
fn same_order() {
let hash1 = get_hash(2);
let hash2 = get_hash(2);
let peers: Vec<_> = topology_test_peers().into_iter().collect();
let peers_1 = sort_peers_by_hash(peers.clone(), &Hash::new([2_u8; Hash::LENGTH]).into());
let peers_2 = sort_peers_by_hash(peers, &Hash::new([2_u8; Hash::LENGTH]).into());
let peers_1 = sort_peers_by_hash(peers.clone(), &hash1);
let peers_2 = sort_peers_by_hash(peers, &hash2);
assert_eq!(peers_1, peers_2);
}

#[test]
fn same_order_by_hash_and_counter() {
let hash1 = get_hash(2);
let hash2 = get_hash(2);
let peers: Vec<_> = topology_test_peers().into_iter().collect();
let peers_1 = sort_peers_by_hash_and_counter(
peers.clone(),
&Hash::new([2_u8; Hash::LENGTH]).into(),
1,
);
let peers_2 =
sort_peers_by_hash_and_counter(peers, &Hash::new([2_u8; Hash::LENGTH]).into(), 1);
let peers_1 = sort_peers_by_hash_and_counter(peers.clone(), &hash1, 1);
let peers_2 = sort_peers_by_hash_and_counter(peers, &hash2, 1);
assert_eq!(peers_1, peers_2);
}

#[test]
fn different_order_by_hash_and_counter() {
let hash1 = get_hash(1);
let hash2 = get_hash(2);
let peers: Vec<_> = topology_test_peers().into_iter().collect();
let peers_1 = sort_peers_by_hash_and_counter(
peers.clone(),
&Hash::new([2_u8; Hash::LENGTH]).into(),
1,
);
let peers_2 =
sort_peers_by_hash_and_counter(peers, &Hash::new([2_u8; Hash::LENGTH]).into(), 2);
let peers_1 = sort_peers_by_hash_and_counter(peers.clone(), &hash1, 1);
let peers_2 = sort_peers_by_hash_and_counter(peers, &hash2, 2);
assert_ne!(peers_1, peers_2);
}
}
Loading

0 comments on commit 4905e4e

Please sign in to comment.