Skip to content

Commit

Permalink
refactor: crate structures
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 15, 2023
1 parent 4c116c2 commit 14c9482
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 338 deletions.
2 changes: 1 addition & 1 deletion subfile-exchange/benches/read_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::{fs::File, path::PathBuf};
use subfile_exchange::file_reader::read_chunk;
use subfile_exchange::subfile::file_reader::read_chunk;

fn read_chunk_benchmark(c: &mut Criterion) {
let file_path = black_box(PathBuf::from("../example-file/0017234600.dbin.zst"));
Expand Down
47 changes: 1 addition & 46 deletions subfile-exchange/benches/verify_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,11 @@ use criterion::black_box;
use criterion::Criterion;

use criterion::{criterion_group, criterion_main};
use subfile_exchange::file_hasher::verify_chunk;
use subfile_exchange::file_reader::read_chunk;
use subfile_exchange::subfile::{file_hasher::verify_chunk, file_reader::read_chunk};
use subfile_exchange::test_util::simple_chunk_file;

fn verify_chunk_benchmark(c: &mut Criterion) {
let file_path = black_box(PathBuf::from("../example-file/0017234600.dbin.zst"));
// let file = black_box(File::open(&file_path).unwrap());

// let file_size = black_box(file.metadata().map(|d| d.len()).unwrap());

// // Define different test ranges
// let ranges = black_box(vec![
// (0, 999), // Small chunk from start
// (file_size / 3, file_size / 2), // partial chunk from middle
// (file_size - 1000, file_size - 1), // Small chunk from end
// ]);

// c.bench_function("read_chunk", |b| {
// b.iter(|| {
// for &(start, end) in &ranges {
// let data = black_box(read_chunk(&file_path, (start, end)).unwrap());

// }
// })
// });

let chunk_file = black_box(simple_chunk_file());
// read a chunk
let (start, end) = black_box((
Expand All @@ -46,27 +25,3 @@ fn verify_chunk_benchmark(c: &mut Criterion) {

criterion_group!(benches, verify_chunk_benchmark);
criterion_main!(benches);

// pub const SUBFILE_MANIFEST = r#"files:
// - name: example-create-17686085.dbin
// hash: QmSgzLLsQzdRAQRA2d7X3wqLEUTBLSbRe2tqv9rJBy7Wqv
// - name: 0017234500.dbin.zst
// hash: Qmexz4ZariJteKHHXMxsSeSjvyLZf7SUWz77bsvLUQG1Vn
// - name: 0017234600.dbin.zst
// hash: QmadNB1AQnap3czUime3gEETBNUj7HHzww6hVh5F6w7Boo
// - name: 0017686111-c1ed20dc4cffd7bd-ebfe6d2b6a25625a-17686021-default.dbin
// hash: QmSEDiCKax7rjxS3kvGJ3dPdHkm2bztFZkR5KDqfpgyuQw
// - name: 0017686115-f8d105f60fa2e78d-7d23a3e458beaff1-17686021-default.dbin
// hash: QmVx3JX5TNrSqMKyP5xQJ2CYmcqG4VaBdPnbji3PuvUFx6
// file_type: flatfiles
// spec_version: 0.0.0
// description: random flatfiles
// chain_id: '0'
// block_range:
// start_block: null
// end_block: null";

// pub fn init_logger() {
// env::set_var("RUST_LOG", "warn,subfile_exchange=trace");
// init_tracing(String::from("pretty")).unwrap();
// }
4 changes: 0 additions & 4 deletions subfile-exchange/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
pub mod config;
pub mod errors;
pub mod file_hasher;
pub mod file_reader;
pub mod ipfs;
pub mod publisher;
pub mod subfile;
pub mod subfile_client;
pub mod subfile_finder;
pub mod subfile_reader;
pub mod test_util;
pub mod util;
2 changes: 1 addition & 1 deletion subfile-exchange/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use dotenv::dotenv;

use subfile_exchange::{
config::{Cli, Role},
ipfs::IpfsClient,
publisher::SubfilePublisher,
subfile::ipfs::IpfsClient,
subfile_client::SubfileDownloader,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use serde_yaml::to_string;

use crate::config::PublisherArgs;
use crate::errors::Error;
use crate::ipfs::{AddResponse, IpfsClient};
use crate::subfile::{BlockRange, ChunkFile, FileMetaInfo, SubfileManifest};
use crate::subfile::{
ipfs::{AddResponse, IpfsClient},
BlockRange, ChunkFile, FileMetaInfo, SubfileManifest,
};

pub struct SubfilePublisher {
ipfs_client: IpfsClient,
Expand Down Expand Up @@ -87,10 +89,6 @@ impl SubfilePublisher {

pub async fn publish(&self) -> Result<String, Error> {
let meta_info = self.hash_and_publish_files().await?;
// {
// Ok(added_hashes) => added_hashes,
// Err(e) => return Err(e),
// };

tracing::trace!(
meta_info = tracing::field::debug(&meta_info),
Expand All @@ -105,11 +103,6 @@ impl SubfilePublisher {
);
Ok(ipfs_hash)
}
// {
// Ok(ipfs_hash) => {
// }
// Err(e) => Err(e),
// },
Err(e) => Err(e),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn verify_chunk(data: &Bytes, chunk_hash: &str) -> bool {

#[cfg(test)]
mod tests {
use crate::{file_reader::chunk_file, subfile::ChunkFile, test_util::*};
use crate::{subfile::file_reader::chunk_file, subfile::ChunkFile, test_util::*};
use std::path::Path;

#[test]
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pub mod file_hasher;
pub mod file_reader;
pub mod ipfs;
pub mod subfile_reader;

use std::{
path::{Path, PathBuf},
str::FromStr,
Expand All @@ -7,9 +12,11 @@ use serde::{Deserialize, Serialize};

use crate::{
errors::Error,
file_hasher::{hash_chunk, verify_chunk},
file_reader::{chunk_file, format_path, read_chunk},
ipfs::is_valid_ipfs_hash,
subfile::{
file_hasher::{hash_chunk, verify_chunk},
file_reader::{chunk_file, format_path, read_chunk},
ipfs::is_valid_ipfs_hash,
},
};

/* Public Manifests */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::de::DeserializeOwned;

use crate::{
errors::Error,
ipfs::IpfsClient,
subfile::ipfs::IpfsClient,
subfile::{ChunkFile, ChunkFileMeta, Subfile, SubfileManifest},
};

Expand Down
8 changes: 4 additions & 4 deletions subfile-exchange/src/subfile_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use tokio::sync::Mutex;

use crate::config::DownloaderArgs;
use crate::errors::Error;
use crate::file_hasher::verify_chunk;
use crate::ipfs::IpfsClient;
use crate::subfile::{ChunkFileMeta, Subfile};
use crate::subfile::{
file_hasher::verify_chunk, ipfs::IpfsClient, subfile_reader::read_subfile, ChunkFileMeta,
Subfile,
};
use crate::subfile_finder::{IndexerEndpoint, SubfileFinder};
use crate::subfile_reader::read_subfile;
use crate::util::build_wallet;

use self::signer::ReceiptSigner;
Expand Down
7 changes: 4 additions & 3 deletions subfile-exchange/src/subfile_finder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use tokio::sync::Mutex;

use crate::errors::Error;

use crate::ipfs::IpfsClient;

use crate::subfile_reader::{fetch_subfile_from_ipfs, read_subfile};
use crate::subfile::{
ipfs::IpfsClient,
subfile_reader::{fetch_subfile_from_ipfs, read_subfile},
};

// Pair indexer operator address and indexer service endpoint (operator, indexer_url)
// persumeably this should not be handled by clients themselves
Expand Down
2 changes: 1 addition & 1 deletion subfile-exchange/tests/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use std::{process::Command, time::Duration};

use subfile_exchange::{
ipfs::IpfsClient,
subfile::ipfs::IpfsClient,
subfile_finder::{unavailable_files, FileAvailbilityMap, IndexerEndpoint, SubfileFinder},
test_util::server_ready,
};
Expand Down
6 changes: 3 additions & 3 deletions subfile-exchange/tests/file_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod tests {
use tokio::fs;

use subfile_exchange::{
config::DownloaderArgs, ipfs::IpfsClient, subfile_client::SubfileDownloader,
config::DownloaderArgs, subfile::ipfs::IpfsClient, subfile_client::SubfileDownloader,
test_util::server_ready,
};

Expand All @@ -21,8 +21,8 @@ mod tests {
let mut server_process = Command::new("cargo")
.arg("run")
.arg("-p")
.arg("subfile-exchange")
.arg("server")
.arg("subfile-service")
.arg("--")
.arg("--mnemonic")
.arg("sheriff obscure trick beauty army fat wink legal flee leader section suit")
.arg("--subfiles")
Expand Down
6 changes: 3 additions & 3 deletions subfile-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing_subscriber::FmtSubscriber;
author = "hopeyen"
)]
#[command(author, version, about, long_about = None, arg_required_else_help = true)]
pub struct Cli {
pub struct Config {
#[command(flatten)]
pub server: ServerArgs,
#[arg(
Expand All @@ -36,10 +36,10 @@ pub struct Cli {
pub log_format: LogFormat,
}

impl Cli {
impl Config {
/// Parse config arguments
pub fn args() -> Self {
let config = Cli::parse();
let config = Config::parse();
// Enables tracing under RUST_LOG variable
init_tracing(config.log_format.to_string()).expect("Could not set up global default subscriber for logger, check environmental variable `RUST_LOG` or the CLI input `log-level`");
config
Expand Down
75 changes: 0 additions & 75 deletions subfile-service/src/errors.rs

This file was deleted.

15 changes: 3 additions & 12 deletions subfile-service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
use dotenv::dotenv;

use subfile_exchange::ipfs::IpfsClient;
use subfile_service::{config::Cli, subfile_server::init_server};
use subfile_service::{config::Config, subfile_server::init_server};

#[tokio::main]
async fn main() {
dotenv().ok();
let cli: Cli = Cli::args();
let config: Config = Config::args();

tracing::info!(cli = tracing::field::debug(&cli), "Running cli");

let client = if let Ok(client) = IpfsClient::new(&cli.ipfs_gateway) {
client
} else {
IpfsClient::localhost()
};

let _ = init_server(&client, cli.server).await;
let _ = init_server(config).await;
}
4 changes: 2 additions & 2 deletions subfile-service/src/subfile_server/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use hyper::{Body, Request, Response, StatusCode};
use serde_json::{json, Value};
use subfile_exchange::errors::ServerError;
use subfile_exchange::{
errors::Error, ipfs::is_valid_ipfs_hash, subfile::validate_subfile_entries,
subfile_reader::read_subfile,
errors::Error,
subfile::{ipfs::is_valid_ipfs_hash, subfile_reader::read_subfile, validate_subfile_entries},
};

use super::{create_error_response, ServerContext};
Expand Down
Loading

0 comments on commit 14c9482

Please sign in to comment.