helia_interface/
routing.rs1use std::time::Duration;
4
5use async_trait::async_trait;
6use cid::Cid;
7use libp2p::{Multiaddr, PeerId};
8use serde::{Deserialize, Serialize};
9
10use crate::{AbortOptions, AwaitIterable, HeliaError, ProgressOptions};
11
12#[derive(Debug, Clone)]
14pub struct RoutingOptions {
15 pub abort: AbortOptions,
17 pub progress: ProgressOptions<()>,
19 pub use_network: bool,
21 pub use_cache: bool,
23 pub validate: bool,
25}
26
27impl Default for RoutingOptions {
28 fn default() -> Self {
29 Self {
30 abort: AbortOptions::default(),
31 progress: ProgressOptions::default(),
32 use_network: true,
33 use_cache: true,
34 validate: true,
35 }
36 }
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct PeerInfo {
42 pub id: PeerId,
44 pub multiaddrs: Vec<Multiaddr>,
46 pub protocols: Vec<String>,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct Provider {
53 #[serde(flatten)]
55 pub peer_info: PeerInfo,
56 pub transport_methods: Vec<TransportMethod>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(rename_all = "kebab-case")]
63pub enum TransportMethod {
64 Bitswap,
66 Http,
68 Libp2pStream,
70 Custom(String),
72}
73
74#[derive(Debug, Clone, Default)]
76pub struct FindProvidersOptions {
77 pub routing: RoutingOptions,
79}
80
81#[derive(Debug, Clone, Default)]
83pub struct ProvideOptions {
84 pub routing: RoutingOptions,
86}
87
88#[derive(Debug, Clone, Default)]
90pub struct FindPeersOptions {
91 pub routing: RoutingOptions,
93}
94
95#[derive(Debug, Clone, Default)]
97pub struct GetOptions {
98 pub routing: RoutingOptions,
100}
101
102#[derive(Debug, Clone, Default)]
104pub struct PutOptions {
105 pub routing: RoutingOptions,
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct RoutingRecord {
112 pub key: Vec<u8>,
114 pub value: Vec<u8>,
116 pub time_received: Option<std::time::SystemTime>,
118 pub ttl: Option<Duration>,
120}
121
122#[async_trait]
124pub trait Routing: Send + Sync {
125 async fn find_providers(
127 &self,
128 cid: &Cid,
129 options: Option<FindProvidersOptions>,
130 ) -> Result<AwaitIterable<Provider>, HeliaError>;
131
132 async fn provide(&self, cid: &Cid, options: Option<ProvideOptions>) -> Result<(), HeliaError>;
134
135 async fn find_peers(
137 &self,
138 peer_id: &PeerId,
139 options: Option<FindPeersOptions>,
140 ) -> Result<AwaitIterable<PeerInfo>, HeliaError>;
141
142 async fn get(
144 &self,
145 key: &[u8],
146 options: Option<GetOptions>,
147 ) -> Result<Option<RoutingRecord>, HeliaError>;
148
149 async fn put(
151 &self,
152 key: &[u8],
153 value: &[u8],
154 options: Option<PutOptions>,
155 ) -> Result<(), HeliaError>;
156}