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` substream message. Includes enough room for protobuf
34/// overhead. Enforced on the transport level.
35pub const MAX_MESSAGE_SIZE: usize = 4 * 1024 * 1024;
36
37/// Maximum batch size of all blocks in a single Bitswap message combined. Enforced on the
38/// application protocol level.
39pub const MAX_BATCH_SIZE: usize = 2 * 1024 * 1024;
40
41/// Bitswap configuration.
42#[derive(Debug)]
43pub struct Config {
44    /// Protocol name.
45    pub(crate) protocol: ProtocolName,
46
47    /// Protocol codec.
48    pub(crate) codec: ProtocolCodec,
49
50    /// TX channel for sending events to the user protocol.
51    pub(super) event_tx: Sender<BitswapEvent>,
52
53    /// RX channel for receiving commands from the user.
54    pub(super) cmd_rx: Receiver<BitswapCommand>,
55}
56
57impl Config {
58    /// Create new [`Config`].
59    pub fn new() -> (Self, BitswapHandle) {
60        let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
61        let (cmd_tx, cmd_rx) = channel(DEFAULT_CHANNEL_SIZE);
62
63        (
64            Self {
65                cmd_rx,
66                event_tx,
67                protocol: ProtocolName::from(PROTOCOL_NAME),
68                codec: ProtocolCodec::UnsignedVarint(Some(MAX_MESSAGE_SIZE)),
69            },
70            BitswapHandle::new(event_rx, cmd_tx),
71        )
72    }
73}