-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup retrieval server and client (#681)
- Loading branch information
Showing
14 changed files
with
667 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license-file.workspace = true | ||
name = "polka-fetch" | ||
repository.workspace = true | ||
version = "0.1.0" | ||
|
||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
cid = { workspace = true } | ||
clap = { workspace = true, features = ["derive"] } | ||
libp2p = { workspace = true } | ||
multihash-codetable = { workspace = true, features = ["sha2"] } | ||
polka-storage-retrieval = { workspace = true } | ||
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } | ||
tracing = { workspace = true } | ||
tracing-subscriber = { workspace = true, features = ["env-filter"] } | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::{path::PathBuf, time::Duration}; | ||
|
||
use cid::Cid; | ||
use clap::{command, Parser}; | ||
use libp2p::Multiaddr; | ||
use polka_storage_retrieval::client::Client; | ||
use tokio::time::timeout; | ||
use tracing::{error, info, level_filters::LevelFilter}; | ||
use tracing_subscriber::{ | ||
filter::FromEnvError, fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, | ||
}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command()] | ||
struct Cli { | ||
/// Provider used for data download | ||
#[arg(long)] | ||
provider: Multiaddr, | ||
/// The CAR file to write to. | ||
#[arg(long)] | ||
output: PathBuf, | ||
/// Cancel the download if not completed after the specified duration in | ||
/// seconds. If not set the download will never timeout. | ||
#[arg(long, value_parser = parse_duration)] | ||
timeout: Option<Duration>, | ||
/// payload CID | ||
#[arg(long)] | ||
payload_cid: Cid, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), anyhow::Error> { | ||
setup_tracing()?; | ||
|
||
let arguments = Cli::parse(); | ||
|
||
let client = Client::new( | ||
arguments.output, | ||
vec![arguments.provider], | ||
vec![arguments.payload_cid], | ||
) | ||
.await?; | ||
|
||
let download_result = match arguments.timeout { | ||
Some(duration) => timeout(duration, client.download()).await, | ||
None => Ok(client.download().await), | ||
}; | ||
|
||
match download_result { | ||
Ok(Ok(_)) => info!("download successfully finished"), | ||
Ok(Err(err)) => error!(?err, "error occurred while downloading"), | ||
Err(_) => error!("download timeout"), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn parse_duration(arg: &str) -> Result<Duration, String> { | ||
let seconds = arg | ||
.parse() | ||
.map_err(|err| format!("failed to parse duration from string: {}", err))?; | ||
Ok(Duration::from_secs(seconds)) | ||
} | ||
|
||
/// Configure and initialize tracing. | ||
fn setup_tracing() -> Result<(), FromEnvError> { | ||
tracing_subscriber::registry() | ||
.with( | ||
fmt::layer().with_filter( | ||
EnvFilter::builder() | ||
.with_default_directive(if cfg!(debug_assertions) { | ||
LevelFilter::DEBUG.into() | ||
} else { | ||
LevelFilter::WARN.into() | ||
}) | ||
.from_env()?, | ||
), | ||
) | ||
.init(); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license-file.workspace = true | ||
name = "polka-storage-retrieval" | ||
repository.workspace = true | ||
version = "0.1.0" | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
beetswap = { workspace = true } | ||
blockstore = { workspace = true } | ||
cid = { workspace = true } | ||
futures = { workspace = true } | ||
ipld-core = { workspace = true, features = ["serde"] } | ||
ipld-dagpb.workspace = true | ||
libp2p = { workspace = true, features = ["macros", "noise", "tcp", "tokio", "yamux"] } | ||
libp2p-core = { workspace = true } | ||
libp2p-swarm = { workspace = true } | ||
mater = { workspace = true, features = ["blockstore"] } | ||
polka-index = { workspace = true } | ||
thiserror = { workspace = true } | ||
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "sync", "time"] } | ||
tracing = { workspace = true } | ||
|
||
[dev-dependencies] | ||
multihash-codetable = { workspace = true, features = ["sha2"] } | ||
tracing-appender = { workspace = true } | ||
tracing-subscriber = { workspace = true, features = ["env-filter"] } |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! The example showcases how to setup a retrieval server with the simple | ||
//! blockstore. Because the server is simple it is used for manual testing of | ||
//! the retrieval client. | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use libp2p::Multiaddr; | ||
use mater::FileBlockstore; | ||
use polka_storage_retrieval::server::Server; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Init tracing | ||
let _guard = init_tracing(); | ||
|
||
// Example blockstore providing only a single file. | ||
let blockstore = Arc::new( | ||
FileBlockstore::from_existing("./mater/lib/tests/fixtures/car_v2/spaceglenda_wrapped.car") | ||
.await?, | ||
); | ||
|
||
// Setup & run the server | ||
let server = Server::new(blockstore)?; | ||
let listener: Multiaddr = format!("/ip4/127.0.0.1/tcp/8989").parse()?; | ||
server.run(vec![listener]).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn init_tracing() -> tracing_appender::non_blocking::WorkerGuard { | ||
let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout()); | ||
|
||
let filter = tracing_subscriber::EnvFilter::builder() | ||
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into()) | ||
.from_env_lossy(); | ||
|
||
tracing_subscriber::fmt() | ||
.event_format( | ||
tracing_subscriber::fmt::format() | ||
.with_file(true) | ||
.with_line_number(true), | ||
) | ||
.with_env_filter(filter) | ||
.with_writer(non_blocking) | ||
.init(); | ||
|
||
guard | ||
} |
Oops, something went wrong.