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