forest-filecoin 0.36.0

Rust Filecoin implementation.
Documentation
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

//! Request manager implementation that is optimized for `filecoin` network
//! usage

use std::sync::LazyLock;
use std::time::{Duration, Instant};

use crate::cid_collections::CidHashMap;
use crate::prelude::*;
use crate::utils::misc::env::env_or_default_logged;
use ahash::HashSet;
use futures::StreamExt;
use libp2p::PeerId;
use nonzero_ext::nonzero;
use parking_lot::RwLock;
use tokio::sync::Semaphore;

use crate::libp2p_bitswap::{event_handlers::*, *};

const BITSWAP_BLOCK_REQUEST_INTERVAL: Duration = Duration::from_millis(500);

/// Bounds the queue of computed wantlist responses awaiting send by the swarm
/// loop. A `Block` response can carry up to
/// [`MAX_BUF_SIZE`](crate::libp2p_bitswap::internals::codec::MAX_BUF_SIZE), so an
/// unbounded queue could buffer gigabytes under a flood of block requests for
/// large stored blocks. When the channel is full the (already blocking) serve
/// task waits, holding its permit and so shedding further inbound serves.
const SERVE_RESPONSE_CHANNEL_CAP: usize = 128;

/// Maximum wantlist entries served from a single inbound bitswap message.
///
/// The peer picks the entry count, bounded only by
/// [`MAX_BUF_SIZE`](crate::libp2p_bitswap::internals::codec::MAX_BUF_SIZE) (~47k
/// entries), and each is a blockstore read; serving them all in one message is
/// an unbounded burst of DB IO. Well-behaved peers that want more simply
/// re-request.
pub(in crate::libp2p_bitswap) const MAX_WANTLIST_ENTRIES_SERVED: usize = 1024;

/// Concurrent inbound wantlist serves allowed at once. Each holds a blocking
/// thread while it reads the blockstore, so this bounds blocking-pool usage
/// under a flood. Excess serves are dropped; the peer can re-request.
pub(in crate::libp2p_bitswap) static MAX_CONCURRENT_INBOUND_WANTLIST_SERVES: LazyLock<usize> =
    LazyLock::new(|| {
        env_or_default_logged(
            "FOREST_MAX_CONCURRENT_INBOUND_WANTLIST_SERVES",
            nonzero!(8_usize),
        )
        .get()
    });

pub type ValidatePeerCallback = dyn Fn(PeerId) -> bool + Send + Sync;

#[derive(Debug, Clone)]
struct ResponseChannels {
    block_have: flume::Sender<PeerId>,
    block_received: flume::Sender<Option<Vec<u8>>>,
}

/// Request manager implementation that is optimized for Filecoin network
/// usage
pub struct BitswapRequestManager {
    // channel for outbound `have` requests
    outbound_have_request_tx: flume::Sender<(PeerId, Cid)>,
    outbound_have_request_rx: flume::Receiver<(PeerId, Cid)>,
    // channel for outbound `cancel` requests
    outbound_cancel_request_tx: flume::Sender<(PeerId, Cid)>,
    outbound_cancel_request_rx: flume::Receiver<(PeerId, Cid)>,
    // channel for outbound `block` requests
    outbound_block_request_tx: flume::Sender<(PeerId, Cid)>,
    outbound_block_request_rx: flume::Receiver<(PeerId, Cid)>,
    // responses to inbound wantlists, computed off the swarm loop and sent from it
    serve_response_tx: flume::Sender<(PeerId, Cid, BitswapResponse)>,
    serve_response_rx: flume::Receiver<(PeerId, Cid, BitswapResponse)>,
    // bounds concurrent off-loop wantlist serving
    inbound_serve_limiter: Arc<Semaphore>,
    peers: RwLock<HashSet<PeerId>>,
    response_channels: RwLock<CidHashMap<ResponseChannels>>,
}

