Skip to content

Commit ea68037

Browse files
committed
Provide channel static fields to ChannelMonitor
1 parent 24cd766 commit ea68037

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 22 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,6 +605,7 @@ 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)>,
@@ -756,6 +757,7 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
756757

757758
// Set in initial Channel-object creation, so should always be set by now:
758759
U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
760+
self.is_outbound.write(writer)?;
759761

760762
self.destination_script.write(writer)?;
761763
if let Some(ref broadcasted_holder_revokable_script) = self.broadcasted_holder_revokable_script {
@@ -934,9 +936,10 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
934936
impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
935937
pub(crate) fn new(keys: ChanSigner, shutdown_pubkey: &PublicKey,
936938
on_counterparty_tx_csv: u16, destination_script: &Script, funding_info: (OutPoint, Script),
937-
counterparty_htlc_base_key: &PublicKey, counterparty_delayed_payment_base_key: &PublicKey,
939+
counterparty_pubkeys: &ChannelPublicKeys,
938940
on_holder_tx_csv: u16, funding_redeemscript: Script, channel_value_satoshis: u64,
939941
commitment_transaction_number_obscure_factor: u64,
942+
is_outbound: bool,
940943
initial_holder_commitment_tx: HolderCommitmentTransaction) -> ChannelMonitor<ChanSigner> {
941944

942945
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
@@ -945,7 +948,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
945948
let payment_key_hash = WPubkeyHash::hash(&keys.pubkeys().payment_point.serialize());
946949
let counterparty_payment_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&payment_key_hash[..]).into_script();
947950

948-
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() };
951+
let counterparty_delayed_payment_base_key = counterparty_pubkeys.delayed_payment_basepoint;
952+
let counterparty_htlc_base_key = counterparty_pubkeys.htlc_basepoint;
953+
let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
949954

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

@@ -969,6 +974,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
969974
ChannelMonitor {
970975
latest_update_id: 0,
971976
commitment_transaction_number_obscure_factor,
977+
is_outbound,
972978

973979
destination_script: destination_script.clone(),
974980
broadcasted_holder_revokable_script: None,
@@ -2199,6 +2205,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
21992205

22002206
let latest_update_id: u64 = Readable::read(reader)?;
22012207
let commitment_transaction_number_obscure_factor = <U48 as Readable>::read(reader)?.0;
2208+
let is_outbound = Readable::read(reader)?;
22022209

22032210
let destination_script = Readable::read(reader)?;
22042211
let broadcasted_holder_revokable_script = match <u8 as Readable>::read(reader)? {
@@ -2422,6 +2429,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
24222429
Ok((last_block_hash.clone(), ChannelMonitor {
24232430
latest_update_id,
24242431
commitment_transaction_number_obscure_factor,
2432+
is_outbound,
24252433

24262434
destination_script,
24272435
broadcasted_holder_revokable_script,
@@ -2485,7 +2493,7 @@ mod tests {
24852493
use ln::channelmanager::{PaymentPreimage, PaymentHash};
24862494
use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
24872495
use ln::chan_utils;
2488-
use ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction};
2496+
use ln::chan_utils::{HTLCOutputInCommitment, HolderCommitmentTransaction, ChannelPublicKeys};
24892497
use util::test_utils::TestLogger;
24902498
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
24912499
use bitcoin::secp256k1::Secp256k1;
@@ -2556,14 +2564,21 @@ mod tests {
25562564
(0, 0)
25572565
);
25582566

2567+
let counterparty_pubkeys = ChannelPublicKeys {
2568+
funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
2569+
revocation_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
2570+
payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
2571+
delayed_payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap()),
2572+
htlc_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())
2573+
};
25592574
// Prune with one old state and a holder commitment tx holding a few overlaps with the
25602575
// old state.
25612576
let mut monitor = ChannelMonitor::new(keys,
25622577
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()), 0, &Script::new(),
25632578
(OutPoint { txid: Txid::from_slice(&[43; 32]).unwrap(), index: 0 }, Script::new()),
2564-
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
2565-
&PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
2566-
10, Script::new(), 46, 0, HolderCommitmentTransaction::dummy());
2579+
&counterparty_pubkeys,
2580+
10, Script::new(), 46, 0,
2581+
true, HolderCommitmentTransaction::dummy());
25672582

25682583
monitor.provide_latest_holder_commitment_tx_info(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
25692584
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);
@@ -1602,9 +1603,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
16021603
let mut channel_monitor = ChannelMonitor::new(self.holder_keys.clone(),
16031604
&self.shutdown_pubkey, self.holder_selected_contest_delay,
16041605
&self.destination_script, (funding_txo.clone(), funding_txo_script.clone()),
1605-
&counterparty_pubkeys.htlc_basepoint, &counterparty_pubkeys.delayed_payment_basepoint,
1606+
&counterparty_pubkeys,
16061607
self.counterparty_selected_contest_delay, funding_redeemscript.clone(), self.channel_value_satoshis,
16071608
self.get_commitment_transaction_number_obscure_factor(),
1609+
self.is_outbound(),
16081610
commitment_tx);
16091611

16101612
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)