Skip to content

Commit

Permalink
[chore] Update digest, rand and sha3
Browse files Browse the repository at this point in the history
- Update crates `digest` `rand` and `sha3` to fix compilation errors
  (Ref tari-project/tari-crypto#49)
- NodeId::from_key is infallible
- This in turn meant that NodeIdentity::new and NodeIdentity::randoma are infallible
- This in turn meant that NodeIdentityError is unused, so it was removed
- Remove multiple copies of the random string test utility (all of which
  needed `rand` crate updates) in favour of using the one provided in tari_test_utils
  • Loading branch information
sdbondi committed Jul 6, 2021
1 parent 868a064 commit e3d0364
Show file tree
Hide file tree
Showing 97 changed files with 1,011 additions and 1,235 deletions.
1,286 changes: 555 additions & 731 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion applications/tari_app_utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ qrcode = { version = "0.12" }
dirs-next = "1.0.2"
serde_json = "1.0"
log = { version = "0.4.8", features = ["std"] }
rand = "0.7.2"
rand = "0.8"
tokio = { version="0.2.10", features = ["signal"] }
structopt = { version = "0.3.13", default_features = false }
strum = "^0.19"
Expand Down
11 changes: 2 additions & 9 deletions applications/tari_app_utilities/src/identity_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ pub fn create_new_identity<P: AsRef<Path>>(
features: PeerFeatures,
) -> Result<NodeIdentity, String> {
let private_key = PrivateKey::random(&mut OsRng);
let node_identity = NodeIdentity::new(private_key, public_addr, features)
.map_err(|e| format!("We were unable to construct a node identity. {}", e.to_string()))?;
let node_identity = NodeIdentity::new(private_key, public_addr, features);
save_as_json(path, &node_identity)?;
Ok(node_identity)
}
Expand All @@ -160,14 +159,8 @@ pub fn recover_node_identity<P: AsRef<Path>>(
public_addr: &Multiaddr,
features: PeerFeatures,
) -> Result<Arc<NodeIdentity>, ExitCodes> {
let node_identity = NodeIdentity::new(private_key, public_addr.clone(), features).map_err(|e| {
ExitCodes::ConfigError(format!(
"We were unable to construct a node identity. {}",
e.to_string()
))
})?;
let node_identity = NodeIdentity::new(private_key, public_addr.clone(), features);
save_as_json(path, &node_identity).map_err(ExitCodes::IOError)?;

Ok(Arc::new(node_identity))
}

Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ chrono = { version = "0.4.6", features = ["serde"]}
chrono-english = "0.1"
futures = { version = "^0.3.1", default-features = false, features = ["alloc"]}
crossterm = { version = "0.17"}
rand = "0.7.2"
rand = "0.8"
unicode-width = "0.1"
unicode-segmentation = "1.6.0"
log = { version = "0.4.8", features = ["std"] }
Expand Down
9 changes: 5 additions & 4 deletions applications/tari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,11 @@ pub async fn init_wallet(
Some(nf) => nf,
};

let node_identity = Arc::new(
NodeIdentity::new(CommsSecretKey::default(), node_address, node_features)
.map_err(|e| ExitCodes::NetworkError(e.to_string()))?,
);
let node_identity = Arc::new(NodeIdentity::new(
CommsSecretKey::default(),
node_address,
node_features,
));

let transport_type = setup_wallet_transport_type(&config);
let transport_type = match transport_type {
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl AppState {
pub async fn set_custom_base_node(&mut self, public_key: String, address: String) -> Result<Peer, UiError> {
let pub_key = PublicKey::from_hex(public_key.as_str())?;
let addr = address.parse::<Multiaddr>().map_err(|_| UiError::AddressParseError)?;
let node_id = NodeId::from_key(&pub_key)?;
let node_id = NodeId::from_key(&pub_key);
let peer = Peer::new(
pub_key,
node_id,
Expand Down
4 changes: 1 addition & 3 deletions applications/tari_console_wallet/src/ui/ui_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tari_comms::{connectivity::ConnectivityError, peer_manager::node_id::NodeIdError};
use tari_comms::connectivity::ConnectivityError;
use tari_crypto::tari_utilities::hex::HexError;
use tari_wallet::{
contacts_service::error::ContactsServiceError,
Expand All @@ -21,8 +21,6 @@ pub enum UiError {
#[error(transparent)]
HexError(#[from] HexError),
#[error(transparent)]
NodeIdError(#[from] NodeIdError),
#[error(transparent)]
WalletError(#[from] WalletError),
#[error(transparent)]
WalletStorageError(#[from] WalletStorageError),
Expand Down
11 changes: 1 addition & 10 deletions applications/tari_console_wallet/src/utils/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,7 @@ pub async fn get_custom_base_node_peer_from_db(wallet: &mut WalletSqlite) -> Opt
},
};

let node_id = match NodeId::from_key(&pub_key) {
Ok(n) => n,
Err(e) => {
debug!(
target: LOG_TARGET,
"Problem converting stored base node public key to Node Id: {}", e
);
return None;
},
};
let node_id = NodeId::from_key(&pub_key);
Some(Peer::new(
pub_key,
node_id,
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_merge_mining_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ hex = "0.4.2"
hyper = "0.13.7"
jsonrpc = "0.11.0"
log = { version = "0.4.8", features = ["std"] }
rand = "0.7.2"
rand = "0.8"
reqwest = {version = "0.10.8", features=["json"]}
serde = { version="1.0.106", features = ["derive"] }
serde_json = "1.0.57"
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_mining_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ futures = "0.3"
log = { version = "0.4", features = ["std"] }
num_cpus = "1.13"
prost-types = "0.6"
rand = "0.7.2"
rand = "0.8"
sha3 = "0.9"
serde = { version = "1.0", default_features = false, features = ["derive"] }
tonic = { version = "0.2", features = ["transport"] }
Expand Down
2 changes: 1 addition & 1 deletion applications/test_faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"
tari_utilities = "^0.3"
serde = { version = "1.0.97", features = ["derive"] }
serde_json = "1.0"
rand = "0.7.2"
rand = "0.8"
tari_crypto = { git = "ssh://git@github.com/tari-project/tari-crypto.git", branch = "main" }

[dependencies.tari_core]
Expand Down
2 changes: 1 addition & 1 deletion base_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
futures = {version = "^0.3.1", features = ["async-await"] }
rand = "0.7.2"
rand = "0.8"
tari_crypto = { git = "ssh://git@github.com/tari-project/tari-crypto.git", branch = "main" }
serde = { version = "1.0.106", features = ["derive"] }
tokio = { version="^0.2", features = ["blocking", "time", "sync"] }
6 changes: 3 additions & 3 deletions base_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ tari_test_utils = { version = "^0.8", path = "../../infrastructure/test_utils" }

bincode = "1.1.4"
bitflags = "1.0.4"
blake2 = "^0.8.0"
blake2 = "^0.9.0"
sha3 = "0.9"
bytes = "0.4.12"
chrono = { version = "0.4.6", features = ["serde"]}
croaring = { version = "=0.4.5", optional = true }
digest = "0.8.0"
digest = "0.9.0"
futures = {version = "^0.3.1", features = ["async-await"] }
fs2 = "0.3.0"
hex = "0.4.2"
Expand All @@ -49,7 +49,7 @@ newtype-ops = "0.1.4"
num = "0.3"
prost = "0.6.1"
prost-types = "0.6.1"
rand = "0.7.2"
rand = "0.8"
randomx-rs = { version = "0.5.0", optional = true }
serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ mod test {

fn random_node_id() -> NodeId {
let (_secret_key, public_key) = CommsPublicKey::random_keypair(&mut OsRng);
NodeId::from_key(&public_key).unwrap()
NodeId::from_key(&public_key)
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/blocks/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl BlockHeader {
.chain(self.kernel_mmr_size.to_le_bytes())
.chain(self.total_kernel_offset.as_bytes())
.chain(self.total_script_offset.as_bytes())
.result()
.finalize()
.to_vec()
}

Expand Down Expand Up @@ -257,7 +257,7 @@ impl Hashable for BlockHeader {
.chain(self.merged_mining_hash())
.chain(self.pow.to_bytes())
.chain(self.nonce.to_le_bytes())
.result()
.finalize()
.to_vec()
}
}
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/test_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn create_peer_manager<P: AsRef<Path>>(data_path: P) -> Arc<PeerManager> {
let peer_database_name = {
let mut rng = rand::thread_rng();
iter::repeat(())
.map(|_| rng.sample(Alphanumeric))
.map(|_| rng.sample(Alphanumeric) as char)
.take(8)
.collect::<String>()
};
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/transactions/bullet_rangeproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct BulletRangeProof(pub Vec<u8>);
/// Implement the hashing function for RangeProof for use in the MMR
impl Hashable for BulletRangeProof {
fn hash(&self) -> Vec<u8> {
HashDigest::new().chain(&self.0).result().to_vec()
HashDigest::new().chain(&self.0).finalize().to_vec()
}
}

Expand Down
22 changes: 10 additions & 12 deletions base_layer/core/src/transactions/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::transactions::{
Signature,
},
};
use digest::Input;
use blake2::Digest;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -253,14 +253,13 @@ impl UnblindedOutput {
.chain(self.input_data.as_bytes().as_slice())
.chain(PublicKey::from_secret_key(&self.script_private_key).as_bytes())
.chain(commitment.as_bytes())
.result()
.to_vec();
.finalize();
let script_signature = ComSignature::sign(
self.value.into(),
self.script_private_key.clone() + self.spending_key.clone(),
script_nonce_a,
script_nonce_b,
&e,
e.as_slice(),
factory,
)
.map_err(|_| TransactionError::InvalidSignatureError("Generating script signature".to_string()))?;
Expand Down Expand Up @@ -445,8 +444,7 @@ impl TransactionInput {
.chain(self.input_data.as_bytes().as_slice())
.chain(public_script_key.as_bytes())
.chain(self.commitment.as_bytes())
.result()
.to_vec();
.finalize();
if self
.script_signature
.verify_challenge(&(&self.commitment + public_script_key), &m, factory)
Expand Down Expand Up @@ -474,7 +472,7 @@ impl TransactionInput {
.chain(self.features.to_bytes())
.chain(self.commitment.as_bytes())
.chain(self.script.as_bytes())
.result()
.finalize()
.to_vec()
}
}
Expand All @@ -491,7 +489,7 @@ impl Hashable for TransactionInput {
.chain(self.script_signature.v().as_bytes())
.chain(self.script_signature.public_nonce().as_bytes())
.chain(self.input_data.as_bytes())
.result()
.finalize()
.to_vec()
}
}
Expand Down Expand Up @@ -650,7 +648,7 @@ impl TransactionOutput {
.chain(features.to_bytes())
.chain(script_offset_public_key.as_bytes())
.chain(public_nonce.as_bytes())
.result()
.finalize()
.to_vec()
}

Expand All @@ -675,7 +673,7 @@ impl TransactionOutput {
.chain(self.proof.as_bytes())
.chain(self.sender_metadata_signature.get_signature().as_bytes())
.chain(self.sender_metadata_signature.get_public_nonce().as_bytes())
.result()
.finalize()
.to_vec()
}
}
Expand All @@ -693,7 +691,7 @@ impl Hashable for TransactionOutput {
.chain(self.commitment.as_bytes())
// .chain(range proof) // See docs as to why we exclude this
.chain(self.script.as_bytes())
.result()
.finalize()
.to_vec()
}
}
Expand Down Expand Up @@ -934,7 +932,7 @@ impl Hashable for TransactionKernel {
.chain(self.excess.as_bytes())
.chain(self.excess_sig.get_public_nonce().as_bytes())
.chain(self.excess_sig.get_signature().as_bytes())
.result()
.finalize()
.to_vec()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ pub fn build_challenge(sum_public_nonces: &PublicKey, metadata: &TransactionMeta
.chain(sum_public_nonces.as_bytes())
.chain(&u64::from(metadata.fee).to_le_bytes())
.chain(&metadata.lock_height.to_le_bytes())
.result()
.finalize()
.to_vec()
}
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ impl fmt::Display for SenderTransactionProtocol {
}

pub fn calculate_tx_id<D: Digest>(pub_nonce: &PublicKey, index: usize) -> u64 {
let hash = D::new().chain(pub_nonce.as_bytes()).chain(index.to_le_bytes()).result();
let hash = D::new()
.chain(pub_nonce.as_bytes())
.chain(index.to_le_bytes())
.finalize();
let mut bytes: [u8; 8] = [0u8; 8];
bytes.copy_from_slice(&hash[..8]);
u64::from_le_bytes(bytes)
Expand Down
9 changes: 6 additions & 3 deletions base_layer/core/tests/helpers/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ use blake2::Digest;
use std::sync::Arc;
use tari_common_types::chain_metadata::ChainMetadata;
use tari_comms::peer_manager::NodeId;
use tari_core::base_node::chain_metadata_service::{ChainMetadataEvent, ChainMetadataHandle, PeerChainMetadata};
use tari_crypto::{common::Blake256, tari_utilities::ByteArray};
use tari_core::{
base_node::chain_metadata_service::{ChainMetadataEvent, ChainMetadataHandle, PeerChainMetadata},
tari_utilities::ByteArray,
};
use tari_crypto::common::Blake256;
use tokio::sync::broadcast;

/// Create a mock Chain Metadata stream.
Expand Down Expand Up @@ -69,7 +72,7 @@ impl MockChainMetadata {
#[allow(dead_code)]
pub fn random_peer_metadata(height: u64, difficulty: u128) -> PeerChainMetadata {
let key: Vec<u8> = (0..13).map(|_| rand::random::<u8>()).collect();
let id = NodeId::from_key(&key).unwrap();
let id = NodeId::from_key(&key);
let block_hash = Blake256::digest(id.as_bytes()).to_vec();
let metadata = ChainMetadata::new(height, block_hash, 2800, 0, difficulty);
PeerChainMetadata::new(id, metadata)
Expand Down
22 changes: 7 additions & 15 deletions base_layer/core/tests/helpers/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

use crate::helpers::mock_state_machine::MockBaseNodeStateMachine;
use futures::Sink;
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng};
use std::{error::Error, iter, path::Path, sync::Arc, time::Duration};
use rand::rngs::OsRng;
use std::{error::Error, path::Path, sync::Arc, time::Duration};
use tari_common::configuration::Network;
use tari_comms::{
peer_manager::{NodeIdentity, PeerFeatures},
Expand Down Expand Up @@ -352,23 +352,15 @@ pub fn create_network_with_3_base_nodes_with_config<P: AsRef<Path>>(
(alice_node, bob_node, carol_node, consensus_manager)
}

#[allow(dead_code)]
fn random_string(len: usize) -> String {
iter::repeat(()).map(|_| OsRng.sample(Alphanumeric)).take(len).collect()
}

// Helper function for creating a random node indentity.
#[allow(dead_code)]
pub fn random_node_identity() -> Arc<NodeIdentity> {
let next_port = MemoryTransport::acquire_next_memsocket_port();
Arc::new(
NodeIdentity::random(
&mut OsRng,
format!("/memory/{}", next_port).parse().unwrap(),
PeerFeatures::COMMUNICATION_NODE,
)
.unwrap(),
)
Arc::new(NodeIdentity::random(
&mut OsRng,
format!("/memory/{}", next_port).parse().unwrap(),
PeerFeatures::COMMUNICATION_NODE,
))
}

// Helper function for starting the comms stack.
Expand Down
6 changes: 3 additions & 3 deletions base_layer/key_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ edition = "2018"

[dependencies]
tari_crypto = { git = "ssh://git@github.com/tari-project/tari-crypto.git", branch = "main" }
rand = "0.7.2"
digest = "0.8.0"
sha2 = "0.8.0"
rand = "0.8"
digest = "0.9.0"
sha2 = "0.9.5"
serde = "1.0.89"
serde_derive = "1.0.89"
serde_json = "1.0.39"
Expand Down
Loading

0 comments on commit e3d0364

Please sign in to comment.