iroh_n0des/
protocol.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use anyhow::Result;
use iroh::NodeId;
use iroh_blobs::{ticket::BlobTicket, Hash};
use irpc::{channel::oneshot, Service};
use irpc_derive::rpc_requests;
use rcan::Rcan;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::caps::Caps;

pub const ALPN: &[u8] = b"/iroh/n0des/1";

pub type N0desClient = irpc::Client<N0desMessage, N0desProtocol, N0desService>;

#[derive(Debug, Clone, Copy)]
pub struct N0desService;

impl Service for N0desService {}

#[rpc_requests(N0desService, message = N0desMessage)]
#[derive(Debug, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum N0desProtocol {
    #[rpc(tx=oneshot::Sender<()>)]
    Auth(Auth),
    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
    PutBlob(PutBlob),
    #[rpc(tx=oneshot::Sender<RemoteResult<Option<Hash>>>)]
    GetTag(GetTag),
    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
    PutTopic(PutTopic),
    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
    DeleteTopic(DeleteTopic),
    #[rpc(tx=oneshot::Sender<RemoteResult<()>>)]
    PutMetrics(PutMetrics),
    #[rpc(tx=oneshot::Sender<Pong>)]
    Ping(Ping),
}

pub type RemoteResult<T> = Result<T, RemoteError>;

// #[derive(Debug, Serialize, Deserialize)]
// pub struct RemoteError(pub String);

#[derive(Serialize, Deserialize, thiserror::Error, Debug)]
pub enum RemoteError {
    #[error("Missing capability: {}", _0.to_strings().join(", "))]
    MissingCapability(Caps),
    #[error("Internal server error")]
    InternalServerError,
}

/// Authentication on first request
#[derive(Debug, Serialize, Deserialize)]
pub struct Auth {
    pub caps: Rcan<Caps>,
}

/// Request that the node fetches the given blob.
#[derive(Debug, Serialize, Deserialize)]
pub struct PutBlob {
    pub ticket: BlobTicket,
    pub name: String,
}

/// Request the name of a blob held by the node
#[derive(Debug, Serialize, Deserialize)]
pub struct GetTag {
    pub name: String,
}

pub type ProtoTopicId = [u8; 32];

/// Request that the node joins the given tossip topic
#[derive(Debug, Serialize, Deserialize)]
pub struct PutTopic {
    pub topic: ProtoTopicId,
    pub label: String,
    pub bootstrap: Vec<NodeId>,
}

/// Request that the node joins the given tossip topic
#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteTopic {
    pub topic: ProtoTopicId,
}

/// Request to store the given metrics data
#[derive(Debug, Serialize, Deserialize)]
pub struct PutMetrics {
    pub encoded: String,
    pub session_id: Uuid,
}

/// Simple ping requests
#[derive(Debug, Serialize, Deserialize)]
pub struct Ping {
    pub req: [u8; 32],
}

/// Simple ping response
#[derive(Debug, Serialize, Deserialize)]
pub struct Pong {
    pub req: [u8; 32],
}