From fb818e09a09b477d30bbb3521f5fffbacc2d6caa Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Thu, 15 Aug 2024 12:22:59 -0700 Subject: [PATCH] Drop Seek and SeekFrom usage. In lieu of using `Seek` and `SeekFrom`, we will instead rely exclusively on the `io::Cursor` type. --- lightning/src/offers/invoice.rs | 28 ++++++++++++------------- lightning/src/offers/invoice_request.rs | 14 ++++++------- lightning/src/offers/parse.rs | 8 +++---- lightning/src/offers/refund.rs | 12 +++++------ lightning/src/offers/static_invoice.rs | 12 +++++------ lightning/src/util/ser.rs | 8 +++---- lightning/src/util/ser_macros.rs | 8 +++---- 7 files changed, 45 insertions(+), 45 deletions(-) diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 835a077f2..39423f94c 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -125,7 +125,7 @@ use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage} use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef}; use crate::offers::refund::{IV_BYTES_WITH_METADATA as REFUND_IV_BYTES_WITH_METADATA, IV_BYTES_WITHOUT_METADATA as REFUND_IV_BYTES_WITHOUT_METADATA, Refund, RefundContents}; use crate::offers::signer::{Metadata, self}; -use crate::util::ser::{HighZeroBytesDroppedBigSize, Iterable, Readable, SeekReadable, WithoutLength, Writeable, Writer}; +use crate::util::ser::{CursorReadable, HighZeroBytesDroppedBigSize, Iterable, Readable, WithoutLength, Writeable, Writer}; use crate::util::string::PrintableString; #[allow(unused_imports)] @@ -1266,13 +1266,13 @@ type FullInvoiceTlvStreamRef<'a> = ( SignatureTlvStreamRef<'a>, ); -impl SeekReadable for FullInvoiceTlvStream { - fn read(r: &mut R) -> Result { - let payer = SeekReadable::read(r)?; - let offer = SeekReadable::read(r)?; - let invoice_request = SeekReadable::read(r)?; - let invoice = SeekReadable::read(r)?; - let signature = SeekReadable::read(r)?; +impl CursorReadable for FullInvoiceTlvStream { + fn read>(r: &mut io::Cursor) -> Result { + let payer = CursorReadable::read(r)?; + let offer = CursorReadable::read(r)?; + let invoice_request = CursorReadable::read(r)?; + let invoice = CursorReadable::read(r)?; + let signature = CursorReadable::read(r)?; Ok((payer, offer, invoice_request, invoice, signature)) } @@ -1288,12 +1288,12 @@ type PartialInvoiceTlvStreamRef<'a> = ( InvoiceTlvStreamRef<'a>, ); -impl SeekReadable for PartialInvoiceTlvStream { - fn read(r: &mut R) -> Result { - let payer = SeekReadable::read(r)?; - let offer = SeekReadable::read(r)?; - let invoice_request = SeekReadable::read(r)?; - let invoice = SeekReadable::read(r)?; +impl CursorReadable for PartialInvoiceTlvStream { + fn read>(r: &mut io::Cursor) -> Result { + let payer = CursorReadable::read(r)?; + let offer = CursorReadable::read(r)?; + let invoice_request = CursorReadable::read(r)?; + let invoice = CursorReadable::read(r)?; Ok((payer, offer, invoice_request, invoice)) } diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index 6ed9b2a12..f8815beb6 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -75,7 +75,7 @@ use crate::offers::offer::{Offer, OfferContents, OfferId, OfferTlvStream, OfferT use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError}; use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef}; use crate::offers::signer::{Metadata, MetadataMaterial}; -use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, SeekReadable, WithoutLength, Writeable, Writer}; +use crate::util::ser::{CursorReadable, HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer}; use crate::util::string::{PrintableString, UntrustedString}; #[cfg(not(c_bindings))] @@ -1070,12 +1070,12 @@ type FullInvoiceRequestTlvStreamRef<'a> = ( SignatureTlvStreamRef<'a>, ); -impl SeekReadable for FullInvoiceRequestTlvStream { - fn read(r: &mut R) -> Result { - let payer = SeekReadable::read(r)?; - let offer = SeekReadable::read(r)?; - let invoice_request = SeekReadable::read(r)?; - let signature = SeekReadable::read(r)?; +impl CursorReadable for FullInvoiceRequestTlvStream { + fn read>(r: &mut io::Cursor) -> Result { + let payer = CursorReadable::read(r)?; + let offer = CursorReadable::read(r)?; + let invoice_request = CursorReadable::read(r)?; + let signature = CursorReadable::read(r)?; Ok((payer, offer, invoice_request, signature)) } diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index c48d745a9..8b9f64d8b 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -12,7 +12,7 @@ use bitcoin::secp256k1; use crate::io; use crate::ln::msgs::DecodeError; -use crate::util::ser::SeekReadable; +use crate::util::ser::CursorReadable; #[allow(unused_imports)] use crate::prelude::*; @@ -91,17 +91,17 @@ mod sealed { /// A wrapper for reading a message as a TLV stream `T` from a byte sequence, while still /// maintaining ownership of the bytes for later use. -pub(super) struct ParsedMessage { +pub(super) struct ParsedMessage { pub bytes: Vec, pub tlv_stream: T, } -impl TryFrom> for ParsedMessage { +impl TryFrom> for ParsedMessage { type Error = DecodeError; fn try_from(bytes: Vec) -> Result { let mut cursor = io::Cursor::new(bytes); - let tlv_stream: T = SeekReadable::read(&mut cursor)?; + let tlv_stream: T = CursorReadable::read(&mut cursor)?; // Ensure that there are no more TLV records left to parse. if cursor.position() < cursor.get_ref().len() as u64 { diff --git a/lightning/src/offers/refund.rs b/lightning/src/offers/refund.rs index 10f56f0a1..0795bbadc 100644 --- a/lightning/src/offers/refund.rs +++ b/lightning/src/offers/refund.rs @@ -104,7 +104,7 @@ use crate::offers::offer::{OfferTlvStream, OfferTlvStreamRef}; use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage}; use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef}; use crate::offers::signer::{Metadata, MetadataMaterial, self}; -use crate::util::ser::{SeekReadable, Readable, WithoutLength, Writeable, Writer}; +use crate::util::ser::{CursorReadable, Readable, WithoutLength, Writeable, Writer}; use crate::util::string::PrintableString; #[cfg(not(c_bindings))] @@ -801,11 +801,11 @@ type RefundTlvStreamRef<'a> = ( InvoiceRequestTlvStreamRef<'a>, ); -impl SeekReadable for RefundTlvStream { - fn read(r: &mut R) -> Result { - let payer = SeekReadable::read(r)?; - let offer = SeekReadable::read(r)?; - let invoice_request = SeekReadable::read(r)?; +impl CursorReadable for RefundTlvStream { + fn read>(r: &mut io::Cursor) -> Result { + let payer = CursorReadable::read(r)?; + let offer = CursorReadable::read(r)?; + let invoice_request = CursorReadable::read(r)?; Ok((payer, offer, invoice_request)) } diff --git a/lightning/src/offers/static_invoice.rs b/lightning/src/offers/static_invoice.rs index 614903bdf..93014fce2 100644 --- a/lightning/src/offers/static_invoice.rs +++ b/lightning/src/offers/static_invoice.rs @@ -27,7 +27,7 @@ use crate::offers::offer::{ Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, Quantity, }; use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage}; -use crate::util::ser::{Iterable, SeekReadable, WithoutLength, Writeable, Writer}; +use crate::util::ser::{CursorReadable, Iterable, WithoutLength, Writeable, Writer}; use crate::util::string::PrintableString; use bitcoin::address::Address; use bitcoin::constants::ChainHash; @@ -457,11 +457,11 @@ impl TryFrom> for StaticInvoice { type FullInvoiceTlvStream = (OfferTlvStream, InvoiceTlvStream, SignatureTlvStream); -impl SeekReadable for FullInvoiceTlvStream { - fn read(r: &mut R) -> Result { - let offer = SeekReadable::read(r)?; - let invoice = SeekReadable::read(r)?; - let signature = SeekReadable::read(r)?; +impl CursorReadable for FullInvoiceTlvStream { + fn read>(r: &mut io::Cursor) -> Result { + let offer = CursorReadable::read(r)?; + let invoice = CursorReadable::read(r)?; + let signature = CursorReadable::read(r)?; Ok((offer, invoice, signature)) } diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 02d57ebf6..e3e97f2d6 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -14,7 +14,7 @@ //! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor use crate::prelude::*; -use crate::io::{self, Read, Seek, Write}; +use crate::io::{self, Read, Write}; use crate::io_extras::{copy, sink}; use core::hash::Hash; use crate::sync::{Mutex, RwLock}; @@ -247,10 +247,10 @@ pub trait Readable } /// A trait that various LDK types implement allowing them to be read in from a -/// [`Read`]` + `[`Seek`]. -pub(crate) trait SeekReadable where Self: Sized { +/// [`io::Cursor`]. +pub(crate) trait CursorReadable where Self: Sized { /// Reads a `Self` in from the given [`Read`]. - fn read(reader: &mut R) -> Result; + fn read>(reader: &mut io::Cursor) -> Result; } /// A trait that various higher-level LDK types implement allowing them to be read in diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index a6f3e08f6..e56b2d9c6 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -952,13 +952,13 @@ macro_rules! tlv_stream { } } - impl $crate::util::ser::SeekReadable for $name { - fn read(reader: &mut R) -> Result { + impl $crate::util::ser::CursorReadable for $name { + fn read>(reader: &mut crate::io::Cursor) -> Result { $( _init_tlv_field_var!($field, option); )* - let rewind = |cursor: &mut R, offset: usize| { - cursor.seek($crate::io::SeekFrom::Current(-(offset as i64))).expect(""); + let rewind = |cursor: &mut crate::io::Cursor, offset: usize| { + cursor.set_position(cursor.position().checked_sub(offset as u64).expect("Cannot rewind past 0.")); }; _decode_tlv_stream_range!(reader, $range, rewind, { $(($type, $field, (option, encoding: $fieldty))),* -- 2.39.5