Skip to content

Commit

Permalink
Merge pull request #43 from a-moreira/style/code-style-improvements
Browse files Browse the repository at this point in the history
Code style improvements and lint fixes.
  • Loading branch information
Davidson-Souza authored Jan 15, 2024
2 parents b590e73 + 3e76bb5 commit fdabb1e
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 327 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ jobs:
linting:

runs-on: ubuntu-latest

steps:
# Install Rust nightly toolchain
- uses: actions/checkout@v3
- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
with:
profile: default
toolchain: nightly
override: true

# cargo fmt
- uses: actions/checkout@v3
- name: fmt
run: cargo fmt --all --check
run: cargo +nightly fmt --all --check

# run cargo clippy
- uses: actions/checkout@v3
- name: Clippy
run: cargo clippy --all
run: cargo +nightly clippy --all

cross-testing:
strategy:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
target
target/

Cargo.lock
13 changes: 13 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# imports
reorder_imports = true
imports_granularity = "Item"
group_imports = "StdExternalCrate"

# lines
newline_style = "Unix"

# comments
format_code_in_doc_comments = true

# macros
format_macro_matchers = true
103 changes: 0 additions & 103 deletions Cargo.lock

This file was deleted.

6 changes: 4 additions & 2 deletions examples/full-accumulator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! An example of a full accumulator. A full accumulator is an accumulator that holds all the
//! elements in the set. It's meant for full nodes, that need to prove membership of arbitrary
//! elements. Clients that only need to verify membership should use a Stump instead.
//!
use std::str::FromStr;

use rustreexo::accumulator::{node_hash::NodeHash, pollard::Pollard, proof::Proof, stump::Stump};
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::pollard::Pollard;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;

fn main() {
let elements = vec![
Expand Down
4 changes: 3 additions & 1 deletion examples/proof-update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use std::str::FromStr;

use rustreexo::accumulator::{node_hash::NodeHash, proof::Proof, stump::Stump};
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;

fn main() {
let s = Stump::new();
Expand Down
7 changes: 5 additions & 2 deletions examples/simple-stump-update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
//! the roots of the accumulator. It's meant for light clients, that don't need to prove membership
//! of arbitrary elements. Instead, they only need to verify.
use std::{str::FromStr, vec};
use std::str::FromStr;
use std::vec;

use rustreexo::accumulator::{node_hash::NodeHash, proof::Proof, stump::Stump};
use rustreexo::accumulator::node_hash::NodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;

fn main() {
// These are the utxos that we want to add to the Stump, in Bitcoin, these would be the
Expand Down
57 changes: 37 additions & 20 deletions src/accumulator/node_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
//! Building from a str
//! ```
//! use std::str::FromStr;
//!
//! use rustreexo::accumulator::node_hash::NodeHash;
//! let hash =
//! NodeHash::from_str("0000000000000000000000000000000000000000000000000000000000000000")
//! .unwrap();
//! NodeHash::from_str("0000000000000000000000000000000000000000000000000000000000000000")
//! .unwrap();
//! assert_eq!(
//! hash.to_string().as_str(),
//! "0000000000000000000000000000000000000000000000000000000000000000"
Expand All @@ -16,6 +17,7 @@
//! Building from a slice
//! ```
//! use std::str::FromStr;
//!
//! use rustreexo::accumulator::node_hash::NodeHash;
//! let hash1 = NodeHash::new([0; 32]);
//! // ... or ...
Expand All @@ -30,23 +32,31 @@
//! Computing a parent hash (i.e a hash of two nodes concatenated)
//! ```
//! use std::str::FromStr;
//!
//! use rustreexo::accumulator::node_hash::NodeHash;
//! let left = NodeHash::new([0; 32]);
//! let right = NodeHash::new([1; 32]);
//! let parent = NodeHash::parent_hash(&left, &right);
//! let expected_parent = NodeHash::from_str("34e33ca0c40b7bd33d28932ca9e35170def7309a3bf91ecda5e1ceb067548a12").unwrap();
//! let expected_parent =
//! NodeHash::from_str("34e33ca0c40b7bd33d28932ca9e35170def7309a3bf91ecda5e1ceb067548a12")
//! .unwrap();
//! assert_eq!(parent, expected_parent);
//! ```
use bitcoin_hashes::{hex, sha256, sha512_256, Hash, HashEngine};
use std::{
convert::TryFrom,
fmt::{Debug, Display},
ops::Deref,
str::FromStr,
};
use std::convert::TryFrom;
use std::fmt::Debug;
use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;

use bitcoin_hashes::hex;
use bitcoin_hashes::sha256;
use bitcoin_hashes::sha512_256;
use bitcoin_hashes::Hash;
use bitcoin_hashes::HashEngine;
#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};
use serde::Deserialize;
#[cfg(feature = "with-serde")]
use serde::Serialize;

#[derive(Eq, PartialEq, Copy, Clone, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
Expand All @@ -55,19 +65,19 @@ use serde::{Deserialize, Serialize};
/// ```
/// use rustreexo::accumulator::node_hash::NodeHash;
/// let hash = NodeHash::new([0; 32]);
/// assert_eq!(hash.to_string().as_str(), "0000000000000000000000000000000000000000000000000000000000000000");
/// assert_eq!(
/// hash.to_string().as_str(),
/// "0000000000000000000000000000000000000000000000000000000000000000"
/// );
/// ```
#[derive(Default)]
pub enum NodeHash {
#[default]
Empty,
Placeholder,
Some([u8; 32]),
}

impl Default for NodeHash {
fn default() -> Self {
NodeHash::Empty
}
}
impl Deref for NodeHash {
type Target = [u8; 32];

Expand Down Expand Up @@ -175,7 +185,10 @@ impl NodeHash {
/// ```
/// use rustreexo::accumulator::node_hash::NodeHash;
/// let hash = NodeHash::new([0; 32]);
/// assert_eq!(hash.to_string().as_str(), "0000000000000000000000000000000000000000000000000000000000000000");
/// assert_eq!(
/// hash.to_string().as_str(),
/// "0000000000000000000000000000000000000000000000000000000000000000"
/// );
/// ```
pub fn new(inner: [u8; 32]) -> Self {
NodeHash::Some(inner)
Expand All @@ -194,11 +207,14 @@ impl NodeHash {
/// # Example
/// ```
/// use std::str::FromStr;
///
/// use rustreexo::accumulator::node_hash::NodeHash;
/// let left = NodeHash::new([0; 32]);
/// let right = NodeHash::new([1; 32]);
/// let parent = NodeHash::parent_hash(&left, &right);
/// let expected_parent = NodeHash::from_str("34e33ca0c40b7bd33d28932ca9e35170def7309a3bf91ecda5e1ceb067548a12").unwrap();
/// let expected_parent =
/// NodeHash::from_str("34e33ca0c40b7bd33d28932ca9e35170def7309a3bf91ecda5e1ceb067548a12")
/// .unwrap();
/// assert_eq!(parent, expected_parent);
/// ```
pub fn parent_hash(left: &NodeHash, right: &NodeHash) -> NodeHash {
Expand All @@ -217,9 +233,10 @@ impl NodeHash {

#[cfg(test)]
mod test {
use std::str::FromStr;

use super::NodeHash;
use crate::accumulator::util::hash_from_u8;
use std::str::FromStr;

#[test]
fn test_parent_hash() {
Expand Down
Loading

0 comments on commit fdabb1e

Please sign in to comment.