Skip to content

Commit e04a728

Browse files
Test blinding point serialization in Channel.
1 parent 5d886ff commit e04a728

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

lightning/src/ln/channel.rs

+102-6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ struct InboundHTLCOutput {
165165
state: InboundHTLCState,
166166
}
167167

168+
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
168169
enum OutboundHTLCState {
169170
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
170171
/// created it we would have put it in the holding cell instead). When they next revoke_and_ack
@@ -198,6 +199,7 @@ enum OutboundHTLCState {
198199
}
199200

200201
#[derive(Clone)]
202+
#[cfg_attr(test, derive(Debug, PartialEq))]
201203
enum OutboundHTLCOutcome {
202204
/// LDK version 0.0.105+ will always fill in the preimage here.
203205
Success(Option<PaymentPreimage>),
@@ -222,6 +224,7 @@ impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
222224
}
223225
}
224226

227+
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
225228
struct OutboundHTLCOutput {
226229
htlc_id: u64,
227230
amount_msat: u64,
@@ -234,6 +237,7 @@ struct OutboundHTLCOutput {
234237
}
235238

236239
/// See AwaitingRemoteRevoke ChannelState for more info
240+
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
237241
enum HTLCUpdateAwaitingACK {
238242
AddHTLC { // TODO: Time out if we're getting close to cltv_expiry
239243
// always outbound
@@ -7847,23 +7851,25 @@ mod tests {
78477851
use bitcoin::blockdata::transaction::{Transaction, TxOut};
78487852
use bitcoin::blockdata::opcodes;
78497853
use bitcoin::network::constants::Network;
7850-
use crate::ln::PaymentHash;
7854+
use crate::ln::{PaymentHash, PaymentPreimage};
78517855
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
7852-
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
7856+
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
78537857
use crate::ln::channel::InitFeatures;
7854-
use crate::ln::channel::{ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
7858+
use crate::ln::channel::{Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_msat};
78557859
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
7856-
use crate::ln::features::ChannelTypeFeatures;
7860+
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
7861+
use crate::ln::msgs;
78577862
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
78587863
use crate::ln::script::ShutdownScript;
78597864
use crate::ln::chan_utils::{self, htlc_success_tx_weight, htlc_timeout_tx_weight};
78607865
use crate::chain::BestBlock;
78617866
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator, ConfirmationTarget};
78627867
use crate::sign::{ChannelSigner, InMemorySigner, EntropySource, SignerProvider};
78637868
use crate::chain::transaction::OutPoint;
7864-
use crate::routing::router::Path;
7869+
use crate::routing::router::{Path, RouteHop};
78657870
use crate::util::config::UserConfig;
78667871
use crate::util::errors::APIError;
7872+
use crate::util::ser::{ReadableArgs, Writeable};
78677873
use crate::util::test_utils;
78687874
use crate::util::test_utils::{OnGetShutdownScriptpubkey, TestKeysInterface};
78697875
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
@@ -8375,6 +8381,96 @@ use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
83758381
assert!(!node_a_chan.channel_update(&update).unwrap());
83768382
}
83778383

8384+
#[test]
8385+
fn blinding_point_ser() {
8386+
// Ensure that channel blinding points are (de)serialized properly.
8387+
let feeest = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
8388+
let secp_ctx = Secp256k1::new();
8389+
let seed = [42; 32];
8390+
let network = Network::Testnet;
8391+
let keys_provider = test_utils::TestKeysInterface::new(&seed, network);
8392+
8393+
let node_b_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
8394+
let config = UserConfig::default();
8395+
let features = channelmanager::provided_init_features(&config);
8396+
let outbound_chan = OutboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &features, 10000000, 100000, 42, &config, 0, 42, None).unwrap();
8397+
let mut chan = Channel { context: outbound_chan.context };
8398+
8399+
let dummy_htlc_source = HTLCSource::OutboundRoute {
8400+
path: Path {
8401+
hops: vec![RouteHop {
8402+
pubkey: test_utils::pubkey(2), channel_features: ChannelFeatures::empty(),
8403+
node_features: NodeFeatures::empty(), short_channel_id: 0, fee_msat: 0,
8404+
cltv_expiry_delta: 0, maybe_announced_channel: false,
8405+
}],
8406+
blinded_tail: None
8407+
},
8408+
session_priv: test_utils::privkey(42),
8409+
first_hop_htlc_msat: 0,
8410+
payment_id: PaymentId([42; 32]),
8411+
};
8412+
let dummy_outbound_output = OutboundHTLCOutput {
8413+
htlc_id: 0,
8414+
amount_msat: 0,
8415+
payment_hash: PaymentHash([43; 32]),
8416+
cltv_expiry: 0,
8417+
state: OutboundHTLCState::Committed,
8418+
source: dummy_htlc_source.clone(),
8419+
skimmed_fee_msat: None,
8420+
blinding_point: None,
8421+
};
8422+
let mut pending_outbound_htlcs = vec![dummy_outbound_output.clone(); 10];
8423+
for (idx, htlc) in pending_outbound_htlcs.iter_mut().enumerate() {
8424+
if idx % 2 == 0 {
8425+
htlc.blinding_point = Some(test_utils::pubkey(42 + idx as u8));
8426+
}
8427+
}
8428+
chan.context.pending_outbound_htlcs = pending_outbound_htlcs.clone();
8429+
8430+
let dummy_holding_cell_add_htlc = HTLCUpdateAwaitingACK::AddHTLC {
8431+
amount_msat: 0,
8432+
cltv_expiry: 0,
8433+
payment_hash: PaymentHash([43; 32]),
8434+
source: dummy_htlc_source.clone(),
8435+
onion_routing_packet: msgs::OnionPacket {
8436+
version: 0,
8437+
public_key: Ok(test_utils::pubkey(1)),
8438+
hop_data: [0; 20*65],
8439+
hmac: [0; 32]
8440+
},
8441+
skimmed_fee_msat: None,
8442+
blinding_point: None,
8443+
};
8444+
let dummy_holding_cell_claim_htlc = HTLCUpdateAwaitingACK::ClaimHTLC {
8445+
payment_preimage: PaymentPreimage([42; 32]),
8446+
htlc_id: 0,
8447+
};
8448+
let mut holding_cell_htlc_updates = Vec::with_capacity(10);
8449+
for i in 0..10 {
8450+
if i % 3 == 0 {
8451+
holding_cell_htlc_updates.push(dummy_holding_cell_add_htlc.clone());
8452+
} else if i % 3 == 1 {
8453+
holding_cell_htlc_updates.push(dummy_holding_cell_claim_htlc.clone());
8454+
} else {
8455+
let mut dummy_add = dummy_holding_cell_add_htlc.clone();
8456+
if let HTLCUpdateAwaitingACK::AddHTLC { ref mut blinding_point, .. } = &mut dummy_add {
8457+
*blinding_point = Some(test_utils::pubkey(42 + i));
8458+
} else { panic!() }
8459+
holding_cell_htlc_updates.push(dummy_add);
8460+
}
8461+
}
8462+
chan.context.holding_cell_htlc_updates = holding_cell_htlc_updates.clone();
8463+
8464+
// Encode and decode the channel and ensure that the HTLCs within are the same.
8465+
let encoded_chan = chan.encode();
8466+
let mut s = crate::io::Cursor::new(&encoded_chan);
8467+
let mut reader = crate::util::ser::FixedLengthReader::new(&mut s, encoded_chan.len() as u64);
8468+
let features = channelmanager::provided_channel_type_features(&config);
8469+
let decoded_chan = Channel::read(&mut reader, (&&keys_provider, &&keys_provider, 0, &features)).unwrap();
8470+
assert_eq!(decoded_chan.context.pending_outbound_htlcs, pending_outbound_htlcs);
8471+
assert_eq!(decoded_chan.context.holding_cell_htlc_updates, holding_cell_htlc_updates);
8472+
}
8473+
83788474
#[cfg(feature = "_test_vectors")]
83798475
#[test]
83808476
fn outbound_commitment_test() {
@@ -9131,7 +9227,7 @@ use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
91319227
assert_eq!(chan_utils::build_commitment_secret(&seed, 1),
91329228
<Vec<u8>>::from_hex("915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c").unwrap()[..]);
91339229
}
9134-
9230+
91359231
#[test]
91369232
fn test_key_derivation() {
91379233
// Test vectors from BOLT 3 Appendix E:

lightning/src/ln/onion_utils.rs

+2
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,11 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(
740740
}
741741

742742
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
743+
#[cfg_attr(test, derive(PartialEq))]
743744
pub(super) struct HTLCFailReason(HTLCFailReasonRepr);
744745

745746
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
747+
#[cfg_attr(test, derive(PartialEq))]
746748
enum HTLCFailReasonRepr {
747749
LightningError {
748750
err: msgs::OnionErrorPacket,

0 commit comments

Comments
 (0)