refactor: remove the generic from IpfsOptions, RepoOptions and SwarmOptions
Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
parent
b95cb29650
commit
f8674a0504
@ -79,17 +79,15 @@ _Note: binaries available via `cargo install` is coming soon._
|
||||
```rust,no_run
|
||||
use async_std::task;
|
||||
use futures::join;
|
||||
use ipfs::{IpfsOptions, IpfsPath, Ipld, Types, UninitializedIpfs};
|
||||
use ipfs::{Ipfs, IpfsOptions, IpfsPath, Ipld, Types, UninitializedIpfs};
|
||||
use libipld::ipld;
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = IpfsOptions::<Types>::default();
|
||||
|
||||
task::block_on(async move {
|
||||
// Start daemon and initialize repo
|
||||
let (ipfs, fut) = UninitializedIpfs::new(options, None).await.start().await.unwrap();
|
||||
let (ipfs, fut): (Ipfs<Types>, _) = UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
// Create a DAG
|
||||
|
@ -1,19 +1,14 @@
|
||||
use async_std::task;
|
||||
use futures::join;
|
||||
use ipfs::{IpfsOptions, Types, UninitializedIpfs};
|
||||
use ipfs::{Ipfs, TestTypes, UninitializedIpfs};
|
||||
use libipld::ipld;
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = IpfsOptions::<Types>::default();
|
||||
|
||||
task::block_on(async move {
|
||||
let (ipfs, fut) = UninitializedIpfs::new(options, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
let f1 = ipfs.put_dag(ipld!("block1"));
|
||||
|
@ -1,21 +1,17 @@
|
||||
use async_std::task;
|
||||
use futures::join;
|
||||
use ipfs::{IpfsOptions, IpfsPath, TestTypes, UninitializedIpfs};
|
||||
use ipfs::{Ipfs, IpfsPath, TestTypes, UninitializedIpfs};
|
||||
use std::str::FromStr;
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = IpfsOptions::<TestTypes>::default();
|
||||
let path =
|
||||
IpfsPath::from_str("/ipfs/zdpuB1caPcm4QNXeegatVfLQ839Lmprd5zosXGwRUBJHwj66X").unwrap();
|
||||
|
||||
task::block_on(async move {
|
||||
let (ipfs, fut) = UninitializedIpfs::new(options, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
let f1 = ipfs.get_dag(path.sub_path("0").unwrap());
|
||||
|
@ -4,7 +4,7 @@ use cid::Cid;
|
||||
use futures::io::AsyncWriteExt;
|
||||
use futures::pin_mut;
|
||||
use futures::stream::StreamExt; // needed for StreamExt::next
|
||||
use ipfs::{IpfsOptions, TestTypes, UninitializedIpfs};
|
||||
use ipfs::{Ipfs, TestTypes, UninitializedIpfs};
|
||||
use std::convert::TryFrom;
|
||||
use std::env;
|
||||
use std::process::exit;
|
||||
@ -12,8 +12,6 @@ use std::process::exit;
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = IpfsOptions::<TestTypes>::default();
|
||||
|
||||
// this example will wait forever attempting to fetch a CID provided at command line. It is
|
||||
// expected to be used by connecting another ipfs peer to it and providing the blocks from that
|
||||
// peer.
|
||||
@ -40,11 +38,8 @@ fn main() {
|
||||
|
||||
async_std::task::block_on(async move {
|
||||
// Start daemon and initialize repo
|
||||
let (ipfs, fut) = UninitializedIpfs::new(options, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
async_std::task::spawn(fut);
|
||||
|
||||
let (public_key, addresses) = ipfs.identity().await.unwrap();
|
||||
|
@ -2,25 +2,20 @@
|
||||
|
||||
use async_std::task;
|
||||
use cid::{Cid, Codec};
|
||||
use ipfs::{Block, IpfsOptions, TestTypes, UninitializedIpfs};
|
||||
use ipfs::{Block, Ipfs, TestTypes, UninitializedIpfs};
|
||||
use multihash::Sha2_256;
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = IpfsOptions::<TestTypes>::default();
|
||||
|
||||
// this example demonstrates
|
||||
// - block building
|
||||
// - local swarm communication with go-ipfs
|
||||
|
||||
task::block_on(async move {
|
||||
// Start daemon and initialize repo
|
||||
let (ipfs, fut) = UninitializedIpfs::new(options, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
let data = b"block-want\n".to_vec().into_boxed_slice();
|
||||
|
@ -1,19 +1,14 @@
|
||||
use async_std::task;
|
||||
use ipfs::{IpfsOptions, IpfsPath, PeerId, TestTypes, UninitializedIpfs};
|
||||
use ipfs::{Ipfs, IpfsPath, PeerId, TestTypes, UninitializedIpfs};
|
||||
use std::str::FromStr;
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = IpfsOptions::<TestTypes>::default();
|
||||
|
||||
task::block_on(async move {
|
||||
// Start daemon and initialize repo
|
||||
let (ipfs, fut) = UninitializedIpfs::new(options, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
// Create a Block
|
||||
|
@ -134,10 +134,10 @@ fn main() {
|
||||
let mut rt = tokio::runtime::Runtime::new().expect("Failed to create event loop");
|
||||
|
||||
rt.block_on(async move {
|
||||
let opts: IpfsOptions<ipfs::TestTypes> =
|
||||
let opts: IpfsOptions =
|
||||
IpfsOptions::new(home.clone().into(), keypair, Vec::new(), false, None);
|
||||
|
||||
let (ipfs, task) = UninitializedIpfs::new(opts, None)
|
||||
let (ipfs, task): (Ipfs<ipfs::TestTypes>, _) = UninitializedIpfs::new(opts, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
|
@ -156,6 +156,7 @@ async fn not_implemented() -> Result<(impl warp::Reply,), std::convert::Infallib
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ipfs::{Ipfs, TestTypes};
|
||||
/// Creates routes for tests, the ipfs will not work as no background task is being spawned.
|
||||
async fn testing_routes(
|
||||
) -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
||||
@ -163,7 +164,7 @@ mod tests {
|
||||
use ipfs::{IpfsOptions, UninitializedIpfs};
|
||||
|
||||
let options = IpfsOptions::inmemory_with_generated_keys();
|
||||
let (ipfs, _) = UninitializedIpfs::new(options, None)
|
||||
let (ipfs, _): (Ipfs<TestTypes>, _) = UninitializedIpfs::new(options, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
|
33
src/lib.rs
33
src/lib.rs
@ -25,7 +25,6 @@ use std::borrow::Borrow;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::future::Future;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Range;
|
||||
use std::pin::Pin;
|
||||
use std::sync::{atomic::Ordering, Arc};
|
||||
@ -76,8 +75,7 @@ impl RepoTypes for TestTypes {
|
||||
|
||||
/// Ipfs options
|
||||
#[derive(Clone)]
|
||||
pub struct IpfsOptions<Types: IpfsTypes> {
|
||||
_marker: PhantomData<Types>,
|
||||
pub struct IpfsOptions {
|
||||
/// The path of the ipfs repo.
|
||||
pub ipfs_path: PathBuf,
|
||||
/// The keypair used with libp2p.
|
||||
@ -90,7 +88,7 @@ pub struct IpfsOptions<Types: IpfsTypes> {
|
||||
pub kad_protocol: Option<String>,
|
||||
}
|
||||
|
||||
impl<Types: IpfsTypes> fmt::Debug for IpfsOptions<Types> {
|
||||
impl fmt::Debug for IpfsOptions {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// needed since libp2p::identity::Keypair does not have a Debug impl, and the IpfsOptions
|
||||
// is a struct with all public fields, so don't enforce users to use this wrapper.
|
||||
@ -104,11 +102,10 @@ impl<Types: IpfsTypes> fmt::Debug for IpfsOptions<Types> {
|
||||
}
|
||||
}
|
||||
|
||||
impl IpfsOptions<TestTypes> {
|
||||
impl IpfsOptions {
|
||||
/// Creates an inmemory store backed node for tests
|
||||
pub fn inmemory_with_generated_keys() -> Self {
|
||||
Self {
|
||||
_marker: PhantomData,
|
||||
ipfs_path: std::env::temp_dir().into(),
|
||||
keypair: Keypair::generate_ed25519(),
|
||||
mdns: Default::default(),
|
||||
@ -141,7 +138,7 @@ impl<I: Borrow<Keypair>> DebuggableKeypair<I> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Types: IpfsTypes> IpfsOptions<Types> {
|
||||
impl IpfsOptions {
|
||||
pub fn new(
|
||||
ipfs_path: PathBuf,
|
||||
keypair: Keypair,
|
||||
@ -155,12 +152,11 @@ impl<Types: IpfsTypes> IpfsOptions<Types> {
|
||||
bootstrap,
|
||||
mdns,
|
||||
kad_protocol,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: IpfsTypes> Default for IpfsOptions<T> {
|
||||
impl Default for IpfsOptions {
|
||||
/// Create `IpfsOptions` from environment.
|
||||
///
|
||||
/// # Panics
|
||||
@ -205,7 +201,6 @@ impl<T: IpfsTypes> Default for IpfsOptions<T> {
|
||||
bootstrap,
|
||||
mdns: true,
|
||||
kad_protocol: None,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -271,7 +266,7 @@ pub struct UninitializedIpfs<Types: IpfsTypes> {
|
||||
repo: Repo<Types>,
|
||||
span: Span,
|
||||
keys: Keypair,
|
||||
options: IpfsOptions<Types>,
|
||||
options: IpfsOptions,
|
||||
repo_events: Receiver<RepoEvent>,
|
||||
}
|
||||
|
||||
@ -282,8 +277,8 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
|
||||
/// The span is attached to all operations called on the later created `Ipfs` along with all
|
||||
/// operations done in the background task as well as tasks spawned by the underlying
|
||||
/// `libp2p::Swarm`.
|
||||
pub async fn new(options: IpfsOptions<Types>, span: Option<Span>) -> Self {
|
||||
let repo_options = RepoOptions::<Types>::from(&options);
|
||||
pub async fn new(options: IpfsOptions, span: Option<Span>) -> Self {
|
||||
let repo_options = RepoOptions::from(&options);
|
||||
let (repo, repo_events) = create_repo(repo_options);
|
||||
let keys = options.keypair.clone();
|
||||
let span = span.unwrap_or_else(|| tracing::trace_span!("ipfs"));
|
||||
@ -297,6 +292,10 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn default() -> Self {
|
||||
Self::new(IpfsOptions::default(), None).await
|
||||
}
|
||||
|
||||
/// Initialize the ipfs node. The returned `Ipfs` value is cloneable, send and sync, and the
|
||||
/// future should be spawned on a executor as soon as possible.
|
||||
pub async fn start(self) -> Result<(Ipfs<Types>, impl Future<Output = ()>), Error> {
|
||||
@ -321,7 +320,7 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
|
||||
to_task,
|
||||
}));
|
||||
|
||||
let swarm_options = SwarmOptions::<Types>::from(&self.options);
|
||||
let swarm_options = SwarmOptions::from(&self.options);
|
||||
let swarm = create_swarm(swarm_options, ipfs.clone()).await;
|
||||
|
||||
let fut = IpfsFuture {
|
||||
@ -1071,10 +1070,10 @@ mod node {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn with_options(opts: IpfsOptions<TestTypes>) -> Self {
|
||||
pub async fn with_options(opts: IpfsOptions) -> Self {
|
||||
let span = Some(Span::current());
|
||||
|
||||
let (ipfs, fut) = UninitializedIpfs::new(opts, span)
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) = UninitializedIpfs::new(opts, span)
|
||||
.in_current_span()
|
||||
.await
|
||||
.start()
|
||||
@ -1312,6 +1311,6 @@ mod tests {
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn default_ipfs_options_disabled_when_testing() {
|
||||
IpfsOptions::<TestTypes>::default();
|
||||
IpfsOptions::default();
|
||||
}
|
||||
}
|
||||
|
@ -336,7 +336,7 @@ impl<Types: IpfsTypes> NetworkBehaviourEventProcess<IdentifyEvent> for Behaviour
|
||||
|
||||
impl<Types: IpfsTypes> Behaviour<Types> {
|
||||
/// Create a Kademlia behaviour with the IPFS bootstrap nodes.
|
||||
pub async fn new(options: SwarmOptions<Types>, ipfs: Ipfs<Types>) -> Self {
|
||||
pub async fn new(options: SwarmOptions, ipfs: Ipfs<Types>) -> Self {
|
||||
info!("net: starting with peer id {}", options.peer_id);
|
||||
|
||||
let mdns = if options.mdns {
|
||||
@ -480,7 +480,7 @@ impl<Types: IpfsTypes> Behaviour<Types> {
|
||||
|
||||
/// Create a IPFS behaviour with the IPFS bootstrap nodes.
|
||||
pub async fn build_behaviour<TIpfsTypes: IpfsTypes>(
|
||||
options: SwarmOptions<TIpfsTypes>,
|
||||
options: SwarmOptions,
|
||||
ipfs: Ipfs<TIpfsTypes>,
|
||||
) -> Behaviour<TIpfsTypes> {
|
||||
Behaviour::new(options, ipfs).await
|
||||
|
@ -1,6 +1,5 @@
|
||||
//! P2P handling for IPFS nodes.
|
||||
use crate::{Ipfs, IpfsOptions, IpfsTypes};
|
||||
use core::marker::PhantomData;
|
||||
use libp2p::identity::Keypair;
|
||||
use libp2p::Swarm;
|
||||
use libp2p::{Multiaddr, PeerId};
|
||||
@ -15,8 +14,7 @@ pub use swarm::{Connection, ConnectionTarget};
|
||||
|
||||
pub type TSwarm<T> = Swarm<behaviour::Behaviour<T>>;
|
||||
|
||||
pub struct SwarmOptions<TIpfsTypes: IpfsTypes> {
|
||||
_marker: PhantomData<TIpfsTypes>,
|
||||
pub struct SwarmOptions {
|
||||
pub keypair: Keypair,
|
||||
pub peer_id: PeerId,
|
||||
pub bootstrap: Vec<(Multiaddr, PeerId)>,
|
||||
@ -24,8 +22,8 @@ pub struct SwarmOptions<TIpfsTypes: IpfsTypes> {
|
||||
pub kad_protocol: Option<String>,
|
||||
}
|
||||
|
||||
impl<TIpfsTypes: IpfsTypes> From<&IpfsOptions<TIpfsTypes>> for SwarmOptions<TIpfsTypes> {
|
||||
fn from(options: &IpfsOptions<TIpfsTypes>) -> Self {
|
||||
impl From<&IpfsOptions> for SwarmOptions {
|
||||
fn from(options: &IpfsOptions) -> Self {
|
||||
let keypair = options.keypair.clone();
|
||||
let peer_id = keypair.public().into_peer_id();
|
||||
let bootstrap = options.bootstrap.clone();
|
||||
@ -33,7 +31,6 @@ impl<TIpfsTypes: IpfsTypes> From<&IpfsOptions<TIpfsTypes>> for SwarmOptions<TIpf
|
||||
let kad_protocol = options.kad_protocol.clone();
|
||||
|
||||
SwarmOptions {
|
||||
_marker: PhantomData,
|
||||
keypair,
|
||||
peer_id,
|
||||
bootstrap,
|
||||
@ -45,7 +42,7 @@ impl<TIpfsTypes: IpfsTypes> From<&IpfsOptions<TIpfsTypes>> for SwarmOptions<TIpf
|
||||
|
||||
/// Creates a new IPFS swarm.
|
||||
pub async fn create_swarm<TIpfsTypes: IpfsTypes>(
|
||||
options: SwarmOptions<TIpfsTypes>,
|
||||
options: SwarmOptions,
|
||||
ipfs: Ipfs<TIpfsTypes>,
|
||||
) -> TSwarm<TIpfsTypes> {
|
||||
let peer_id = options.peer_id.clone();
|
||||
|
@ -9,7 +9,6 @@ use bitswap::Block;
|
||||
use cid::{self, Cid};
|
||||
use core::convert::TryFrom;
|
||||
use core::fmt::Debug;
|
||||
use core::marker::PhantomData;
|
||||
use futures::channel::{
|
||||
mpsc::{channel, Receiver, Sender},
|
||||
oneshot,
|
||||
@ -27,22 +26,20 @@ pub trait RepoTypes: Send + Sync + 'static {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RepoOptions<TRepoTypes: RepoTypes> {
|
||||
_marker: PhantomData<TRepoTypes>,
|
||||
pub struct RepoOptions {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl<TRepoTypes: RepoTypes> From<&IpfsOptions<TRepoTypes>> for RepoOptions<TRepoTypes> {
|
||||
fn from(options: &IpfsOptions<TRepoTypes>) -> Self {
|
||||
impl From<&IpfsOptions> for RepoOptions {
|
||||
fn from(options: &IpfsOptions) -> Self {
|
||||
RepoOptions {
|
||||
_marker: PhantomData,
|
||||
path: options.ipfs_path.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_repo<TRepoTypes: RepoTypes>(
|
||||
options: RepoOptions<TRepoTypes>,
|
||||
options: RepoOptions,
|
||||
) -> (Repo<TRepoTypes>, Receiver<RepoEvent>) {
|
||||
Repo::new(options)
|
||||
}
|
||||
@ -151,7 +148,7 @@ impl TryFrom<RequestKind> for RepoEvent {
|
||||
}
|
||||
|
||||
impl<TRepoTypes: RepoTypes> Repo<TRepoTypes> {
|
||||
pub fn new(options: RepoOptions<TRepoTypes>) -> (Self, Receiver<RepoEvent>) {
|
||||
pub fn new(options: RepoOptions) -> (Self, Receiver<RepoEvent>) {
|
||||
let mut blockstore_path = options.path.clone();
|
||||
let mut datastore_path = options.path;
|
||||
blockstore_path.push("blockstore");
|
||||
@ -369,10 +366,7 @@ pub(crate) mod tests {
|
||||
pub fn create_mock_repo() -> (Repo<Types>, Receiver<RepoEvent>) {
|
||||
let mut tmp = temp_dir();
|
||||
tmp.push("rust-ipfs-repo");
|
||||
let options: RepoOptions<Types> = RepoOptions {
|
||||
_marker: PhantomData,
|
||||
path: tmp.into(),
|
||||
};
|
||||
let options: RepoOptions = RepoOptions { path: tmp.into() };
|
||||
Repo::new(options)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user