Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(network): Port Types to Alloy #239

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 3 additions & 39 deletions src/network/service/discovery.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Module handles Discv5 discovery

use std::{str::FromStr, time::Duration};

use super::enr::OpStackEnrData;
use discv5::{
enr::{CombinedKey, Enr, EnrBuilder, NodeId},
Discv5, Discv5Config,
};
use ethers::utils::rlp;
use eyre::Result;
use tokio::{
sync::mpsc::{self, Receiver},
time::sleep,
};
use unsigned_varint::{decode, encode};

use super::types::{NetworkAddress, Peer};

Expand Down Expand Up @@ -81,43 +82,6 @@ fn create_disc(chain_id: u64) -> Result<Discv5> {
Discv5::new(enr, key, config).map_err(|_| eyre::eyre!("could not create disc service"))
}

/// The unique L2 network identifier
#[derive(Debug)]
struct OpStackEnrData {
/// Chain ID
chain_id: u64,
/// The version. Always set to 0.
version: u64,
}

impl TryFrom<&[u8]> for OpStackEnrData {
type Error = eyre::Report;

/// Converts a slice of RLP encoded bytes to [OpStackEnrData]
fn try_from(value: &[u8]) -> Result<Self> {
let bytes: Vec<u8> = rlp::decode(value)?;
let (chain_id, rest) = decode::u64(&bytes)?;
let (version, _) = decode::u64(rest)?;

Ok(Self { chain_id, version })
}
}

impl From<OpStackEnrData> for Vec<u8> {
/// Converts [OpStackEnrData] to a vector of bytes.
fn from(value: OpStackEnrData) -> Vec<u8> {
let mut chain_id_buf = encode::u128_buffer();
let chain_id_slice = encode::u128(value.chain_id as u128, &mut chain_id_buf);

let mut version_buf = encode::u128_buffer();
let version_slice = encode::u128(value.version as u128, &mut version_buf);

let opstack = [chain_id_slice, version_slice].concat();

rlp::encode(&opstack).to_vec()
}
}

/// Default bootnodes to use. Currently consists of 2 Base bootnodes & 1 Optimism bootnode.
fn bootnodes() -> Vec<Enr<CombinedKey>> {
let bootnodes = [
Expand Down
47 changes: 47 additions & 0 deletions src/network/service/enr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Contains the [OpStackEnrData] struct.

use alloy_rlp::{Decodable, Encodable, RlpEncodable, RlpDecodable};
use eyre::Result;

#[derive(Debug, RlpEncodable, RlpDecodable, PartialEq, Clone)]
pub struct OpStackEnrData {
/// Chain ID
pub chain_id: u64,
/// The version. Always set to 0.
pub version: u64,
}

impl TryFrom<&[u8]> for OpStackEnrData {
type Error = eyre::Report;

/// Converts a slice of RLP encoded bytes to [OpStackEnrData]
fn try_from(mut value: &[u8]) -> Result<Self> {
Ok(OpStackEnrData::decode(&mut value)?)
}
}

impl From<OpStackEnrData> for Vec<u8> {
/// Converts [OpStackEnrData] to a vector of bytes.
fn from(value: OpStackEnrData) -> Vec<u8> {
let mut bytes = Vec::new();
value.encode(&mut bytes);
bytes
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_round_trip_opstack() {
let data = OpStackEnrData {
chain_id: 123,
version: 1,
};
let bytes: Vec<u8> = data.into();
let decoded = OpStackEnrData::try_from(bytes.as_slice()).unwrap();
assert_eq!(decoded.chain_id, 123);
assert_eq!(decoded.version, 1);
}
}
3 changes: 1 addition & 2 deletions src/network/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use openssl::sha::sha256;

use super::{handlers::Handler, service::types::NetworkAddress};

/// A module to handle peer discovery
mod discovery;
/// A module to handle commonly used types in the p2p system.
mod enr;
mod types;

/// Responsible for management of the `Discv5` & `libp2p` services.
Expand Down
1 change: 1 addition & 0 deletions src/network/service/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Module contains commonly used types in the p2p system.
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use discv5::enr::{CombinedKey, Enr};
Expand Down
Loading