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

Upgrade libp2p to 0.51 #142

Merged
merged 11 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
1,842 changes: 1,039 additions & 803 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ delay_map = "0.3.0"
discv5 = "0.2.2"
futures = "0.3.28"
hex = "0.4.3"
libp2p = { version = "0.50.0", default-features = false, features = ["dns", "tokio", "ecdsa", "noise", "mplex", "secp256k1", "tcp", "yamux"] }
libp2p = { version = "0.51.0", default-features = false, features = ["dns", "tokio", "ecdsa", "noise", "macros", "mplex", "secp256k1", "tcp", "yamux"] }
#libp2p-identity = { version = "0.2.1", features = ["secp256k1"] }
lru = "0.10.1"
parking_lot = "0.12.1"
primitive-types = "0.12.1"
Expand Down Expand Up @@ -60,3 +61,5 @@ mev-rs = { git = "https://github.com/ralexstokes//mev-rs", rev = "7813d4a4a564e0
ethereum-consensus = { git = "https://github.com/ralexstokes//ethereum-consensus", rev = "9b0ee0a8a45b968c8df5e7e64ea1c094e16f053d" }
[patch."https://github.com/ralexstokes/ssz-rs"]
ssz-rs = { git = "https://github.com/ralexstokes//ssz-rs", rev = "adf1a0b14cef90b9536f28ef89da1fab316465e1" }
[patch."https://github.com/ralexstokes/beacon-api-client"]
beacon-api-client = { git = "https://github.com/ralexstokes//beacon-api-client", rev = "30679e9e25d61731cde54e14cd8a3688a39d8e5b" }
47 changes: 35 additions & 12 deletions src/discovery/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use discv5::enr::{CombinedKey, NodeId};
use discv5::{Discv5, Discv5ConfigBuilder, Discv5Event, Enr, QueryError};
use futures::stream::FuturesUnordered;
use futures::{Future, FutureExt, StreamExt};
use libp2p::core::connection::ConnectionId;
use libp2p::swarm::dummy::ConnectionHandler as DummyConnectionHandler;
use libp2p::swarm::{
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction,
PollParameters,
ConnectionId, DialFailure, FromSwarm, NetworkBehaviour, PollParameters, THandlerInEvent,
THandlerOutEvent, ToSwarm,
};
use libp2p::{Multiaddr, PeerId};
use lru::LruCache;
Expand Down Expand Up @@ -160,22 +159,48 @@ impl NetworkBehaviour for Behaviour {
}
}