impl BitswapRequestManager {
    /// A receiver channel of the outbound `bitswap` network requests that the
    /// [`BitswapRequestManager`] emits. The messages from this channel need
    /// to be sent with [`BitswapBehaviour::send_request`] to make
    /// [`BitswapRequestManager::get_block`] work.
    pub fn outbound_request_stream(
        &self,
    ) -> impl futures::stream::Stream<Item = (PeerId, BitswapRequest)> + '_ {
        type MapperType = fn((libp2p::PeerId, Cid)) -> (libp2p::PeerId, BitswapRequest);

        fn new_block((peer, cid): (PeerId, Cid)) -> (PeerId, BitswapRequest) {
            (peer, BitswapRequest::new_block(cid).send_dont_have(false))
        }

        fn new_have((peer, cid): (PeerId, Cid)) -> (PeerId, BitswapRequest) {
            (peer, BitswapRequest::new_have(cid).send_dont_have(false))
        }

        fn new_cancel((peer, cid): (PeerId, Cid)) -> (PeerId, BitswapRequest) {
            (peer, BitswapRequest::new_cancel(cid).send_dont_have(false))
        }

        // Use separate channels here to not block `block` requests when too many other type of requests are queued.
        let streams = vec![
            self.outbound_block_request_rx
                .stream()
                .map(new_block as MapperType),
            self.outbound_have_request_rx
                .stream()
                .map(new_have as MapperType),
            self.outbound_cancel_request_rx
                .stream()
                .map(new_cancel as MapperType),
        ];
        futures::stream::select_all(streams)
    }

    /// Responses to inbound wantlists, computed off the swarm loop by
    /// [`Self::serve_inbound_requests`]. Each item must be sent with
    /// [`BitswapBehaviour::send_response`].
    pub fn outbound_serve_response_stream(
        &self,
    ) -> impl futures::stream::Stream<Item = (PeerId, Cid, BitswapResponse)> + '_ {
        self.serve_response_rx.stream()
    }

    /// Serves an inbound wantlist off the swarm loop: the blockstore reads run
    /// on a blocking task (bounded by [`MAX_CONCURRENT_INBOUND_WANTLIST_SERVES`],
    /// dropped when saturated) and the responses are streamed back via
    /// [`Self::outbound_serve_response_stream`] for the loop to send. Keeping the
    /// reads off the loop is what stops a large wantlist from stalling all p2p.
    pub(in crate::libp2p_bitswap) fn serve_inbound_requests<S>(
        self: &Arc<Self>,
        store: &S,
        peer: PeerId,
        requests: Vec<BitswapRequest>,
    ) where
        S: BitswapStoreRead + ShallowClone + Send + Sync + 'static,
    {
        if requests.is_empty() {
            return;
        }

        let Ok(permit) = self
            .inbound_serve_limiter
            .shallow_clone()
            .try_acquire_owned()
        else {
            debug!(%peer, "dropping inbound bitswap wantlist: too many serves in flight");
            return;
        };
        if requests.len() > MAX_WANTLIST_ENTRIES_SERVED {
            debug!(
                %peer,
                "truncating inbound bitswap wantlist from {} to {MAX_WANTLIST_ENTRIES_SERVED} entries",
                requests.len(),
            );
        }
        let store = store.shallow_clone();
        let serve_response_tx = self.serve_response_tx.clone();
        task::spawn_blocking(move || {
            let _permit = permit;
            for request in requests.into_iter().take(MAX_WANTLIST_ENTRIES_SERVED) {
                if let Some(response) = handle_inbound_request(&store, &request)
                    && serve_response_tx
                        .send((peer, request.cid, response))
                        .is_err()
                {
                    break; // receiver gone (shutdown)
                }
            }
        });
    }
}

