Skip to content

Commit 75e536d

Browse files
authored
fix: send status request on connection and use real status (#54)
* feat: send Status request when peer connects * fix: send real status instead of dummy * fix: clone store before passing * chore: change log messages
1 parent 18314c8 commit 75e536d

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

Cargo.lock

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

bin/ethlambda/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ async fn main() {
102102
p2p_socket,
103103
blockchain,
104104
p2p_rx,
105+
store.clone(),
105106
));
106107

107108
ethlambda_rpc::start_rpc_server(metrics_socket, store)

crates/net/p2p/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version.workspace = true
1111

1212
[dependencies]
1313
ethlambda-blockchain.workspace = true
14+
ethlambda-storage.workspace = true
1415
ethlambda-types.workspace = true
1516

1617
async-trait = "0.1"

crates/net/p2p/src/lib.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::{
44
};
55

66
use ethlambda_blockchain::{BlockChain, OutboundGossip};
7+
use ethlambda_storage::Store;
8+
use ethlambda_types::state::Checkpoint;
79
use ethrex_common::H264;
810
use ethrex_p2p::types::NodeRecord;
911
use ethrex_rlp::decode::RLPDecode;
@@ -41,6 +43,7 @@ pub async fn start_p2p(
4143
listening_socket: SocketAddr,
4244
blockchain: BlockChain,
4345
p2p_rx: mpsc::UnboundedReceiver<OutboundGossip>,
46+
store: Store,
4447
) {
4548
let config = libp2p::gossipsub::ConfigBuilder::default()
4649
// d
@@ -142,7 +145,15 @@ pub async fn start_p2p(
142145

143146
info!("P2P node started on {listening_socket}");
144147

145-
event_loop(swarm, blockchain, p2p_rx, attestation_topic, block_topic).await;
148+
event_loop(
149+
swarm,
150+
blockchain,
151+
p2p_rx,
152+
attestation_topic,
153+
block_topic,
154+
store,
155+
)
156+
.await;
146157
}
147158

148159
/// [libp2p Behaviour](libp2p::swarm::NetworkBehaviour) combining Gossipsub and Request-Response Behaviours
@@ -160,6 +171,7 @@ async fn event_loop(
160171
mut p2p_rx: mpsc::UnboundedReceiver<OutboundGossip>,
161172
attestation_topic: libp2p::gossipsub::IdentTopic,
162173
block_topic: libp2p::gossipsub::IdentTopic,
174+
store: Store,
163175
) {
164176
loop {
165177
tokio::select! {
@@ -179,7 +191,7 @@ async fn event_loop(
179191
SwarmEvent::Behaviour(BehaviourEvent::ReqResp(
180192
message @ request_response::Event::Message { .. },
181193
)) => {
182-
handle_req_resp_message(&mut swarm, message).await;
194+
handle_req_resp_message(&mut swarm, message, &store).await;
183195
}
184196
SwarmEvent::Behaviour(BehaviourEvent::Gossipsub(
185197
message @ libp2p::gossipsub::Event::Message { .. },
@@ -195,8 +207,13 @@ async fn event_loop(
195207
let direction = connection_direction(&endpoint);
196208
if num_established.get() == 1 {
197209
metrics::notify_peer_connected(&Some(peer_id), direction, "success");
210+
// Send status request on first connection to this peer
211+
let our_status = build_status(&store);
212+
info!(%peer_id, %direction, finalized_slot=%our_status.finalized.slot, head_slot=%our_status.head.slot, "Added connection to new peer, sending status request");
213+
swarm.behaviour_mut().req_resp.send_request(&peer_id, our_status);
214+
} else {
215+
info!(%peer_id, %direction, "Added peer connection");
198216
}
199-
info!(%peer_id, %direction, "Peer connected");
200217
}
201218
SwarmEvent::ConnectionClosed {
202219
peer_id,
@@ -296,6 +313,7 @@ async fn handle_outgoing_gossip(
296313
async fn handle_req_resp_message(
297314
swarm: &mut libp2p::Swarm<Behaviour>,
298315
event: request_response::Event<Status, Status>,
316+
store: &Store,
299317
) {
300318
let request_response::Event::Message {
301319
peer,
@@ -312,13 +330,12 @@ async fn handle_req_resp_message(
312330
channel,
313331
} => {
314332
info!(finalized_slot=%request.finalized.slot, head_slot=%request.head.slot, "Received status request from peer {peer}");
315-
// TODO: send real status
333+
let our_status = build_status(store);
316334
swarm
317335
.behaviour_mut()
318336
.req_resp
319-
.send_response(channel, request.clone())
337+
.send_response(channel, our_status)
320338
.unwrap();
321-
swarm.behaviour_mut().req_resp.send_request(&peer, request);
322339
}
323340
request_response::Message::Response {
324341
request_id: _,
@@ -377,6 +394,20 @@ fn connection_direction(endpoint: &libp2p::core::ConnectedPoint) -> &'static str
377394
}
378395
}
379396

397+
/// Build a Status message from the current Store state.
398+
fn build_status(store: &Store) -> Status {
399+
let finalized = store.latest_finalized();
400+
let head_root = store.head();
401+
let head_slot = store.get_block(&head_root).expect("head block exists").slot;
402+
Status {
403+
finalized,
404+
head: Checkpoint {
405+
root: head_root,
406+
slot: head_slot,
407+
},
408+
}
409+
}
410+
380411
fn compute_message_id(message: &libp2p::gossipsub::Message) -> libp2p::gossipsub::MessageId {
381412
const MESSAGE_DOMAIN_INVALID_SNAPPY: [u8; 4] = [0x00, 0x00, 0x00, 0x00];
382413
const MESSAGE_DOMAIN_VALID_SNAPPY: [u8; 4] = [0x01, 0x00, 0x00, 0x00];

0 commit comments

Comments
 (0)