Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calculate piece digest by blake3 #456

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions Cargo.lock

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

19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
]

[workspace.package]
version = "0.1.49"
version = "0.1.50"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
Expand All @@ -22,15 +22,15 @@ readme = "README.md"
edition = "2021"

[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "0.1.49" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.49" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.49" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.49" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.49" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.49" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.49" }
dragonfly-client = { path = "dragonfly-client", version = "0.1.50" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.50" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.50" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.50" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.50" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.50" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.50" }
thiserror = "1.0"
dragonfly-api = "2.0.110"
dragonfly-api = "2.0.112"
reqwest = { version = "0.11.27", features = ["stream", "native-tls", "default-tls", "rustls-tls"] }
rcgen = { version = "0.12.1", features = ["x509-parser"] }
hyper = { version = "1.2", features = ["full"] }
Expand All @@ -50,6 +50,7 @@ rustls = "0.22.2"
rustls-pki-types = "1.4.1"
rustls-pemfile = "2.1.1"
sha2 = "0.10"
blake3 = "1.5.1"
uuid = { version = "1.8", features = ["v4"] }
hex = "0.4"
rocksdb = "0.22.0"
Expand Down
1 change: 1 addition & 0 deletions dragonfly-client-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ prost-wkt-types.workspace = true
tokio.workspace = true
tokio-util.workspace = true
sha2.workspace = true
blake3.workspace = true
num_cpus = "1.0"
base16ct = { version = "0.2", features = ["alloc"] }

Expand Down
11 changes: 6 additions & 5 deletions dragonfly-client-storage/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use dragonfly_api::common::v2::Range;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::Result;
use sha2::{Digest, Sha256};
use std::cmp::{max, min};
use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand Down Expand Up @@ -245,11 +244,13 @@ impl Content {
// Use a buffer to read the piece.
let reader = BufReader::with_capacity(self.config.storage.write_buffer_size, reader);

// Sha256 is used to calculate the hash of the piece.
let mut hasher = Sha256::new();
// Blake3 is used to calculate the hash of the piece.
let mut hasher = blake3::Hasher::new();

// InspectReader is used to calculate the hash of the piece.
let mut tee = InspectReader::new(reader, |bytes| hasher.update(bytes));
let mut tee = InspectReader::new(reader, |bytes| {
hasher.update(bytes);
});

// Open the file and seek to the offset.
let mut f = OpenOptions::new()
Expand Down Expand Up @@ -279,7 +280,7 @@ impl Content {

Ok(WritePieceResponse {
length,
hash: base16ct::lower::encode_string(&hash),
hash: base16ct::lower::encode_string(hash.as_bytes()),
})
}
}
4 changes: 2 additions & 2 deletions dragonfly-client-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Storage {
reader: &mut R,
) -> Result<metadata::Piece> {
let response = self.content.write_piece(task_id, offset, reader).await?;
let digest = Digest::new(Algorithm::Sha256, response.hash);
let digest = Digest::new(Algorithm::Blake3, response.hash);

self.metadata.download_piece_finished(
task_id,
Expand All @@ -169,7 +169,7 @@ impl Storage {
) -> Result<metadata::Piece> {
let response = self.content.write_piece(task_id, offset, reader).await?;
let length = response.length;
let digest = Digest::new(Algorithm::Sha256, response.hash);
let digest = Digest::new(Algorithm::Blake3, response.hash);

// Check the digest of the piece.
if expected_digest != digest.to_string() {
Expand Down
6 changes: 6 additions & 0 deletions dragonfly-client-util/src/digest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub const SEPARATOR: &str = ":";
// Algorithm is a enum of the algorithm that is used to generate digest.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Algorithm {
// Blake3 is blake3 algorithm for generate digest.
Blake3,

// Sha256 is sha256 algorithm for generate digest.
Sha256,

Expand All @@ -35,6 +38,7 @@ impl fmt::Display for Algorithm {
// fmt formats the value using the given formatter.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Algorithm::Blake3 => write!(f, "blake3"),
Algorithm::Sha256 => write!(f, "sha256"),
Algorithm::Sha512 => write!(f, "sha512"),
}
Expand All @@ -48,6 +52,7 @@ impl FromStr for Algorithm {
// from_str parses a algorithm string.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"blake3" => Ok(Algorithm::Blake3),
"sha256" => Ok(Algorithm::Sha256),
"sha512" => Ok(Algorithm::Sha512),
_ => Err(format!("invalid digest algorithm: {}", s)),
Expand Down Expand Up @@ -102,6 +107,7 @@ impl FromStr for Digest {
}

let algorithm = match parts[0] {
"blake3" => Algorithm::Blake3,
"sha256" => Algorithm::Sha256,
"sha512" => Algorithm::Sha512,
_ => return Err(format!("invalid digest algorithm: {}", parts[0])),
Expand Down
Loading