Skip to content
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(iroh): make use of quic-rpc-derive macros to prettify the rpc declarations #2508

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions iroh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ iroh-gossip = { version = "0.20.0", path = "../iroh-gossip" }
parking_lot = "0.12.1"
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"] }
quic-rpc = { version = "0.11", default-features = false, features = ["flume-transport", "quinn-transport"] }
quic-rpc-derive = { version = "0.11" }
quinn = { package = "iroh-quinn", version = "0.10" }
rand = "0.8"
serde = { version = "1", features = ["derive"] }
Expand Down
18 changes: 13 additions & 5 deletions iroh/src/rpc_protocol.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
//! This defines the RPC protocol used for communication between a CLI and an iroh node.
//!
//! RPC using the [`quic-rpc`](https://docs.rs/quic-rpc) crate.
//! RPC is done using the [`quic-rpc`](https://docs.rs/quic-rpc) crate.
//!
//! This file contains request messages, response messages and definitions of
//! the interaction pattern. Some requests like version and shutdown have a single
//! response, while others like provide have a stream of responses.
//! The RPC protocol is split into subsystems. In each subsystem, there is an
//! enum for the requests and an enum for the responses. The top level request
//! and response enums have a variant for each subsystem.
//!
//! Note that this is subject to change. The RPC protocol is not yet stable.
//! Request and response enums for each subsystem derive conversions to the
//! top level enums using the
//! [`enum_conversions``](https://docs.rs/nested_enum_utils/0.1.0/nested_enum_utils/attr.enum_conversions.html)
//! macro.
//!
//! For each rpc request, the quic-rpc interaction pattern is defined using
//! attributes provided by the
//! [`rpc_requests`](https://docs.rs/quic-rpc-derive/latest/quic_rpc_derive/attr.rpc_requests.html)
//! macro.
use serde::{Deserialize, Serialize};

pub mod authors;
Expand Down
47 changes: 12 additions & 35 deletions iroh/src/rpc_protocol/authors.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
use iroh_base::rpc::RpcResult;
use iroh_docs::{Author, AuthorId};
use quic_rpc::message::{Msg, RpcMsg, ServerStreaming, ServerStreamingMsg};
use nested_enum_utils::enum_conversions;
use quic_rpc_derive::rpc_requests;
use serde::{Deserialize, Serialize};

use super::RpcService;

#[allow(missing_docs)]
#[derive(strum::Display, Debug, Serialize, Deserialize)]
#[nested_enum_utils::enum_conversions(super::Request)]
#[enum_conversions(super::Request)]
#[rpc_requests(RpcService)]
pub enum Request {
#[server_streaming(response = RpcResult<ListResponse>)]
List(ListRequest),
#[rpc(response = RpcResult<CreateResponse>)]
Create(CreateRequest),
#[rpc(response = RpcResult<GetDefaultResponse>)]
GetDefault(GetDefaultRequest),
#[rpc(response = RpcResult<SetDefaultResponse>)]
SetDefault(SetDefaultRequest),
#[rpc(response = RpcResult<ImportResponse>)]
Import(ImportRequest),
#[rpc(response = RpcResult<ExportResponse>)]
Export(ExportRequest),
#[rpc(response = RpcResult<DeleteResponse>)]
Delete(DeleteRequest),
}

