forked from libp2p/rust-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcache.rs
382 lines (325 loc) · 12.7 KB
/
mcache.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2020 Sigma Prime Pty Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::topic::TopicHash;
use crate::types::{MessageId, RawMessage};
use libp2p_identity::PeerId;
use std::fmt::Debug;
use std::{
collections::{HashMap, HashSet},
fmt,
time::Duration,
};
use lru_time_cache::LruCache;
/// CacheEntry stored in the history.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct CacheEntry {
mid: MessageId,
topic: TopicHash,
}
/// MessageCache struct holding history of messages.
#[derive(Clone)]
pub(crate) struct MessageCache {
msgs: LruCache<MessageId, (RawMessage, HashSet<PeerId>)>,
/// For every message and peer the number of times this peer asked for the message
iwant_counts: HashMap<MessageId, HashMap<PeerId, u32>>,
history: Vec<Vec<CacheEntry>>,
/// The number of indices in the cache history used for gossiping. That means that a message
/// won't get gossiped anymore when shift got called `gossip` many times after inserting the
/// message in the cache.
gossip: usize,
}
impl fmt::Debug for MessageCache {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MessageCache")
.field("msgs", &format!("LruCache with {} entries", self.msgs.len()))
.field("history", &self.history)
.field("gossip", &self.gossip)
.finish()
}
}
/// Implementation of the MessageCache.
impl MessageCache {
pub(crate) fn new(gossip: usize, history_capacity: usize, time_to_live: Duration, max_count: usize) -> Self {
MessageCache {
gossip,
msgs: LruCache::with_expiry_duration_and_capacity(time_to_live, max_count),
iwant_counts: HashMap::default(),
history: vec![Vec::new(); history_capacity],
}
}
/// Put a message into the memory cache.
///
/// Returns true if the message didn't already exist in the cache.
pub(crate) fn put(&mut self, message_id: &MessageId, msg: RawMessage) -> bool {
if self.msgs.contains_key(message_id) {
// Don't add duplicate entries to the cache.
false
} else {
let cache_entry = CacheEntry {
mid: message_id.clone(),
topic: msg.topic.clone(),
};
self.msgs.insert(message_id.clone(), (msg, HashSet::default()));
self.history[0].push(cache_entry);
true
}
}
/// Keeps track of peers we know have received the message to prevent forwarding to said peers.
pub(crate) fn observe_duplicate(&mut self, message_id: &MessageId, source: &PeerId) {
if let Some((message, originating_peers)) = self.msgs.get_mut(message_id) {
// if the message is already validated, we don't need to store extra peers sending us
// duplicates as the message has already been forwarded
if message.validated {
return;
}
originating_peers.insert(*source);
}
}
/// Get a message with `message_id`
#[cfg(test)]
pub(crate) fn get(&self, message_id: &MessageId) -> Option<&RawMessage> {
self.msgs.peek(message_id).map(|(message, _)| message)
}
/// Increases the iwant count for the given message by one and returns the message together
/// with the iwant if the message exists.
pub(crate) fn get_with_iwant_counts(
&mut self,
message_id: &MessageId,
peer: &PeerId,
) -> Option<(&RawMessage, u32)> {
let iwant_counts = &mut self.iwant_counts;
self.msgs.get(message_id).and_then(|(message, _)| {
if !message.validated {
None
} else {
Some((message, {
let count = iwant_counts
.entry(message_id.clone())
.or_default()
.entry(*peer)
.or_default();
*count += 1;
*count
}))
}
})
}
/// Gets a message with [`MessageId`] and tags it as validated.
/// This function also returns the known peers that have sent us this message. This is used to
/// prevent us sending redundant messages to peers who have already propagated it.
pub(crate) fn validate(
&mut self,
message_id: &MessageId,
) -> Option<(&RawMessage, HashSet<PeerId>)> {
self.msgs.get_mut(message_id).map(|(message, known_peers)| {
message.validated = true;
// Clear the known peers list (after a message is validated, it is forwarded and we no
// longer need to store the originating peers).
let originating_peers = std::mem::take(known_peers);
(&*message, originating_peers)
})
}
/// Get a list of [`MessageId`]s for a given topic.
pub(crate) fn get_gossip_message_ids(&self, topic: &TopicHash) -> Vec<MessageId> {
self.history[..self.gossip]
.iter()
.fold(vec![], |mut current_entries, entries| {
// search for entries with desired topic
let mut found_entries: Vec<MessageId> = entries
.iter()
.filter_map(|entry| {
if &entry.topic == topic {
let mid = &entry.mid;
// Only gossip validated messages
if let Some(true) = self.msgs.peek(mid).map(|(msg, _)| msg.validated) {
Some(mid.clone())
} else {
None
}
} else {
None
}
})
.collect();
// generate the list
current_entries.append(&mut found_entries);
current_entries
})
}
/// Shift the history array down one and delete messages associated with the
/// last entry.
pub(crate) fn shift(&mut self) {
for entry in self.history.pop().expect("history is always > 1") {
if let Some((msg, _)) = self.msgs.remove(&entry.mid) {
if !msg.validated {
// If GossipsubConfig::validate_messages is true, the implementing
// application has to ensure that Gossipsub::validate_message gets called for
// each received message within the cache timeout time."
tracing::debug!(
message=%&entry.mid,
"The message got removed from the cache without being validated."
);
}
}
tracing::trace!(message=%&entry.mid, "Remove message from the cache");
self.iwant_counts.remove(&entry.mid);
}
// Insert an empty vec in position 0
self.history.insert(0, Vec::new());
}
/// Removes a message from the cache and returns it if existent
pub(crate) fn remove(
&mut self,
message_id: &MessageId,
) -> Option<(RawMessage, HashSet<PeerId>)> {
//We only remove the message from msgs and iwant_count and keep the message_id in the
// history vector. Zhe id in the history vector will simply be ignored on popping.
self.iwant_counts.remove(message_id);
self.msgs.remove(message_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::IdentTopic as Topic;
fn gen_testm(x: u64, topic: TopicHash) -> (MessageId, RawMessage) {
let default_id = |message: &RawMessage| {
// default message id is: source + sequence number
let mut source_string = message.source.as_ref().unwrap().to_base58();
source_string.push_str(&message.sequence_number.unwrap().to_string());
MessageId::from(source_string)
};
let u8x: u8 = x as u8;
let source = Some(PeerId::random());
let data: Vec<u8> = vec![u8x];
let sequence_number = Some(x);
let m = RawMessage {
source,
data,
sequence_number,
topic,
signature: None,
key: None,
validated: false,
};
let id = default_id(&m);
(id, m)
}
fn new_cache(gossip_size: usize, history: usize) -> MessageCache {
MessageCache::new(gossip_size, history, Duration::from_secs(60), 1000)
}
#[test]
/// Test that the message cache can be created.
fn test_new_cache() {
let x: usize = 3;
let mc = new_cache(x, 5);
assert_eq!(mc.gossip, x);
}
#[test]
/// Test you can put one message and get one.
fn test_put_get_one() {
let mut mc = new_cache(10, 15);
let topic1_hash = Topic::new("topic1").hash();
let (id, m) = gen_testm(10, topic1_hash);
mc.put(&id, m.clone());
assert_eq!(mc.history[0].len(), 1);
let fetched = mc.get(&id);
assert_eq!(fetched.unwrap(), &m);
}
#[test]
/// Test attempting to 'get' with a wrong id.
fn test_get_wrong() {
let mut mc = new_cache(10, 15);
let topic1_hash = Topic::new("topic1").hash();
let (id, m) = gen_testm(10, topic1_hash);
mc.put(&id, m);
// Try to get an incorrect ID
let wrong_id = MessageId::new(b"wrongid");
let fetched = mc.get(&wrong_id);
assert!(fetched.is_none());
}
#[test]
/// Test attempting to 'get' empty message cache.
fn test_get_empty() {
let mc = new_cache(10, 15);
// Try to get an incorrect ID
let wrong_string = MessageId::new(b"imempty");
let fetched = mc.get(&wrong_string);
assert!(fetched.is_none());
}
#[test]
/// Test shift mechanism.
fn test_shift() {
let mut mc = new_cache(1, 5);
let topic1_hash = Topic::new("topic1").hash();
// Build the message
for i in 0..10 {
let (id, m) = gen_testm(i, topic1_hash.clone());
mc.put(&id, m.clone());
}
mc.shift();
// Ensure the shift occurred
assert!(mc.history[0].is_empty());
assert!(mc.history[1].len() == 10);
// Make sure no messages deleted
assert!(mc.msgs.len() == 10);
}
#[test]
/// Test Shift with no additions.
fn test_empty_shift() {
let mut mc = new_cache(1, 5);
let topic1_hash = Topic::new("topic1").hash();
// Build the message
for i in 0..10 {
let (id, m) = gen_testm(i, topic1_hash.clone());
mc.put(&id, m.clone());
}
mc.shift();
// Ensure the shift occurred
assert!(mc.history[0].is_empty());
assert!(mc.history[1].len() == 10);
mc.shift();
assert!(mc.history[2].len() == 10);
assert!(mc.history[1].is_empty());
assert!(mc.history[0].is_empty());
}
#[test]
/// Test shift to see if the last history messages are removed.
fn test_remove_last_from_shift() {
let mut mc = new_cache(4, 5);
let topic1_hash = Topic::new("topic1").hash();
// Build the message
for i in 0..10 {
let (id, m) = gen_testm(i, topic1_hash.clone());
mc.put(&id, m.clone());
}
// Shift right until deleting messages
mc.shift();
mc.shift();
mc.shift();
mc.shift();
assert_eq!(mc.history[mc.history.len() - 1].len(), 10);
// Shift and delete the messages
mc.shift();
assert_eq!(mc.history[mc.history.len() - 1].len(), 0);
assert_eq!(mc.history[0].len(), 0);
assert_eq!(mc.msgs.len(), 0);
}
}