From aca3454c915fce029d484048d4abc119a4039f58 Mon Sep 17 00:00:00 2001 From: StemCll Date: Fri, 23 Dec 2022 00:47:23 +0100 Subject: [PATCH] refactor(gossipsub): remove `derive-builder` dev-dependency (#3270) Remove the `derive_builder` dev-dependency in gossipsub. We can manually implement the builder functionality on top of the `Default` instance of `InjectNodes`. Resolved #3228. --- protocols/gossipsub/Cargo.toml | 1 - protocols/gossipsub/src/behaviour/tests.rs | 58 ++++++++++++++++------ protocols/gossipsub/src/lib.rs | 4 -- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index dd085cfc267..6e69da58d4f 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -37,7 +37,6 @@ prometheus-client = "0.18.0" [dev-dependencies] async-std = "1.6.3" -derive_builder = "0.11.1" env_logger = "0.10.0" hex = "0.4.2" libp2p-mplex = { path = "../../muxers/mplex" } diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index f5fd8a3dc40..26af832e05f 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -38,8 +38,7 @@ use std::hash::{Hash, Hasher}; use std::thread::sleep; use std::time::Duration; -#[derive(Default, Builder, Debug)] -#[builder(default)] +#[derive(Default, Debug)] struct InjectNodes // TODO: remove trait bound Default when this issue is fixed: // https://github.com/colin-kiegel/rust-derive-builder/issues/93 @@ -108,28 +107,59 @@ where (gs, peers, topic_hashes) } -} -impl InjectNodesBuilder -where - D: DataTransform + Default + Clone + Send + 'static, - F: TopicSubscriptionFilter + Clone + Default + Send + 'static, -{ - pub fn create_network(&self) -> (Gossipsub, Vec, Vec) { - self.build().unwrap().create_network() + fn peer_no(mut self, peer_no: usize) -> Self { + self.peer_no = peer_no; + self + } + + fn topics(mut self, topics: Vec) -> Self { + self.topics = topics; + self + } + + #[allow(clippy::wrong_self_convention)] + fn to_subscribe(mut self, to_subscribe: bool) -> Self { + self.to_subscribe = to_subscribe; + self + } + + fn gs_config(mut self, gs_config: GossipsubConfig) -> Self { + self.gs_config = gs_config; + self + } + + fn explicit(mut self, explicit: usize) -> Self { + self.explicit = explicit; + self + } + + fn outbound(mut self, outbound: usize) -> Self { + self.outbound = outbound; + self + } + + fn scoring(mut self, scoring: Option<(PeerScoreParams, PeerScoreThresholds)>) -> Self { + self.scoring = scoring; + self + } + + fn subscription_filter(mut self, subscription_filter: F) -> Self { + self.subscription_filter = subscription_filter; + self } } -fn inject_nodes() -> InjectNodesBuilder +fn inject_nodes() -> InjectNodes where D: DataTransform + Default + Clone + Send + 'static, F: TopicSubscriptionFilter + Clone + Default + Send + 'static, { - InjectNodesBuilder::default() + InjectNodes::default() } -fn inject_nodes1() -> InjectNodesBuilder { - inject_nodes() +fn inject_nodes1() -> InjectNodes { + InjectNodes::::default() } // helper functions for testing diff --git a/protocols/gossipsub/src/lib.rs b/protocols/gossipsub/src/lib.rs index bd520aa987d..ad2c1f1fbf0 100644 --- a/protocols/gossipsub/src/lib.rs +++ b/protocols/gossipsub/src/lib.rs @@ -154,10 +154,6 @@ mod topic; mod transform; mod types; -#[cfg(test)] -#[macro_use] -extern crate derive_builder; - mod rpc_proto; pub use self::behaviour::{Gossipsub, GossipsubEvent, MessageAuthenticity};