#[allow(missing_docs)]
#[derive(strum::Display, Debug, Serialize, Deserialize)]
#[nested_enum_utils::enum_conversions(super::Response)]
#[enum_conversions(super::Response)]
pub enum Response {
List(RpcResult<ListResponse>),
Create(RpcResult<CreateResponse>),
Expand All @@ -35,14 +44,6 @@ pub enum Response {
#[derive(Serialize, Deserialize, Debug)]
pub struct ListRequest {}

impl Msg<RpcService> for ListRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ListRequest {
type Response = RpcResult<ListResponse>;
}

/// Response for [`ListRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct ListResponse {
Expand All @@ -54,10 +55,6 @@ pub struct ListResponse {
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateRequest;

impl RpcMsg<RpcService> for CreateRequest {
type Response = RpcResult<CreateResponse>;
}

/// Response for [`CreateRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateResponse {
Expand All @@ -69,10 +66,6 @@ pub struct CreateResponse {
#[derive(Serialize, Deserialize, Debug)]
pub struct GetDefaultRequest;

impl RpcMsg<RpcService> for GetDefaultRequest {
type Response = RpcResult<GetDefaultResponse>;
}

/// Response for [`GetDefaultRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct GetDefaultResponse {
Expand All @@ -86,10 +79,6 @@ pub struct SetDefaultRequest {
pub author_id: AuthorId,
}

impl RpcMsg<RpcService> for SetDefaultRequest {
type Response = RpcResult<SetDefaultResponse>;
}

/// Response for [`GetDefaultRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct SetDefaultResponse;
Expand All @@ -101,10 +90,6 @@ pub struct DeleteRequest {
pub author: AuthorId,
}

impl RpcMsg<RpcService> for DeleteRequest {
type Response = RpcResult<DeleteResponse>;
}

/// Response for [`DeleteRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct DeleteResponse;
Expand All @@ -116,10 +101,6 @@ pub struct ExportRequest {
pub author: AuthorId,
}

impl RpcMsg<RpcService> for ExportRequest {
type Response = RpcResult<ExportResponse>;
}

/// Response for [`ExportRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct ExportResponse {
Expand All @@ -134,10 +115,6 @@ pub struct ImportRequest {
pub author: Author,
}

impl RpcMsg<RpcService> for ImportRequest {
type Response = RpcResult<ImportResponse>;
}

/// Response to [`ImportRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct ImportResponse {
Expand Down
102 changes: 16 additions & 86 deletions iroh/src/rpc_protocol/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use iroh_blobs::{
BlobFormat, Tag,
};
use iroh_net::NodeAddr;
use quic_rpc::message::{
BidiStreaming, BidiStreamingMsg, Msg, RpcMsg, ServerStreaming, ServerStreamingMsg,
};
use nested_enum_utils::enum_conversions;
use quic_rpc_derive::rpc_requests;
use serde::{Deserialize, Serialize};

use crate::client::blobs::{BlobInfo, DownloadMode, IncompleteBlobInfo, WrapOption};
Expand All @@ -24,25 +23,37 @@ use super::RpcService;

#[allow(missing_docs)]
#[derive(strum::Display, Debug, Serialize, Deserialize)]
#[nested_enum_utils::enum_conversions(super::Request)]
#[enum_conversions(super::Request)]
#[rpc_requests(RpcService)]
pub enum Request {
#[server_streaming(response = RpcResult<ReadAtResponse>)]
ReadAt(ReadAtRequest),
#[bidi_streaming(update = AddStreamUpdate, response = AddStreamResponse)]
AddStream(AddStreamRequest),
AddStreamUpdate(AddStreamUpdate),
#[server_streaming(response = AddPathResponse)]
AddPath(AddPathRequest),
#[server_streaming(response = DownloadResponse)]
Download(DownloadRequest),
#[server_streaming(response = ExportResponse)]
Export(ExportRequest),
#[server_streaming(response = RpcResult<BlobInfo>)]
List(ListRequest),
#[server_streaming(response = RpcResult<IncompleteBlobInfo>)]
ListIncomplete(ListIncompleteRequest),
#[rpc(response = RpcResult<()>)]
Delete(DeleteRequest),
#[server_streaming(response = ValidateProgress)]
Validate(ValidateRequest),
#[server_streaming(response = ConsistencyCheckProgress)]
Fsck(ConsistencyCheckRequest),
#[rpc(response = RpcResult<CreateCollectionResponse>)]
CreateCollection(CreateCollectionRequest),
}

#[allow(missing_docs)]
#[derive(strum::Display, Debug, Serialize, Deserialize)]
#[nested_enum_utils::enum_conversions(super::Response)]
#[enum_conversions(super::Response)]
pub enum Response {
ReadAt(RpcResult<ReadAtResponse>),
AddStream(AddStreamResponse),
Expand Down Expand Up @@ -76,14 +87,6 @@ pub struct AddPathRequest {
pub wrap: WrapOption,
}

impl Msg<RpcService> for AddPathRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for AddPathRequest {
type Response = AddPathResponse;
}

/// Wrapper around [`AddProgress`].
#[derive(Debug, Serialize, Deserialize, derive_more::Into)]
pub struct AddPathResponse(pub AddProgress);
Expand All @@ -109,14 +112,6 @@ pub struct DownloadRequest {
pub mode: DownloadMode,
}

impl Msg<RpcService> for DownloadRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for DownloadRequest {
type Response = DownloadResponse;
}

/// Progress response for [`DownloadRequest`]
#[derive(Debug, Clone, Serialize, Deserialize, derive_more::From, derive_more::Into)]
pub struct DownloadResponse(pub DownloadProgress);
Expand All @@ -140,14 +135,6 @@ pub struct ExportRequest {
pub mode: ExportMode,
}

impl Msg<RpcService> for ExportRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ExportRequest {
type Response = ExportResponse;
}

/// Progress response for [`ExportRequest`]
#[derive(Debug, Clone, Serialize, Deserialize, derive_more::From, derive_more::Into)]
pub struct ExportResponse(pub ExportProgress);
Expand All @@ -159,53 +146,21 @@ pub struct ConsistencyCheckRequest {
pub repair: bool,
}

impl Msg<RpcService> for ConsistencyCheckRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ConsistencyCheckRequest {
type Response = ConsistencyCheckProgress;
}

/// A request to the node to validate the integrity of all provided data
#[derive(Debug, Serialize, Deserialize)]
pub struct ValidateRequest {
/// repair the store by downgrading blobs from complete to partial
pub repair: bool,
}

impl Msg<RpcService> for ValidateRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ValidateRequest {
type Response = ValidateProgress;
}

/// List all blobs, including collections
#[derive(Debug, Serialize, Deserialize)]
pub struct ListRequest;

impl Msg<RpcService> for ListRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ListRequest {
type Response = RpcResult<BlobInfo>;
}

/// List all blobs, including collections
#[derive(Debug, Serialize, Deserialize)]
pub struct ListIncompleteRequest;

impl Msg<RpcService> for ListIncompleteRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ListIncompleteRequest {
type Response = RpcResult<IncompleteBlobInfo>;
}

/// Get the bytes for a hash
#[derive(Serialize, Deserialize, Debug)]
pub struct ReadAtRequest {
Expand All @@ -217,14 +172,6 @@ pub struct ReadAtRequest {
pub len: Option<usize>,
}

impl Msg<RpcService> for ReadAtRequest {
type Pattern = ServerStreaming;
}

impl ServerStreamingMsg<RpcService> for ReadAtRequest {
type Response = RpcResult<ReadAtResponse>;
}

/// Response to [`ReadAtRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub enum ReadAtResponse {
Expand Down Expand Up @@ -258,15 +205,6 @@ pub enum AddStreamUpdate {
Abort,
}

impl Msg<RpcService> for AddStreamRequest {
type Pattern = BidiStreaming;
}

impl BidiStreamingMsg<RpcService> for AddStreamRequest {
type Update = AddStreamUpdate;
type Response = AddStreamResponse;
}

/// Wrapper around [`AddProgress`].
#[derive(Debug, Serialize, Deserialize, derive_more::Into)]
pub struct AddStreamResponse(pub AddProgress);
Expand All @@ -278,10 +216,6 @@ pub struct DeleteRequest {
pub hash: Hash,
}

impl RpcMsg<RpcService> for DeleteRequest {
type Response = RpcResult<()>;
}

/// Create a collection.
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateCollectionRequest {
Expand All @@ -301,7 +235,3 @@ pub struct CreateCollectionResponse {
/// The resulting tag.
pub tag: Tag,
}

impl RpcMsg<RpcService> for CreateCollectionRequest {
type Response = RpcResult<CreateCollectionResponse>;
}
Loading
Loading