impl Default for BitswapRequestManager {
    fn default() -> Self {
        let (outbound_have_request_tx, outbound_have_request_rx) = flume::unbounded();
        let (outbound_cancel_request_tx, outbound_cancel_request_rx) = flume::unbounded();
        let (outbound_block_request_tx, outbound_block_request_rx) = flume::unbounded();
        let (serve_response_tx, serve_response_rx) = flume::bounded(SERVE_RESPONSE_CHANNEL_CAP);
        Self {
            outbound_have_request_tx,
            outbound_have_request_rx,
            outbound_cancel_request_tx,
            outbound_cancel_request_rx,
            outbound_block_request_tx,
            outbound_block_request_rx,
            serve_response_tx,
            serve_response_rx,
            inbound_serve_limiter: Arc::new(Semaphore::new(
                *MAX_CONCURRENT_INBOUND_WANTLIST_SERVES,
            )),
            peers: RwLock::new(HashSet::new()),
            response_channels: RwLock::new(CidHashMap::new()),
        }
    }
}

impl BitswapRequestManager {
    /// Hook the `bitswap` network event into the [`BitswapRequestManager`]
    pub fn handle_event<S: BitswapStoreRead + ShallowClone + Send + Sync + 'static>(
        self: &Arc<Self>,
        bitswap: &mut BitswapBehaviour,
        store: &S,
        event: BitswapBehaviourEvent,
    ) -> anyhow::Result<()> {
        handle_event_impl(self, bitswap, store, event)
    }

    /// Gets a block, writing it to the given block store that implements
    /// [`BitswapStoreReadWrite`] and respond to the channel. Note: this
    /// method is a non-blocking, it is intended to return immediately.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn get_block(
        self: Arc<Self>,
        store: impl BitswapStoreReadWrite + ShallowClone,
        cid: Cid,
        timeout: Duration,
        responder: Option<flume::Sender<bool>>,
        validate_peer: Option<Arc<ValidatePeerCallback>>,
    ) {
        let start = Instant::now();
        task::spawn(async move {
            let mut success = store.contains(&cid).unwrap_or_default();
            if !success {
                let deadline = start.checked_add(timeout).expect("Infallible");
                success = self
                    .get_block_inner(&store, cid, deadline, validate_peer)
                    .await;
                // Spin check db when `get_block_inner` fails fast,
                // which means there is other task actually processing the same `cid`
                while !success && Instant::now() < deadline {
                    task::sleep(BITSWAP_BLOCK_REQUEST_INTERVAL).await;
                    success = store.contains(&cid).unwrap_or_default();
                }
            }

            if success {
                metrics::message_counter_get_block_success().inc();
            } else {
                metrics::message_counter_get_block_failure().inc();
            }

            if let Some(responder) = responder
                && let Err(e) = responder.send_async(success).await
            {
                debug!("{e}");
            }

            metrics::GET_BLOCK_TIME.observe((Instant::now() - start).as_secs_f64());
        });
    }

    async fn get_block_inner(
        &self,
        store: &(impl BitswapStoreReadWrite + ShallowClone),
        cid: Cid,
        deadline: Instant,
        validate_peer: Option<Arc<ValidatePeerCallback>>,
    ) -> bool {
        // Fail fast here when the given `cid` is being processed by other tasks
        if self.response_channels.read().contains_key(&cid) {
            return false;
        }

        let (block_have_tx, block_have_rx) = flume::unbounded();
        let (block_saved_tx, block_saved_rx) = flume::unbounded();
        let channels = ResponseChannels {
            block_have: block_have_tx,
            block_received: block_saved_tx,
        };
        {
            self.response_channels.write().insert(cid, channels);
        }

        let peers: Vec<_> = self.peers.read().iter().cloned().collect();
        let validated_peers: Vec<_> = peers
            .iter()
            .filter(|&&p| validate_peer.as_ref().map(|f| f(p)).unwrap_or(true))
            .cloned()
            .collect();

        debug!("Found {} valid peers for {cid}", validated_peers.len());
        let selected_peers = if validated_peers.is_empty() {
            // Fallback to all peers
            peers
        } else {
            validated_peers
        };

        for peer in selected_peers {
            if let Err(e) = self.outbound_have_request_tx.send((peer, cid)) {
                debug!("{e}");
            }
        }

        // Wait for the block off the blocking pool: react to `have` offers by
        // requesting the block, and take the first saved response, bounded by the
        // deadline. `have_open` stops polling the `have` channel once it closes
        // while still awaiting a saved response. `biased` keeps a saved response
        // that arrives right at the deadline from being dropped by a tie.
        let timeout = tokio::time::sleep_until(tokio::time::Instant::from_std(deadline));
        tokio::pin!(timeout);
        let mut have_open = true;
        let response = loop {
            tokio::select! {
                biased;
                saved = block_saved_rx.recv_async() => break saved.ok(),
                () = &mut timeout => break None,
                have = block_have_rx.recv_async(), if have_open => match have {
                    Ok(peer) => {
                        _ = self.outbound_block_request_tx.send((peer, cid));
                    }
                    Err(_) => have_open = false,
                },
            }
        };

        let success = match response {
            // Block already in the db, nothing to insert.
            Some(None) => true,
            // Timed out or channel closed.
            None => false,
            // Inserting is blocking db IO with an unbounded tail (lock, commit,
            // compaction), so it stays off the async runtime.
            Some(Some(data)) => {
                let store = store.shallow_clone();
                task::spawn_blocking(move || match Block::new(cid, data) {
                    Ok(block) => match store.insert(&block) {
                        Ok(()) => {
                            metrics::message_counter_inbound_response_block_update_db().inc();
                            true
                        }
                        Err(e) => {
                            metrics::message_counter_inbound_response_block_update_db_failure()
                                .inc();
                            warn!(
                                "Failed to update db, cid: {cid}, data: {:?}, error: {e:#}",
                                block.data()
                            );
                            false
                        }
                    },
                    Err(e) => {
                        warn!("Failed to construct block, cid: {cid}, error: {e:#}");
                        false
                    }
                })
                .await
                .unwrap_or(false)
            }
        };

        // Cleanup
        {
            let mut response_channels = self.response_channels.write();
            if response_channels.remove(&cid).is_some() {
                response_channels.shrink_to_fit();
                metrics::response_channel_container_capacity()
                    .set(response_channels.total_capacity() as _);
            }
        }

        success
    }

    pub(in crate::libp2p_bitswap) fn on_inbound_response_event<S: BitswapStoreRead>(
        &self,
        store: &S,
        response: BitswapInboundResponseEvent,
    ) {
        use BitswapInboundResponseEvent::*;

        match response {
            HaveBlock(peer, cid) => {
                if let Some(chans) = self.response_channels.read().get(&cid) {
                    _ = chans.block_have.send(peer);
                }
            }
            DataBlock(_peer, cid, data) => {
                if let Some(chans) = self.response_channels.read().get(&cid) {
                    if let Ok(true) = store.contains(&cid) {
                        // Avoid duplicate writes, still notify the receiver
                        metrics::message_counter_inbound_response_block_already_exists_in_db()
                            .inc();
                        _ = chans.block_received.send(None);
                    } else {
                        _ = chans.block_received.send(Some(data));
                    }

                    // <https://github.com/ipfs/go-libipfs/tree/main/bitswap#background>
                    // When a node receives blocks that it asked for, the node should send out a
                    // notification called a 'Cancel' to tell its peers that the
                    // node no longer wants those blocks.
                    for &peer in self.peers.read().iter() {
                        if let Err(e) = self.outbound_cancel_request_tx.send((peer, cid)) {
                            debug!("{e}");
                        }
                    }
                } else {
                    metrics::message_counter_inbound_response_block_not_requested().inc();
                }
            }
        }
    }

    pub(in crate::libp2p_bitswap) fn on_peer_connected(&self, peer: PeerId) -> bool {
        let mut peers = self.peers.write();
        let success = peers.insert(peer);
        if success {
            metrics::peer_container_capacity().set(peers.capacity() as _);
        }
        success
    }

    pub(in crate::libp2p_bitswap) fn on_peer_disconnected(&self, peer: &PeerId) -> bool {
        let mut peers = self.peers.write();
        let success = peers.remove(peer);
        if success {
            peers.shrink_to_fit();
            metrics::peer_container_capacity().set(peers.capacity() as _);
        }
        success
    }
}