-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: extract RPC definitions into here #5
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
176715a
refactor: extract RPC definitions into here
dignifiedquire a761c37
add new method
dignifiedquire 70c565f
minimal test working
dignifiedquire 558ca8a
extract authors
dignifiedquire 552a3d8
Merge branch 'main' into refactor-extract-rpc
rklaehn 7044d0e
use latest quic-rpc
rklaehn 979c33a
Add blob store to engine itself as well
rklaehn cad1d0b
reenable blobs related fns
rklaehn 2e988b0
Add unicode-3.0 licnese to deny.toml allow list
rklaehn 02de27d
allow iroh-gossip git
rklaehn fc83d5c
Add complete iroh gossip and docs node so that tests pass.
rklaehn f745aa8
Reactivate all old tests and fix warnings
rklaehn db497b0
Only run the big tests when --all-features is given
rklaehn a2a8c64
more tests - will it ever end
rklaehn 7fc22a0
Fix last remaining test
rklaehn dc9de49
clippy
rklaehn bf8338c
use TestResult
rklaehn e87f0a9
fix unused warning
rklaehn 38e2e7f
WIP
rklaehn e9aa128
Accept unmaintained instant crate
rklaehn 6d32cdc
Go back to authors and docs split
rklaehn ce59f9b
fix docs
rklaehn ac68d39
dedup flatten
rklaehn ac9400a
Remove redundant tests
rklaehn c94a761
Reexport LiveEvent instead of duplicating it
rklaehn 4bc6531
fmt
rklaehn 622d1c3
clippy
rklaehn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,67 @@ | ||
//! Quic RPC implementation for docs. | ||
|
||
use proto::RpcService; | ||
use quic_rpc::server::{ChannelTypes, RpcChannel}; | ||
|
||
use crate::engine::Engine; | ||
|
||
pub mod client; | ||
pub mod proto; | ||
|
||
mod docs_handle_request; | ||
|
||
type RpcError = serde_error::Error; | ||
type RpcResult<T> = std::result::Result<T, RpcError>; | ||
|
||
impl<D: iroh_blobs::store::Store> Engine<D> { | ||
/// Handle a docs request from the RPC server. | ||
pub async fn handle_rpc_request<C: ChannelTypes<RpcService>>( | ||
&self, | ||
msg: crate::rpc::proto::Request, | ||
chan: RpcChannel<RpcService, C>, | ||
) -> Result<(), quic_rpc::server::RpcServerError<C>> { | ||
use crate::rpc::proto::Request::*; | ||
|
||
let this = self.clone(); | ||
match msg { | ||
Open(msg) => chan.rpc(msg, this, Self::doc_open).await, | ||
Close(msg) => chan.rpc(msg, this, Self::doc_close).await, | ||
Status(msg) => chan.rpc(msg, this, Self::doc_status).await, | ||
List(msg) => chan.server_streaming(msg, this, Self::doc_list).await, | ||
Create(msg) => chan.rpc(msg, this, Self::doc_create).await, | ||
Drop(msg) => chan.rpc(msg, this, Self::doc_drop).await, | ||
Import(msg) => chan.rpc(msg, this, Self::doc_import).await, | ||
Set(msg) => chan.rpc(msg, this, Self::doc_set).await, | ||
ImportFile(msg) => { | ||
chan.server_streaming(msg, this, Self::doc_import_file) | ||
.await | ||
} | ||
ExportFile(msg) => { | ||
chan.server_streaming(msg, this, Self::doc_export_file) | ||
.await | ||
} | ||
Del(msg) => chan.rpc(msg, this, Self::doc_del).await, | ||
SetHash(msg) => chan.rpc(msg, this, Self::doc_set_hash).await, | ||
Get(msg) => chan.server_streaming(msg, this, Self::doc_get_many).await, | ||
GetExact(msg) => chan.rpc(msg, this, Self::doc_get_exact).await, | ||
StartSync(msg) => chan.rpc(msg, this, Self::doc_start_sync).await, | ||
Leave(msg) => chan.rpc(msg, this, Self::doc_leave).await, | ||
Share(msg) => chan.rpc(msg, this, Self::doc_share).await, | ||
Subscribe(msg) => { | ||
chan.try_server_streaming(msg, this, Self::doc_subscribe) | ||
.await | ||
} | ||
SetDownloadPolicy(msg) => chan.rpc(msg, this, Self::doc_set_download_policy).await, | ||
GetDownloadPolicy(msg) => chan.rpc(msg, this, Self::doc_get_download_policy).await, | ||
GetSyncPeers(msg) => chan.rpc(msg, this, Self::doc_get_sync_peers).await, | ||
|
||
AuthorList(msg) => chan.server_streaming(msg, this, Self::author_list).await, | ||
AuthorCreate(msg) => chan.rpc(msg, this, Self::author_create).await, | ||
AuthorImport(msg) => chan.rpc(msg, this, Self::author_import).await, | ||
AuthorExport(msg) => chan.rpc(msg, this, Self::author_export).await, | ||
AuthorDelete(msg) => chan.rpc(msg, this, Self::author_delete).await, | ||
AuthorGetDefault(msg) => chan.rpc(msg, this, Self::author_default).await, | ||
AuthorSetDefault(msg) => chan.rpc(msg, this, Self::author_set_default).await, | ||
} | ||
} | ||
} |
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,3 @@ | ||
//! RPC Client for docs and authors | ||
pub mod authors; | ||
pub mod docs; |
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,113 @@ | ||
//! API for document management. | ||
//! | ||
//! The main entry point is the [`Client`]. | ||
|
||
use anyhow::Result; | ||
use futures_lite::{Stream, StreamExt}; | ||
use quic_rpc::{client::BoxedConnector, Connector}; | ||
|
||
#[doc(inline)] | ||
pub use crate::engine::{Origin, SyncEvent, SyncReason}; | ||
use crate::{ | ||
rpc::proto::{ | ||
AuthorCreateRequest, AuthorDeleteRequest, AuthorExportRequest, AuthorGetDefaultRequest, | ||
AuthorImportRequest, AuthorListRequest, AuthorSetDefaultRequest, RpcService, | ||
}, | ||
Author, AuthorId, | ||
}; | ||
|
||
/// Iroh docs client. | ||
#[derive(Debug, Clone)] | ||
pub struct Client<C = BoxedConnector<RpcService>> { | ||
pub(super) rpc: quic_rpc::RpcClient<RpcService, C>, | ||
} | ||
|
||
impl<C: Connector<RpcService>> Client<C> { | ||
/// Creates a new docs client. | ||
pub fn new(rpc: quic_rpc::RpcClient<RpcService, C>) -> Self { | ||
Self { rpc } | ||
} | ||
|
||
/// Creates a new document author. | ||
/// | ||
/// You likely want to save the returned [`AuthorId`] somewhere so that you can use this author | ||
/// again. | ||
/// | ||
/// If you need only a single author, use [`Self::default`]. | ||
pub async fn create(&self) -> Result<AuthorId> { | ||
let res = self.rpc.rpc(AuthorCreateRequest).await??; | ||
Ok(res.author_id) | ||
} | ||
|
||
/// Returns the default document author of this node. | ||
/// | ||
/// On persistent nodes, the author is created on first start and its public key is saved | ||
/// in the data directory. | ||
/// | ||
/// The default author can be set with [`Self::set_default`]. | ||
pub async fn default(&self) -> Result<AuthorId> { | ||
let res = self.rpc.rpc(AuthorGetDefaultRequest).await??; | ||
Ok(res.author_id) | ||
} | ||
|
||
/// Sets the node-wide default author. | ||
/// | ||
/// If the author does not exist, an error is returned. | ||
/// | ||
/// On a persistent node, the author id will be saved to a file in the data directory and | ||
/// reloaded after a restart. | ||
pub async fn set_default(&self, author_id: AuthorId) -> Result<()> { | ||
self.rpc | ||
.rpc(AuthorSetDefaultRequest { author_id }) | ||
.await??; | ||
Ok(()) | ||
} | ||
|
||
/// Lists document authors for which we have a secret key. | ||
/// | ||
/// It's only possible to create writes from authors that we have the secret key of. | ||
pub async fn list(&self) -> Result<impl Stream<Item = Result<AuthorId>>> { | ||
let stream = self.rpc.server_streaming(AuthorListRequest {}).await?; | ||
Ok(flatten(stream).map(|res| res.map(|res| res.author_id))) | ||
} | ||
|
||
/// Exports the given author. | ||
/// | ||
/// Warning: The [`Author`] struct contains sensitive data. | ||
pub async fn export(&self, author: AuthorId) -> Result<Option<Author>> { | ||
let res = self.rpc.rpc(AuthorExportRequest { author }).await??; | ||
Ok(res.author) | ||
} | ||
|
||
/// Imports the given author. | ||
/// | ||
/// Warning: The [`Author`] struct contains sensitive data. | ||
pub async fn import(&self, author: Author) -> Result<()> { | ||
self.rpc.rpc(AuthorImportRequest { author }).await??; | ||
Ok(()) | ||
} | ||
|
||
/// Deletes the given author by id. | ||
/// | ||
/// Warning: This permanently removes this author. | ||
/// | ||
/// Returns an error if attempting to delete the default author. | ||
pub async fn delete(&self, author: AuthorId) -> Result<()> { | ||
self.rpc.rpc(AuthorDeleteRequest { author }).await??; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn flatten<T, E1, E2>( | ||
s: impl Stream<Item = Result<Result<T, E1>, E2>>, | ||
) -> impl Stream<Item = Result<T>> | ||
where | ||
E1: std::error::Error + Send + Sync + 'static, | ||
E2: std::error::Error + Send + Sync + 'static, | ||
{ | ||
s.map(|res| match res { | ||
Ok(Ok(res)) => Ok(res), | ||
Ok(Err(err)) => Err(err.into()), | ||
Err(err) => Err(err.into()), | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be feature
rpc