Skip to content

Commit

Permalink
[refactor] hyperledger-iroha#1982: encapsulate access to `iroha_crypt…
Browse files Browse the repository at this point in the history
…o` structures (hyperledger-iroha#2077)
  • Loading branch information
mversic committed May 13, 2022
1 parent 072b811 commit 9f49dfd
Show file tree
Hide file tree
Showing 94 changed files with 1,260 additions and 1,056 deletions.
18 changes: 15 additions & 3 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ keywords = ["crypto", "blockchain", "ledger"]
categories = ["cryptography::cryptocurrencies"]

[features]
default = ["bridge", "telemetry", "schema-endpoint"]

bridge = ["iroha_core/bridge"]
dex = ["iroha_core/dex"]
# Include support for account roles
roles = ["iroha_core/roles", "iroha_permissions_validators/roles"]
telemetry = ["iroha_telemetry", "iroha_core/telemetry"]
dev-telemetry = ["iroha_core/dev-telemetry", "iroha_telemetry"]
schema-endpoint = ["iroha_schema_bin"]
test-network = []

default = ["bridge", "telemetry", "schema-endpoint"]

[badges]
is-it-maintained-issue-resolution = { repository = "https://github.com/hyperledger/iroha" }
is-it-maintained-open-issues = { repository = "https://github.com/hyperledger/iroha" }
maintenance = { status = "actively-developed" }

[dependencies]
iroha_core = { version = "=2.0.0-pre-rc.3", path = "../core" }
iroha_macro = { version = "=2.0.0-pre-rc.3", path = "../macro" }
iroha_permissions_validators = { version = "=2.0.0-pre-rc.3", path = "../permissions_validators" }
iroha_logger = { version = "=2.0.0-pre-rc.3", path = "../logger" }
iroha_futures = { version = "=2.0.0-pre-rc.3", path = "../futures" }
Expand All @@ -46,12 +48,12 @@ futures = { version = "0.3.17", default-features = false, features = ["std", "as
parity-scale-codec = { version = "2.3.1", default-features = false, features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
async-trait = "0.1"
tokio = { version = "1.6.0", features = ["sync", "time", "rt", "io-util", "rt-multi-thread", "macros", "fs"] }
warp = "0.3"
thiserror = "1.0.28"
color-eyre = "0.5.11"

[dev-dependencies]
unique_port = "0.1.0"
iroha_core = { version = "=2.0.0-pre-rc.3", path = "../core", features = ["cross_crate_testing"] }
hex = "0.4.0"
137 changes: 35 additions & 102 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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};

use super::torii::config::ToriiConfiguration;

/// Configuration parameters container.
#[derive(Clone, Deserialize, Serialize, Debug, Configurable, Default)]
#[derive(Debug, Clone, Deserialize, Serialize, Configurable)]
#[serde(default)]
#[serde(rename_all = "UPPERCASE")]
#[config(env_prefix = "IROHA_")]
Expand Down Expand Up @@ -53,17 +53,41 @@ pub struct Configuration {
/// Configuration for `WorldStateView`.
#[config(inner)]
pub wsv: WorldStateViewConfiguration,
#[cfg(feature = "telemetry")]
/// Configuration for telemetry
#[config(inner)]
pub telemetry: iroha_telemetry::Configuration,
/// Network configuration
#[config(inner)]
pub network: NetworkConfiguration,
/// Configuration for telemetry
#[config(inner)]
#[cfg(feature = "telemetry")]
pub telemetry: iroha_telemetry::Configuration,
}

impl Default for Configuration {
fn default() -> Self {
let sumeragi_configuration = SumeragiConfiguration::default();
let (public_key, private_key) = sumeragi_configuration.key_pair.clone().into();

Self {
public_key,
private_key,
disable_panic_terminal_colors: bool::default(),
kura: KuraConfiguration::default(),
sumeragi: sumeragi_configuration,
torii: ToriiConfiguration::default(),
block_sync: BlockSyncConfiguration::default(),
queue: QueueConfiguration::default(),
logger: LoggerConfiguration::default(),
genesis: GenesisConfiguration::default(),
wsv: WorldStateViewConfiguration::default(),
network: NetworkConfiguration::default(),
#[cfg(feature = "telemetry")]
telemetry: iroha_telemetry::Configuration::default(),
}
}
}

/// Network Configuration parameters container.
#[derive(Clone, Copy, Deserialize, Serialize, Debug, Configurable, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Configurable)]
#[serde(default)]
#[serde(rename_all = "UPPERCASE")]
#[config(env_prefix = "IROHA_NETWORK_")]
Expand Down Expand Up @@ -104,7 +128,7 @@ impl Configuration {
}

fn finalize(&mut self) {
self.sumeragi.key_pair = self.key_pair().into();
self.sumeragi.key_pair = self.key_pair();
self.sumeragi.peer_id = PeerId::new(&self.torii.p2p_addr, &self.public_key.clone());
}

Expand All @@ -120,17 +144,15 @@ impl Configuration {
}

/// Get `public_key` and `private_key` configuration parameters.
pub fn key_pair(&self) -> (PublicKey, PrivateKey) {
(self.public_key.clone(), self.private_key.clone())
pub fn key_pair(&self) -> iroha_crypto::KeyPair {
iroha_crypto::KeyPair::new(self.public_key.clone(), self.private_key.clone())
}
}

#[cfg(test)]
mod tests {
#![allow(clippy::restriction)]

use std::collections::HashSet;

use iroha_core::sumeragi::config::TrustedPeers;

use super::*;
Expand All @@ -146,99 +168,10 @@ mod tests {
Ok(())
}

#[test]
fn parse_example_trusted_peers_json() -> Result<(), String> {
let configuration = Configuration::from_path(CONFIGURATION_PATH)
.map_err(|e| format!("Failed to read configuration from example config: {}", e))?;
let public_key1 = PublicKey {
digest_function: iroha_crypto::ED_25519.to_string(),
payload: hex::decode(
"7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0",
)
.expect("Failed to decode"),
};
let public_key2 = PublicKey {
digest_function: iroha_crypto::ED_25519.to_string(),
payload: hex::decode(
"CC25624D62896D3A0BFD8940F928DC2ABF27CC57CEFEB442AA96D9081AAE58A1",
)
.expect("Failed to decode"),
};
let public_key3 = PublicKey {
digest_function: iroha_crypto::ED_25519.to_string(),
payload: hex::decode(
"FACA9E8AA83225CB4D16D67F27DD4F93FC30FFA11ADC1F5C88FD5495ECC91020",
)
.expect("Failed to decode"),
};
let public_key4 = PublicKey {
digest_function: iroha_crypto::ED_25519.to_string(),
payload: hex::decode(
"8E351A70B6A603ED285D666B8D689B680865913BA03CE29FB7D13A166C4E7F1F",
)
.expect("Failed to decode"),
};
let expected_trusted_peers = vec![
PeerId {
address: "127.0.0.1:1337".to_owned(),
public_key: public_key1,
},
PeerId {
address: "127.0.0.1:1338".to_owned(),
public_key: public_key2,
},
PeerId {
address: "127.0.0.1:1339".to_owned(),
public_key: public_key3,
},
PeerId {
address: "127.0.0.1:1340".to_owned(),
public_key: public_key4,
},
]
.into_iter()
.collect::<HashSet<_>>();
assert_eq!(1000, configuration.sumeragi.block_time_ms);
assert_eq!(
expected_trusted_peers,
configuration.sumeragi.trusted_peers.peers
);
Ok(())
}

#[test]
fn parse_trusted_peers_success() {
let trusted_peers_string = r#"[{"address":"127.0.0.1:1337", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}, {"address":"localhost:1338", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}, {"address": "195.162.0.1:23", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}]"#;
let public_key = PublicKey {
digest_function: iroha_crypto::ED_25519.to_string(),
payload: hex::decode(
"7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0",
)
.expect("Failed to decode"),
};
let expected_trusted_peers = vec![
PeerId {
address: "127.0.0.1:1337".to_string(),
public_key: public_key.clone(),
},
PeerId {
address: "localhost:1338".to_string(),
public_key: public_key.clone(),
},
PeerId {
address: "195.162.0.1:23".to_string(),
public_key,
},
];
let result: Vec<PeerId> =
serde_json::from_str(trusted_peers_string).expect("Failed to parse Trusted Peers.");
assert_eq!(expected_trusted_peers, result);
}

#[test]
#[should_panic]
fn parse_trusted_peers_fail_duplicate_peer_id() {
let trusted_peers_string = r#"[{"address":"127.0.0.1:1337", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}, {"address":"127.0.0.1:1337", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}, {"address":"localhost:1338", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}, {"address": "195.162.0.1:23", "public_key": "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"}]"#;
let trusted_peers_string = r#"[{"address":"127.0.0.1:1337", "public_key": "ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10"}, {"address":"127.0.0.1:1337", "public_key": "ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10"}, {"address":"localhost:1338", "public_key": "ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10"}, {"address": "195.162.0.1:23", "public_key": "ed0120954c83a4220faffb2c1d23fc5225b3e7952d53acbb2a065ff30c631e5e1d6b10"}]"#;
let _result: TrustedPeers =
serde_json::from_str(trusted_peers_string).expect("Failed to parse Trusted Peers");
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/event.rs → cli/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
use futures::TryStreamExt;
use iroha_data_model::events::prelude::*;
use iroha_macro::error::ErrorTryFromEnum;
use tokio::sync::broadcast;
use warp::ws::WebSocket;

use crate::stream::{self, Sink, Stream};

/// Type of `Sender<Event>` which should be used for channels of `Event` messages.
pub type EventsSender = broadcast::Sender<Event>;
/// Type of `Receiver<Event>` which should be used for channels of `Event` messages.
pub type EventsReceiver = broadcast::Receiver<Event>;
/// Type of Stream error
pub type StreamError = stream::Error<<WebSocket as Stream<VersionedEventSubscriberMessage>>::Err>;

Expand Down
15 changes: 6 additions & 9 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use tokio::sync::broadcast;
use torii::Torii;

pub mod config;
mod event;
pub mod samples;
mod stream;
pub mod torii;

/// Arguments for Iroha2 - usually parsed from cli.
Expand Down Expand Up @@ -163,7 +165,7 @@ where
WorldStateView::from_configuration(
config.wsv,
W::with(
domains(&config).wrap_err("Failed to get initial domains")?,
domains(&config),
config.sumeragi.trusted_peers.peers.clone(),
),
)
Expand Down Expand Up @@ -305,12 +307,7 @@ where
///
/// # Errors
/// - Genesis account public key not specified.
fn domains(configuration: &config::Configuration) -> Result<impl Iterator<Item = Domain>> {
let key = configuration
.genesis
.account_public_key
.clone()
.ok_or_else(|| eyre!("Genesis account public key is not specified."))?;

Ok([Domain::from(GenesisDomain::new(key))].into_iter())
fn domains(configuration: &config::Configuration) -> impl Iterator<Item = Domain> {
let key = configuration.genesis.account_public_key.clone();
[Domain::from(GenesisDomain::new(key))].into_iter()
}
7 changes: 2 additions & 5 deletions cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ pub fn get_config(trusted_peers: HashSet<PeerId>, key_pair: Option<KeyPair>) ->
..KuraConfiguration::default()
},
sumeragi: SumeragiConfiguration {
key_pair: KeyPair {
public_key: public_key.clone(),
private_key: private_key.clone(),
},
key_pair: KeyPair::new(public_key.clone(), private_key.clone()),
peer_id: PeerId::new(DEFAULT_TORII_P2P_ADDR, &public_key),
trusted_peers: TrustedPeers {
peers: trusted_peers,
Expand All @@ -100,7 +97,7 @@ pub fn get_config(trusted_peers: HashSet<PeerId>, key_pair: Option<KeyPair>) ->
..QueueConfiguration::default()
},
genesis: GenesisConfiguration {
account_public_key: Some(public_key),
account_public_key: public_key,
account_private_key: Some(private_key),
..GenesisConfiguration::default()
},
Expand Down
Loading

0 comments on commit 9f49dfd

Please sign in to comment.