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

Switch target crate to sigp's gossipsub implementation #17

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2,080 changes: 1,460 additions & 620 deletions censoring/Cargo.lock

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions censoring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# unstable branch
gossipsub = { git = "https://github.com/sigp/lighthouse.git", rev = "f8fdb71f50851bf7792e68a4ee31125bd645e19a" }

chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
delay_map = "0.1.1"

# TODO: Update libp2p once the next version, which includes prometheus-client v0.20, has been released.
# See https://github.com/ackintosh/rust-libp2p/commit/df09870c8c2294cbaeb881f58d4f9752125562bc
libp2p = { git = "https://github.com/ackintosh/rust-libp2p.git", branch = "prometheus-client-0.20.0", default-features = false, features = ["gossipsub", "dns", "tcp", "tokio", "noise", "mplex", "yamux", "serde"] }
libp2p = { version = "0.53.2", default-features = false, features = ["dns", "tcp", "tokio", "noise", "yamux", "serde"] }

# This is a fork of `libp2p` in order to implement malicious behaviour in the `attacker` module.
# This `libp2p-testground` is used in `attacker` module instead of `libp2p`.
Expand All @@ -26,9 +26,5 @@ testground = { git = "https://github.com/testground/sdk-rust.git", rev = "1fd032
tokio = { version = "1.20.0", features = ["macros"] }
tracing = "0.1.35"
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }

# TODO: Update prometheus-client once the next version, which includes the fix, has been released.
# See https://github.com/prometheus/client_rust/pull/123
prometheus-client = { git = "https://github.com/ackintosh/client_rust.git", branch = "fix/protobuf-labels", features = ["protobuf"] }

prometheus-client = { version = "0.22.2", features = ["protobuf"] }
prost = "0.11"
2 changes: 1 addition & 1 deletion censoring/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This Dockerfile is for the `docker:generic` builder.
# See https://docs.testground.ai/builder-library/docker-generic for details about the builder.
FROM rust:1.67-bullseye as builder
FROM rust:1.77-bullseye as builder
WORKDIR /usr/src/test-plan

# * `prost-build`, a dependency of `libp2p-gossipsub`, requires cmake.
Expand Down
13 changes: 10 additions & 3 deletions censoring/src/attacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ pub(crate) async fn run(
// Start libp2p
// ////////////////////////////////////////////////////////////////////////
let mut swarm = build_swarm(keypair);
swarm.listen_on(instance_info.multiaddr.clone())?;
swarm.listen_on(recreate_multiaddr(&instance_info.multiaddr))?;
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { address, .. } if address == instance_info.multiaddr => {}
SwarmEvent::NewListenAddr { address, .. }
if address == recreate_multiaddr(&instance_info.multiaddr) => {}
e => panic!("Unexpected event {:?}", e),
}

Expand All @@ -79,7 +80,7 @@ pub(crate) async fn run(
};
client.record_message(format!("Victim: {:?}", victim));

swarm.dial(victim.multiaddr)?;
swarm.dial(recreate_multiaddr(&victim.multiaddr))?;

barrier_and_drive_swarm(&client, &mut swarm, BARRIER_WARMUP).await?;

Expand Down Expand Up @@ -119,6 +120,12 @@ fn build_transport(
.boxed()
}

// We are recreating from `libp2p::Multiaddr` to `libp2p_testground::Multiaddr` because we use two
// different versions of libp2p.
fn recreate_multiaddr(addr: &libp2p::Multiaddr) -> libp2p_testground::Multiaddr {
libp2p_testground::Multiaddr::try_from(addr.to_vec()).unwrap()
}

type GossipsubNetworkBehaviourAction =
NetworkBehaviourAction<GossipsubEvent, GossipsubHandler, GossipsubHandlerIn>;

