Skip to content

Dual funding message follow-ups #2281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 5 additions & 45 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use bitcoin::secp256k1::PublicKey;
use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::{secp256k1, Witness, Transaction};
use bitcoin::{secp256k1, Witness};
use bitcoin::blockdata::script::Script;
use bitcoin::hash_types::{Txid, BlockHash};

Expand All @@ -42,7 +42,7 @@ use crate::io_extras::read_to_end;

use crate::events::{MessageSendEventsProvider, OnionMessageProvider};
use crate::util::logger;
use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, TransactionU16LenLimited};

use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};

Expand Down Expand Up @@ -425,45 +425,6 @@ pub struct ChannelReady {
pub short_channel_id_alias: Option<u64>,
}

/// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
/// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
///
/// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransactionU16LenLimited(Transaction);

impl TransactionU16LenLimited {
/// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
/// serialized length is <= u16::MAX.
pub fn new(transaction: Transaction) -> Result<Self, ()> {
if transaction.serialized_length() > (u16::MAX as usize) {
Err(())
} else {
Ok(Self(transaction))
}
}

/// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
pub fn into_transaction(self) -> Transaction {
self.0
}
}

impl Writeable for TransactionU16LenLimited {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
(self.0.serialized_length() as u16).write(w)?;
self.0.write(w)
}
}

impl Readable for TransactionU16LenLimited {
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let len = <u16 as Readable>::read(r)?;
let mut tx_reader = FixedLengthReader::new(r, len as u64);
Ok(Self(Readable::read(&mut tx_reader)?))
}
}

/// A tx_add_input message for adding an input during interactive transaction construction
///
// TODO(dual_funding): Add spec link for `tx_add_input`.
Expand Down Expand Up @@ -850,7 +811,7 @@ impl NetAddress {
}

impl Writeable for NetAddress {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
match self {
&NetAddress::IPv4 { ref addr, ref port } => {
1u8.write(writer)?;
Expand Down Expand Up @@ -2454,10 +2415,9 @@ mod tests {
use hex;
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
use crate::ln::msgs::{self, TransactionU16LenLimited};
use crate::ln::msgs::{FinalOnionHopData, OnionErrorPacket, OnionHopDataFormat};
use crate::ln::msgs::{self, FinalOnionHopData, OnionErrorPacket, OnionHopDataFormat};
use crate::routing::gossip::{NodeAlias, NodeId};
use crate::util::ser::{Writeable, Readable, Hostname};
use crate::util::ser::{Writeable, Readable, Hostname, TransactionU16LenLimited};

use bitcoin::hashes::hex::FromHex;
use bitcoin::util::address::Address;
Expand Down
44 changes: 44 additions & 0 deletions lightning/src/util/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,50 @@ impl Readable for Duration {
}
}

/// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
/// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
///
/// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransactionU16LenLimited(Transaction);

impl TransactionU16LenLimited {
/// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
/// serialized length is <= u16::MAX.
pub fn new(transaction: Transaction) -> Result<Self, ()> {
if transaction.serialized_length() > (u16::MAX as usize) {
Err(())
} else {
Ok(Self(transaction))
}
}

/// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
pub fn into_transaction(self) -> Transaction {
self.0
}
}

impl Writeable for TransactionU16LenLimited {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
(self.0.serialized_length() as u16).write(w)?;
self.0.write(w)
}
}

impl Readable for TransactionU16LenLimited {
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let len = <u16 as Readable>::read(r)?;
let mut tx_reader = FixedLengthReader::new(r, len as u64);
let tx: Transaction = Readable::read(&mut tx_reader)?;
if tx_reader.bytes_remain() {
Err(DecodeError::BadLengthDescriptor)
} else {
Ok(Self(tx))
}
}
}

#[cfg(test)]
mod tests {
use core::convert::TryFrom;
Expand Down