1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
// Copyright 2023 litep2p developers
//
// 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.
//! [`/ipfs/kad/1.0.0`](https://github.com/libp2p/specs/blob/master/kad-dht/README.md) implementation.
use crate::{
error::{Error, ImmediateDialError, SubstreamError},
protocol::{
libp2p::kademlia::{
bucket::KBucketEntry,
executor::{QueryContext, QueryExecutor, QueryResult},
handle::KademliaCommand,
message::KademliaMessage,
query::{QueryAction, QueryEngine},
record::ProviderRecord,
routing_table::RoutingTable,
store::MemoryStore,
types::{ConnectionType, KademliaPeer, Key},
},
Direction, TransportEvent, TransportService,
},
substream::Substream,
types::SubstreamId,
PeerId,
};
use bytes::{Bytes, BytesMut};
use futures::StreamExt;
use multiaddr::Multiaddr;
use tokio::sync::mpsc::{Receiver, Sender};
use std::{
collections::{hash_map::Entry, HashMap},
time::{Duration, Instant},
};
pub use self::handle::RecordsType;
pub use config::{Config, ConfigBuilder};
pub use handle::{
IncomingRecordValidationMode, KademliaEvent, KademliaHandle, Quorum, RoutingTableUpdateMode,
};
pub use query::QueryId;
pub use record::{Key as RecordKey, PeerRecord, Record};
/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::ipfs::kademlia";
/// Parallelism factor, `α`.
const PARALLELISM_FACTOR: usize = 3;
mod bucket;
mod config;
mod executor;
mod handle;
mod message;
mod query;
mod record;
mod routing_table;
mod store;
mod types;
mod schema {
pub(super) mod kademlia {
include!(concat!(env!("OUT_DIR"), "/kademlia.rs"));
}
}
/// Peer action.
#[derive(Debug)]
enum PeerAction {
/// Send `FIND_NODE` message to peer.
SendFindNode(QueryId),
/// Send `PUT_VALUE` message to peer.
SendPutValue(Bytes),
}
/// Peer context.
#[derive(Default)]
struct PeerContext {
/// Pending action, if any.
pending_actions: HashMap<SubstreamId, PeerAction>,
}
impl PeerContext {
/// Create new [`PeerContext`].
pub fn new() -> Self {
Self {
pending_actions: HashMap::new(),
}
}
/// Add pending action for peer.
pub fn add_pending_action(&mut self, substream_id: SubstreamId, action: PeerAction) {
self.pending_actions.insert(substream_id, action);
}
}
/// Main Kademlia object.
pub(crate) struct Kademlia {
/// Transport service.
service: TransportService,
/// Local Kademlia key.
local_key: Key<PeerId>,
/// Connected peers,
peers: HashMap<PeerId, PeerContext>,
/// TX channel for sending events to `KademliaHandle`.
event_tx: Sender<KademliaEvent>,
/// RX channel for receiving commands from `KademliaHandle`.
cmd_rx: Receiver<KademliaCommand>,
/// Routing table.
routing_table: RoutingTable,
/// Replication factor.
replication_factor: usize,
/// Record store.
store: MemoryStore,
/// Pending outbound substreams.
pending_substreams: HashMap<SubstreamId, PeerId>,
/// Pending dials.
pending_dials: HashMap<PeerId, Vec<PeerAction>>,
/// Routing table update mode.
update_mode: RoutingTableUpdateMode,
/// Incoming records validation mode.
validation_mode: IncomingRecordValidationMode,
/// Default record TTL.
record_ttl: Duration,
/// Provider record TTL.
provider_ttl: Duration,
/// Query engine.
engine: QueryEngine,
/// Query executor.
executor: QueryExecutor,
}
impl Kademlia {
/// Create new [`Kademlia`].
pub(crate) fn new(mut service: TransportService, config: Config) -> Self {
let local_peer_id = service.local_peer_id();
let local_key = Key::from(service.local_peer_id());
let mut routing_table = RoutingTable::new(local_key.clone());
for (peer, addresses) in config.known_peers {
tracing::trace!(target: LOG_TARGET, ?peer, ?addresses, "add bootstrap peer");
routing_table.add_known_peer(peer, addresses.clone(), ConnectionType::NotConnected);
service.add_known_address(&peer, addresses.into_iter());
}
Self {
service,
routing_table,
peers: HashMap::new(),
cmd_rx: config.cmd_rx,
store: MemoryStore::new(),
event_tx: config.event_tx,
local_key,
pending_dials: HashMap::new(),
executor: QueryExecutor::new(),
pending_substreams: HashMap::new(),
update_mode: config.update_mode,
validation_mode: config.validation_mode,
record_ttl: config.record_ttl,
provider_ttl: config.provider_ttl,
replication_factor: config.replication_factor,
engine: QueryEngine::new(local_peer_id, config.replication_factor, PARALLELISM_FACTOR),
}
}
/// Connection established to remote peer.
fn on_connection_established(&mut self, peer: PeerId) -> crate::Result<()> {
tracing::trace!(target: LOG_TARGET, ?peer, "connection established");
match self.peers.entry(peer) {
Entry::Vacant(entry) => {
if let KBucketEntry::Occupied(entry) = self.routing_table.entry(Key::from(peer)) {
entry.connection = ConnectionType::Connected;
}
let Some(actions) = self.pending_dials.remove(&peer) else {
entry.insert(PeerContext::new());
return Ok(());
};
// go over all pending actions, open substreams and save the state to `PeerContext`
// from which it will be later queried when the substream opens
let mut context = PeerContext::new();
for action in actions {
match self.service.open_substream(peer) {
Ok(substream_id) => {
context.add_pending_action(substream_id, action);
}
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
?peer,
?action,
?error,
"connection established to peer but failed to open substream",
);
if let PeerAction::SendFindNode(query_id) = action {
self.engine.register_response_failure(query_id, peer);
}
}
}
}
entry.insert(context);
Ok(())
}
Entry::Occupied(_) => Err(Error::PeerAlreadyExists(peer)),
}
}
/// Disconnect peer from `Kademlia`.
///
/// Peer is disconnected either because the substream was detected closed
/// or because the connection was closed.
///
/// The peer is kept in the routing table but its connection state is set
/// as `NotConnected`, meaning it can be evicted from a k-bucket if another
/// peer that shares the bucket connects.
async fn disconnect_peer(&mut self, peer: PeerId, query: Option<QueryId>) {
tracing::trace!(target: LOG_TARGET, ?peer, ?query, "disconnect peer");
if let Some(query) = query {
self.engine.register_response_failure(query, peer);
}
if let Some(PeerContext { pending_actions }) = self.peers.remove(&peer) {
pending_actions.into_iter().for_each(|(_, action)| {
if let PeerAction::SendFindNode(query_id) = action {
self.engine.register_response_failure(query_id, peer);
}
});
}
if let KBucketEntry::Occupied(entry) = self.routing_table.entry(Key::from(peer)) {
entry.connection = ConnectionType::NotConnected;
}
}
/// Local node opened a substream to remote node.
async fn on_outbound_substream(
&mut self,
peer: PeerId,
substream_id: SubstreamId,
substream: Substream,
) -> crate::Result<()> {
tracing::trace!(
target: LOG_TARGET,
?peer,
?substream_id,
"outbound substream opened",
);
let _ = self.pending_substreams.remove(&substream_id);
let pending_action = &mut self
.peers
.get_mut(&peer)
.ok_or(Error::PeerDoesntExist(peer))?
.pending_actions
.remove(&substream_id);
match pending_action.take() {
None => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?substream_id,
"pending action doesn't exist for peer, closing substream",
);
let _ = substream.close().await;
return Ok(());
}
Some(PeerAction::SendFindNode(query)) => {
match self.engine.next_peer_action(&query, &peer) {
Some(QueryAction::SendMessage {
query,
peer,
message,
}) => {
tracing::trace!(target: LOG_TARGET, ?peer, ?query, "start sending message to peer");
self.executor.send_request_read_response(
peer,
Some(query),
message,
substream,
);
}
// query finished while the substream was being opened
None => {
let _ = substream.close().await;
}
action => {
tracing::warn!(target: LOG_TARGET, ?query, ?peer, ?action, "unexpected action for `FIND_NODE`");
let _ = substream.close().await;
debug_assert!(false);
}
}
}
Some(PeerAction::SendPutValue(message)) => {
tracing::trace!(target: LOG_TARGET, ?peer, "send `PUT_VALUE` response");
self.executor.send_message(peer, message, substream);
}
}
Ok(())
}
/// Remote opened a substream to local node.
async fn on_inbound_substream(&mut self, peer: PeerId, substream: Substream) {
tracing::trace!(target: LOG_TARGET, ?peer, "inbound substream opened");
self.executor.read_message(peer, None, substream);
}
/// Update routing table if the routing table update mode was set to automatic.
///
/// Inform user about the potential routing table, allowing them to update it manually if
/// the mode was set to manual.
async fn update_routing_table(&mut self, peers: &[KademliaPeer]) {
let peers: Vec<_> =
peers.iter().filter(|peer| peer.peer != self.service.local_peer_id()).collect();
// inform user about the routing table update, regardless of what the routing table update
// mode is
let _ = self
.event_tx
.send(KademliaEvent::RoutingTableUpdate {
peers: peers.iter().map(|peer| peer.peer).collect::<Vec<PeerId>>(),
})
.await;
for info in peers {
self.service.add_known_address(&info.peer, info.addresses.iter().cloned());
if std::matches!(self.update_mode, RoutingTableUpdateMode::Automatic) {
self.routing_table.add_known_peer(
info.peer,
info.addresses.clone(),
self.peers
.get(&info.peer)
.map_or(ConnectionType::NotConnected, |_| ConnectionType::Connected),
);
}
}
}
/// Handle received message.
async fn on_message_received(
&mut self,
peer: PeerId,
query_id: Option<QueryId>,
message: BytesMut,
substream: Substream,
) -> crate::Result<()> {
tracing::trace!(target: LOG_TARGET, ?peer, query = ?query_id, "handle message from peer");
match KademliaMessage::from_bytes(message).ok_or(Error::InvalidData)? {
ref message @ KademliaMessage::FindNode {
ref target,
ref peers,
} => {
match query_id {
Some(query_id) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?target,
query = ?query_id,
"handle `FIND_NODE` response",
);
// update routing table and inform user about the update
self.update_routing_table(peers).await;
self.engine.register_response(query_id, peer, message.clone());
}
None => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?target,
"handle `FIND_NODE` request",
);
let message = KademliaMessage::find_node_response(
target,
self.routing_table
.closest(Key::from(target.clone()), self.replication_factor),
);
self.executor.send_message(peer, message.into(), substream);
}
}
}
KademliaMessage::PutValue { record } => {
tracing::trace!(
target: LOG_TARGET,
?peer,
record_key = ?record.key,
"handle `PUT_VALUE` message",
);
if let IncomingRecordValidationMode::Automatic = self.validation_mode {
self.store.put(record.clone());
}
let _ = self.event_tx.send(KademliaEvent::IncomingRecord { record }).await;
}
ref message @ KademliaMessage::GetRecord {
ref key,
ref record,
ref peers,
} => {
match (query_id, key) {
(Some(query_id), _) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
query = ?query_id,
?peers,
?record,
"handle `GET_VALUE` response",
);
// update routing table and inform user about the update
self.update_routing_table(peers).await;
self.engine.register_response(query_id, peer, message.clone());
}
(None, Some(key)) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?key,
"handle `GET_VALUE` request",
);
let value = self.store.get(key).cloned();
let closest_peers = self
.routing_table
.closest(Key::from(key.to_vec()), self.replication_factor);
let message = KademliaMessage::get_value_response(
(*key).clone(),
closest_peers,
value,
);
self.executor.send_message(peer, message.into(), substream);
}
(None, None) => tracing::debug!(
target: LOG_TARGET,
?peer,
?message,
"unable to handle `GET_RECORD` request with empty key",
),
}
}
KademliaMessage::AddProvider { key, providers } => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?key,
?providers,
"handle `ADD_PROVIDER` message",
);
match (providers.len(), providers.first()) {
(1, Some(provider)) =>
if provider.peer == peer {
self.store.put_provider(ProviderRecord {
key,
provider: peer,
addresses: provider.addresses.clone(),
expires: Instant::now() + self.provider_ttl,
});
} else {
tracing::trace!(
target: LOG_TARGET,
publisher = ?peer,
provider = ?provider.peer,
"ignoring `ADD_PROVIDER` message with `publisher` != `provider`"
)
},
(n, _) => {
tracing::trace!(
target: LOG_TARGET,
publisher = ?peer,
?n,
"ignoring `ADD_PROVIDER` message with `n` != 1 providers"
)
}
}
}
ref message @ KademliaMessage::GetProviders {
ref key,
ref peers,
ref providers,
} => {
match (query_id, key) {
(Some(query_id), key) => {
// Note: key is not required, but can be non-empty. We just ignore it here.
tracing::trace!(
target: LOG_TARGET,
?peer,
query = ?query_id,
?key,
?peers,
?providers,
"handle `GET_PROVIDERS` response",
);
// update routing table and inform user about the update
self.update_routing_table(peers).await;
self.engine.register_response(query_id, peer, message.clone());
}
(None, Some(key)) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?key,
"handle `GET_PROVIDERS` request",
);
let providers = self.store.get_providers(key);
// TODO: if local peer is among the providers, update its `ProviderRecord`
// to have up-to-date addresses.
// Requires https://github.com/paritytech/litep2p/issues/211.
let closer_peers = self
.routing_table
.closest(Key::from(key.to_vec()), self.replication_factor);
let message = KademliaMessage::get_providers_response(
key.clone(),
providers,
&closer_peers,
);
self.executor.send_message(peer, message.into(), substream);
}
(None, None) => tracing::debug!(
target: LOG_TARGET,
?peer,
?message,
"unable to handle `GET_PROVIDERS` request with empty key",
),
}
}
}
Ok(())
}
/// Failed to open substream to remote peer.
async fn on_substream_open_failure(
&mut self,
substream_id: SubstreamId,
error: SubstreamError,
) {
tracing::trace!(
target: LOG_TARGET,
?substream_id,
?error,
"failed to open substream"
);
let Some(peer) = self.pending_substreams.remove(&substream_id) else {
tracing::debug!(
target: LOG_TARGET,
?substream_id,
"outbound substream failed for non-existent peer"
);
return;
};
if let Some(context) = self.peers.get_mut(&peer) {
let query = match context.pending_actions.remove(&substream_id) {
Some(PeerAction::SendFindNode(query)) => Some(query),
_ => None,
};
self.disconnect_peer(peer, query).await;
}
}
/// Handle dial failure.
fn on_dial_failure(&mut self, peer: PeerId, address: Multiaddr) {
tracing::trace!(target: LOG_TARGET, ?peer, ?address, "failed to dial peer");
let Some(actions) = self.pending_dials.remove(&peer) else {
return;
};
for action in actions {
if let PeerAction::SendFindNode(query_id) = action {
tracing::trace!(
target: LOG_TARGET,
?peer,
query = ?query_id,
?address,
"report failure for pending query",
);
self.engine.register_response_failure(query_id, peer);
}
}
}
/// Open a substream with a peer or dial the peer.
fn open_substream_or_dial(
&mut self,
peer: PeerId,
action: PeerAction,
query: Option<QueryId>,
) -> Result<(), Error> {
match self.service.open_substream(peer) {
Ok(substream_id) => {
self.pending_substreams.insert(substream_id, peer);
self.peers.entry(peer).or_default().pending_actions.insert(substream_id, action);
Ok(())
}
Err(err) => {
tracing::trace!(target: LOG_TARGET, ?query, ?peer, ?err, "Failed to open substream. Dialing peer");
match self.service.dial(&peer) {
Ok(()) => {
self.pending_dials.entry(peer).or_default().push(action);
Ok(())
}
// Already connected is a recoverable error.
Err(ImmediateDialError::AlreadyConnected) => {
// Dial returned `Error::AlreadyConnected`, retry opening the substream.
match self.service.open_substream(peer) {
Ok(substream_id) => {
self.pending_substreams.insert(substream_id, peer);
self.peers
.entry(peer)
.or_default()
.pending_actions
.insert(substream_id, action);
Ok(())
}
Err(err) => {
tracing::trace!(target: LOG_TARGET, ?query, ?peer, ?err, "Failed to open substream a second time");
Err(err.into())
}
}
}
Err(error) => {
tracing::trace!(target: LOG_TARGET, ?query, ?peer, ?error, "Failed to dial peer");
Err(error.into())
}
}
}
}
}
/// Handle next query action.
async fn on_query_action(&mut self, action: QueryAction) -> Result<(), (QueryId, PeerId)> {
match action {
QueryAction::SendMessage { query, peer, .. } => {
if self
.open_substream_or_dial(peer, PeerAction::SendFindNode(query), Some(query))
.is_err()
{
// Announce the error to the query engine.
self.engine.register_response_failure(query, peer);
}
Ok(())
}
QueryAction::FindNodeQuerySucceeded {
target,
peers,
query,
} => {
tracing::debug!(
target: LOG_TARGET,
?query,
peer = ?target,
num_peers = ?peers.len(),
"`FIND_NODE` succeeded",
);
let _ = self
.event_tx
.send(KademliaEvent::FindNodeSuccess {
target,
query_id: query,
peers: peers.into_iter().map(|info| (info.peer, info.addresses)).collect(),
})
.await;
Ok(())
}
QueryAction::PutRecordToFoundNodes { record, peers } => {
tracing::trace!(
target: LOG_TARGET,
record_key = ?record.key,
num_peers = ?peers.len(),
"store record to found peers",
);
let key = record.key.clone();
let message = KademliaMessage::put_value(record);
for peer in peers {
if let Err(error) = self.open_substream_or_dial(
peer.peer,
PeerAction::SendPutValue(message.clone()),
None,
) {
tracing::debug!(
target: LOG_TARGET,
?peer,
?key,
?error,
"failed to put record to peer",
);
}
}
Ok(())
}
QueryAction::GetRecordQueryDone { query_id, records } => {
let _ = self
.event_tx
.send(KademliaEvent::GetRecordSuccess {
query_id,
records: RecordsType::Network(records),
})
.await;
Ok(())
}
QueryAction::QueryFailed { query } => {
tracing::debug!(target: LOG_TARGET, ?query, "query failed");
let _ = self.event_tx.send(KademliaEvent::QueryFailed { query_id: query }).await;
Ok(())
}
QueryAction::QuerySucceeded { .. } => unreachable!(),
}
}
/// [`Kademlia`] event loop.
pub async fn run(mut self) -> crate::Result<()> {
tracing::debug!(target: LOG_TARGET, "starting kademlia event loop");
loop {
// poll `QueryEngine` for next actions.
while let Some(action) = self.engine.next_action() {
if let Err((query, peer)) = self.on_query_action(action).await {
self.disconnect_peer(peer, Some(query)).await;
}
}
tokio::select! {
event = self.service.next() => match event {
Some(TransportEvent::ConnectionEstablished { peer, .. }) => {
if let Err(error) = self.on_connection_established(peer) {
tracing::debug!(target: LOG_TARGET, ?error, "failed to handle established connection");
}
}
Some(TransportEvent::ConnectionClosed { peer }) => {
self.disconnect_peer(peer, None).await;
}
Some(TransportEvent::SubstreamOpened { peer, direction, substream, .. }) => {
match direction {
Direction::Inbound => self.on_inbound_substream(peer, substream).await,
Direction::Outbound(substream_id) => {
if let Err(error) = self.on_outbound_substream(peer, substream_id, substream).await {
tracing::debug!(
target: LOG_TARGET,
?peer,
?substream_id,
?error,
"failed to handle outbound substream",
);
}
}
}
},
Some(TransportEvent::SubstreamOpenFailure { substream, error }) => {
self.on_substream_open_failure(substream, error).await;
}
Some(TransportEvent::DialFailure { peer, address, .. }) => self.on_dial_failure(peer, address),
None => return Err(Error::EssentialTaskClosed),
},
context = self.executor.next() => {
let QueryContext { peer, query_id, result } = context.unwrap();
match result {
QueryResult::SendSuccess { substream } => {
tracing::trace!(target: LOG_TARGET, ?peer, query = ?query_id, "message sent to peer");
let _ = substream.close().await;
}
QueryResult::ReadSuccess { substream, message } => {
tracing::trace!(target: LOG_TARGET, ?peer, query = ?query_id, "message read from peer");
if let Err(error) = self.on_message_received(peer, query_id, message, substream).await {
tracing::debug!(target: LOG_TARGET, ?peer, ?error, "failed to process message");
}
}
QueryResult::SubstreamClosed | QueryResult::Timeout => {
tracing::debug!(
target: LOG_TARGET,
?peer,
query = ?query_id,
?result,
"failed to read message from substream",
);
self.disconnect_peer(peer, query_id).await;
}
}
}
command = self.cmd_rx.recv() => {
match command {
Some(KademliaCommand::FindNode { peer, query_id }) => {
tracing::debug!(target: LOG_TARGET, ?peer, query = ?query_id, "starting `FIND_NODE` query");
self.engine.start_find_node(
query_id,
peer,
self.routing_table.closest(Key::from(peer), self.replication_factor).into()
);
}
Some(KademliaCommand::PutRecord { mut record, query_id }) => {
tracing::debug!(target: LOG_TARGET, query = ?query_id, key = ?record.key, "store record to DHT");
// For `PUT_VALUE` requests originating locally we are always the publisher.
record.publisher = Some(self.local_key.clone().into_preimage());
// Make sure TTL is set.
record.expires = record.expires.or_else(|| Some(Instant::now() + self.record_ttl));
let key = Key::new(record.key.clone());
self.store.put(record.clone());
self.engine.start_put_record(
query_id,
record,
self.routing_table.closest(key, self.replication_factor).into(),
);
}
Some(KademliaCommand::PutRecordToPeers { mut record, query_id, peers, update_local_store }) => {
tracing::debug!(target: LOG_TARGET, query = ?query_id, key = ?record.key, "store record to DHT to specified peers");
// Make sure TTL is set.
record.expires = record.expires.or_else(|| Some(Instant::now() + self.record_ttl));
if update_local_store {
self.store.put(record.clone());
}
// Put the record to the specified peers.
let peers = peers.into_iter().filter_map(|peer| {
if peer == self.service.local_peer_id() {
return None;
}
match self.routing_table.entry(Key::from(peer)) {
KBucketEntry::Occupied(entry) => Some(entry.clone()),
KBucketEntry::Vacant(entry) if !entry.addresses.is_empty() => Some(entry.clone()),
_ => None,
}
}).collect();
self.engine.start_put_record_to_peers(
query_id,
record,
peers,
);
}
Some(KademliaCommand::GetRecord { key, quorum, query_id }) => {
tracing::debug!(target: LOG_TARGET, ?key, "get record from DHT");
match (self.store.get(&key), quorum) {
(Some(record), Quorum::One) => {
let _ = self
.event_tx
.send(KademliaEvent::GetRecordSuccess { query_id, records: RecordsType::LocalStore(record.clone()) })
.await;
}
(record, _) => {
self.engine.start_get_record(
query_id,
key.clone(),
self.routing_table.closest(Key::new(key.clone()), self.replication_factor).into(),
quorum,
if record.is_some() { 1 } else { 0 },
);
}
}
}
Some(KademliaCommand::AddKnownPeer { peer, addresses }) => {
tracing::trace!(
target: LOG_TARGET,
?peer,
?addresses,
"add known peer",
);
self.routing_table.add_known_peer(
peer,
addresses.clone(),
self.peers
.get(&peer)
.map_or(ConnectionType::NotConnected, |_| ConnectionType::Connected),
);
self.service.add_known_address(&peer, addresses.into_iter());
}
Some(KademliaCommand::StoreRecord { mut record }) => {
tracing::debug!(
target: LOG_TARGET,
key = ?record.key,
"store record in local store",
);
// Make sure TTL is set.
record.expires = record.expires.or_else(|| Some(Instant::now() + self.record_ttl));
self.store.put(record);
}
None => return Err(Error::EssentialTaskClosed),
}
},
}
}
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
use crate::{
codec::ProtocolCodec,
crypto::ed25519::Keypair,
transport::{
manager::{limits::ConnectionLimitsConfig, TransportManager},
KEEP_ALIVE_TIMEOUT,
},
types::protocol::ProtocolName,
BandwidthSink,
};
use tokio::sync::mpsc::channel;
#[allow(unused)]
struct Context {
_cmd_tx: Sender<KademliaCommand>,
event_rx: Receiver<KademliaEvent>,
}
fn make_kademlia() -> (Kademlia, Context, TransportManager) {
let (manager, handle) = TransportManager::new(
Keypair::generate(),
HashSet::new(),
BandwidthSink::new(),
8usize,
ConnectionLimitsConfig::default(),
);
let peer = PeerId::random();
let (transport_service, _tx) = TransportService::new(
peer,
ProtocolName::from("/kad/1"),
Vec::new(),
Default::default(),
handle,
KEEP_ALIVE_TIMEOUT,
);
let (event_tx, event_rx) = channel(64);
let (_cmd_tx, cmd_rx) = channel(64);
let config = Config {
protocol_names: vec![ProtocolName::from("/kad/1")],
known_peers: HashMap::new(),
codec: ProtocolCodec::UnsignedVarint(None),
replication_factor: 20usize,
update_mode: RoutingTableUpdateMode::Automatic,
validation_mode: IncomingRecordValidationMode::Automatic,
record_ttl: Duration::from_secs(36 * 60 * 60),
provider_ttl: Duration::from_secs(48 * 60 * 60),
event_tx,
cmd_rx,
};
(
Kademlia::new(transport_service, config),
Context { _cmd_tx, event_rx },
manager,
)
}
#[tokio::test]
async fn check_get_records_update() {
let (mut kademlia, _context, _manager) = make_kademlia();
let key = RecordKey::from(vec![1, 2, 3]);
let records = vec![
// 2 peers backing the same record.
PeerRecord {
peer: PeerId::random(),
record: Record::new(key.clone(), vec![0x1]),
},
PeerRecord {
peer: PeerId::random(),
record: Record::new(key.clone(), vec![0x1]),
},
// only 1 peer backing the record.
PeerRecord {
peer: PeerId::random(),
record: Record::new(key.clone(), vec![0x2]),
},
];
let query_id = QueryId(1);
let action = QueryAction::GetRecordQueryDone { query_id, records };
assert!(kademlia.on_query_action(action).await.is_ok());
// Check the local storage should not get updated.
assert!(kademlia.store.get(&key).is_none());
}
#[tokio::test]
async fn check_get_records_update_with_expired_records() {
let (mut kademlia, _context, _manager) = make_kademlia();
let key = RecordKey::from(vec![1, 2, 3]);
let expired = std::time::Instant::now() - std::time::Duration::from_secs(10);
let records = vec![
// 2 peers backing the same record, one record is expired.
PeerRecord {
peer: PeerId::random(),
record: Record {
key: key.clone(),
value: vec![0x1],
publisher: None,
expires: Some(expired),
},
},
PeerRecord {
peer: PeerId::random(),
record: Record::new(key.clone(), vec![0x1]),
},
// 2 peer backing the record.
PeerRecord {
peer: PeerId::random(),
record: Record::new(key.clone(), vec![0x2]),
},
PeerRecord {
peer: PeerId::random(),
record: Record::new(key.clone(), vec![0x2]),
},
];
let query_id = QueryId(1);
let action = QueryAction::GetRecordQueryDone { query_id, records };
assert!(kademlia.on_query_action(action).await.is_ok());
// Check the local storage should not get updated.
assert!(kademlia.store.get(&key).is_none());
}
}