-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Libp2p] Bootstrap from env variable instead (#1601)
bootstrap from env variable
- Loading branch information
Showing
15 changed files
with
82 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,14 @@ | ||
use anyhow::Result; | ||
use libp2p::{multiaddr::Protocol, Multiaddr, PeerId}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A bootstrap node. Contains the multiaddress and peer ID | ||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
#[serde(try_from = "Multiaddr", into = "Multiaddr")] | ||
pub struct BootstrapNode { | ||
pub address: Multiaddr, | ||
pub peer_id: PeerId, | ||
} | ||
|
||
impl From<BootstrapNode> for Multiaddr { | ||
fn from(node: BootstrapNode) -> Self { | ||
let mut address = node.address; | ||
|
||
// The standard is to have the peer ID as the last part of the address | ||
address.push(Protocol::P2p(node.peer_id)); | ||
|
||
address | ||
} | ||
} | ||
|
||
impl TryFrom<Multiaddr> for BootstrapNode { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(address: Multiaddr) -> Result<Self> { | ||
// Clone the address so we can pop the peer ID off the end | ||
let mut address = address.clone(); | ||
|
||
// The standard is to have the peer ID as the last part of the address | ||
let Some(Protocol::P2p(peer_id)) = address.pop() else { | ||
return Err(anyhow::anyhow!("Failed to parse peer ID from address")); | ||
}; | ||
/// Split off the peer ID from a multiaddress, returning the shortened address and the peer ID. | ||
/// | ||
/// # Errors | ||
/// - If the last protocol in the address is not a peer ID. | ||
pub fn split_off_peer_id(mut address: Multiaddr) -> Result<(PeerId, Multiaddr)> { | ||
let Some(Protocol::P2p(peer_id)) = address.pop() else { | ||
return Err(anyhow::anyhow!("Failed to parse peer ID from address")); | ||
}; | ||
|
||
Ok(BootstrapNode { address, peer_id }) | ||
} | ||
Ok((peer_id, address)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters