Skip to content

Commit 41ec2ee

Browse files
refactor(taiko-client-rs, preconfirmation-p2p): simplify codebase by consolidating single-file submodules (#21142)
1 parent 6088b45 commit 41ec2ee

File tree

47 files changed

+2436
-3143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2436
-3143
lines changed

packages/preconfirmation-p2p/Cargo.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/preconfirmation-p2p/crates/net/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ quic-transport = ["libp2p/quic"]
1515
alloy-primitives.workspace = true
1616
anyhow.workspace = true
1717
async-trait.workspace = true
18-
clap = { version = "4", features = ["derive"] }
1918
crossbeam-skiplist.workspace = true
2019
dashmap.workspace = true
2120
discv5.workspace = true
@@ -49,4 +48,5 @@ workspace = true
4948
workspace = true
5049

5150
[dev-dependencies]
51+
clap = { version = "4", features = ["derive"] }
5252
tokio = { workspace = true, features = ["full"] }

packages/preconfirmation-p2p/crates/net/src/behaviour.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,13 @@ impl NetBehaviour {
139139
reqresp_cfg,
140140
);
141141

142-
let (pend_in, pend_out, est_in, est_out, est_total, per_peer, _dial_factor) =
143-
cfg.resolve_connection_caps();
144-
145-
let mut limits = ConnectionLimits::default();
146-
limits = limits.with_max_pending_incoming(pend_in);
147-
limits = limits.with_max_pending_outgoing(pend_out);
148-
limits = limits.with_max_established_incoming(est_in);
149-
limits = limits.with_max_established_outgoing(est_out);
150-
limits = limits.with_max_established(est_total);
151-
limits = limits.with_max_established_per_peer(per_peer);
142+
let limits = ConnectionLimits::default()
143+
.with_max_pending_incoming(cfg.max_pending_incoming)
144+
.with_max_pending_outgoing(cfg.max_pending_outgoing)
145+
.with_max_established_incoming(cfg.max_established_incoming)
146+
.with_max_established_outgoing(cfg.max_established_outgoing)
147+
.with_max_established(cfg.max_established_total)
148+
.with_max_established_per_peer(cfg.max_established_per_peer);
152149

153150
let block_list = BlockListBehaviour::<BlockedPeers>::default();
154151
let conn_limits = ConnectionLimitsBehaviour::new(limits);

packages/preconfirmation-p2p/crates/net/src/builder.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ pub fn build_transport_and_behaviour(cfg: &NetworkConfig) -> anyhow::Result<Buil
8888
libp2p::gossipsub::IdentTopic::new(preconfirmation_types::topic_raw_txlists(cfg.chain_id)),
8989
);
9090
let protocols = crate::codec::Protocols {
91-
commitments: crate::codec::SszProtocol(
92-
preconfirmation_types::protocol_get_commitments_by_number(cfg.chain_id),
93-
),
94-
raw_txlists: crate::codec::SszProtocol(preconfirmation_types::protocol_get_raw_txlist(
95-
cfg.chain_id,
96-
)),
97-
head: crate::codec::SszProtocol(preconfirmation_types::protocol_get_head(cfg.chain_id)),
91+
commitments: preconfirmation_types::protocol_get_commitments_by_number(cfg.chain_id),
92+
raw_txlists: preconfirmation_types::protocol_get_raw_txlist(cfg.chain_id),
93+
head: preconfirmation_types::protocol_get_head(cfg.chain_id),
9894
};
9995
let behaviour = NetBehaviour::new(keypair.clone(), topics.clone(), protocols, cfg)?;
10096

packages/preconfirmation-p2p/crates/net/src/codec.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,15 @@ const HEAD_REQ_MAX_BYTES: usize = 128;
7474
/// Maximum encoded size for head response frames.
7575
const HEAD_RESP_MAX_BYTES: usize = 64 * 1024;
7676

77-
#[derive(Clone)]
7877
/// Holds the protocol IDs for various request-response protocols.
78+
#[derive(Clone)]
7979
pub struct Protocols {
8080
/// Protocol ID for commitments request-response.
81-
pub commitments: SszProtocol,
81+
pub commitments: String,
8282
/// Protocol ID for raw transaction list request-response.
83-
pub raw_txlists: SszProtocol,
83+
pub raw_txlists: String,
8484
/// Protocol ID for get_head request-response.
85-
pub head: SszProtocol,
86-
}
87-
88-
#[derive(Clone)]
89-
/// A wrapper for a protocol ID string.
90-
///
91-
/// Implements `AsRef<str>` to allow easy conversion to `&str`.
92-
pub struct SszProtocol(
93-
/// Protocol identifier string.
94-
pub String,
95-
);
96-
97-
impl AsRef<str> for SszProtocol {
98-
/// Return the protocol identifier as a string slice.
99-
fn as_ref(&self) -> &str {
100-
&self.0
101-
}
85+
pub head: String,
10286
}
10387

10488
#[async_trait]
@@ -108,20 +92,20 @@ where
10892
Req: SimpleSerialize + Clone + Send + Sync + 'static,
10993
Resp: SimpleSerialize + Clone + Send + Sync + 'static,
11094
{
111-
type Protocol = SszProtocol;
95+
type Protocol = String;
11296
type Request = Req;
11397
type Response = Resp;
11498

11599
/// Read and decode a request frame.
116-
async fn read_request<R>(&mut self, _: &SszProtocol, io: &mut R) -> io::Result<Self::Request>
100+
async fn read_request<R>(&mut self, _: &String, io: &mut R) -> io::Result<Self::Request>
117101
where
118102
R: AsyncRead + Unpin + Send,
119103
{
120104
read_ssz(io, MAX_REQ).await
121105
}
122106

123107
/// Read and decode a response frame.
124-
async fn read_response<R>(&mut self, _: &SszProtocol, io: &mut R) -> io::Result<Self::Response>
108+
async fn read_response<R>(&mut self, _: &String, io: &mut R) -> io::Result<Self::Response>
125109
where
126110
R: AsyncRead + Unpin + Send,
127111
{
@@ -131,7 +115,7 @@ where
131115
/// Encode and write a request frame.
132116
async fn write_request<W>(
133117
&mut self,
134-
_: &SszProtocol,
118+
_: &String,
135119
io: &mut W,
136120
req: Self::Request,
137121
) -> io::Result<()>
@@ -144,7 +128,7 @@ where
144128
/// Encode and write a response frame.
145129
async fn write_response<W>(
146130
&mut self,
147-
_: &SszProtocol,
131+
_: &String,
148132
io: &mut W,
149133
res: Self::Response,
150134
) -> io::Result<()>
@@ -160,7 +144,7 @@ where
160144
///
161145
/// The function reads a u32 length encoded as unsigned-varint, rejects frames larger than
162146
/// `max_len`, then reads the payload and attempts SSZ deserialization.
163-
async fn read_ssz<T: SimpleSerialize + Default, R: AsyncRead + Unpin + Send>(
147+
async fn read_ssz<T: SimpleSerialize, R: AsyncRead + Unpin + Send>(
164148
io: &mut R,
165149
max_len: usize,
166150
) -> io::Result<T> {
@@ -219,7 +203,7 @@ mod tests {
219203
#[tokio::test]
220204
async fn commitments_codec_roundtrip() {
221205
let mut codec = CommitmentsCodec::default();
222-
let proto = SszProtocol("/taiko/test/commitments/1".into());
206+
let proto = "/taiko/test/commitments/1".to_string();
223207

224208
let req =
225209
GetCommitmentsByNumberRequest { start_block_number: Uint256::from(1u64), max_count: 5 };
@@ -241,7 +225,7 @@ mod tests {
241225
#[tokio::test]
242226
async fn oversized_response_is_rejected_on_read() {
243227
let mut codec = RawTxListCodec::default();
244-
let proto = SszProtocol("/taiko/test/rawtx/1".into());
228+
let proto = "/taiko/test/rawtx/1".to_string();
245229

246230
// Craft a frame with a length one byte over the allowed bound.
247231
let over = RAW_TXLIST_RESP_MAX_BYTES + 1;
@@ -267,7 +251,7 @@ mod tests {
267251
16,
268252
>;
269253
let mut codec = TinyCodec::default();
270-
let proto = SszProtocol("/taiko/test/rawtx/1".into());
254+
let proto = "/taiko/test/rawtx/1".to_string();
271255

272256
let txlist = preconfirmation_types::TxListBytes::try_from(vec![0u8; 32]).unwrap();
273257
let resp = GetRawTxListResponse {

packages/preconfirmation-p2p/crates/net/src/config.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,32 +193,7 @@ impl Default for NetworkConfig {
193193
}
194194
}
195195

196-
/// Logical grouping for the resolved connection caps and dial factor returned by
197-
/// `NetworkConfig::resolve_connection_caps`.
198-
pub(crate) type ConnectionCaps = (
199-
Option<u32>, // pending inbound cap
200-
Option<u32>, // pending outbound cap
201-
Option<u32>, // established inbound cap
202-
Option<u32>, // established outbound cap
203-
Option<u32>, // total established cap
204-
Option<u32>, // established per peer cap
205-
u8, // dial concurrency factor
206-
);
207-
208196
impl NetworkConfig {
209-
/// Resolve connection caps and dial factor.
210-
pub(crate) fn resolve_connection_caps(&self) -> ConnectionCaps {
211-
(
212-
self.max_pending_incoming,
213-
self.max_pending_outgoing,
214-
self.max_established_incoming,
215-
self.max_established_outgoing,
216-
self.max_established_total,
217-
self.max_established_per_peer,
218-
self.dial_concurrency_factor,
219-
)
220-
}
221-
222197
/// Ensure rate-limit parameters are sane before constructing a limiter.
223198
pub(crate) fn validate_request_rate_limits(&self) {
224199
debug_assert!(self.request_window > Duration::ZERO, "request_window must be > 0");

packages/preconfirmation-p2p/crates/net/src/driver/gossip.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,24 @@ use super::*;
1212
/// Identifies which gossip topic payload is being processed.
1313
#[derive(Clone, Copy)]
1414
enum GossipKind {
15-
/// Signed commitment payloads on the commitments topic.
1615
Commitment,
17-
/// Raw transaction list payloads on the raw txlists topic.
1816
RawTxList,
1917
}
2018

2119
impl GossipKind {
22-
/// Returns the label used for metrics for this gossip kind.
20+
/// Returns the label used for metrics.
2321
fn label(self) -> &'static str {
2422
match self {
25-
GossipKind::Commitment => "commitment",
26-
GossipKind::RawTxList => "raw_txlists",
23+
Self::Commitment => "commitment",
24+
Self::RawTxList => "raw_txlists",
2725
}
2826
}
2927

30-
/// Returns the error string used when decoding fails.
31-
fn decode_error(self) -> &'static str {
28+
/// Returns the error string used for decode/validation failures.
29+
fn error_msg(self) -> &'static str {
3230
match self {
33-
GossipKind::Commitment => "invalid signed commitment gossip",
34-
GossipKind::RawTxList => "invalid raw txlist gossip",
35-
}
36-
}
37-
38-
/// Returns the error string used when validation fails.
39-
fn validation_error(self) -> &'static str {
40-
match self {
41-
GossipKind::Commitment => "invalid signed commitment gossip",
42-
GossipKind::RawTxList => "invalid raw txlist gossip",
31+
Self::Commitment => "invalid signed commitment gossip",
32+
Self::RawTxList => "invalid raw txlist gossip",
4333
}
4434
}
4535
}
@@ -88,7 +78,7 @@ impl NetworkDriver {
8878
);
8979
metrics::counter!("p2p_gossip_invalid", "kind" => kind.label(), "reason" => "validation").increment(1);
9080
self.apply_reputation(propagation_source, PeerAction::GossipInvalid);
91-
self.emit_error(NetworkErrorKind::GossipValidation, kind.validation_error());
81+
self.emit_error(NetworkErrorKind::GossipValidation, kind.error_msg());
9282
}
9383
}
9484
Err(_) => {
@@ -100,7 +90,7 @@ impl NetworkDriver {
10090
metrics::counter!("p2p_gossip_invalid", "kind" => kind.label(), "reason" => "decode")
10191
.increment(1);
10292
self.apply_reputation(propagation_source, PeerAction::GossipInvalid);
103-
self.emit_error(NetworkErrorKind::GossipDecode, kind.decode_error());
93+
self.emit_error(NetworkErrorKind::GossipDecode, kind.error_msg());
10494
}
10595
}
10696
}

packages/preconfirmation-p2p/crates/net/src/driver/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ impl NetworkDriver {
111111
validator: Box<dyn ValidationAdapter>,
112112
storage: Option<Arc<dyn PreconfStorage>>,
113113
) -> anyhow::Result<(Self, NetworkHandle)> {
114-
let dial_factor = {
115-
let (_, _, _, _, _, _, dial) = cfg.resolve_connection_caps();
116-
NonZeroU8::new(dial).unwrap_or_else(|| NonZeroU8::new(1).unwrap())
117-
};
114+
let dial_factor =
115+
NonZeroU8::new(cfg.dial_concurrency_factor).unwrap_or(NonZeroU8::new(1).unwrap());
118116

119117
let parts = build_transport_and_behaviour(&cfg)?;
120118
let peer_id = parts.keypair.public().to_peer_id();

0 commit comments

Comments
 (0)