File size: 22,349 Bytes
f0f4f2b |
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 |
// Copyright 2021 COMIT Network.
//
// 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.
use crate::DEFAULT_TTL;
use async_trait::async_trait;
use asynchronous_codec::{BytesMut, Decoder, Encoder};
use asynchronous_codec::{FramedRead, FramedWrite};
use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt};
use libp2p_core::{peer_record, signed_envelope, PeerRecord, SignedEnvelope};
use libp2p_swarm::StreamProtocol;
use quick_protobuf_codec::Codec as ProtobufCodec;
use rand::RngCore;
use std::{fmt, io};
pub type Ttl = u64;
pub(crate) type Limit = u64;
const MAX_MESSAGE_LEN_BYTES: usize = 1024 * 1024;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum Message {
Register(NewRegistration),
RegisterResponse(Result<Ttl, ErrorCode>),
Unregister(Namespace),
Discover {
namespace: Option<Namespace>,
cookie: Option<Cookie>,
limit: Option<Limit>,
},
DiscoverResponse(Result<(Vec<Registration>, Cookie), ErrorCode>),
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Namespace(String);
impl Namespace {
/// Creates a new [`Namespace`] from a static string.
///
/// This will panic if the namespace is too long. We accepting panicking in this case because we are enforcing a `static lifetime which means this value can only be a constant in the program and hence we hope the developer checked that it is of an acceptable length.
pub fn from_static(value: &'static str) -> Self {
if value.len() > crate::MAX_NAMESPACE {
panic!("Namespace '{value}' is too long!")
}
Namespace(value.to_owned())
}
pub fn new(value: String) -> Result<Self, NamespaceTooLong> {
if value.len() > crate::MAX_NAMESPACE {
return Err(NamespaceTooLong);
}
Ok(Namespace(value))
}
}
impl From<Namespace> for String {
fn from(namespace: Namespace) -> Self {
namespace.0
}
}
impl fmt::Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl PartialEq<str> for Namespace {
fn eq(&self, other: &str) -> bool {
self.0.eq(other)
}
}
impl PartialEq<Namespace> for str {
fn eq(&self, other: &Namespace) -> bool {
other.0.eq(self)
}
}
#[derive(Debug, thiserror::Error)]
#[error("Namespace is too long")]
pub struct NamespaceTooLong;
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct Cookie {
id: u64,
namespace: Option<Namespace>,
}
impl Cookie {
/// Construct a new [`Cookie`] for a given namespace.
///
/// This cookie will only be valid for subsequent DISCOVER requests targeting the same namespace.
pub fn for_namespace(namespace: Namespace) -> Self {
Self {
id: rand::thread_rng().next_u64(),
namespace: Some(namespace),
}
}
/// Construct a new [`Cookie`] for a DISCOVER request that inquires about all namespaces.
pub fn for_all_namespaces() -> Self {
Self {
id: rand::random(),
namespace: None,
}
}
pub fn into_wire_encoding(self) -> Vec<u8> {
let id_bytes = self.id.to_be_bytes();
let namespace = self.namespace.map(|ns| ns.0).unwrap_or_default();
let mut buffer = Vec::with_capacity(id_bytes.len() + namespace.len());
buffer.extend_from_slice(&id_bytes);
buffer.extend_from_slice(namespace.as_bytes());
buffer
}
pub fn from_wire_encoding(mut bytes: Vec<u8>) -> Result<Self, InvalidCookie> {
// check length early to avoid panic during slicing
if bytes.len() < 8 {
return Err(InvalidCookie);
}
let namespace = bytes.split_off(8);
let namespace = if namespace.is_empty() {
None
} else {
Some(
Namespace::new(String::from_utf8(namespace).map_err(|_| InvalidCookie)?)
.map_err(|_| InvalidCookie)?,
)
};
let bytes = <[u8; 8]>::try_from(bytes).map_err(|_| InvalidCookie)?;
let id = u64::from_be_bytes(bytes);
Ok(Self { id, namespace })
}
pub fn namespace(&self) -> Option<&Namespace> {
self.namespace.as_ref()
}
}
#[derive(Debug, thiserror::Error)]
#[error("The cookie was malformed")]
pub struct InvalidCookie;
#[derive(Debug, Clone, PartialEq)]
pub struct NewRegistration {
pub namespace: Namespace,
pub record: PeerRecord,
pub ttl: Option<u64>,
}
impl NewRegistration {
pub fn new(namespace: Namespace, record: PeerRecord, ttl: Option<Ttl>) -> Self {
Self {
namespace,
record,
ttl,
}
}
pub fn effective_ttl(&self) -> Ttl {
self.ttl.unwrap_or(DEFAULT_TTL)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Registration {
pub namespace: Namespace,
pub record: PeerRecord,
pub ttl: Ttl,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ErrorCode {
InvalidNamespace,
InvalidSignedPeerRecord,
InvalidTtl,
InvalidCookie,
NotAuthorized,
InternalError,
Unavailable,
}
impl Encoder for Codec {
type Item<'a> = Message;
type Error = Error;
fn encode(&mut self, item: Self::Item<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> {
let mut pb: ProtobufCodec<proto::Message> = ProtobufCodec::new(MAX_MESSAGE_LEN_BYTES);
pb.encode(proto::Message::from(item), dst)?;
Ok(())
}
}
impl Decoder for Codec {
type Item = Message;
type Error = Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let mut pb: ProtobufCodec<proto::Message> = ProtobufCodec::new(MAX_MESSAGE_LEN_BYTES);
let Some(message) = pb.decode(src)? else {
return Ok(None);
};
Ok(Some(message.try_into()?))
}
}
#[derive(Clone, Default)]
pub struct Codec {}
#[async_trait]
impl libp2p_request_response::Codec for Codec {
type Protocol = StreamProtocol;
type Request = Message;
type Response = Message;
async fn read_request<T>(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result<Self::Request>
where
T: AsyncRead + Unpin + Send,
{
let message = FramedRead::new(io, self.clone())
.next()
.await
.ok_or(io::ErrorKind::UnexpectedEof)??;
Ok(message)
}
async fn read_response<T>(
&mut self,
_: &Self::Protocol,
io: &mut T,
) -> io::Result<Self::Response>
where
T: AsyncRead + Unpin + Send,
{
let message = FramedRead::new(io, self.clone())
.next()
.await
.ok_or(io::ErrorKind::UnexpectedEof)??;
Ok(message)
}
async fn write_request<T>(
&mut self,
_: &Self::Protocol,
io: &mut T,
req: Self::Request,
) -> io::Result<()>
where
T: AsyncWrite + Unpin + Send,
{
FramedWrite::new(io, self.clone()).send(req).await?;
Ok(())
}
async fn write_response<T>(
&mut self,
_: &Self::Protocol,
io: &mut T,
res: Self::Response,
) -> io::Result<()>
where
T: AsyncWrite + Unpin + Send,
{
FramedWrite::new(io, self.clone()).send(res).await?;
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Codec(#[from] quick_protobuf_codec::Error),
#[error("Failed to read/write")]
Io(#[from] std::io::Error),
#[error("Failed to convert wire message to internal data model")]
Conversion(#[from] ConversionError),
}
impl From<Error> for std::io::Error {
fn from(value: Error) -> Self {
match value {
Error::Io(e) => e,
Error::Codec(e) => io::Error::from(e),
Error::Conversion(e) => io::Error::new(io::ErrorKind::InvalidInput, e),
}
}
}
impl From<Message> for proto::Message {
fn from(message: Message) -> Self {
match message {
Message::Register(NewRegistration {
namespace,
record,
ttl,
}) => proto::Message {
type_pb: Some(proto::MessageType::REGISTER),
register: Some(proto::Register {
ns: Some(namespace.into()),
ttl,
signedPeerRecord: Some(record.into_signed_envelope().into_protobuf_encoding()),
}),
registerResponse: None,
unregister: None,
discover: None,
discoverResponse: None,
},
Message::RegisterResponse(Ok(ttl)) => proto::Message {
type_pb: Some(proto::MessageType::REGISTER_RESPONSE),
registerResponse: Some(proto::RegisterResponse {
status: Some(proto::ResponseStatus::OK),
statusText: None,
ttl: Some(ttl),
}),
register: None,
discover: None,
unregister: None,
discoverResponse: None,
},
Message::RegisterResponse(Err(error)) => proto::Message {
type_pb: Some(proto::MessageType::REGISTER_RESPONSE),
registerResponse: Some(proto::RegisterResponse {
status: Some(proto::ResponseStatus::from(error)),
statusText: None,
ttl: None,
}),
register: None,
discover: None,
unregister: None,
discoverResponse: None,
},
Message::Unregister(namespace) => proto::Message {
type_pb: Some(proto::MessageType::UNREGISTER),
unregister: Some(proto::Unregister {
ns: Some(namespace.into()),
id: None,
}),
register: None,
registerResponse: None,
discover: None,
discoverResponse: None,
},
Message::Discover {
namespace,
cookie,
limit,
} => proto::Message {
type_pb: Some(proto::MessageType::DISCOVER),
discover: Some(proto::Discover {
ns: namespace.map(|ns| ns.into()),
cookie: cookie.map(|cookie| cookie.into_wire_encoding()),
limit,
}),
register: None,
registerResponse: None,
unregister: None,
discoverResponse: None,
},
Message::DiscoverResponse(Ok((registrations, cookie))) => proto::Message {
type_pb: Some(proto::MessageType::DISCOVER_RESPONSE),
discoverResponse: Some(proto::DiscoverResponse {
registrations: registrations
.into_iter()
.map(|reggo| proto::Register {
ns: Some(reggo.namespace.into()),
ttl: Some(reggo.ttl),
signedPeerRecord: Some(
reggo.record.into_signed_envelope().into_protobuf_encoding(),
),
})
.collect(),
status: Some(proto::ResponseStatus::OK),
statusText: None,
cookie: Some(cookie.into_wire_encoding()),
}),
register: None,
discover: None,
unregister: None,
registerResponse: None,
},
Message::DiscoverResponse(Err(error)) => proto::Message {
type_pb: Some(proto::MessageType::DISCOVER_RESPONSE),
discoverResponse: Some(proto::DiscoverResponse {
registrations: Vec::new(),
status: Some(proto::ResponseStatus::from(error)),
statusText: None,
cookie: None,
}),
register: None,
discover: None,
unregister: None,
registerResponse: None,
},
}
}
}
impl TryFrom<proto::Message> for Message {
type Error = ConversionError;
fn try_from(message: proto::Message) -> Result<Self, Self::Error> {
let message = match message {
proto::Message {
type_pb: Some(proto::MessageType::REGISTER),
register:
Some(proto::Register {
ns,
ttl,
signedPeerRecord: Some(signed_peer_record),
}),
..
} => Message::Register(NewRegistration {
namespace: ns
.map(Namespace::new)
.transpose()?
.ok_or(ConversionError::MissingNamespace)?,
ttl,
record: PeerRecord::from_signed_envelope(SignedEnvelope::from_protobuf_encoding(
&signed_peer_record,
)?)?,
}),
proto::Message {
type_pb: Some(proto::MessageType::REGISTER_RESPONSE),
registerResponse:
Some(proto::RegisterResponse {
status: Some(proto::ResponseStatus::OK),
ttl,
..
}),
..
} => Message::RegisterResponse(Ok(ttl.ok_or(ConversionError::MissingTtl)?)),
proto::Message {
type_pb: Some(proto::MessageType::DISCOVER),
discover: Some(proto::Discover { ns, limit, cookie }),
..
} => Message::Discover {
namespace: ns.map(Namespace::new).transpose()?,
cookie: cookie.map(Cookie::from_wire_encoding).transpose()?,
limit,
},
proto::Message {
type_pb: Some(proto::MessageType::DISCOVER_RESPONSE),
discoverResponse:
Some(proto::DiscoverResponse {
registrations,
status: Some(proto::ResponseStatus::OK),
cookie: Some(cookie),
..
}),
..
} => {
let registrations = registrations
.into_iter()
.map(|reggo| {
Ok(Registration {
namespace: reggo
.ns
.map(Namespace::new)
.transpose()?
.ok_or(ConversionError::MissingNamespace)?,
record: PeerRecord::from_signed_envelope(
SignedEnvelope::from_protobuf_encoding(
®go
.signedPeerRecord
.ok_or(ConversionError::MissingSignedPeerRecord)?,
)?,
)?,
ttl: reggo.ttl.ok_or(ConversionError::MissingTtl)?,
})
})
.collect::<Result<Vec<_>, ConversionError>>()?;
let cookie = Cookie::from_wire_encoding(cookie)?;
Message::DiscoverResponse(Ok((registrations, cookie)))
}
proto::Message {
type_pb: Some(proto::MessageType::REGISTER_RESPONSE),
registerResponse:
Some(proto::RegisterResponse {
status: Some(response_status),
..
}),
..
} => Message::RegisterResponse(Err(response_status.try_into()?)),
proto::Message {
type_pb: Some(proto::MessageType::UNREGISTER),
unregister: Some(proto::Unregister { ns, .. }),
..
} => Message::Unregister(
ns.map(Namespace::new)
.transpose()?
.ok_or(ConversionError::MissingNamespace)?,
),
proto::Message {
type_pb: Some(proto::MessageType::DISCOVER_RESPONSE),
discoverResponse:
Some(proto::DiscoverResponse {
status: Some(response_status),
..
}),
..
} => Message::DiscoverResponse(Err(response_status.try_into()?)),
_ => return Err(ConversionError::InconsistentWireMessage),
};
Ok(message)
}
}
#[derive(Debug, thiserror::Error)]
pub enum ConversionError {
#[error("The wire message is consistent")]
InconsistentWireMessage,
#[error("Missing namespace field")]
MissingNamespace,
#[error("Invalid namespace")]
InvalidNamespace(#[from] NamespaceTooLong),
#[error("Missing signed peer record field")]
MissingSignedPeerRecord,
#[error("Missing TTL field")]
MissingTtl,
#[error("Bad status code")]
BadStatusCode,
#[error("Failed to decode signed envelope")]
BadSignedEnvelope(#[from] signed_envelope::DecodingError),
#[error("Failed to decode envelope as signed peer record")]
BadSignedPeerRecord(#[from] peer_record::FromEnvelopeError),
#[error(transparent)]
BadCookie(#[from] InvalidCookie),
#[error("The requested PoW difficulty is out of range")]
PoWDifficultyOutOfRange,
#[error("The provided PoW hash is not 32 bytes long")]
BadPoWHash,
}
impl ConversionError {
pub fn to_error_code(&self) -> ErrorCode {
match self {
ConversionError::MissingNamespace => ErrorCode::InvalidNamespace,
ConversionError::MissingSignedPeerRecord => ErrorCode::InvalidSignedPeerRecord,
ConversionError::BadSignedEnvelope(_) => ErrorCode::InvalidSignedPeerRecord,
ConversionError::BadSignedPeerRecord(_) => ErrorCode::InvalidSignedPeerRecord,
ConversionError::BadCookie(_) => ErrorCode::InvalidCookie,
ConversionError::MissingTtl => ErrorCode::InvalidTtl,
ConversionError::InconsistentWireMessage => ErrorCode::InternalError,
ConversionError::BadStatusCode => ErrorCode::InternalError,
ConversionError::PoWDifficultyOutOfRange => ErrorCode::InternalError,
ConversionError::BadPoWHash => ErrorCode::InternalError,
ConversionError::InvalidNamespace(_) => ErrorCode::InvalidNamespace,
}
}
}
impl TryFrom<proto::ResponseStatus> for ErrorCode {
type Error = UnmappableStatusCode;
fn try_from(value: proto::ResponseStatus) -> Result<Self, Self::Error> {
use proto::ResponseStatus::*;
let code = match value {
OK => return Err(UnmappableStatusCode(value)),
E_INVALID_NAMESPACE => ErrorCode::InvalidNamespace,
E_INVALID_SIGNED_PEER_RECORD => ErrorCode::InvalidSignedPeerRecord,
E_INVALID_TTL => ErrorCode::InvalidTtl,
E_INVALID_COOKIE => ErrorCode::InvalidCookie,
E_NOT_AUTHORIZED => ErrorCode::NotAuthorized,
E_INTERNAL_ERROR => ErrorCode::InternalError,
E_UNAVAILABLE => ErrorCode::Unavailable,
};
Ok(code)
}
}
impl From<ErrorCode> for proto::ResponseStatus {
fn from(error_code: ErrorCode) -> Self {
use proto::ResponseStatus::*;
match error_code {
ErrorCode::InvalidNamespace => E_INVALID_NAMESPACE,
ErrorCode::InvalidSignedPeerRecord => E_INVALID_SIGNED_PEER_RECORD,
ErrorCode::InvalidTtl => E_INVALID_TTL,
ErrorCode::InvalidCookie => E_INVALID_COOKIE,
ErrorCode::NotAuthorized => E_NOT_AUTHORIZED,
ErrorCode::InternalError => E_INTERNAL_ERROR,
ErrorCode::Unavailable => E_UNAVAILABLE,
}
}
}
impl From<UnmappableStatusCode> for ConversionError {
fn from(_: UnmappableStatusCode) -> Self {
ConversionError::InconsistentWireMessage
}
}
#[derive(Debug, thiserror::Error)]
#[error("The response code ({0:?}) cannot be mapped to our ErrorCode enum")]
pub struct UnmappableStatusCode(proto::ResponseStatus);
mod proto {
#![allow(unreachable_pub)]
include!("generated/mod.rs");
pub(crate) use self::rendezvous::pb::{mod_Message::*, Message};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cookie_wire_encoding_roundtrip() {
let cookie = Cookie::for_namespace(Namespace::from_static("foo"));
let bytes = cookie.clone().into_wire_encoding();
let parsed = Cookie::from_wire_encoding(bytes).unwrap();
assert_eq!(parsed, cookie);
}
#[test]
fn cookie_wire_encoding_length() {
let cookie = Cookie::for_namespace(Namespace::from_static("foo"));
let bytes = cookie.into_wire_encoding();
assert_eq!(bytes.len(), 8 + 3)
}
}
|