mod common;
use std::sync::Arc;
use anyhow::{Context, Result};
use decdn_client::{NoCache, ProgressClock, PullConfig, StaticSources, StopPolicy, Streamer};
use common::{Buyer, Env, NoFunding};
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.init();
let hash = blake3::Hash::from_hex(
std::env::args()
.nth(1)
.context("pass the blob's BLAKE3 hash")?,
)?;
let buyer = Buyer::connect(Env::from_env()?).await?;
let (holders, total_bytes) = buyer.holders(*hash.as_bytes()).await?;
let (candidates, lanes) = buyer.lanes(holders).await?;
let scratch = tempfile::tempdir()?;
let streamer = Streamer::new(StaticSources::new(candidates)?, NoFunding, scratch.path());
let stop = StopPolicy::new(
std::io::IsTerminal::is_terminal(&std::io::stderr()),
None,
Arc::new(ProgressClock::new()),
);
let (mut reader, mut drive) = streamer
.open(
*hash.as_bytes(),
total_bytes,
&PullConfig::new(),
Arc::new(NoCache),
stop,
)
.await?;
let mut stdout = tokio::io::stdout();
let copied = drive
.alongside(tokio::io::copy(&mut reader, &mut stdout))
.await;
buyer.record_payments(&lanes);
copied?;
Ok(())
}