-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathipfs.rs
328 lines (274 loc) · 11.2 KB
/
ipfs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use anyhow::anyhow;
use async_trait::async_trait;
use bytes::BytesMut;
use derivative::Derivative;
use futures03::compat::Stream01CompatExt;
use futures03::stream::StreamExt;
use futures03::stream::TryStreamExt;
use lru_time_cache::LruCache;
use serde_json::Value;
use crate::derive::CheapClone;
use crate::env::EnvVars;
use crate::futures01::stream::poll_fn;
use crate::futures01::stream::Stream;
use crate::futures01::try_ready;
use crate::futures01::Async;
use crate::futures01::Poll;
use crate::ipfs::ContentPath;
use crate::ipfs::IpfsClient;
use crate::ipfs::RetryPolicy;
use crate::prelude::{LinkResolver as LinkResolverTrait, *};
#[derive(Clone, CheapClone, Derivative)]
#[derivative(Debug)]
pub struct IpfsResolver {
#[derivative(Debug = "ignore")]
client: Arc<dyn IpfsClient>,
#[derivative(Debug = "ignore")]
cache: Arc<Mutex<LruCache<ContentPath, Vec<u8>>>>,
timeout: Duration,
max_file_size: usize,
max_map_file_size: usize,
max_cache_file_size: usize,
/// When set to `true`, it means infinite retries, ignoring the timeout setting.
retry: bool,
}
impl IpfsResolver {
pub fn new(client: Arc<dyn IpfsClient>, env_vars: Arc<EnvVars>) -> Self {
let env = &env_vars.mappings;
Self {
client,
cache: Arc::new(Mutex::new(LruCache::with_capacity(
env.max_ipfs_cache_size as usize,
))),
timeout: env.ipfs_timeout,
max_file_size: env.max_ipfs_file_bytes,
max_map_file_size: env.max_ipfs_map_file_size,
max_cache_file_size: env.max_ipfs_cache_file_size,
retry: false,
}
}
}
#[async_trait]
impl LinkResolverTrait for IpfsResolver {
fn with_timeout(&self, timeout: Duration) -> Box<dyn LinkResolverTrait> {
let mut s = self.cheap_clone();
s.timeout = timeout;
Box::new(s)
}
fn with_retries(&self) -> Box<dyn LinkResolverTrait> {
let mut s = self.cheap_clone();
s.retry = true;
Box::new(s)
}
async fn cat(&self, logger: &Logger, link: &Link) -> Result<Vec<u8>, Error> {
let path = ContentPath::new(&link.link)?;
let timeout = self.timeout;
let max_file_size = self.max_file_size;
let max_cache_file_size = self.max_cache_file_size;
if let Some(data) = self.cache.lock().unwrap().get(&path) {
trace!(logger, "IPFS cat cache hit"; "hash" => path.to_string());
return Ok(data.to_owned());
}
trace!(logger, "IPFS cat cache miss"; "hash" => path.to_string());
let (timeout, retry_policy) = if self.retry {
(None, RetryPolicy::NonDeterministic)
} else {
(Some(timeout), RetryPolicy::Networking)
};
let data = self
.client
.clone()
.cat(&path, max_file_size, timeout, retry_policy)
.await?
.to_vec();
if data.len() <= max_cache_file_size {
let mut cache = self.cache.lock().unwrap();
if !cache.contains_key(&path) {
cache.insert(path.clone(), data.clone());
}
} else {
debug!(
logger,
"IPFS file too large for cache";
"path" => path.to_string(),
"size" => data.len(),
);
}
Ok(data)
}
async fn get_block(&self, logger: &Logger, link: &Link) -> Result<Vec<u8>, Error> {
let path = ContentPath::new(&link.link)?;
let timeout = self.timeout;
trace!(logger, "IPFS block get"; "hash" => path.to_string());
let (timeout, retry_policy) = if self.retry {
(None, RetryPolicy::NonDeterministic)
} else {
(Some(timeout), RetryPolicy::Networking)
};
let data = self
.client
.clone()
.get_block(&path, timeout, retry_policy)
.await?
.to_vec();
Ok(data)
}
async fn json_stream(&self, logger: &Logger, link: &Link) -> Result<JsonValueStream, Error> {
let path = ContentPath::new(&link.link)?;
let max_map_file_size = self.max_map_file_size;
let timeout = self.timeout;
trace!(logger, "IPFS JSON stream"; "hash" => path.to_string());
let (timeout, retry_policy) = if self.retry {
(None, RetryPolicy::NonDeterministic)
} else {
(Some(timeout), RetryPolicy::Networking)
};
let mut stream = self
.client
.clone()
.cat_stream(&path, timeout, retry_policy)
.await?
.fuse()
.boxed()
.compat();
let mut buf = BytesMut::with_capacity(1024);
// Count the number of lines we've already successfully deserialized.
// We need that to adjust the line number in error messages from serde_json
// to translate from line numbers in the snippet we are deserializing
// to the line number in the overall file
let mut count = 0;
let mut cumulative_file_size = 0;
let stream: JsonValueStream = Box::pin(
poll_fn(move || -> Poll<Option<JsonStreamValue>, Error> {
loop {
cumulative_file_size += buf.len();
if cumulative_file_size > max_map_file_size {
return Err(anyhow!(
"IPFS file {} is too large. It can be at most {} bytes",
path,
max_map_file_size,
));
}
if let Some(offset) = buf.iter().position(|b| *b == b'\n') {
let line_bytes = buf.split_to(offset + 1);
count += 1;
if line_bytes.len() > 1 {
let line = std::str::from_utf8(&line_bytes)?;
let res = match serde_json::from_str::<Value>(line) {
Ok(v) => Ok(Async::Ready(Some(JsonStreamValue {
value: v,
line: count,
}))),
Err(e) => {
// Adjust the line number in the serde error. This
// is fun because we can only get at the full error
// message, and not the error message without line number
let msg = e.to_string();
let msg = msg.split(" at line ").next().unwrap();
Err(anyhow!(
"{} at line {} column {}: '{}'",
msg,
e.line() + count - 1,
e.column(),
line
))
}
};
return res;
}
} else {
// We only get here if there is no complete line in buf, and
// it is therefore ok to immediately pass an Async::NotReady
// from stream through.
// If we get a None from poll, but still have something in buf,
// that means the input was not terminated with a newline. We
// add that so that the last line gets picked up in the next
// run through the loop.
match try_ready!(stream.poll().map_err(|e| anyhow::anyhow!("{}", e))) {
Some(b) => buf.extend_from_slice(&b),
None if !buf.is_empty() => buf.extend_from_slice(&[b'\n']),
None => return Ok(Async::Ready(None)),
}
}
}
})
.compat(),
);
Ok(stream)
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
use crate::env::EnvVars;
use crate::ipfs::test_utils::add_files_to_local_ipfs_node_for_testing;
use crate::ipfs::IpfsRpcClient;
use crate::ipfs::ServerAddress;
#[tokio::test]
async fn max_file_size() {
let mut env_vars = EnvVars::default();
env_vars.mappings.max_ipfs_file_bytes = 200;
let file: &[u8] = &[0u8; 201];
let cid = add_files_to_local_ipfs_node_for_testing([file.to_vec()])
.await
.unwrap()[0]
.hash
.to_owned();
let logger = crate::log::discard();
let client = IpfsRpcClient::new_unchecked(ServerAddress::local_rpc_api(), &logger).unwrap();
let resolver = IpfsResolver::new(Arc::new(client), Arc::new(env_vars));
let err = IpfsResolver::cat(&resolver, &logger, &Link { link: cid.clone() })
.await
.unwrap_err();
assert_eq!(
err.to_string(),
format!("IPFS content from '{cid}' exceeds the 200 bytes limit")
);
}
async fn json_round_trip(text: &'static str, env_vars: EnvVars) -> Result<Vec<Value>, Error> {
let cid = add_files_to_local_ipfs_node_for_testing([text.as_bytes().to_vec()]).await?[0]
.hash
.to_owned();
let logger = crate::log::discard();
let client = IpfsRpcClient::new_unchecked(ServerAddress::local_rpc_api(), &logger)?;
let resolver = IpfsResolver::new(Arc::new(client), Arc::new(env_vars));
let stream = IpfsResolver::json_stream(&resolver, &logger, &Link { link: cid }).await?;
stream.map_ok(|sv| sv.value).try_collect().await
}
#[tokio::test]
async fn read_json_stream() {
let values = json_round_trip("\"with newline\"\n", EnvVars::default()).await;
assert_eq!(vec![json!("with newline")], values.unwrap());
let values = json_round_trip("\"without newline\"", EnvVars::default()).await;
assert_eq!(vec![json!("without newline")], values.unwrap());
let values = json_round_trip("\"two\" \n \"things\"", EnvVars::default()).await;
assert_eq!(vec![json!("two"), json!("things")], values.unwrap());
let values = json_round_trip(
"\"one\"\n \"two\" \n [\"bad\" \n \"split\"]",
EnvVars::default(),
)
.await;
assert_eq!(
"EOF while parsing a list at line 4 column 0: ' [\"bad\" \n'",
values.unwrap_err().to_string()
);
}
#[tokio::test]
async fn ipfs_map_file_size() {
let file = "\"small test string that trips the size restriction\"";
let mut env_vars = EnvVars::default();
env_vars.mappings.max_ipfs_map_file_size = file.len() - 1;
let err = json_round_trip(file, env_vars).await.unwrap_err();
assert!(err.to_string().contains(" is too large"));
env_vars = EnvVars::default();
let values = json_round_trip(file, env_vars).await;
assert_eq!(
vec!["small test string that trips the size restriction"],
values.unwrap()
);
}
}