fn inject_event(
fn on_connection_handler_event(
&mut self,
_peer_id: PeerId,
_connection: ConnectionId,
_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
_connection_id: ConnectionId,
_event: THandlerOutEvent<Self>,
) {
trace!("inject_event -> nothing to do");
// SEE https://github.com/sigp/lighthouse/blob/73ec29c267f057e70e89856403060c4c35b5c0c8/beacon_node/eth2_libp2p/src/discovery/mod.rs#L948-L954
// Nothing to do.
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
match event {
FromSwarm::DialFailure(DialFailure {
peer_id,
error,
connection_id,
}) => {
// TODO: handle DialFailure
// https://github.com/sigp/lighthouse/blob/3b117f4bf68666747b20e39e4333073a7764b1e2/beacon_node/lighthouse_network/src/discovery/mod.rs#L1083
warn!("TODO: handle DialFailure. peer_id:{peer_id:?}, error:{error}, connection_id:{connection_id:?}");
}
FromSwarm::ConnectionEstablished(_)
| FromSwarm::ConnectionClosed(_)
| FromSwarm::AddressChange(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddr(_)
| FromSwarm::ExpiredExternalAddr(_) => {
// Ignore events not relevant to discovery
}
}
}

#[allow(clippy::single_match)]
fn poll(
&mut self,
cx: &mut Context<'_>,
_params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
trace!("poll");

if let Poll::Ready(Some(query_result)) = self.active_queries.poll_next_unpin(cx) {
Expand All @@ -199,9 +224,7 @@ impl NetworkBehaviour for Behaviour {
self.cached_enrs.put(*p, e.clone());
}

Poll::Ready(NetworkBehaviourAction::GenerateEvent(
DiscoveryEvent::FoundPeers(peers),
))
Poll::Ready(ToSwarm::GenerateEvent(DiscoveryEvent::FoundPeers(peers)))
}
Err(query_error) => {
error!("Discovery query failed: {}", query_error);
Expand Down
52 changes: 26 additions & 26 deletions src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use discv5::enr::CombinedPublicKey;
use discv5::Enr;
use libp2p::identity::PublicKey;
use libp2p::multiaddr::Protocol;
use libp2p::{Multiaddr, PeerId};
use tiny_keccak::{Hasher, Keccak};
Expand All @@ -10,11 +9,10 @@
match enr.public_key() {
CombinedPublicKey::Secp256k1(pk) => {
let pk_bytes = pk.to_bytes();
let libp2p_pk = libp2p::core::PublicKey::Secp256k1(
libp2p::core::identity::secp256k1::PublicKey::decode(&pk_bytes)
.expect("valid public key"),
);
PeerId::from(libp2p_pk)
let libp2p_pk = libp2p::identity::secp256k1::PublicKey::try_from_bytes(&pk_bytes)
.expect("valid public key");
let public_key: libp2p::identity::PublicKey = libp2p_pk.into();
PeerId::from(public_key)
}
CombinedPublicKey::Ed25519(_) => unreachable!(), // not implemented as the ENR key is generated with secp256k1
}
Expand All @@ -26,32 +24,34 @@
// if generated from a PublicKey with Identity multihash.
let pk_bytes = &peer_id.to_bytes()[2..];

match PublicKey::from_protobuf_encoding(pk_bytes).map_err(|e| {
let public_key = libp2p::identity::PublicKey::try_decode_protobuf(pk_bytes).map_err(|e| {
format!(
" Cannot parse libp2p public key public key from peer id: {}",
e
)
})? {
PublicKey::Secp256k1(pk) => {
let uncompressed_key_bytes = &pk.encode_uncompressed()[1..];
let mut output = [0_u8; 32];
let mut hasher = Keccak::v256();
hasher.update(uncompressed_key_bytes);
hasher.finalize(&mut output);
Ok(discv5::enr::NodeId::parse(&output).expect("Must be correct length"))
}
PublicKey::Ed25519(pk) => {
let uncompressed_key_bytes = pk.encode();
let mut output = [0_u8; 32];
let mut hasher = Keccak::v256();
hasher.update(&uncompressed_key_bytes);
hasher.finalize(&mut output);
Ok(discv5::enr::NodeId::parse(&output).expect("Must be correct length"))
}
PublicKey::Ecdsa(_) => Err(format!(
})?;

// TODO: matching public_key.key_type() after libp2p upgrading.
// ref: https://github.com/sigp/lighthouse/blob/8dff926c70b7c7558e7c41316770d22608dbba4c/beacon_node/lighthouse_network/src/discovery/enr_ext.rs#L266-L293
if let Ok(pk) = public_key.clone().try_into_secp256k1() {
let uncompressed_key_bytes = &pk.encode_uncompressed()[1..];

Check warning on line 37 in src/identity.rs

View workflow job for this annotation

GitHub Actions / Build

use of deprecated method `libp2p::libp2p_identity::secp256k1::PublicKey::encode_uncompressed`: Renamed to `PublicKey::to_bytes_uncompressed`.

Check warning on line 37 in src/identity.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated method `libp2p::libp2p_identity::secp256k1::PublicKey::encode_uncompressed`: Renamed to `PublicKey::to_bytes_uncompressed`.
Fixed Show fixed Hide fixed
let mut output = [0_u8; 32];
let mut hasher = Keccak::v256();
hasher.update(uncompressed_key_bytes);
hasher.finalize(&mut output);
Ok(discv5::enr::NodeId::parse(&output).expect("Must be correct length"))
} else if let Ok(pk) = public_key.clone().try_into_ed25519() {
let uncompressed_key_bytes = pk.encode();

Check warning on line 44 in src/identity.rs

View workflow job for this annotation

GitHub Actions / Build

use of deprecated method `libp2p::libp2p_identity::ed25519::PublicKey::encode`: Renamed to `PublicKey::to_bytes` to reflect actual behaviour.

Check warning on line 44 in src/identity.rs

View workflow job for this annotation

GitHub Actions / Check

use of deprecated method `libp2p::libp2p_identity::ed25519::PublicKey::encode`: Renamed to `PublicKey::to_bytes` to reflect actual behaviour.
Fixed Show fixed Hide fixed
let mut output = [0_u8; 32];
let mut hasher = Keccak::v256();
hasher.update(&uncompressed_key_bytes);
hasher.finalize(&mut output);
Ok(discv5::enr::NodeId::parse(&output).expect("Must be correct length"))
} else {
Err(format!(
"Unsupported public key (Ecdsa) from peer {}",
peer_id
)),
))
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use discv5::enr::{CombinedKey, EnrBuilder};
use environment::{EnvironmentBuilder, LoggerConfig};
use eth2_network_config::Eth2NetworkConfig;
use libp2p::identity::Keypair;
// use libp2p::identity::Keypair;
use parking_lot::RwLock;
use std::sync::Arc;
use tracing::info;
Expand All @@ -36,15 +36,15 @@

// generate private key
let enr_key = CombinedKey::generate_secp256k1();
let key_pair = {
let key_pair: libp2p::identity::Keypair = {
match enr_key {
CombinedKey::Secp256k1(ref key) => {
let mut key_bytes = key.to_bytes();
let secret_key =
libp2p::core::identity::secp256k1::SecretKey::from_bytes(&mut key_bytes)
.expect("valid secp256k1 key");
let kp: libp2p::core::identity::secp256k1::Keypair = secret_key.into();
Keypair::Secp256k1(kp)
let secret_key = libp2p::identity::secp256k1::SecretKey::from_bytes(&mut key_bytes)
Fixed Show fixed Hide fixed
.expect("valid secp256k1 key");

let kp: libp2p::identity::secp256k1::Keypair = secret_key.into();
kp.into()
}
CombinedKey::Ed25519(_) => unreachable!(), // not implemented as the ENR key is generated with secp256k1
}
Expand Down
Loading
Loading