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

protocols/kad: Refactor KademliaEvent #2297

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 changes: 1 addition & 1 deletion misc/metrics/src/kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl super::Recorder<libp2p_kad::KademliaEvent> for super::Metrics {
}
}

libp2p_kad::KademliaEvent::InboundRequestServed { request } => {
libp2p_kad::KademliaEvent::InboundRequest { request } => {
self.kad
.inbound_requests
.get_or_create(&request.into())
Expand Down
87 changes: 32 additions & 55 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ pub enum KademliaStoreInserts {
/// Whenever a (provider) record is received,
/// the record is forwarded immediately to the [`RecordStore`].
Unfiltered,
/// Whenever a (provider) record is received, an event is emitted.
supercmmetry marked this conversation as resolved.
Show resolved Hide resolved
/// Provider records generate a [`KademliaEvent::InboundAddProviderRequest`],
/// normal records generate a [`KademliaEvent::InboundPutRecordRequest`].
///
/// When deemed valid, a (provider) record needs to be explicitly stored in
/// the [`RecordStore`] via [`RecordStore::put`] or [`RecordStore::add_provider`],
/// whichever is applicable. A mutable reference to the [`RecordStore`] can
Expand Down Expand Up @@ -1641,17 +1637,19 @@ where
return;
}
},
KademliaStoreInserts::FilterBoth => {
self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundPutRecordRequest {
source,
connection,
record: record.clone(),
},
));
}
KademliaStoreInserts::FilterBoth => {}
}

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequest {
request: InboundRequest::PutRecord {
source,
connection,
record: record.clone(),
supercmmetry marked this conversation as resolved.
Show resolved Hide resolved
},
},
));
}

// The remote receives a [`KademliaHandlerIn::PutRecordRes`] even in the
Expand Down Expand Up @@ -1684,17 +1682,20 @@ where
};
match self.record_filtering {
KademliaStoreInserts::Unfiltered => {
if let Err(e) = self.store.add_provider(record) {
if let Err(e) = self.store.add_provider(record.clone()) {
info!("Provider record not stored: {:?}", e);
return;
}
}
KademliaStoreInserts::FilterBoth => {
self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundAddProviderRequest { record },
));
}
KademliaStoreInserts::FilterBoth => {}
}

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequest {
request: InboundRequest::AddProvider { record },
},
));
}
}

Expand Down Expand Up @@ -1951,7 +1952,7 @@ where

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequestServed {
KademliaEvent::InboundRequest {
request: InboundRequest::FindNode {
num_closer_peers: closer_peers.len(),
},
Expand Down Expand Up @@ -1982,7 +1983,7 @@ where

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequestServed {
KademliaEvent::InboundRequest {
request: InboundRequest::GetProvider {
num_closer_peers: closer_peers.len(),
num_provider_peers: provider_peers.len(),
Expand Down Expand Up @@ -2039,13 +2040,6 @@ where
}

self.provider_received(key, provider);

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequestServed {
request: InboundRequest::AddProvider {},
},
));
}

KademliaHandlerEvent::GetRecord { key, request_id } => {
Expand All @@ -2066,7 +2060,7 @@ where

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequestServed {
KademliaEvent::InboundRequest {
request: InboundRequest::GetRecord {
num_closer_peers: closer_peers.len(),
present_locally: record.is_some(),
Expand Down Expand Up @@ -2150,13 +2144,6 @@ where

KademliaHandlerEvent::PutRecord { record, request_id } => {
self.record_received(source, connection, request_id, record);

self.queued_events
.push_back(NetworkBehaviourAction::GenerateEvent(
KademliaEvent::InboundRequestServed {
request: InboundRequest::PutRecord {},
},
));
}

KademliaHandlerEvent::PutRecordRes { user_data, .. } => {
Expand Down Expand Up @@ -2371,26 +2358,12 @@ pub struct PeerRecord {
/// See [`NetworkBehaviour::poll`].
#[derive(Debug)]
pub enum KademliaEvent {
/// A peer sent a [`KademliaHandlerIn::PutRecord`] request and filtering is enabled.
///
/// See [`KademliaStoreInserts`] and [`KademliaConfig::set_record_filtering`].
supercmmetry marked this conversation as resolved.
Show resolved Hide resolved
InboundPutRecordRequest {
source: PeerId,
connection: ConnectionId,
record: Record,
},

/// A peer sent a [`KademliaHandlerIn::AddProvider`] request and filtering [`KademliaStoreInserts::FilterBoth`] is enabled.
///
/// See [`KademliaStoreInserts`] and [`KademliaConfig::set_record_filtering`] for details..
InboundAddProviderRequest { record: ProviderRecord },

/// An inbound request has been received and handled.
//
// Note on the difference between 'request' and 'query': A request is a
// single request-response style exchange with a single remote peer. A query
// is made of multiple requests across multiple remote peers.
InboundRequestServed { request: InboundRequest },
InboundRequest { request: InboundRequest },

/// An outbound query has produced a result.
OutboundQueryCompleted {
Expand Down Expand Up @@ -2465,14 +2438,18 @@ pub enum InboundRequest {
num_provider_peers: usize,
},
/// Request to store a peer as a provider.
AddProvider {},
AddProvider { record: ProviderRecord },
/// Request to retrieve a record.
GetRecord {
num_closer_peers: usize,
present_locally: bool,
},
/// Request to store a record.
PutRecord {},
PutRecord {
source: PeerId,
connection: ConnectionId,
record: Record,
},
}

/// The results of Kademlia queries.
Expand Down
8 changes: 3 additions & 5 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,10 @@ fn put_record() {
}
}
Poll::Ready(Some(SwarmEvent::Behaviour(
KademliaEvent::InboundPutRecordRequest { record, .. },
KademliaEvent::InboundRequest {
request: InboundRequest::PutRecord { record, .. },
},
))) => {
assert_ne!(
swarm.behaviour().record_filtering,
KademliaStoreInserts::Unfiltered
);
if !drop_records {
// Accept the record
swarm
Expand Down