Skip to content

Commit

Permalink
Change rlp to alloy-rlp (#212)
Browse files Browse the repository at this point in the history
* alloy-rlp encode decode

* fmt

* remove unused dependency

* working on tests

* fmt

* clippy

* improve encode decode

* improve encode decode

* change enr version to my latest commit

* update rpc Request encode

* reject the extra bytes

* fix couple problems after run fuzzers

* change alloy_rlp and enr version

* clippy

* fix max enr size issue

* update enr create with latest commits

* fmt

* fmt

* update enr version

* check payload size for preventing crashes

* fmt

* update enr version

* Change the enr version

* Change the alloy-rlp version

* change enr version 10.1 to 11.0
  • Loading branch information
armaganyildirak authored Mar 20, 2024
1 parent 04ac004 commit 15b1725
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 134 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ categories = ["network-programming", "asynchronous"]
exclude = [".gitignore", ".github/*"]

[dependencies]
enr = { version = "0.10", features = ["k256", "ed25519"] }
enr = { version = "0.11.0", features = ["k256", "ed25519"] }
tokio = { version = "1", features = ["net", "sync", "macros", "rt"] }
libp2p = { version = "0.53", features = ["ed25519", "secp256k1"], optional = true }
zeroize = { version = "1", features = ["zeroize_derive"] }
futures = "0.3"
uint = { version = "0.9", default-features = false }
rlp = "0.5"
alloy-rlp = { version = "0.3.4", default-features = true }
# This version must be kept up to date do it uses the same dependencies as ENR
hkdf = "0.12"
hex = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion examples/request_enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use discv5::ConfigBuilder;
#[cfg(feature = "libp2p")]
use discv5::ListenConfig;
#[cfg(feature = "libp2p")]
use discv5::{enr, enr::CombinedKey, Discv5};
use discv5::{enr::CombinedKey, Discv5};
#[cfg(feature = "libp2p")]
use std::net::Ipv4Addr;

Expand Down
4 changes: 2 additions & 2 deletions src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
service::{QueryKind, Service, ServiceRequest, TalkRequest},
Config, DefaultProtocolId, Enr, IpMode,
};
use enr::{CombinedKey, EnrError, EnrKey, NodeId};
use enr::{CombinedKey, EnrKey, Error as EnrError, NodeId};
use parking_lot::RwLock;
use std::{
future::Future,
Expand Down Expand Up @@ -433,7 +433,7 @@ impl<P: ProtocolIdentity> Discv5<P> {
}

/// Allows application layer to insert an arbitrary field into the local ENR.
pub fn enr_insert<T: rlp::Encodable>(
pub fn enr_insert<T: alloy_rlp::Encodable>(
&self,
key: &str,
value: &T,
Expand Down
10 changes: 6 additions & 4 deletions src/discv5/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(test)]

use crate::{socket::ListenConfig, Discv5, *};
use alloy_rlp::bytes::Bytes;
use enr::{k256, CombinedKey, Enr, EnrKey, NodeId};
use rand_core::{RngCore, SeedableRng};
use std::{
Expand All @@ -14,7 +15,7 @@ fn init() {
.try_init();
}

fn update_enr<T: rlp::Encodable>(discv5: &mut Discv5, key: &str, value: &T) -> bool {
fn update_enr<T: alloy_rlp::Encodable>(discv5: &mut Discv5, key: &str, value: &T) -> bool {
discv5.enr_insert(key, value).is_ok()
}

Expand Down Expand Up @@ -675,8 +676,9 @@ async fn test_predicate_search() {

// Update `num_nodes` with the required attnet value
let num_nodes = total_nodes / 2;
let required_attnet_value = vec![1, 0, 0, 0];
let unwanted_attnet_value = vec![0, 0, 0, 0];

let required_attnet_value = Bytes::copy_from_slice(&[1, 0, 0, 0]);
let unwanted_attnet_value = Bytes::copy_from_slice(&[0, 0, 0, 0]);
println!("Bootstrap node: {}", bootstrap_node.local_enr().node_id());
println!("Target node: {}", target_node.local_enr().node_id());

Expand All @@ -703,7 +705,7 @@ async fn test_predicate_search() {
// Predicate function for filtering enrs
let predicate = move |enr: &Enr<CombinedKey>| {
if let Some(v) = enr.get("attnets") {
v == required_attnet_value.as_slice()
v == required_attnet_value.to_vec().as_slice()
} else {
false
}
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{handler::Challenge, node_info::NonContactable};
use rlp::DecoderError;
use alloy_rlp::Error as DecoderError;
use std::fmt;

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! A simple example of creating this service is as follows:
//!
//! ```rust
//! use discv5::{enr, enr::{CombinedKey, NodeId}, TokioExecutor, Discv5, ConfigBuilder};
//! use discv5::{enr, enr::{CombinedKey, Enr, NodeId}, TokioExecutor, Discv5, ConfigBuilder};
//! use discv5::socket::ListenConfig;
//! use std::net::{Ipv4Addr, SocketAddr};
//!
Expand Down
5 changes: 3 additions & 2 deletions src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use aes::{
cipher::{generic_array::GenericArray, NewCipher, StreamCipher},
Aes128Ctr,
};
use alloy_rlp::Decodable;
use enr::NodeId;
use rand::Rng;
use std::convert::TryInto;
Expand Down Expand Up @@ -173,7 +174,7 @@ impl PacketKind {
} => {
let sig_size = id_nonce_sig.len();
let pubkey_size = ephem_pubkey.len();
let node_record = enr_record.as_ref().map(rlp::encode);
let node_record = enr_record.as_ref().map(alloy_rlp::encode);
let expected_len = 34
+ sig_size
+ pubkey_size
Expand Down Expand Up @@ -259,7 +260,7 @@ impl PacketKind {

let enr_record = if remaining_data.len() > total_size {
Some(
rlp::decode::<Enr>(&remaining_data[total_size..])
<Enr>::decode(&mut &remaining_data[total_size..])
.map_err(PacketError::InvalidEnr)?,
)
} else {
Expand Down
Loading

0 comments on commit 15b1725

Please sign in to comment.