Skip to content

Commit ec16cf2

Browse files
committed
provide channel static fields to ChannelMonitor
1 parent bed68ce commit ec16cf2

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use bitcoin::secp256k1;
3939

4040
use ln::msgs::DecodeError;
4141
use ln::chan_utils;
42-
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction, HTLCType};
42+
use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HolderCommitmentTransaction, HTLCType, ChannelPublicKeys};
4343
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
4444
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
4545
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
@@ -605,13 +605,15 @@ impl Readable for ChannelMonitorUpdateStep {
605605
pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
606606
latest_update_id: u64,
607607
commitment_transaction_number_obscure_factor: u64,
608+
is_outbound: bool,
608609

609610
destination_script: Script,
610611
broadcasted_holder_revokable_script: Option<(Script, PublicKey, PublicKey)>,
611612
counterparty_payment_script: Script,
612613
shutdown_script: Script,
613614

614615
keys: ChanSigner,
616+
counterparty_pubkeys: ChannelPublicKeys,
615617
funding_info: (OutPoint, Script),
616618
current_counterparty_commitment_txid: Option<Txid>,
617619
prev_counterparty_commitment_txid: Option<Txid>,
@@ -756,6 +758,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
756758

757759
// Set in initial Channel-object creation, so should always be set by now:
758760
U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
761+
self.is_outbound.write(writer)?;
759762

760763
self.destination_script.write(writer)?;
761764
if let Some(ref broadcasted_holder_revokable_script) = self.broadcasted_holder_revokable_script {
@@ -771,6 +774,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
771774
self.shutdown_script.write(writer)?;
772775

773776
self.keys.write(writer)?;
777+
self.counterparty_pubkeys.write(writer)?;
774778
writer.write_all(&self.funding_info.0.txid[..])?;
775779
writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
776780
self.funding_info.1.write(writer)?;
@@ -933,9 +937,10 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
933937
impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
934938
pub(crate) fn new(keys: ChanSigner, shutdown_pubkey: &PublicKey,
935939
on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
936-
counterparty_htlc_base_key: &PublicKey, counterparty_delayed_payment_base_key: &PublicKey,
940+
counterparty_pubkeys: &ChannelPublicKeys,
937941
on_holder_tx_csv: u16, funding_redeemscript: Script, channel_value_satoshis: u64,
938942
commitment_transaction_number_obscure_factor: u64,
943+
is_outbound: bool,
939944
initial_holder_commitment_tx: HolderCommitmentTransaction) -> ChannelMonitor<ChanSigner> {
940945

941946
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
@@ -944,7 +949,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
944949
let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
945950
let counterparty_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
946951

947-
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key: *counterparty_delayed_payment_base_key, counterparty_htlc_base_key: *counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
952+
let counterparty_delayed_payment_base_key = counterparty_pubkeys.delayed_payment_basepoint;
953+
let counterparty_htlc_base_key = counterparty_pubkeys.htlc_basepoint;
954+
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
948955

949956
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys.clone(), on_holder_tx_csv);
950957

@@ -968,13 +975,15 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
968975
ChannelMonitor {
969976
latest_update_id: 0,
970977
commitment_transaction_number_obscure_factor,
978+
is_outbound,
971979

972980
destination_script: destination_script.clone(),
973981
broadcasted_holder_revokable_script: None,
974982
counterparty_payment_script,
975983
shutdown_script,
976984

977985
keys,
986+
counterparty_pubkeys: counterparty_pubkeys.clone(),
978987
funding_info,
979988
current_counterparty_commitment_txid: None,
980989
prev_counterparty_commitment_txid: None,
@@ -2113,6 +2122,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
21132122

21142123
let latest_update_id: u64 = Readable::read(reader)?;
21152124
let commitment_transaction_number_obscure_factor = <U48 as Readable>::read(reader)?.0;
2125+
let is_outbound = Readable::read(reader)?;
21162126

21172127
let destination_script = Readable::read(reader)?;
21182128
let broadcasted_holder_revokable_script = match <u8 as Readable>::read(reader)? {
@@ -2129,6 +2139,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
21292139
let shutdown_script = Readable::read(reader)?;
21302140

21312141
let keys = Readable::read(reader)?;
2142+
let counterparty_pubkeys = Readable::read(reader)?;
21322143
// Technically this can fail and serialize fail a round-trip, but only for serialization of
21332144
// barely-init'd ChannelMonitors that we can't do anything with.
21342145
let outpoint = OutPoint {
@@ -2336,13 +2347,15 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
23362347
Ok((last_block_hash.clone(), ChannelMonitor {
23372348
latest_update_id,
23382349
commitment_transaction_number_obscure_factor,
2350+
is_outbound,
23392351

23402352
destination_script,
23412353
broadcasted_holder_revokable_script,
23422354
counterparty_payment_script,
23432355
shutdown_script,
23442356

23452357
keys,
2358+
counterparty_pubkeys,
23462359
funding_info,
23472360
current_counterparty_commitment_txid,
23482361
prev_counterparty_commitment_txid,
@@ -2399,7 +2412,7 @@ mod tests {
23992412
use ln::channelmanager::{PaymentPreimage, PaymentHash};
24002413
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
24012414
use ln::chan_utils;
2402-
use ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction};
2415+
use ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction, ChannelPublicKeys};
24032416
use util::test_utils::TestLogger;
24042417
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
24052418
use bitcoin::secp256k1::Secp256k1;
@@ -2470,14 +2483,21 @@ mod tests {
24702483
(0, 0)
24712484
);
24722485

2486+
let counterparty_pubkeys = ChannelPublicKeys {
2487+
funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
2488+
revocation_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
2489+
payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
2490+
delayed_payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap()),
2491+
htlc_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())
2492+
};
24732493
// Prune with one old state and a holder commitment tx holding a few overlaps with the
24742494
// old state.
24752495
let mut monitor = ChannelMonitor::new(keys,
24762496
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
24772497
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
2478-
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
2479-
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
2480-
10, Script::new(), 46, 0, HolderCommitmentTransaction::dummy());
2498+
&counterparty_pubkeys,
2499+
10, Script::new(), 46, 0,
2500+
true, HolderCommitmentTransaction::dummy());
24812501

24822502
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
24832503
monitor.provide_latest_counterparty_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);

lightning/src/ln/channel.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,9 +1536,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15361536
let mut channel_monitor = ChannelMonitor::new(self.holder_keys.clone(),
15371537
&self.shutdown_pubkey, self.holder_selected_contest_delay,
15381538
&self.destination_script, (funding_txo, funding_txo_script.clone()),
1539-
&counterparty_pubkeys.htlc_basepoint, &counterparty_pubkeys.delayed_payment_basepoint,
1539+
&counterparty_pubkeys,
15401540
self.counterparty_selected_contest_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
15411541
self.get_commitment_transaction_number_obscure_factor(),
1542+
self.is_outbound(),
15421543
initial_commitment_tx);
15431544

15441545
channel_monitor.provide_latest_counterparty_commitment_tx_info(&counterparty_initial_commitment_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);
@@ -1604,9 +1605,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
16041605
let mut channel_monitor = ChannelMonitor::new(self.holder_keys.clone(),
16051606
&self.shutdown_pubkey, self.holder_selected_contest_delay,
16061607
&self.destination_script, (funding_txo.clone(), funding_txo_script.clone()),
1607-
&counterparty_pubkeys.htlc_basepoint, &counterparty_pubkeys.delayed_payment_basepoint,
1608+
&counterparty_pubkeys,
16081609
self.counterparty_selected_contest_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
16091610
self.get_commitment_transaction_number_obscure_factor(),
1611+
self.is_outbound(),
16101612
commitment_tx);
16111613

16121614
channel_monitor.provide_latest_counterparty_commitment_tx_info(&counterparty_initial_commitment_tx, Vec::new(), self.cur_counterparty_commitment_transaction_number, self.counterparty_cur_commitment_point.unwrap(), logger);

0 commit comments

Comments
 (0)