Skip to content

Commit e82318d

Browse files
committed
Use Infallible for the unconstructable default custom message type
When we landed custom messages, we used the empty tuple for the custom message type for `IgnoringMessageHandler`. This was fine, except that we also implemented `Writeable` to panic when writing a `()`. Later, we added support for anchor output construction in CommitmentTransaction, signified by setting a field to `Some(())`, which is serialized as-is. This causes us to panic when writing a `CommitmentTransaction` with `opt_anchors` set. Note that we never set it inside of LDK, but downstream users may. Instead, we implement `Writeable` to write nothing for `()` and use `core::convert::Infallible` for the default custom message type as it is, appropriately, unconstructable. This also makes it easier to implement various things in bindings, as we can always assume `Infallible`-conversion logic is unreachable.
1 parent e12d88d commit e82318d

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

lightning/src/ln/peer_handler.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use sync::{Arc, Mutex};
3737
use core::sync::atomic::{AtomicUsize, Ordering};
3838
use core::{cmp, hash, fmt, mem};
3939
use core::ops::Deref;
40+
use core::convert::Infallible;
4041
#[cfg(feature = "std")] use std::error;
4142

4243
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -80,21 +81,21 @@ impl Deref for IgnoringMessageHandler {
8081
fn deref(&self) -> &Self { self }
8182
}
8283

83-
impl wire::Type for () {
84+
// Implement Type for Infallible, note that it cannot be constructed, and thus you can never call a
85+
// method that takes self for it.
86+
impl wire::Type for Infallible {
8487
fn type_id(&self) -> u16 {
85-
// We should never call this for `DummyCustomType`
8688
unreachable!();
8789
}
8890
}
89-
90-
impl Writeable for () {
91+
impl Writeable for Infallible {
9192
fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
9293
unreachable!();
9394
}
9495
}
9596

9697
impl wire::CustomMessageReader for IgnoringMessageHandler {
97-
type CustomMessage = ();
98+
type CustomMessage = Infallible;
9899
fn read<R: io::Read>(&self, _message_type: u16, _buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
99100
Ok(None)
100101
}

lightning/src/ln/wire.rs

+4
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ mod tests {
457457
}
458458
}
459459

460+
impl Type for () {
461+
fn type_id(&self) -> u16 { unreachable!(); }
462+
}
463+
460464
#[test]
461465
fn is_even_message_type() {
462466
let message = Message::<()>::Unknown(42);

lightning/src/util/ser.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,11 @@ impl<A: Writeable, B: Writeable, C: Writeable> Writeable for (A, B, C) {
879879
}
880880
}
881881

882+
impl Writeable for () {
883+
fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
884+
Ok(())
885+
}
886+
}
882887
impl Readable for () {
883888
fn read<R: Read>(_r: &mut R) -> Result<Self, DecodeError> {
884889
Ok(())
@@ -892,7 +897,6 @@ impl Writeable for String {
892897
w.write_all(self.as_bytes())
893898
}
894899
}
895-
896900
impl Readable for String {
897901
#[inline]
898902
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {

0 commit comments

Comments
 (0)