forest/utils/p2p/
mod.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use libp2p::Multiaddr;
5
6pub trait MultiaddrExt: Sized {
7    fn without_p2p(self) -> Self;
8}
9
10impl MultiaddrExt for Multiaddr {
11    fn without_p2p(mut self) -> Self {
12        if let Some(multiaddr::Protocol::P2p(_)) = self.iter().last() {
13            self.pop();
14            self
15        } else {
16            self
17        }
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use std::str::FromStr as _;
25
26    #[test]
27    fn test_without_p2p_positive() {
28        let ma = Multiaddr::from_str("/dns/bootstrap-calibnet-1.chainsafe-fil.io/tcp/34000/p2p/12D3KooWS3ZRhMYL67b4bD5XQ6fcpTyVQXnDe8H89LvwrDqaSbiT").unwrap();
29        assert_eq!(
30            ma.without_p2p().to_string().as_str(),
31            "/dns/bootstrap-calibnet-1.chainsafe-fil.io/tcp/34000"
32        );
33    }
34
35    #[test]
36    fn test_without_p2p_negative() {
37        let ma =
38            Multiaddr::from_str("/dns/bootstrap-calibnet-1.chainsafe-fil.io/tcp/34000").unwrap();
39        assert_eq!(
40            ma.without_p2p().to_string().as_str(),
41            "/dns/bootstrap-calibnet-1.chainsafe-fil.io/tcp/34000"
42        );
43    }
44}