litep2p/protocol/libp2p/bitswap/
handle.rs

1// Copyright 2023 litep2p developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Bitswap handle for communicating with the bitswap protocol implementation.
22
23use crate::{
24    protocol::libp2p::bitswap::{BlockPresenceType, WantType},
25    PeerId,
26};
27
28use cid::Cid;
29use tokio::sync::mpsc::{Receiver, Sender};
30
31use std::{
32    pin::Pin,
33    task::{Context, Poll},
34};
35
36/// Events emitted by the bitswap protocol.
37#[derive(Debug)]
38pub enum BitswapEvent {
39    /// Bitswap request.
40    Request {
41        /// Peer ID.
42        peer: PeerId,
43
44        /// Requested CIDs.
45        cids: Vec<(Cid, WantType)>,
46    },
47}
48
49/// Response type for received bitswap request.
50#[derive(Debug)]
51pub enum ResponseType {
52    /// Block.
53    Block {
54        /// CID.
55        cid: Cid,
56
57        /// Found block.
58        block: Vec<u8>,
59    },
60
61    /// Presense.
62    Presence {
63        /// CID.
64        cid: Cid,
65
66        /// Whether the requested block exists or not.
67        presence: BlockPresenceType,
68    },
69}
70
71/// Commands sent from the user to `Bitswap`.
72#[derive(Debug)]
73pub(super) enum BitswapCommand {
74    /// Send bitswap response.
75    SendResponse {
76        /// Peer ID.
77        peer: PeerId,
78
79        /// CIDs.
80        responses: Vec<ResponseType>,
81    },
82}
83
84/// Handle for communicating with the bitswap protocol.
85pub struct BitswapHandle {
86    /// RX channel for receiving bitswap events.
87    event_rx: Receiver<BitswapEvent>,
88
89    /// TX channel for sending commads to `Bitswap`.
90    cmd_tx: Sender<BitswapCommand>,
91}
92
93impl BitswapHandle {
94    /// Create new [`BitswapHandle`].
95    pub(super) fn new(event_rx: Receiver<BitswapEvent>, cmd_tx: Sender<BitswapCommand>) -> Self {
96        Self { event_rx, cmd_tx }
97    }
98
99    /// Send `request` to `peer`.
100    ///
101    /// Not supported by the current implementation.
102    pub async fn send_request(&self, _peer: PeerId, _request: Vec<u8>) {
103        unimplemented!("bitswap requests are not supported");
104    }
105
106    /// Send `response` to `peer`.
107    pub async fn send_response(&self, peer: PeerId, responses: Vec<ResponseType>) {
108        let _ = self.cmd_tx.send(BitswapCommand::SendResponse { peer, responses }).await;
109    }
110}
111
112impl futures::Stream for BitswapHandle {
113    type Item = BitswapEvent;
114
115    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
116        Pin::new(&mut self.event_rx).poll_recv(cx)
117    }
118}