2018-11-29 12:11:35 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2019-01-21 10:33:51 +00:00
|
|
|
use crate::protocol::{
|
|
|
|
KadInStreamSink, KadOutStreamSink, KadPeer, KadRequestMsg, KadResponseMsg,
|
|
|
|
KademliaProtocolConfig,
|
|
|
|
};
|
2019-08-15 11:36:47 +02:00
|
|
|
use crate::record::{self, Record};
|
2023-01-18 13:35:07 +11:00
|
|
|
use either::Either;
|
2018-11-29 12:11:35 +01:00
|
|
|
use futures::prelude::*;
|
2022-11-03 06:20:55 +11:00
|
|
|
use futures::stream::SelectAll;
|
2021-10-30 12:41:30 +02:00
|
|
|
use instant::Instant;
|
2022-11-17 17:19:36 +00:00
|
|
|
use libp2p_core::{either::EitherOutput, upgrade, ConnectedPoint, PeerId};
|
|
|
|
use libp2p_swarm::handler::{
|
|
|
|
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
|
2021-08-11 13:12:12 +02:00
|
|
|
};
|
|
|
|
use libp2p_swarm::{
|
2022-02-21 13:32:24 +01:00
|
|
|
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
|
|
|
|
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
|
2019-07-04 14:47:59 +02:00
|
|
|
};
|
[multistream-select] Reduce roundtrips in protocol negotiation. (#1212)
* Remove tokio-codec dependency from multistream-select.
In preparation for the eventual switch from tokio to std futures.
Includes some initial refactoring in preparation for further work
in the context of https://github.com/libp2p/rust-libp2p/issues/659.
* Reduce default buffer sizes.
* Allow more than one frame to be buffered for sending.
* Doc tweaks.
* Remove superfluous (duplicated) Message types.
* Reduce roundtrips in multistream-select negotiation.
1. Enable 0-RTT: If the dialer only supports a single protocol, it can send
protocol data (e.g. the actual application request) together with
the multistream-select header and protocol proposal. Similarly,
if the listener supports a proposed protocol, it can send protocol
data (e.g. the actual application response) together with the
multistream-select header and protocol confirmation.
2. In general, the dialer "settles on" an expected protocol as soon
as it runs out of alternatives. Furthermore, both dialer and listener
do not immediately flush the final protocol confirmation, allowing it
to be sent together with application protocol data. Attempts to read
from the negotiated I/O stream implicitly flushes any pending data.
3. A clean / graceful shutdown of an I/O stream always completes protocol
negotiation.
The publich API of multistream-select changed slightly, requiring both
AsyncRead and AsyncWrite bounds for async reading and writing due to
the implicit buffering and "lazy" negotiation. The error types have
also been changed, but they were not previously fully exported.
Includes some general refactoring with simplifications and some more tests,
e.g. there was an edge case relating to a possible ambiguity when parsing
multistream-select protocol messages.
* Further missing commentary.
* Remove unused test dependency.
* Adjust commentary.
* Cleanup NegotiatedComplete::poll()
* Fix deflate protocol tests.
* Stabilise network_simult test.
The test implicitly relied on "slow" connection establishment
in order to have a sufficient probability of passing.
With the removal of roundtrips in multistream-select, it is now
more likely that within the up to 50ms duration between swarm1
and swarm2 dialing, the connection is already established, causing
the expectation of step == 1 to fail when receiving a Connected event,
since the step may then still be 0.
This commit aims to avoid these spurious errors by detecting runs
during which a connection is established "too quickly", repeating
the test run.
It still seems theoretically possible that, if connections are always
established "too quickly", the test runs forever. However, given that
the delta between swarm1 and swarm2 dialing is 0-50ms and that the
TCP transport is used, that seems probabilistically unlikely.
Nevertheless, the purpose of the artificial dialing delay between
swarm1 and swarm2 should be re-evaluated and possibly at least
the maximum delay further reduced.
* Complete negotiation between upgrades in libp2p-core.
While multistream-select, as a standalone library and providing
an API at the granularity of a single negotiation, supports
lazy negotiation (and in particular 0-RTT negotiation), in the
context of libp2p-core where any number of negotiations are
composed generically within the concept of composable "upgrades",
it is necessary to wait for protocol negotiation between upgrades
to complete.
* Clarify docs. Simplify listener upgrades.
Since reading from a Negotiated I/O stream implicitly flushes any pending
negotiation data, there is no pitfall involved in not waiting for completion.
2019-08-12 12:09:53 +02:00
|
|
|
use log::trace;
|
2022-11-03 06:20:55 +11:00
|
|
|
use std::task::Waker;
|
2021-08-11 13:12:12 +02:00
|
|
|
use std::{
|
|
|
|
error, fmt, io, marker::PhantomData, pin::Pin, task::Context, task::Poll, time::Duration,
|
|
|
|
};
|
2018-11-29 12:11:35 +01:00
|
|
|
|
2022-06-09 15:12:03 +02:00
|
|
|
const MAX_NUM_INBOUND_SUBSTREAMS: usize = 32;
|
|
|
|
|
2020-11-05 20:58:14 +01:00
|
|
|
/// A prototype from which [`KademliaHandler`]s can be constructed.
|
|
|
|
pub struct KademliaHandlerProto<T> {
|
|
|
|
config: KademliaHandlerConfig,
|
|
|
|
_type: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> KademliaHandlerProto<T> {
|
|
|
|
pub fn new(config: KademliaHandlerConfig) -> Self {
|
2021-08-11 13:12:12 +02:00
|
|
|
KademliaHandlerProto {
|
|
|
|
config,
|
|
|
|
_type: PhantomData,
|
|
|
|
}
|
2020-11-05 20:58:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
impl<T: Clone + fmt::Debug + Send + 'static + Unpin> IntoConnectionHandler
|
|
|
|
for KademliaHandlerProto<T>
|
|
|
|
{
|
2020-11-05 20:58:14 +01:00
|
|
|
type Handler = KademliaHandler<T>;
|
|
|
|
|
2022-06-09 15:12:03 +02:00
|
|
|
fn into_handler(self, remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
|
|
|
|
KademliaHandler::new(self.config, endpoint.clone(), *remote_peer_id)
|
2020-11-05 20:58:14 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
|
2020-11-05 20:58:14 +01:00
|
|
|
if self.config.allow_listening {
|
2023-01-18 13:35:07 +11:00
|
|
|
Either::Left(self.config.protocol_config.clone())
|
2020-11-05 20:58:14 +01:00
|
|
|
} else {
|
2023-01-18 13:35:07 +11:00
|
|
|
Either::Right(upgrade::DeniedUpgrade)
|
2020-11-05 20:58:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Protocol handler that manages substreams for the Kademlia protocol
|
|
|
|
/// on a single connection with a peer.
|
2018-11-29 12:11:35 +01:00
|
|
|
///
|
|
|
|
/// The handler will automatically open a Kademlia substream with the remote for each request we
|
|
|
|
/// make.
|
|
|
|
///
|
|
|
|
/// It also handles requests made by the remote.
|
2020-02-07 16:29:30 +01:00
|
|
|
pub struct KademliaHandler<TUserData> {
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Configuration for the Kademlia protocol.
|
2020-03-19 17:01:34 +01:00
|
|
|
config: KademliaHandlerConfig,
|
2018-11-29 12:11:35 +01:00
|
|
|
|
|
|
|
/// Next unique ID of a connection.
|
|
|
|
next_connec_unique_id: UniqueConnecId,
|
|
|
|
|
2022-06-09 15:12:03 +02:00
|
|
|
/// List of active outbound substreams with the state they are in.
|
2022-11-03 06:20:55 +11:00
|
|
|
outbound_substreams: SelectAll<OutboundSubstreamState<TUserData>>,
|
2022-06-09 15:12:03 +02:00
|
|
|
|
|
|
|
/// List of active inbound substreams with the state they are in.
|
2022-11-03 06:20:55 +11:00
|
|
|
inbound_substreams: SelectAll<InboundSubstreamState<TUserData>>,
|
2019-01-30 16:37:34 +01:00
|
|
|
|
|
|
|
/// Until when to keep the connection alive.
|
|
|
|
keep_alive: KeepAlive,
|
2020-11-05 20:58:14 +01:00
|
|
|
|
|
|
|
/// The connected endpoint of the connection that the handler
|
|
|
|
/// is associated with.
|
|
|
|
endpoint: ConnectedPoint,
|
|
|
|
|
2022-06-09 15:12:03 +02:00
|
|
|
/// The [`PeerId`] of the remote.
|
|
|
|
remote_peer_id: PeerId,
|
|
|
|
|
2020-11-05 20:58:14 +01:00
|
|
|
/// The current state of protocol confirmation.
|
|
|
|
protocol_status: ProtocolStatus,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The states of protocol confirmation that a connection
|
|
|
|
/// handler transitions through.
|
|
|
|
enum ProtocolStatus {
|
|
|
|
/// It is as yet unknown whether the remote supports the
|
|
|
|
/// configured protocol name.
|
|
|
|
Unconfirmed,
|
|
|
|
/// The configured protocol name has been confirmed by the remote
|
|
|
|
/// but has not yet been reported to the `Kademlia` behaviour.
|
|
|
|
Confirmed,
|
|
|
|
/// The configured protocol has been confirmed by the remote
|
|
|
|
/// and the confirmation reported to the `Kademlia` behaviour.
|
|
|
|
Reported,
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:01:34 +01:00
|
|
|
/// Configuration of a [`KademliaHandler`].
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct KademliaHandlerConfig {
|
|
|
|
/// Configuration of the wire protocol.
|
|
|
|
pub protocol_config: KademliaProtocolConfig,
|
|
|
|
|
|
|
|
/// If false, we deny incoming requests.
|
|
|
|
pub allow_listening: bool,
|
|
|
|
|
|
|
|
/// Time after which we close an idle connection.
|
|
|
|
pub idle_timeout: Duration,
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:12:03 +02:00
|
|
|
/// State of an active outbound substream.
|
|
|
|
enum OutboundSubstreamState<TUserData> {
|
2018-11-29 12:11:35 +01:00
|
|
|
/// We haven't started opening the outgoing substream yet.
|
|
|
|
/// Contains the request we want to send, and the user data if we expect an answer.
|
2022-11-03 06:20:55 +11:00
|
|
|
PendingOpen(SubstreamProtocol<KademliaProtocolConfig, (KadRequestMsg, Option<TUserData>)>),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Waiting to send a message to the remote.
|
2022-06-09 15:12:03 +02:00
|
|
|
PendingSend(
|
2020-02-07 16:29:30 +01:00
|
|
|
KadOutStreamSink<NegotiatedSubstream>,
|
2018-11-29 12:11:35 +01:00
|
|
|
KadRequestMsg,
|
|
|
|
Option<TUserData>,
|
|
|
|
),
|
|
|
|
/// Waiting to flush the substream so that the data arrives to the remote.
|
2022-06-09 15:12:03 +02:00
|
|
|
PendingFlush(KadOutStreamSink<NegotiatedSubstream>, Option<TUserData>),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Waiting for an answer back from the remote.
|
|
|
|
// TODO: add timeout
|
2022-06-09 15:12:03 +02:00
|
|
|
WaitingAnswer(KadOutStreamSink<NegotiatedSubstream>, TUserData),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// An error happened on the substream and we should report the error to the user.
|
2022-06-09 15:12:03 +02:00
|
|
|
ReportError(KademliaHandlerQueryErr, TUserData),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// The substream is being closed.
|
2022-06-09 15:12:03 +02:00
|
|
|
Closing(KadOutStreamSink<NegotiatedSubstream>),
|
2022-11-03 06:20:55 +11:00
|
|
|
/// The substream is complete and will not perform any more work.
|
|
|
|
Done,
|
|
|
|
Poisoned,
|
2022-06-09 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// State of an active inbound substream.
|
2022-11-03 06:20:55 +11:00
|
|
|
enum InboundSubstreamState<TUserData> {
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Waiting for a request from the remote.
|
2022-06-09 15:12:03 +02:00
|
|
|
WaitingMessage {
|
|
|
|
/// Whether it is the first message to be awaited on this stream.
|
|
|
|
first: bool,
|
|
|
|
connection_id: UniqueConnecId,
|
|
|
|
substream: KadInStreamSink<NegotiatedSubstream>,
|
|
|
|
},
|
2022-11-24 04:46:43 +11:00
|
|
|
/// Waiting for the behaviour to send a [`KademliaHandlerIn`] event containing the response.
|
|
|
|
WaitingBehaviour(
|
2022-11-03 06:20:55 +11:00
|
|
|
UniqueConnecId,
|
|
|
|
KadInStreamSink<NegotiatedSubstream>,
|
|
|
|
Option<Waker>,
|
|
|
|
),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Waiting to send an answer back to the remote.
|
2022-06-09 15:12:03 +02:00
|
|
|
PendingSend(
|
2021-08-11 13:12:12 +02:00
|
|
|
UniqueConnecId,
|
|
|
|
KadInStreamSink<NegotiatedSubstream>,
|
|
|
|
KadResponseMsg,
|
|
|
|
),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Waiting to flush an answer back to the remote.
|
2022-06-09 15:12:03 +02:00
|
|
|
PendingFlush(UniqueConnecId, KadInStreamSink<NegotiatedSubstream>),
|
2018-11-29 12:11:35 +01:00
|
|
|
/// The substream is being closed.
|
2022-06-09 15:12:03 +02:00
|
|
|
Closing(KadInStreamSink<NegotiatedSubstream>),
|
2022-11-03 06:20:55 +11:00
|
|
|
/// The substream was cancelled in favor of a new one.
|
|
|
|
Cancelled,
|
|
|
|
|
|
|
|
Poisoned {
|
|
|
|
phantom: PhantomData<TUserData>,
|
|
|
|
},
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
impl<TUserData> InboundSubstreamState<TUserData> {
|
|
|
|
fn try_answer_with(
|
|
|
|
&mut self,
|
|
|
|
id: KademliaRequestId,
|
|
|
|
msg: KadResponseMsg,
|
|
|
|
) -> Result<(), KadResponseMsg> {
|
|
|
|
match std::mem::replace(
|
|
|
|
self,
|
|
|
|
InboundSubstreamState::Poisoned {
|
|
|
|
phantom: PhantomData,
|
|
|
|
},
|
|
|
|
) {
|
2022-11-24 04:46:43 +11:00
|
|
|
InboundSubstreamState::WaitingBehaviour(conn_id, substream, mut waker)
|
2022-11-03 06:20:55 +11:00
|
|
|
if conn_id == id.connec_unique_id =>
|
|
|
|
{
|
|
|
|
*self = InboundSubstreamState::PendingSend(conn_id, substream, msg);
|
|
|
|
|
|
|
|
if let Some(waker) = waker.take() {
|
|
|
|
waker.wake();
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
*self = other;
|
|
|
|
|
|
|
|
Err(msg)
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-06-09 15:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
fn close(&mut self) {
|
|
|
|
match std::mem::replace(
|
|
|
|
self,
|
|
|
|
InboundSubstreamState::Poisoned {
|
|
|
|
phantom: PhantomData,
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
InboundSubstreamState::WaitingMessage { substream, .. }
|
2022-11-24 04:46:43 +11:00
|
|
|
| InboundSubstreamState::WaitingBehaviour(_, substream, _)
|
2022-11-03 06:20:55 +11:00
|
|
|
| InboundSubstreamState::PendingSend(_, substream, _)
|
|
|
|
| InboundSubstreamState::PendingFlush(_, substream)
|
|
|
|
| InboundSubstreamState::Closing(substream) => {
|
|
|
|
*self = InboundSubstreamState::Closing(substream);
|
2022-06-09 15:12:03 +02:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
InboundSubstreamState::Cancelled => {
|
|
|
|
*self = InboundSubstreamState::Cancelled;
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
InboundSubstreamState::Poisoned { .. } => unreachable!(),
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Event produced by the Kademlia handler.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum KademliaHandlerEvent<TUserData> {
|
2020-11-05 20:58:14 +01:00
|
|
|
/// The configured protocol name has been confirmed by the peer through
|
|
|
|
/// a successfully negotiated substream.
|
|
|
|
///
|
|
|
|
/// This event is only emitted once by a handler upon the first
|
|
|
|
/// successfully negotiated inbound or outbound substream and
|
|
|
|
/// indicates that the connected peer participates in the Kademlia
|
|
|
|
/// overlay network identified by the configured protocol name.
|
|
|
|
ProtocolConfirmed { endpoint: ConnectedPoint },
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Request for the list of nodes whose IDs are the closest to `key`. The number of nodes
|
|
|
|
/// returned is not specified, but should be around 20.
|
|
|
|
FindNodeReq {
|
2019-07-03 16:16:25 +02:00
|
|
|
/// The key for which to locate the closest nodes.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: Vec<u8>,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Identifier of the request. Needs to be passed back when answering.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Response to an `KademliaHandlerIn::FindNodeReq`.
|
|
|
|
FindNodeRes {
|
|
|
|
/// Results of the request.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// The user data passed to the `FindNodeReq`.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Same as `FindNodeReq`, but should also return the entries of the local providers list for
|
|
|
|
/// this key.
|
|
|
|
GetProvidersReq {
|
2019-08-15 11:36:47 +02:00
|
|
|
/// The key for which providers are requested.
|
|
|
|
key: record::Key,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Identifier of the request. Needs to be passed back when answering.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Response to an `KademliaHandlerIn::GetProvidersReq`.
|
|
|
|
GetProvidersRes {
|
|
|
|
/// Nodes closest to the key.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// Known providers for this key.
|
|
|
|
provider_peers: Vec<KadPeer>,
|
|
|
|
/// The user data passed to the `GetProvidersReq`.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// An error happened when performing a query.
|
|
|
|
QueryError {
|
|
|
|
/// The error that happened.
|
2018-12-18 11:23:13 +01:00
|
|
|
error: KademliaHandlerQueryErr,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// The user data passed to the query.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// The peer announced itself as a provider of a key.
|
2018-11-29 12:11:35 +01:00
|
|
|
AddProvider {
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// The key for which the peer is a provider of the associated value.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// The peer that is the provider of the value for `key`.
|
|
|
|
provider: KadPeer,
|
2018-11-29 12:11:35 +01:00
|
|
|
},
|
2019-06-04 14:44:24 +03:00
|
|
|
|
|
|
|
/// Request to get a value from the dht records
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
GetRecord {
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Key for which we should look in the dht
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Identifier of the request. Needs to be passed back when answering.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// Response to a `KademliaHandlerIn::GetRecord`.
|
|
|
|
GetRecordRes {
|
2019-06-04 14:44:24 +03:00
|
|
|
/// The result is present if the key has been found
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
record: Option<Record>,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Nodes closest to the key.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// The user data passed to the `GetValue`.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Request to put a value in the dht records
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
PutRecord {
|
|
|
|
record: Record,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Identifier of the request. Needs to be passed back when answering.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// Response to a request to store a record.
|
|
|
|
PutRecordRes {
|
|
|
|
/// The key of the stored record.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// The value of the stored record.
|
|
|
|
value: Vec<u8>,
|
|
|
|
/// The user data passed to the `PutValue`.
|
2019-06-04 14:44:24 +03:00
|
|
|
user_data: TUserData,
|
2021-08-11 13:12:12 +02:00
|
|
|
},
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 11:23:13 +01:00
|
|
|
/// Error that can happen when requesting an RPC query.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum KademliaHandlerQueryErr {
|
|
|
|
/// Error while trying to perform the query.
|
2022-02-21 13:32:24 +01:00
|
|
|
Upgrade(ConnectionHandlerUpgrErr<io::Error>),
|
2018-12-18 11:23:13 +01:00
|
|
|
/// Received an answer that doesn't correspond to the request.
|
|
|
|
UnexpectedMessage,
|
|
|
|
/// I/O error in the substream.
|
|
|
|
Io(io::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for KademliaHandlerQueryErr {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-18 11:23:13 +01:00
|
|
|
match self {
|
|
|
|
KademliaHandlerQueryErr::Upgrade(err) => {
|
2022-12-14 16:45:04 +01:00
|
|
|
write!(f, "Error while performing Kademlia query: {err}")
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2018-12-18 11:23:13 +01:00
|
|
|
KademliaHandlerQueryErr::UnexpectedMessage => {
|
2021-08-11 13:12:12 +02:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Remote answered our Kademlia RPC query with the wrong message type"
|
|
|
|
)
|
|
|
|
}
|
2018-12-18 11:23:13 +01:00
|
|
|
KademliaHandlerQueryErr::Io(err) => {
|
2022-12-14 16:45:04 +01:00
|
|
|
write!(f, "I/O error during a Kademlia RPC query: {err}")
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2018-12-18 11:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for KademliaHandlerQueryErr {
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
KademliaHandlerQueryErr::Upgrade(err) => Some(err),
|
|
|
|
KademliaHandlerQueryErr::UnexpectedMessage => None,
|
|
|
|
KademliaHandlerQueryErr::Io(err) => Some(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
impl From<ConnectionHandlerUpgrErr<io::Error>> for KademliaHandlerQueryErr {
|
|
|
|
fn from(err: ConnectionHandlerUpgrErr<io::Error>) -> Self {
|
2018-12-18 11:23:13 +01:00
|
|
|
KademliaHandlerQueryErr::Upgrade(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Event to send to the handler.
|
2020-12-17 11:01:45 +01:00
|
|
|
#[derive(Debug)]
|
2018-11-29 12:11:35 +01:00
|
|
|
pub enum KademliaHandlerIn<TUserData> {
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// Resets the (sub)stream associated with the given request ID,
|
|
|
|
/// thus signaling an error to the remote.
|
|
|
|
///
|
|
|
|
/// Explicitly resetting the (sub)stream associated with a request
|
|
|
|
/// can be used as an alternative to letting requests simply time
|
|
|
|
/// out on the remote peer, thus potentially avoiding some delay
|
|
|
|
/// for the query on the remote.
|
|
|
|
Reset(KademliaRequestId),
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Request for the list of nodes whose IDs are the closest to `key`. The number of nodes
|
|
|
|
/// returned is not specified, but should be around 20.
|
|
|
|
FindNodeReq {
|
|
|
|
/// Identifier of the node.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: Vec<u8>,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Custom user data. Passed back in the out event when the results arrive.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Response to a `FindNodeReq`.
|
|
|
|
FindNodeRes {
|
|
|
|
/// Results of the request.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// Identifier of the request that was made by the remote.
|
|
|
|
///
|
|
|
|
/// It is a logic error to use an id of the handler of a different node.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Same as `FindNodeReq`, but should also return the entries of the local providers list for
|
|
|
|
/// this key.
|
|
|
|
GetProvidersReq {
|
|
|
|
/// Identifier being searched.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Custom user data. Passed back in the out event when the results arrive.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Response to a `GetProvidersReq`.
|
|
|
|
GetProvidersRes {
|
|
|
|
/// Nodes closest to the key.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// Known providers for this key.
|
|
|
|
provider_peers: Vec<KadPeer>,
|
|
|
|
/// Identifier of the request that was made by the remote.
|
|
|
|
///
|
|
|
|
/// It is a logic error to use an id of the handler of a different node.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Indicates that this provider is known for this key.
|
|
|
|
///
|
|
|
|
/// The API of the handler doesn't expose any event that allows you to know whether this
|
|
|
|
/// succeeded.
|
|
|
|
AddProvider {
|
|
|
|
/// Key for which we should add providers.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Known provider for this key.
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
provider: KadPeer,
|
2018-11-29 12:11:35 +01:00
|
|
|
},
|
2019-06-04 14:44:24 +03:00
|
|
|
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// Request to retrieve a record from the DHT.
|
|
|
|
GetRecord {
|
|
|
|
/// The key of the record.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Custom data. Passed back in the out event when the results arrive.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// Response to a `GetRecord` request.
|
|
|
|
GetRecordRes {
|
2019-06-04 14:44:24 +03:00
|
|
|
/// The value that might have been found in our storage.
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
record: Option<Record>,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Nodes that are closer to the key we were searching for.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// Identifier of the request that was made by the remote.
|
|
|
|
request_id: KademliaRequestId,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Put a value into the dht records.
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
PutRecord {
|
|
|
|
record: Record,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Custom data. Passed back in the out event when the results arrive.
|
|
|
|
user_data: TUserData,
|
|
|
|
},
|
|
|
|
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
/// Response to a `PutRecord`.
|
|
|
|
PutRecordRes {
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Key of the value that was put.
|
2019-08-15 11:36:47 +02:00
|
|
|
key: record::Key,
|
2019-06-04 14:44:24 +03:00
|
|
|
/// Value that was put.
|
|
|
|
value: Vec<u8>,
|
|
|
|
/// Identifier of the request that was made by the remote.
|
|
|
|
request_id: KademliaRequestId,
|
2021-08-11 13:12:12 +02:00
|
|
|
},
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unique identifier for a request. Must be passed back in order to answer a request from
|
|
|
|
/// the remote.
|
2022-11-03 06:20:55 +11:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2018-11-29 12:11:35 +01:00
|
|
|
pub struct KademliaRequestId {
|
|
|
|
/// Unique identifier for an incoming connection.
|
|
|
|
connec_unique_id: UniqueConnecId,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unique identifier for a connection.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
struct UniqueConnecId(u64);
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
impl<TUserData> KademliaHandler<TUserData>
|
|
|
|
where
|
2022-11-17 17:19:36 +00:00
|
|
|
TUserData: Clone + fmt::Debug + Send + 'static + Unpin,
|
2022-11-03 06:20:55 +11:00
|
|
|
{
|
2020-03-19 17:01:34 +01:00
|
|
|
/// Create a [`KademliaHandler`] using the given configuration.
|
2022-06-09 15:12:03 +02:00
|
|
|
pub fn new(
|
|
|
|
config: KademliaHandlerConfig,
|
|
|
|
endpoint: ConnectedPoint,
|
|
|
|
remote_peer_id: PeerId,
|
|
|
|
) -> Self {
|
2020-03-19 17:01:34 +01:00
|
|
|
let keep_alive = KeepAlive::Until(Instant::now() + config.idle_timeout);
|
2018-11-29 12:11:35 +01:00
|
|
|
|
|
|
|
KademliaHandler {
|
2020-03-19 17:01:34 +01:00
|
|
|
config,
|
2020-11-05 20:58:14 +01:00
|
|
|
endpoint,
|
2022-06-09 15:12:03 +02:00
|
|
|
remote_peer_id,
|
2018-11-29 12:11:35 +01:00
|
|
|
next_connec_unique_id: UniqueConnecId(0),
|
2022-06-09 15:12:03 +02:00
|
|
|
inbound_substreams: Default::default(),
|
|
|
|
outbound_substreams: Default::default(),
|
2020-03-19 17:01:34 +01:00
|
|
|
keep_alive,
|
2020-11-05 20:58:14 +01:00
|
|
|
protocol_status: ProtocolStatus::Unconfirmed,
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:19:36 +00:00
|
|
|
fn on_fully_negotiated_outbound(
|
2018-11-29 12:11:35 +01:00
|
|
|
&mut self,
|
2022-11-17 17:19:36 +00:00
|
|
|
FullyNegotiatedOutbound {
|
|
|
|
protocol,
|
|
|
|
info: (msg, user_data),
|
|
|
|
}: FullyNegotiatedOutbound<
|
|
|
|
<Self as ConnectionHandler>::OutboundProtocol,
|
|
|
|
<Self as ConnectionHandler>::OutboundOpenInfo,
|
|
|
|
>,
|
2018-11-29 12:11:35 +01:00
|
|
|
) {
|
2022-06-09 15:12:03 +02:00
|
|
|
self.outbound_substreams
|
|
|
|
.push(OutboundSubstreamState::PendingSend(
|
|
|
|
protocol, msg, user_data,
|
|
|
|
));
|
2020-11-05 20:58:14 +01:00
|
|
|
if let ProtocolStatus::Unconfirmed = self.protocol_status {
|
|
|
|
// Upon the first successfully negotiated substream, we know that the
|
|
|
|
// remote is configured with the same protocol name and we want
|
|
|
|
// the behaviour to add this peer to the routing table, if possible.
|
|
|
|
self.protocol_status = ProtocolStatus::Confirmed;
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 17:19:36 +00:00
|
|
|
fn on_fully_negotiated_inbound(
|
2018-11-29 12:11:35 +01:00
|
|
|
&mut self,
|
2022-11-17 17:19:36 +00:00
|
|
|
FullyNegotiatedInbound { protocol, .. }: FullyNegotiatedInbound<
|
|
|
|
<Self as ConnectionHandler>::InboundProtocol,
|
|
|
|
<Self as ConnectionHandler>::InboundOpenInfo,
|
|
|
|
>,
|
2018-11-29 12:11:35 +01:00
|
|
|
) {
|
|
|
|
// If `self.allow_listening` is false, then we produced a `DeniedUpgrade` and `protocol`
|
|
|
|
// is a `Void`.
|
|
|
|
let protocol = match protocol {
|
|
|
|
EitherOutput::First(p) => p,
|
|
|
|
EitherOutput::Second(p) => void::unreachable(p),
|
|
|
|
};
|
|
|
|
|
2020-11-05 20:58:14 +01:00
|
|
|
if let ProtocolStatus::Unconfirmed = self.protocol_status {
|
|
|
|
// Upon the first successfully negotiated substream, we know that the
|
|
|
|
// remote is configured with the same protocol name and we want
|
|
|
|
// the behaviour to add this peer to the routing table, if possible.
|
|
|
|
self.protocol_status = ProtocolStatus::Confirmed;
|
|
|
|
}
|
2022-06-09 15:12:03 +02:00
|
|
|
|
|
|
|
if self.inbound_substreams.len() == MAX_NUM_INBOUND_SUBSTREAMS {
|
2022-11-03 06:20:55 +11:00
|
|
|
if let Some(s) = self.inbound_substreams.iter_mut().find(|s| {
|
2022-06-09 15:12:03 +02:00
|
|
|
matches!(
|
|
|
|
s,
|
|
|
|
// An inbound substream waiting to be reused.
|
|
|
|
InboundSubstreamState::WaitingMessage { first: false, .. }
|
|
|
|
)
|
|
|
|
}) {
|
2022-11-03 06:20:55 +11:00
|
|
|
*s = InboundSubstreamState::Cancelled;
|
2022-12-19 14:54:39 +02:00
|
|
|
log::debug!(
|
2022-06-09 15:12:03 +02:00
|
|
|
"New inbound substream to {:?} exceeds inbound substream limit. \
|
|
|
|
Removed older substream waiting to be reused.",
|
|
|
|
self.remote_peer_id,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
log::warn!(
|
|
|
|
"New inbound substream to {:?} exceeds inbound substream limit. \
|
|
|
|
No older substream waiting to be reused. Dropping new substream.",
|
|
|
|
self.remote_peer_id,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert!(self.config.allow_listening);
|
|
|
|
let connec_unique_id = self.next_connec_unique_id;
|
|
|
|
self.next_connec_unique_id.0 += 1;
|
|
|
|
self.inbound_substreams
|
|
|
|
.push(InboundSubstreamState::WaitingMessage {
|
|
|
|
first: true,
|
|
|
|
connection_id: connec_unique_id,
|
|
|
|
substream: protocol,
|
|
|
|
});
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 17:19:36 +00:00
|
|
|
fn on_dial_upgrade_error(
|
|
|
|
&mut self,
|
|
|
|
DialUpgradeError {
|
|
|
|
info: (_, user_data),
|
|
|
|
error,
|
|
|
|
..
|
|
|
|
}: DialUpgradeError<
|
|
|
|
<Self as ConnectionHandler>::OutboundOpenInfo,
|
|
|
|
<Self as ConnectionHandler>::OutboundProtocol,
|
|
|
|
>,
|
|
|
|
) {
|
|
|
|
// TODO: cache the fact that the remote doesn't support kademlia at all, so that we don't
|
|
|
|
// continue trying
|
|
|
|
if let Some(user_data) = user_data {
|
|
|
|
self.outbound_substreams
|
|
|
|
.push(OutboundSubstreamState::ReportError(error.into(), user_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TUserData> ConnectionHandler for KademliaHandler<TUserData>
|
|
|
|
where
|
|
|
|
TUserData: Clone + fmt::Debug + Send + 'static + Unpin,
|
|
|
|
{
|
|
|
|
type InEvent = KademliaHandlerIn<TUserData>;
|
|
|
|
type OutEvent = KademliaHandlerEvent<TUserData>;
|
|
|
|
type Error = io::Error; // TODO: better error type?
|
2023-01-18 13:35:07 +11:00
|
|
|
type InboundProtocol = Either<KademliaProtocolConfig, upgrade::DeniedUpgrade>;
|
2022-11-17 17:19:36 +00:00
|
|
|
type OutboundProtocol = KademliaProtocolConfig;
|
|
|
|
// Message of the request to send to the remote, and user data if we expect an answer.
|
|
|
|
type OutboundOpenInfo = (KadRequestMsg, Option<TUserData>);
|
|
|
|
type InboundOpenInfo = ();
|
|
|
|
|
|
|
|
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
|
|
|
|
if self.config.allow_listening {
|
|
|
|
SubstreamProtocol::new(self.config.protocol_config.clone(), ())
|
2023-01-18 13:35:07 +11:00
|
|
|
.map_upgrade(Either::Left)
|
2022-11-17 17:19:36 +00:00
|
|
|
} else {
|
2023-01-18 13:35:07 +11:00
|
|
|
SubstreamProtocol::new(Either::Right(upgrade::DeniedUpgrade), ())
|
2022-11-17 17:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_behaviour_event(&mut self, message: KademliaHandlerIn<TUserData>) {
|
2018-11-29 12:11:35 +01:00
|
|
|
match message {
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
KademliaHandlerIn::Reset(request_id) => {
|
2022-11-03 06:20:55 +11:00
|
|
|
if let Some(state) = self
|
2022-06-09 15:12:03 +02:00
|
|
|
.inbound_substreams
|
2022-11-03 06:20:55 +11:00
|
|
|
.iter_mut()
|
|
|
|
.find(|state| match state {
|
2022-11-24 04:46:43 +11:00
|
|
|
InboundSubstreamState::WaitingBehaviour(conn_id, _, _) => {
|
2022-06-09 15:12:03 +02:00
|
|
|
conn_id == &request_id.connec_unique_id
|
|
|
|
}
|
|
|
|
_ => false,
|
2022-11-03 06:20:55 +11:00
|
|
|
})
|
|
|
|
{
|
|
|
|
state.close();
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
KademliaHandlerIn::FindNodeReq { key, user_data } => {
|
2019-07-03 16:16:25 +02:00
|
|
|
let msg = KadRequestMsg::FindNode { key };
|
2022-06-09 15:12:03 +02:00
|
|
|
self.outbound_substreams
|
2022-11-03 06:20:55 +11:00
|
|
|
.push(OutboundSubstreamState::PendingOpen(SubstreamProtocol::new(
|
|
|
|
self.config.protocol_config.clone(),
|
|
|
|
(msg, Some(user_data)),
|
|
|
|
)));
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
KademliaHandlerIn::FindNodeRes {
|
|
|
|
closer_peers,
|
|
|
|
request_id,
|
2022-11-03 06:20:55 +11:00
|
|
|
} => self.answer_pending_request(request_id, KadResponseMsg::FindNode { closer_peers }),
|
2018-11-29 12:11:35 +01:00
|
|
|
KademliaHandlerIn::GetProvidersReq { key, user_data } => {
|
2019-08-15 11:36:47 +02:00
|
|
|
let msg = KadRequestMsg::GetProviders { key };
|
2022-06-09 15:12:03 +02:00
|
|
|
self.outbound_substreams
|
2022-11-03 06:20:55 +11:00
|
|
|
.push(OutboundSubstreamState::PendingOpen(SubstreamProtocol::new(
|
|
|
|
self.config.protocol_config.clone(),
|
|
|
|
(msg, Some(user_data)),
|
|
|
|
)));
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
KademliaHandlerIn::GetProvidersRes {
|
|
|
|
closer_peers,
|
|
|
|
provider_peers,
|
|
|
|
request_id,
|
2022-11-03 06:20:55 +11:00
|
|
|
} => self.answer_pending_request(
|
|
|
|
request_id,
|
|
|
|
KadResponseMsg::GetProviders {
|
|
|
|
closer_peers,
|
|
|
|
provider_peers,
|
|
|
|
},
|
|
|
|
),
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
KademliaHandlerIn::AddProvider { key, provider } => {
|
2019-08-15 11:36:47 +02:00
|
|
|
let msg = KadRequestMsg::AddProvider { key, provider };
|
2022-06-09 15:12:03 +02:00
|
|
|
self.outbound_substreams
|
2022-11-03 06:20:55 +11:00
|
|
|
.push(OutboundSubstreamState::PendingOpen(SubstreamProtocol::new(
|
|
|
|
self.config.protocol_config.clone(),
|
|
|
|
(msg, None),
|
|
|
|
)));
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
KademliaHandlerIn::GetRecord { key, user_data } => {
|
2019-06-04 14:44:24 +03:00
|
|
|
let msg = KadRequestMsg::GetValue { key };
|
2022-06-09 15:12:03 +02:00
|
|
|
self.outbound_substreams
|
2022-11-03 06:20:55 +11:00
|
|
|
.push(OutboundSubstreamState::PendingOpen(SubstreamProtocol::new(
|
|
|
|
self.config.protocol_config.clone(),
|
|
|
|
(msg, Some(user_data)),
|
|
|
|
)));
|
2019-06-04 14:44:24 +03:00
|
|
|
}
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
KademliaHandlerIn::PutRecord { record, user_data } => {
|
|
|
|
let msg = KadRequestMsg::PutValue { record };
|
2022-06-09 15:12:03 +02:00
|
|
|
self.outbound_substreams
|
2022-11-03 06:20:55 +11:00
|
|
|
.push(OutboundSubstreamState::PendingOpen(SubstreamProtocol::new(
|
|
|
|
self.config.protocol_config.clone(),
|
|
|
|
(msg, Some(user_data)),
|
|
|
|
)));
|
2019-06-04 14:44:24 +03:00
|
|
|
}
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
KademliaHandlerIn::GetRecordRes {
|
|
|
|
record,
|
2019-06-04 14:44:24 +03:00
|
|
|
closer_peers,
|
|
|
|
request_id,
|
|
|
|
} => {
|
2022-11-03 06:20:55 +11:00
|
|
|
self.answer_pending_request(
|
|
|
|
request_id,
|
|
|
|
KadResponseMsg::GetValue {
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
record,
|
2021-02-15 11:59:51 +01:00
|
|
|
closer_peers,
|
2022-11-03 06:20:55 +11:00
|
|
|
},
|
|
|
|
);
|
2019-06-04 14:44:24 +03:00
|
|
|
}
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
KademliaHandlerIn::PutRecordRes {
|
2019-06-04 14:44:24 +03:00
|
|
|
key,
|
|
|
|
request_id,
|
|
|
|
value,
|
|
|
|
} => {
|
2022-11-03 06:20:55 +11:00
|
|
|
self.answer_pending_request(request_id, KadResponseMsg::PutValue { key, value });
|
2019-06-04 14:44:24 +03:00
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 16:37:34 +01:00
|
|
|
fn connection_keep_alive(&self) -> KeepAlive {
|
|
|
|
self.keep_alive
|
2019-01-04 12:02:39 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
fn poll(
|
|
|
|
&mut self,
|
2020-07-27 20:27:33 +00:00
|
|
|
cx: &mut Context<'_>,
|
2018-11-29 12:11:35 +01:00
|
|
|
) -> Poll<
|
2022-02-21 13:32:24 +01:00
|
|
|
ConnectionHandlerEvent<
|
2021-08-11 13:12:12 +02:00
|
|
|
Self::OutboundProtocol,
|
|
|
|
Self::OutboundOpenInfo,
|
|
|
|
Self::OutEvent,
|
|
|
|
Self::Error,
|
|
|
|
>,
|
2018-11-29 12:11:35 +01:00
|
|
|
> {
|
2020-11-05 20:58:14 +01:00
|
|
|
if let ProtocolStatus::Confirmed = self.protocol_status {
|
|
|
|
self.protocol_status = ProtocolStatus::Reported;
|
2022-02-21 13:32:24 +01:00
|
|
|
return Poll::Ready(ConnectionHandlerEvent::Custom(
|
2020-11-05 20:58:14 +01:00
|
|
|
KademliaHandlerEvent::ProtocolConfirmed {
|
2021-08-11 13:12:12 +02:00
|
|
|
endpoint: self.endpoint.clone(),
|
|
|
|
},
|
|
|
|
));
|
2020-11-05 20:58:14 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
if let Poll::Ready(Some(event)) = self.outbound_substreams.poll_next_unpin(cx) {
|
|
|
|
return Poll::Ready(event);
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
if let Poll::Ready(Some(event)) = self.inbound_substreams.poll_next_unpin(cx) {
|
|
|
|
return Poll::Ready(event);
|
2022-06-09 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.outbound_substreams.is_empty() && self.inbound_substreams.is_empty() {
|
2020-01-20 13:55:25 +01:00
|
|
|
// We destroyed all substreams in this function.
|
2020-06-30 07:50:45 +02:00
|
|
|
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.idle_timeout);
|
2019-01-30 16:37:34 +01:00
|
|
|
} else {
|
2019-04-23 11:58:49 +02:00
|
|
|
self.keep_alive = KeepAlive::Yes;
|
2019-01-30 16:37:34 +01:00
|
|
|
}
|
|
|
|
|
2019-09-26 10:11:16 +02:00
|
|
|
Poll::Pending
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
2022-11-17 17:19:36 +00:00
|
|
|
|
|
|
|
fn on_connection_event(
|
|
|
|
&mut self,
|
|
|
|
event: ConnectionEvent<
|
|
|
|
Self::InboundProtocol,
|
|
|
|
Self::OutboundProtocol,
|
|
|
|
Self::InboundOpenInfo,
|
|
|
|
Self::OutboundOpenInfo,
|
|
|
|
>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => {
|
|
|
|
self.on_fully_negotiated_outbound(fully_negotiated_outbound)
|
|
|
|
}
|
|
|
|
ConnectionEvent::FullyNegotiatedInbound(fully_negotiated_inbound) => {
|
|
|
|
self.on_fully_negotiated_inbound(fully_negotiated_inbound)
|
|
|
|
}
|
|
|
|
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
|
|
|
|
self.on_dial_upgrade_error(dial_upgrade_error)
|
|
|
|
}
|
|
|
|
ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) => {}
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
impl<TUserData> KademliaHandler<TUserData>
|
|
|
|
where
|
|
|
|
TUserData: 'static + Clone + Send + Unpin + fmt::Debug,
|
|
|
|
{
|
|
|
|
fn answer_pending_request(&mut self, request_id: KademliaRequestId, mut msg: KadResponseMsg) {
|
|
|
|
for state in self.inbound_substreams.iter_mut() {
|
|
|
|
match state.try_answer_with(request_id, msg) {
|
|
|
|
Ok(()) => return,
|
|
|
|
Err(m) => {
|
|
|
|
msg = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert!(false, "Cannot find inbound substream for {request_id:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 17:01:34 +01:00
|
|
|
impl Default for KademliaHandlerConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
KademliaHandlerConfig {
|
|
|
|
protocol_config: Default::default(),
|
|
|
|
allow_listening: true,
|
|
|
|
idle_timeout: Duration::from_secs(10),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 06:20:55 +11:00
|
|
|
impl<TUserData> Stream for OutboundSubstreamState<TUserData>
|
|
|
|
where
|
|
|
|
TUserData: Unpin,
|
|
|
|
{
|
|
|
|
type Item = ConnectionHandlerEvent<
|
|
|
|
KademliaProtocolConfig,
|
|
|
|
(KadRequestMsg, Option<TUserData>),
|
|
|
|
KademliaHandlerEvent<TUserData>,
|
|
|
|
io::Error,
|
|
|
|
>;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.get_mut();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match std::mem::replace(this, OutboundSubstreamState::Poisoned) {
|
|
|
|
OutboundSubstreamState::PendingOpen(protocol) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::OutboundSubstreamRequest {
|
|
|
|
protocol,
|
|
|
|
}));
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
OutboundSubstreamState::PendingSend(mut substream, msg, user_data) => {
|
|
|
|
match substream.poll_ready_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(())) => match substream.start_send_unpin(msg) {
|
|
|
|
Ok(()) => {
|
|
|
|
*this = OutboundSubstreamState::PendingFlush(substream, user_data);
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
let event = user_data.map(|user_data| {
|
|
|
|
ConnectionHandlerEvent::Custom(
|
|
|
|
KademliaHandlerEvent::QueryError {
|
|
|
|
error: KademliaHandlerQueryErr::Io(error),
|
|
|
|
user_data,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
return Poll::Ready(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = OutboundSubstreamState::PendingSend(substream, msg, user_data);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(error)) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
let event = user_data.map(|user_data| {
|
|
|
|
ConnectionHandlerEvent::Custom(KademliaHandlerEvent::QueryError {
|
|
|
|
error: KademliaHandlerQueryErr::Io(error),
|
|
|
|
user_data,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
return Poll::Ready(event);
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
OutboundSubstreamState::PendingFlush(mut substream, user_data) => {
|
|
|
|
match substream.poll_flush_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(())) => {
|
|
|
|
if let Some(user_data) = user_data {
|
|
|
|
*this = OutboundSubstreamState::WaitingAnswer(substream, user_data);
|
|
|
|
} else {
|
|
|
|
*this = OutboundSubstreamState::Closing(substream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = OutboundSubstreamState::PendingFlush(substream, user_data);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(error)) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
let event = user_data.map(|user_data| {
|
|
|
|
ConnectionHandlerEvent::Custom(KademliaHandlerEvent::QueryError {
|
|
|
|
error: KademliaHandlerQueryErr::Io(error),
|
|
|
|
user_data,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
return Poll::Ready(event);
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
OutboundSubstreamState::WaitingAnswer(mut substream, user_data) => {
|
|
|
|
match substream.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(Some(Ok(msg))) => {
|
|
|
|
*this = OutboundSubstreamState::Closing(substream);
|
|
|
|
let event = process_kad_response(msg, user_data);
|
|
|
|
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event)));
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = OutboundSubstreamState::WaitingAnswer(substream, user_data);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Err(error))) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
let event = KademliaHandlerEvent::QueryError {
|
|
|
|
error: KademliaHandlerQueryErr::Io(error),
|
|
|
|
user_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event)));
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
let event = KademliaHandlerEvent::QueryError {
|
|
|
|
error: KademliaHandlerQueryErr::Io(
|
|
|
|
io::ErrorKind::UnexpectedEof.into(),
|
|
|
|
),
|
|
|
|
user_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event)));
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
OutboundSubstreamState::ReportError(error, user_data) => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
let event = KademliaHandlerEvent::QueryError { error, user_data };
|
|
|
|
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event)));
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
OutboundSubstreamState::Closing(mut stream) => match stream.poll_close_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(())) | Poll::Ready(Err(_)) => return Poll::Ready(None),
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = OutboundSubstreamState::Closing(stream);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
OutboundSubstreamState::Done => {
|
|
|
|
*this = OutboundSubstreamState::Done;
|
|
|
|
return Poll::Ready(None);
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
OutboundSubstreamState::Poisoned => unreachable!(),
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2022-06-09 15:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-03 06:20:55 +11:00
|
|
|
|
|
|
|
impl<TUserData> Stream for InboundSubstreamState<TUserData>
|
|
|
|
where
|
|
|
|
TUserData: Unpin,
|
|
|
|
{
|
|
|
|
type Item = ConnectionHandlerEvent<
|
|
|
|
KademliaProtocolConfig,
|
|
|
|
(KadRequestMsg, Option<TUserData>),
|
|
|
|
KademliaHandlerEvent<TUserData>,
|
|
|
|
io::Error,
|
|
|
|
>;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.get_mut();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match std::mem::replace(
|
|
|
|
this,
|
|
|
|
Self::Poisoned {
|
|
|
|
phantom: PhantomData,
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
InboundSubstreamState::WaitingMessage {
|
2022-06-09 15:12:03 +02:00
|
|
|
first,
|
|
|
|
connection_id,
|
2022-11-03 06:20:55 +11:00
|
|
|
mut substream,
|
|
|
|
} => match substream.poll_next_unpin(cx) {
|
2022-11-24 04:46:43 +11:00
|
|
|
Poll::Ready(Some(Ok(KadRequestMsg::Ping))) => {
|
|
|
|
log::warn!("Kademlia PING messages are unsupported");
|
|
|
|
|
|
|
|
*this = InboundSubstreamState::Closing(substream);
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(KadRequestMsg::FindNode { key }))) => {
|
|
|
|
*this =
|
|
|
|
InboundSubstreamState::WaitingBehaviour(connection_id, substream, None);
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(
|
|
|
|
KademliaHandlerEvent::FindNodeReq {
|
|
|
|
key,
|
|
|
|
request_id: KademliaRequestId {
|
|
|
|
connec_unique_id: connection_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(KadRequestMsg::GetProviders { key }))) => {
|
|
|
|
*this =
|
|
|
|
InboundSubstreamState::WaitingBehaviour(connection_id, substream, None);
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(
|
|
|
|
KademliaHandlerEvent::GetProvidersReq {
|
|
|
|
key,
|
|
|
|
request_id: KademliaRequestId {
|
|
|
|
connec_unique_id: connection_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(KadRequestMsg::AddProvider { key, provider }))) => {
|
|
|
|
*this = InboundSubstreamState::WaitingMessage {
|
|
|
|
first: false,
|
|
|
|
connection_id,
|
|
|
|
substream,
|
|
|
|
};
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(
|
|
|
|
KademliaHandlerEvent::AddProvider { key, provider },
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(KadRequestMsg::GetValue { key }))) => {
|
|
|
|
*this =
|
|
|
|
InboundSubstreamState::WaitingBehaviour(connection_id, substream, None);
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(
|
|
|
|
KademliaHandlerEvent::GetRecord {
|
|
|
|
key,
|
|
|
|
request_id: KademliaRequestId {
|
|
|
|
connec_unique_id: connection_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Ok(KadRequestMsg::PutValue { record }))) => {
|
|
|
|
*this =
|
|
|
|
InboundSubstreamState::WaitingBehaviour(connection_id, substream, None);
|
|
|
|
return Poll::Ready(Some(ConnectionHandlerEvent::Custom(
|
|
|
|
KademliaHandlerEvent::PutRecord {
|
|
|
|
record,
|
|
|
|
request_id: KademliaRequestId {
|
|
|
|
connec_unique_id: connection_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)));
|
2022-11-03 06:20:55 +11:00
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = InboundSubstreamState::WaitingMessage {
|
|
|
|
first,
|
|
|
|
connection_id,
|
|
|
|
substream,
|
|
|
|
};
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(None) => {
|
|
|
|
return Poll::Ready(None);
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(Err(e))) => {
|
|
|
|
trace!("Inbound substream error: {:?}", e);
|
|
|
|
return Poll::Ready(None);
|
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
},
|
2022-11-24 04:46:43 +11:00
|
|
|
InboundSubstreamState::WaitingBehaviour(id, substream, _) => {
|
|
|
|
*this = InboundSubstreamState::WaitingBehaviour(
|
|
|
|
id,
|
|
|
|
substream,
|
|
|
|
Some(cx.waker().clone()),
|
|
|
|
);
|
2022-11-03 06:20:55 +11:00
|
|
|
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
InboundSubstreamState::PendingSend(id, mut substream, msg) => {
|
|
|
|
match substream.poll_ready_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(())) => match substream.start_send_unpin(msg) {
|
|
|
|
Ok(()) => {
|
|
|
|
*this = InboundSubstreamState::PendingFlush(id, substream);
|
|
|
|
}
|
|
|
|
Err(_) => return Poll::Ready(None),
|
|
|
|
},
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = InboundSubstreamState::PendingSend(id, substream, msg);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(_)) => return Poll::Ready(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InboundSubstreamState::PendingFlush(id, mut substream) => {
|
|
|
|
match substream.poll_flush_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(())) => {
|
|
|
|
*this = InboundSubstreamState::WaitingMessage {
|
|
|
|
first: false,
|
|
|
|
connection_id: id,
|
|
|
|
substream,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = InboundSubstreamState::PendingFlush(id, substream);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Poll::Ready(Err(_)) => return Poll::Ready(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InboundSubstreamState::Closing(mut stream) => match stream.poll_close_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(())) | Poll::Ready(Err(_)) => return Poll::Ready(None),
|
|
|
|
Poll::Pending => {
|
|
|
|
*this = InboundSubstreamState::Closing(stream);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
InboundSubstreamState::Poisoned { .. } => unreachable!(),
|
|
|
|
InboundSubstreamState::Cancelled => return Poll::Ready(None),
|
2021-08-11 13:12:12 +02:00
|
|
|
}
|
2019-09-26 10:11:16 +02:00
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process a Kademlia message that's supposed to be a response to one of our requests.
|
|
|
|
fn process_kad_response<TUserData>(
|
|
|
|
event: KadResponseMsg,
|
|
|
|
user_data: TUserData,
|
|
|
|
) -> KademliaHandlerEvent<TUserData> {
|
|
|
|
// TODO: must check that the response corresponds to the request
|
|
|
|
match event {
|
|
|
|
KadResponseMsg::Pong => {
|
|
|
|
// We never send out pings.
|
|
|
|
KademliaHandlerEvent::QueryError {
|
2018-12-18 11:23:13 +01:00
|
|
|
error: KademliaHandlerQueryErr::UnexpectedMessage,
|
2018-11-29 12:11:35 +01:00
|
|
|
user_data,
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 13:12:12 +02:00
|
|
|
KadResponseMsg::FindNode { closer_peers } => KademliaHandlerEvent::FindNodeRes {
|
|
|
|
closer_peers,
|
|
|
|
user_data,
|
2018-11-29 12:11:35 +01:00
|
|
|
},
|
|
|
|
KadResponseMsg::GetProviders {
|
|
|
|
closer_peers,
|
|
|
|
provider_peers,
|
|
|
|
} => KademliaHandlerEvent::GetProvidersRes {
|
|
|
|
closer_peers,
|
|
|
|
provider_peers,
|
|
|
|
user_data,
|
|
|
|
},
|
2019-06-04 14:44:24 +03:00
|
|
|
KadResponseMsg::GetValue {
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
record,
|
2019-06-04 14:44:24 +03:00
|
|
|
closer_peers,
|
Kademlia: Somewhat complete the records implementation. (#1189)
* Somewhat complete the implementation of Kademlia records.
This commit relates to [libp2p-146] and [libp2p-1089].
* All records expire (by default, configurable).
* Provider records are also stored in the RecordStore, and the RecordStore
API extended.
* Background jobs for periodic (re-)replication and (re-)publication
of records. Regular (value-)records are subject to re-replication and
re-publication as per standard Kademlia. Provider records are only
subject to re-publication.
* For standard Kademlia value lookups (quorum = 1), the record is cached
at the closest peer to the key that did not return the value, as per
standard Kademlia.
* Expiration times of regular (value-)records is computed exponentially
inversely proportional to the number of nodes between the local node
and the closest node known to the key (beyond the k closest), as per
standard Kademlia.
The protobuf messages are extended with two fields: `ttl` and `publisher`
in order to implement the different semantics of re-replication (by any
of the k closest peers to the key, not affecting expiry) and re-publication
(by the original publisher, resetting the expiry). This is not done yet in
other libp2p Kademlia implementations, see e.g. [libp2p-go-323]. The new protobuf fields
have been given somewhat unique identifiers to prevent future collision.
Similarly, periodic re-publication of provider records does not seem to
be done yet in other implementations, see e.g. [libp2p-js-98].
[libp2p-146]: https://github.com/libp2p/rust-libp2p/issues/146
[libp2p-1089]: https://github.com/libp2p/rust-libp2p/issues/1089
[libp2p-go-323]: https://github.com/libp2p/go-libp2p-kad-dht/issues/323
[libp2p-js-98]: https://github.com/libp2p/js-libp2p-kad-dht/issues/98
* Tweak kad-ipfs example.
* Add missing files.
* Ensure new delays are polled immediately.
To ensure task notification, since `NotReady` is returned right after.
* Fix ipfs-kad example and use wasm_timer.
* Small cleanup.
* Incorporate some feedback.
* Adjustments after rebase.
* Distinguish events further.
In order for a user to easily distinguish the result of e.g.
a `put_record` operation from the result of a later republication,
different event constructors are used. Furthermore, for now,
re-replication and "caching" of records (at the closest peer to
the key that did not return a value during a successful lookup)
do not yield events for now as they are less interesting.
* Speed up tests for CI.
* Small refinements and more documentation.
* Guard a node against overriding records for which it considers
itself to be the publisher.
* Document the jobs module more extensively.
* More inline docs around removal of "unreachable" addresses.
* Remove wildcard re-exports.
* Use NonZeroUsize for the constants.
* Re-add method lost on merge.
* Add missing 'pub'.
* Further increase the timeout in the ipfs-kad example.
* Readd log dependency to libp2p-kad.
* Simplify RecordStore API slightly.
* Some more commentary.
* Change Addresses::remove to return Result<(),()>.
Change the semantics of `Addresses::remove` so that the error case
is unambiguous, instead of the success case. Use the `Result` for
clearer semantics to that effect.
* Add some documentation to .
2019-07-17 14:40:48 +02:00
|
|
|
} => KademliaHandlerEvent::GetRecordRes {
|
|
|
|
record,
|
2019-06-04 14:44:24 +03:00
|
|
|
closer_peers,
|
|
|
|
user_data,
|
|
|
|
},
|
2021-08-11 13:12:12 +02:00
|
|
|
KadResponseMsg::PutValue { key, value, .. } => KademliaHandlerEvent::PutRecordRes {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
user_data,
|
|
|
|
},
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|