Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Expose GetSyncPeers
Browse files Browse the repository at this point in the history
ppodolsky committed Mar 19, 2024
1 parent 7bb4bfc commit 1a6054e
Showing 4 changed files with 48 additions and 5 deletions.
16 changes: 12 additions & 4 deletions iroh/src/client.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ use iroh_bytes::{BlobFormat, Tag};
use iroh_net::{key::PublicKey, magic_endpoint::ConnectionInfo, NodeAddr};
use iroh_sync::actor::OpenState;
use iroh_sync::store::DownloadPolicy;
use iroh_sync::{store::Query, AuthorId, CapabilityKind, NamespaceId};
use iroh_sync::{store::Query, AuthorId, CapabilityKind, NamespaceId, PeerIdBytes};
use iroh_sync::{ContentStatus, RecordIdentifier};
use quic_rpc::message::RpcMsg;
use quic_rpc::{client::BoxStreamSync, RpcClient, ServiceConnection};
@@ -41,9 +41,9 @@ use crate::rpc_protocol::{
BlobReadAtRequest, BlobReadAtResponse, BlobValidateRequest, CounterStats,
CreateCollectionRequest, CreateCollectionResponse, DeleteTagRequest, DocCloseRequest,
DocCreateRequest, DocDelRequest, DocDelResponse, DocDropRequest, DocExportFileRequest,
DocGetDownloadPolicyRequest, DocGetExactRequest, DocGetManyRequest, DocImportFileRequest,
DocImportProgress, DocImportRequest, DocLeaveRequest, DocListRequest, DocOpenRequest,
DocSetDownloadPolicyRequest, DocSetHashRequest, DocSetRequest, DocShareRequest,
DocGetDownloadPolicyRequest, DocGetExactRequest, DocGetManyRequest, DocGetSyncPeersRequest,
DocImportFileRequest, DocImportProgress, DocImportRequest, DocLeaveRequest, DocListRequest,
DocOpenRequest, DocSetDownloadPolicyRequest, DocSetHashRequest, DocSetRequest, DocShareRequest,
DocStartSyncRequest, DocStatusRequest, DocSubscribeRequest, DocTicket, DownloadProgress,
ListTagsRequest, ListTagsResponse, NodeConnectionInfoRequest, NodeConnectionInfoResponse,
NodeConnectionsRequest, NodeShutdownRequest, NodeStatsRequest, NodeStatusRequest,
@@ -1046,6 +1046,14 @@ where
.await??;
Ok(res.policy)
}

/// Get sync peers for this document
pub async fn get_sync_peers(&self) -> Result<Option<Vec<PeerIdBytes>>> {
let res = self
.rpc(DocGetSyncPeersRequest { doc_id: self.id() })
.await??;
Ok(res.peers)
}
}

impl<'a, C: ServiceConnection<ProviderService>> From<&'a Doc<C>>
6 changes: 6 additions & 0 deletions iroh/src/node/rpc.rs
Original file line number Diff line number Diff line change
@@ -247,6 +247,12 @@ impl<D: BaoStore> Handler<D> {
})
.await
}
DocGetSyncPeers(msg) => {
chan.rpc(msg, handler, |handler, req| async move {
handler.inner.sync.doc_get_sync_peers(req).await
})
.await
}
}
});
}
22 changes: 21 additions & 1 deletion iroh/src/rpc_protocol.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ use iroh_net::{
use iroh_sync::{
actor::OpenState,
store::{DownloadPolicy, Query},
{AuthorId, CapabilityKind, Entry, NamespaceId, SignedEntry},
PeerIdBytes, {AuthorId, CapabilityKind, Entry, NamespaceId, SignedEntry},
};
use quic_rpc::{
message::{BidiStreaming, BidiStreamingMsg, Msg, RpcMsg, ServerStreaming, ServerStreamingMsg},
@@ -929,6 +929,24 @@ pub struct DocGetDownloadPolicyResponse {
pub policy: DownloadPolicy,
}

/// Get peers for document
#[derive(Serialize, Deserialize, Debug)]
pub struct DocGetSyncPeersRequest {
/// The document id
pub doc_id: NamespaceId,
}

impl RpcMsg<ProviderService> for DocGetSyncPeersRequest {
type Response = RpcResult<DocGetSyncPeersResponse>;
}

/// Response to [`DocGetSyncPeersRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct DocGetSyncPeersResponse {
/// List of peers ids
pub peers: Option<Vec<PeerIdBytes>>,
}

/// Get the bytes for a hash
#[derive(Serialize, Deserialize, Debug)]
pub struct BlobReadAtRequest {
@@ -1069,6 +1087,7 @@ pub enum ProviderRequest {
DocSubscribe(DocSubscribeRequest),
DocGetDownloadPolicy(DocGetDownloadPolicyRequest),
DocSetDownloadPolicy(DocSetDownloadPolicyRequest),
DocGetSyncPeers(DocGetSyncPeersRequest),

AuthorList(AuthorListRequest),
AuthorCreate(AuthorCreateRequest),
@@ -1120,6 +1139,7 @@ pub enum ProviderResponse {
DocSubscribe(RpcResult<DocSubscribeResponse>),
DocGetDownloadPolicy(RpcResult<DocGetDownloadPolicyResponse>),
DocSetDownloadPolicy(RpcResult<DocSetDownloadPolicyResponse>),
DocGetSyncPeers(RpcResult<DocGetSyncPeersResponse>),

AuthorList(RpcResult<AuthorListResponse>),
AuthorCreate(RpcResult<AuthorCreateResponse>),
9 changes: 9 additions & 0 deletions iroh/src/sync_engine/rpc.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ use iroh_bytes::{store::Store as BaoStore, BlobFormat};
use iroh_sync::{Author, NamespaceSecret};
use tokio_stream::StreamExt;

use crate::rpc_protocol::{DocGetSyncPeersRequest, DocGetSyncPeersResponse};
use crate::{
rpc_protocol::{
AuthorCreateRequest, AuthorCreateResponse, AuthorListRequest, AuthorListResponse,
@@ -258,4 +259,12 @@ impl SyncEngine {
let policy = self.sync.get_download_policy(req.doc_id).await?;
Ok(DocGetDownloadPolicyResponse { policy })
}

pub async fn doc_get_sync_peers(
&self,
req: DocGetSyncPeersRequest,
) -> RpcResult<DocGetSyncPeersResponse> {
let peers = self.sync.get_sync_peers(req.doc_id).await?;
Ok(DocGetSyncPeersResponse { peers })
}
}

0 comments on commit 1a6054e

Please sign in to comment.