Skip to content

Commit

Permalink
Make imports in core crate more Rust 2018 idiomatic (#3112)
Browse files Browse the repository at this point in the history
We use crate renaming to remove grin prefix, as  result imports for util and keychain crates start with crate::
which looks like a part of the current crate. This PR fixes it.
Also some small improvements were made.
If this approach works I'll replicate it to other crates.
hashmap authored and quentinlesceller committed Nov 14, 2019
1 parent 928097a commit 43bd5a5
Showing 32 changed files with 132 additions and 180 deletions.
6 changes: 3 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ workspace = ".."
edition = "2018"

[dependencies]
blake2-rfc = "0.2"
blake2 = { package = "blake2-rfc", version = "0.2"}
byteorder = "1"
croaring = "0.3.9"
enum_primitive = "0.1"
@@ -29,8 +29,8 @@ log = "0.4"
chrono = { version = "0.4.4", features = ["serde"] }
zeroize = "0.9"

grin_keychain = { path = "../keychain", version = "3.0.0-alpha.1" }
grin_util = { path = "../util", version = "3.0.0-alpha.1" }
keychain = { package = "grin_keychain", path = "../keychain", version = "3.0.0-alpha.1" }
util = { package = "grin_util", path = "../util", version = "3.0.0-alpha.1" }

[dev-dependencies]
serde_json = "1"
3 changes: 1 addition & 2 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -18,12 +18,11 @@
//! enough, consensus-relevant constants and short functions should be kept
//! here.
use std::cmp::{max, min};

use crate::core::block::HeaderVersion;
use crate::core::hash::{Hash, ZERO_HASH};
use crate::global;
use crate::pow::Difficulty;
use std::cmp::{max, min};

/// A grin is divisible to 10^9, following the SI prefixes
pub const GRIN_BASE: u64 = 1_000_000_000;
3 changes: 1 addition & 2 deletions core/src/core.rs
Original file line number Diff line number Diff line change
@@ -26,8 +26,7 @@ pub mod transaction;
pub mod verifier_cache;

use crate::consensus::GRIN_BASE;

use crate::util::secp::pedersen::Commitment;
use util::secp::pedersen::Commitment;

pub use self::block::*;
pub use self::block_sums::*;
22 changes: 10 additions & 12 deletions core/src/core/block.rs
Original file line number Diff line number Diff line change
@@ -14,15 +14,6 @@

//! Blocks and blockheaders
use crate::util::RwLock;
use chrono::naive::{MAX_DATE, MIN_DATE};
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::Duration;
use std::collections::HashSet;
use std::fmt;
use std::iter::FromIterator;
use std::sync::Arc;

use crate::consensus::{self, reward, REWARD};
use crate::core::committed::{self, Committed};
use crate::core::compact_block::{CompactBlock, CompactBlockBody};
@@ -32,12 +23,19 @@ use crate::core::{
transaction, Commitment, Input, KernelFeatures, Output, Transaction, TransactionBody, TxKernel,
Weighting,
};

use crate::global;
use crate::keychain::{self, BlindingFactor};
use crate::pow::{verify_size, Difficulty, Proof, ProofOfWork};
use crate::ser::{self, FixedLength, PMMRable, Readable, Reader, Writeable, Writer};
use crate::util::{secp, static_secp_instance};
use chrono::naive::{MAX_DATE, MIN_DATE};
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::Duration;
use keychain::{self, BlindingFactor};
use std::collections::HashSet;
use std::fmt;
use std::iter::FromIterator;
use std::sync::Arc;
use util::RwLock;
use util::{secp, static_secp_instance};

/// Errors thrown by Block validation
#[derive(Debug, Clone, Eq, PartialEq, Fail)]
4 changes: 2 additions & 2 deletions core/src/core/block_sums.rs
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@
use crate::core::committed::Committed;
use crate::ser::{self, Readable, Reader, Writeable, Writer};
use crate::util::secp::pedersen::Commitment;
use crate::util::secp_static;
use util::secp::pedersen::Commitment;
use util::secp_static;

/// The output_sum and kernel_sum for a given block.
/// This is used to validate the next block being processed by applying
11 changes: 5 additions & 6 deletions core/src/core/committed.rs
Original file line number Diff line number Diff line change
@@ -14,13 +14,12 @@

//! The Committed trait and associated errors.
use crate::keychain;
use crate::keychain::BlindingFactor;

use crate::util::secp::key::SecretKey;
use crate::util::secp::pedersen::Commitment;
use crate::util::{secp, secp_static, static_secp_instance};
use failure::Fail;
use keychain;
use keychain::BlindingFactor;
use util::secp::key::SecretKey;
use util::secp::pedersen::Commitment;
use util::{secp, secp_static, static_secp_instance};

/// Errors from summing and verifying kernel excesses via committed trait.
#[derive(Debug, Clone, PartialEq, Eq, Fail)]
3 changes: 1 addition & 2 deletions core/src/core/compact_block.rs
Original file line number Diff line number Diff line change
@@ -14,13 +14,12 @@

//! Compact Blocks.
use rand::{thread_rng, Rng};

use crate::core::block::{Block, BlockHeader, Error, UntrustedBlockHeader};
use crate::core::hash::{DefaultHashable, Hashed};
use crate::core::id::ShortIdentifiable;
use crate::core::{Output, ShortId, TxKernel};
use crate::ser::{self, read_multi, Readable, Reader, VerifySortedAndUnique, Writeable, Writer};
use rand::{thread_rng, Rng};

/// Container for full (full) outputs and kernels and kern_ids for a compact block.
#[derive(Debug, Clone)]
14 changes: 6 additions & 8 deletions core/src/core/hash.rs
Original file line number Diff line number Diff line change
@@ -17,18 +17,16 @@
//! Primary hash function used in the protocol
//!
use crate::ser::{
self, AsFixedBytes, Error, FixedLength, ProtocolVersion, Readable, Reader, Writeable, Writer,
};
use blake2::blake2b::Blake2b;
use byteorder::{BigEndian, ByteOrder};
use std::cmp::min;
use std::convert::AsRef;
use std::ops::Add;
use std::{fmt, ops};

use crate::blake2::blake2b::Blake2b;

use crate::ser::{
self, AsFixedBytes, Error, FixedLength, ProtocolVersion, Readable, Reader, Writeable, Writer,
};
use crate::util;
use util;

/// A hash consisting of all zeroes, used as a sentinel. No known preimage.
pub const ZERO_HASH: Hash = Hash([0; 32]);
@@ -251,7 +249,7 @@ impl<D: DefaultHashable, E: DefaultHashable> DefaultHashable for (D, E) {}
impl<D: DefaultHashable, E: DefaultHashable, F: DefaultHashable> DefaultHashable for (D, E, F) {}

/// Implement Hashed trait for external types here
impl DefaultHashable for crate::util::secp::pedersen::RangeProof {}
impl DefaultHashable for util::secp::pedersen::RangeProof {}
impl DefaultHashable for Vec<u8> {}
impl DefaultHashable for u8 {}
impl DefaultHashable for u64 {}
11 changes: 4 additions & 7 deletions core/src/core/id.rs
Original file line number Diff line number Diff line change
@@ -14,15 +14,12 @@

//! short ids for compact blocks
use std::cmp::min;
use std::cmp::Ordering;

use byteorder::{ByteOrder, LittleEndian};
use siphasher::sip::SipHasher24;

use crate::core::hash::{DefaultHashable, Hash, Hashed};
use crate::ser::{self, Readable, Reader, Writeable, Writer};
use crate::util;
use byteorder::{ByteOrder, LittleEndian};
use siphasher::sip::SipHasher24;
use std::cmp::{min, Ordering};
use util;

/// The size of a short id used to identify inputs|outputs|kernels (6 bytes)
pub const SHORT_ID_SIZE: usize = 6;
2 changes: 1 addition & 1 deletion core/src/core/merkle_proof.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ use crate::core::hash::Hash;
use crate::core::pmmr;
use crate::ser;
use crate::ser::{PMMRIndexHashable, Readable, Reader, Writeable, Writer};
use crate::util;
use util;

/// Merkle proof errors.
#[derive(Clone, Debug, PartialEq)]
16 changes: 8 additions & 8 deletions core/src/core/transaction.rs
Original file line number Diff line number Diff line change
@@ -17,23 +17,23 @@
use crate::core::hash::{DefaultHashable, Hashed};
use crate::core::verifier_cache::VerifierCache;
use crate::core::{committed, Committed};
use crate::keychain::{self, BlindingFactor};
use crate::libtx::secp_ser;
use crate::ser::{
self, read_multi, FixedLength, PMMRable, ProtocolVersion, Readable, Reader,
VerifySortedAndUnique, Writeable, Writer,
};
use crate::util;
use crate::util::secp;
use crate::util::secp::pedersen::{Commitment, RangeProof};
use crate::util::static_secp_instance;
use crate::util::RwLock;
use crate::{consensus, global};
use enum_primitive::FromPrimitive;
use keychain::{self, BlindingFactor};
use std::cmp::Ordering;
use std::cmp::{max, min};
use std::sync::Arc;
use std::{error, fmt};
use util;
use util::secp;
use util::secp::pedersen::{Commitment, RangeProof};
use util::static_secp_instance;
use util::RwLock;

/// Various tx kernel variants.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
@@ -1584,8 +1584,8 @@ mod test {
use super::*;
use crate::core::hash::Hash;
use crate::core::id::{ShortId, ShortIdentifiable};
use crate::keychain::{ExtKeychain, Keychain, SwitchCommitmentType};
use crate::util::secp;
use keychain::{ExtKeychain, Keychain, SwitchCommitmentType};
use util::secp;

#[test]
fn test_kernel_ser_deser() {
3 changes: 1 addition & 2 deletions core/src/core/verifier_cache.rs
Original file line number Diff line number Diff line change
@@ -15,10 +15,9 @@
//! VerifierCache trait for batch verifying outputs and kernels.
//! We pass a "caching verifier" into the block validation processing with this.
use lru_cache::LruCache;

use crate::core::hash::{Hash, Hashed};
use crate::core::{Output, TxKernel};
use lru_cache::LruCache;

/// Verifier cache for caching expensive verification results.
/// Specifically the following -
16 changes: 7 additions & 9 deletions core/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -19,17 +19,15 @@
#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]

use chrono::prelude::{TimeZone, Utc};

use crate::core;
use crate::pow::{Difficulty, Proof, ProofOfWork};
use crate::util;
use crate::util::secp::constants::SINGLE_BULLET_PROOF_SIZE;
use crate::util::secp::pedersen::{Commitment, RangeProof};
use crate::util::secp::Signature;

use crate::core::hash::Hash;
use crate::keychain::BlindingFactor;
use crate::pow::{Difficulty, Proof, ProofOfWork};
use chrono::prelude::{TimeZone, Utc};
use keychain::BlindingFactor;
use util;
use util::secp::constants::SINGLE_BULLET_PROOF_SIZE;
use util::secp::pedersen::{Commitment, RangeProof};
use util::secp::Signature;

/// Genesis block definition for development networks. The proof of work size
/// is small enough to mine it on the fly, so it does not contain its own
3 changes: 2 additions & 1 deletion core/src/global.rs
Original file line number Diff line number Diff line change
@@ -26,11 +26,12 @@ use crate::core::block::HeaderVersion;
use crate::pow::{
self, new_cuckaroo_ctx, new_cuckarood_ctx, new_cuckatoo_ctx, EdgeType, PoWContext,
};
use util::RwLock;

/// An enum collecting sets of parameters used throughout the
/// code wherever mining is needed. This should allow for
/// different sets of parameters for different purposes,
/// e.g. CI, User testing, production values
use crate::util::RwLock;
/// Define these here, as they should be developer-set, not really tweakable
/// by users
6 changes: 1 addition & 5 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -21,22 +21,18 @@
#![deny(unused_mut)]
#![warn(missing_docs)]

use blake2_rfc as blake2;
#[macro_use]
extern crate enum_primitive;
use grin_keychain as keychain;
use grin_util as util;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
extern crate serde;
use serde;
#[macro_use]
extern crate log;
use failure;
#[macro_use]
extern crate failure_derive;
extern crate zeroize;
#[macro_use]
pub mod macros;

17 changes: 4 additions & 13 deletions core/src/libtx/aggsig.rs
Original file line number Diff line number Diff line change
@@ -16,12 +16,11 @@
//! This module interfaces into the underlying
//! [Rust Aggsig library](https://github.com/mimblewimble/rust-secp256k1-zkp/blob/master/src/aggsig.rs)
use crate::keychain::{BlindingFactor, Identifier, Keychain};
use crate::libtx::error::{Error, ErrorKind};
use crate::util::secp::key::{PublicKey, SecretKey};
use crate::util::secp::pedersen::Commitment;
use crate::util::secp::{self, aggsig, Message, Secp256k1, Signature};
use grin_keychain::SwitchCommitmentType;
use keychain::{BlindingFactor, Identifier, Keychain, SwitchCommitmentType};
use util::secp::key::{PublicKey, SecretKey};
use util::secp::pedersen::Commitment;
use util::secp::{self, aggsig, Message, Secp256k1, Signature};

/// Creates a new secure nonce (as a SecretKey), guaranteed to be usable during
/// aggsig creation.
@@ -34,7 +33,6 @@ use grin_keychain::SwitchCommitmentType;
///
/// ```
/// # extern crate grin_core as core;
/// # extern crate grin_util as util;
/// use core::libtx::aggsig;
/// use util::secp::{ContextFlag, Secp256k1};
/// let secp = Secp256k1::with_caps(ContextFlag::SignOnly);
@@ -68,7 +66,6 @@ pub fn create_secnonce(secp: &Secp256k1) -> Result<SecretKey, Error> {
///
/// ```
/// # extern crate grin_core as core;
/// # extern crate grin_util as util;
/// # extern crate rand;
/// use rand::thread_rng;
/// use core::libtx::aggsig;
@@ -139,7 +136,6 @@ pub fn calculate_partial_sig(
///
/// ```
/// # extern crate grin_core as core;
/// # extern crate grin_util as util;
/// # extern crate rand;
/// use rand::thread_rng;
/// use core::libtx::aggsig;
@@ -223,9 +219,7 @@ pub fn verify_partial_sig(
/// # Example
///
/// ```
/// # extern crate grin_util as util;
/// # extern crate grin_core as core;
/// # extern crate grin_keychain as keychain;
/// use core::consensus::reward;
/// use util::secp::key::{PublicKey, SecretKey};
/// use util::secp::{ContextFlag, Secp256k1};
@@ -291,9 +285,7 @@ where
/// # Example
///
/// ```
/// # extern crate grin_util as util;
/// # extern crate grin_core as core;
/// # extern crate grin_keychain as keychain;
/// use core::consensus::reward;
/// use core::libtx::{aggsig, proof};
/// use util::secp::key::{PublicKey, SecretKey};
@@ -367,7 +359,6 @@ pub fn verify_single_from_commit(
///
/// ```
/// # extern crate grin_core as core;
/// # extern crate grin_util as util;
/// # extern crate rand;
/// use rand::thread_rng;
/// use core::libtx::aggsig;
Loading

0 comments on commit 43bd5a5

Please sign in to comment.