Skip to content

Commit 2ed7a64

Browse files
committed
Support de/ser of the new channel_type field in open_channel
1 parent 7d9f7ad commit 2ed7a64

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lightning/src/ln/channel.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4217,6 +4217,7 @@ impl<Signer: Sign> Channel<Signer> {
42174217
Some(script) => script.clone().into_inner(),
42184218
None => Builder::new().into_script(),
42194219
}),
4220+
channel_type: None,
42204221
}
42214222
}
42224223

lightning/src/ln/msgs.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use bitcoin::secp256k1;
3030
use bitcoin::blockdata::script::Script;
3131
use bitcoin::hash_types::{Txid, BlockHash};
3232

33-
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
33+
use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
3434

3535
use prelude::*;
3636
use core::{cmp, fmt};
@@ -148,6 +148,10 @@ pub struct OpenChannel {
148148
pub channel_flags: u8,
149149
/// Optionally, a request to pre-set the to-sender output's scriptPubkey for when we collaboratively close
150150
pub shutdown_scriptpubkey: OptionalField<Script>,
151+
/// The channel type that this channel will represent. If none is set, we derive the channel
152+
/// type from the intersection of our feature bits with our counterparty's feature bits from
153+
/// the Init message.
154+
pub channel_type: Option<ChannelTypeFeatures>,
151155
}
152156

153157
/// An accept_channel message to be sent or received from a peer
@@ -1162,7 +1166,9 @@ impl_writeable_msg!(OpenChannel, {
11621166
first_per_commitment_point,
11631167
channel_flags,
11641168
shutdown_scriptpubkey
1165-
}, {});
1169+
}, {
1170+
(1, channel_type, option),
1171+
});
11661172

11671173
impl_writeable_msg!(RevokeAndACK, {
11681174
channel_id,
@@ -1747,8 +1753,9 @@ impl_writeable_msg!(GossipTimestampFilter, {
17471753
mod tests {
17481754
use hex;
17491755
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
1756+
use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
17501757
use ln::msgs;
1751-
use ln::msgs::{ChannelFeatures, FinalOnionHopData, InitFeatures, NodeFeatures, OptionalField, OnionErrorPacket, OnionHopDataFormat};
1758+
use ln::msgs::{FinalOnionHopData, OptionalField, OnionErrorPacket, OnionHopDataFormat};
17521759
use util::ser::{Writeable, Readable};
17531760

17541761
use bitcoin::hashes::hex::FromHex;
@@ -2052,7 +2059,7 @@ mod tests {
20522059
do_encoding_channel_update(true, true, true, true);
20532060
}
20542061

2055-
fn do_encoding_open_channel(random_bit: bool, shutdown: bool) {
2062+
fn do_encoding_open_channel(random_bit: bool, shutdown: bool, chan_type: bool) {
20562063
let secp_ctx = Secp256k1::new();
20572064
let (_, pubkey_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
20582065
let (_, pubkey_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202", secp_ctx);
@@ -2079,7 +2086,8 @@ mod tests {
20792086
htlc_basepoint: pubkey_5,
20802087
first_per_commitment_point: pubkey_6,
20812088
channel_flags: if random_bit { 1 << 5 } else { 0 },
2082-
shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
2089+
shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent },
2090+
channel_type: if chan_type { Some(ChannelTypeFeatures::empty()) } else { None },
20832091
};
20842092
let encoded_value = open_channel.encode();
20852093
let mut target_value = Vec::new();
@@ -2093,15 +2101,22 @@ mod tests {
20932101
if shutdown {
20942102
target_value.append(&mut hex::decode("001976a91479b000887626b294a914501a4cd226b58b23598388ac").unwrap());
20952103
}
2104+
if chan_type {
2105+
target_value.append(&mut hex::decode("0100").unwrap());
2106+
}
20962107
assert_eq!(encoded_value, target_value);
20972108
}
20982109

20992110
#[test]
21002111
fn encoding_open_channel() {
2101-
do_encoding_open_channel(false, false);
2102-
do_encoding_open_channel(true, false);
2103-
do_encoding_open_channel(false, true);
2104-
do_encoding_open_channel(true, true);
2112+
do_encoding_open_channel(false, false, false);
2113+
do_encoding_open_channel(false, false, true);
2114+
do_encoding_open_channel(false, true, false);
2115+
do_encoding_open_channel(false, true, true);
2116+
do_encoding_open_channel(true, false, false);
2117+
do_encoding_open_channel(true, false, true);
2118+
do_encoding_open_channel(true, true, false);
2119+
do_encoding_open_channel(true, true, true);
21052120
}
21062121

21072122
fn do_encoding_accept_channel(shutdown: bool) {

0 commit comments

Comments
 (0)