Skip to content

Commit f569e9f

Browse files
authored
Merge pull request #2281 from dunxen/2023-05-dfmsgfollowups
Dual funding message follow-ups
2 parents 7884bc4 + 33e901a commit f569e9f

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

lightning/src/ln/msgs.rs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
2727
use bitcoin::secp256k1::PublicKey;
2828
use bitcoin::secp256k1::ecdsa::Signature;
29-
use bitcoin::{secp256k1, Witness, Transaction};
29+
use bitcoin::{secp256k1, Witness};
3030
use bitcoin::blockdata::script::Script;
3131
use bitcoin::hash_types::{Txid, BlockHash};
3232

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

4343
use crate::events::{MessageSendEventsProvider, OnionMessageProvider};
4444
use crate::util::logger;
45-
use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
45+
use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, TransactionU16LenLimited};
4646

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

@@ -425,45 +425,6 @@ pub struct ChannelReady {
425425
pub short_channel_id_alias: Option<u64>,
426426
}
427427

428-
/// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
429-
/// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
430-
///
431-
/// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
432-
#[derive(Clone, Debug, PartialEq, Eq)]
433-
pub struct TransactionU16LenLimited(Transaction);
434-
435-
impl TransactionU16LenLimited {
436-
/// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
437-
/// serialized length is <= u16::MAX.
438-
pub fn new(transaction: Transaction) -> Result<Self, ()> {
439-
if transaction.serialized_length() > (u16::MAX as usize) {
440-
Err(())
441-
} else {
442-
Ok(Self(transaction))
443-
}
444-
}
445-
446-
/// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
447-
pub fn into_transaction(self) -> Transaction {
448-
self.0
449-
}
450-
}
451-
452-
impl Writeable for TransactionU16LenLimited {
453-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
454-
(self.0.serialized_length() as u16).write(w)?;
455-
self.0.write(w)
456-
}
457-
}
458-
459-
impl Readable for TransactionU16LenLimited {
460-
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
461-
let len = <u16 as Readable>::read(r)?;
462-
let mut tx_reader = FixedLengthReader::new(r, len as u64);
463-
Ok(Self(Readable::read(&mut tx_reader)?))
464-
}
465-
}
466-
467428
/// A tx_add_input message for adding an input during interactive transaction construction
468429
///
469430
// TODO(dual_funding): Add spec link for `tx_add_input`.
@@ -850,7 +811,7 @@ impl NetAddress {
850811
}
851812

852813
impl Writeable for NetAddress {
853-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
814+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
854815
match self {
855816
&NetAddress::IPv4 { ref addr, ref port } => {
856817
1u8.write(writer)?;
@@ -2454,10 +2415,9 @@ mod tests {
24542415
use hex;
24552416
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
24562417
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
2457-
use crate::ln::msgs::{self, TransactionU16LenLimited};
2458-
use crate::ln::msgs::{FinalOnionHopData, OnionErrorPacket, OnionHopDataFormat};
2418+
use crate::ln::msgs::{self, FinalOnionHopData, OnionErrorPacket, OnionHopDataFormat};
24592419
use crate::routing::gossip::{NodeAlias, NodeId};
2460-
use crate::util::ser::{Writeable, Readable, Hostname};
2420+
use crate::util::ser::{Writeable, Readable, Hostname, TransactionU16LenLimited};
24612421

24622422
use bitcoin::hashes::hex::FromHex;
24632423
use bitcoin::util::address::Address;

lightning/src/util/ser.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,50 @@ impl Readable for Duration {
13491349
}
13501350
}
13511351

1352+
/// A wrapper for a `Transaction` which can only be constructed with [`TransactionU16LenLimited::new`]
1353+
/// if the `Transaction`'s consensus-serialized length is <= u16::MAX.
1354+
///
1355+
/// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`.
1356+
#[derive(Clone, Debug, PartialEq, Eq)]
1357+
pub struct TransactionU16LenLimited(Transaction);
1358+
1359+
impl TransactionU16LenLimited {
1360+
/// Constructs a new `TransactionU16LenLimited` from a `Transaction` only if it's consensus-
1361+
/// serialized length is <= u16::MAX.
1362+
pub fn new(transaction: Transaction) -> Result<Self, ()> {
1363+
if transaction.serialized_length() > (u16::MAX as usize) {
1364+
Err(())
1365+
} else {
1366+
Ok(Self(transaction))
1367+
}
1368+
}
1369+
1370+
/// Consumes this `TransactionU16LenLimited` and returns its contained `Transaction`.
1371+
pub fn into_transaction(self) -> Transaction {
1372+
self.0
1373+
}
1374+
}
1375+
1376+
impl Writeable for TransactionU16LenLimited {
1377+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1378+
(self.0.serialized_length() as u16).write(w)?;
1379+
self.0.write(w)
1380+
}
1381+
}
1382+
1383+
impl Readable for TransactionU16LenLimited {
1384+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1385+
let len = <u16 as Readable>::read(r)?;
1386+
let mut tx_reader = FixedLengthReader::new(r, len as u64);
1387+
let tx: Transaction = Readable::read(&mut tx_reader)?;
1388+
if tx_reader.bytes_remain() {
1389+
Err(DecodeError::BadLengthDescriptor)
1390+
} else {
1391+
Ok(Self(tx))
1392+
}
1393+
}
1394+
}
1395+
13521396
#[cfg(test)]
13531397
mod tests {
13541398
use core::convert::TryFrom;

0 commit comments

Comments
 (0)