Expand Down
61 changes: 25 additions & 36 deletions censoring/src/honest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@ use crate::utils::{
};
use crate::{InstanceInfo, Role};
use chrono::Local;
use gossipsub::{
AllowAllSubscriptionFilter, Behaviour, ConfigBuilder, IdentTopic, IdentityTransform,
MessageAuthenticity, MetricsConfig, PeerScoreParams, PeerScoreThresholds, Topic,
TopicScoreParams,
};
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::upgrade::{SelectUpgrade, Version};
use libp2p::dns::TokioDnsConfig;
use libp2p::futures::StreamExt;
use libp2p::gossipsub::metrics::Config;
use libp2p::gossipsub::subscription_filter::AllowAllSubscriptionFilter;
use libp2p::gossipsub::{
Behaviour, ConfigBuilder, IdentTopic, IdentityTransform, MessageAuthenticity, PeerScoreParams,
PeerScoreThresholds, Topic, TopicScoreParams,
};
use libp2p::identity::Keypair;
use libp2p::mplex::MplexConfig;
use libp2p::noise::NoiseConfig;
use libp2p::swarm::{DialError, SwarmBuilder, SwarmEvent};
use libp2p::tcp::tokio::Transport as TcpTransport;
use libp2p::tcp::Config as TcpConfig;
use libp2p::yamux::YamuxConfig;
use libp2p::PeerId;
use libp2p::Transport;
use libp2p::{Multiaddr, Swarm};
use libp2p::swarm::{DialError, SwarmEvent};
use libp2p::{noise, yamux, Multiaddr, PeerId, Swarm, SwarmBuilder, Transport};
use prometheus_client::registry::Registry;
use rand::seq::SliceRandom;
use rand::SeedableRng;
Expand Down Expand Up @@ -289,20 +279,17 @@ pub(crate) async fn run(

/// Set up an encrypted TCP transport over the Mplex and Yamux protocols.
fn build_transport(keypair: &Keypair) -> libp2p::core::transport::Boxed<(PeerId, StreamMuxerBox)> {
let transport = TokioDnsConfig::system(TcpTransport::new(TcpConfig::default().nodelay(true)))
.expect("DNS config");

let noise_keys = libp2p::noise::Keypair::<libp2p::noise::X25519Spec>::new()
.into_authentic(keypair)
.expect("Signing libp2p-noise static DH keypair failed.");
let tcp = libp2p::tcp::tokio::Transport::new(libp2p::tcp::Config::default().nodelay(true));
let transport = libp2p::dns::tokio::Transport::system(tcp)
.expect("DNS")
.boxed();

transport
.upgrade(Version::V1)
.authenticate(NoiseConfig::xx(noise_keys).into_authenticated())
.multiplex(SelectUpgrade::new(
YamuxConfig::default(),
MplexConfig::default(),
))
.upgrade(libp2p::core::upgrade::Version::V1)
.authenticate(
noise::Config::new(keypair).expect("signing can fail only once during starting a node"),
)
.multiplex(yamux::Config::default())
.timeout(Duration::from_secs(20))
.boxed()
}
Expand Down Expand Up @@ -354,7 +341,7 @@ impl HonestNetwork {
let mut gs = Behaviour::new_with_subscription_filter_and_transform(
MessageAuthenticity::Signed(keypair.clone()),
gossipsub_config,
Some((registry, Config::default())),
Some((registry, MetricsConfig::default())),
AllowAllSubscriptionFilter {},
IdentityTransform {},
)
Expand All @@ -371,12 +358,14 @@ impl HonestNetwork {
gs
};

let swarm = SwarmBuilder::with_tokio_executor(
build_transport(&keypair),
gossipsub,
PeerId::from(keypair.public()),
)
.build();
let transport = build_transport(&keypair);
let swarm = SwarmBuilder::with_existing_identity(keypair)
.with_tokio()
.with_other_transport(|_| transport)
.expect("infallible")
.with_behaviour(|_| gossipsub)
.expect("infallible")
.build();

let mut peer_to_instance_name = HashMap::new();
for info in participants {
Expand Down
Loading
Loading