litep2p/protocol/libp2p/bitswap/
config.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
21use crate::{
22    codec::ProtocolCodec,
23    protocol::libp2p::bitswap::{BitswapCommand, BitswapEvent, BitswapHandle},
24    types::protocol::ProtocolName,
25    DEFAULT_CHANNEL_SIZE,
26};
27
28use tokio::sync::mpsc::{channel, Receiver, Sender};
29
30/// IPFS Bitswap protocol name as a string.
31pub const PROTOCOL_NAME: &str = "/ipfs/bitswap/1.2.0";
32
33/// Maximum Size for `/ipfs/bitswap/1.2.0` payloads.
34const MAX_PAYLOAD_SIZE: usize = 2_097_152;
35
36/// Bitswap configuration.
37#[derive(Debug)]
38pub struct Config {
39    /// Protocol name.
40    pub(crate) protocol: ProtocolName,
41
42    /// Protocol codec.
43    pub(crate) codec: ProtocolCodec,
44
45    /// TX channel for sending events to the user protocol.
46    pub(super) event_tx: Sender<BitswapEvent>,
47
48    /// RX channel for receiving commands from the user.
49    pub(super) cmd_rx: Receiver<BitswapCommand>,
50}
51
52impl Config {
53    /// Create new [`Config`].
54    pub fn new() -> (Self, BitswapHandle) {
55        let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
56        let (cmd_tx, cmd_rx) = channel(DEFAULT_CHANNEL_SIZE);
57
58        (
59            Self {
60                cmd_rx,
61                event_tx,
62                protocol: ProtocolName::from(PROTOCOL_NAME),
63                codec: ProtocolCodec::UnsignedVarint(Some(MAX_PAYLOAD_SIZE)),
64            },
65            BitswapHandle::new(event_rx, cmd_tx),
66        )
67    }
68}