Skip to content

Commit 93c8760

Browse files
authored
Merge pull request #255 from TheBlueMatt/2018-11-230-ext
Two post-#230 fixups
2 parents 7687e02 + 3af20fd commit 93c8760

File tree

2 files changed

+49
-66
lines changed

2 files changed

+49
-66
lines changed

src/ln/channel.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,6 @@ impl Channel {
633633
&chan_keys.htlc_base_key, &chan_keys.payment_base_key, &keys_provider.get_shutdown_pubkey(), BREAKDOWN_TIMEOUT,
634634
keys_provider.get_destination_script(), logger.clone());
635635
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
636-
channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));
637636
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
638637

639638
let mut chan = Channel {
@@ -1358,7 +1357,6 @@ impl Channel {
13581357
}
13591358

13601359
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
1361-
self.channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER, msg.first_per_commitment_point)));
13621360

13631361
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
13641362
self.their_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.channel_value_satoshis * 1000);
@@ -1434,7 +1432,7 @@ impl Channel {
14341432

14351433
// Now that we're past error-generating stuff, update our local state:
14361434

1437-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number);
1435+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
14381436
self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
14391437
self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
14401438
self.channel_state = ChannelState::FundingSent as u32;
@@ -1506,7 +1504,6 @@ impl Channel {
15061504
return Err(ChannelError::Close("Peer sent a funding_locked at a strange time"));
15071505
}
15081506

1509-
self.channel_monitor.provide_their_next_revocation_point(Some((INITIAL_COMMITMENT_NUMBER - 1 , msg.next_per_commitment_point)));
15101507
self.their_prev_commitment_point = self.their_cur_commitment_point;
15111508
self.their_cur_commitment_point = Some(msg.next_per_commitment_point);
15121509
Ok(())
@@ -1901,7 +1898,6 @@ impl Channel {
19011898
}
19021899
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
19031900
.map_err(|e| ChannelError::Close(e.0))?;
1904-
self.channel_monitor.provide_their_next_revocation_point(Some((self.cur_remote_commitment_transaction_number - 1, msg.next_per_commitment_point)));
19051901

19061902
// Update state now that we've passed all the can-fail calls...
19071903
// (note that we may still fail to generate the new commitment_signed message, but that's
@@ -3002,7 +2998,7 @@ impl Channel {
30022998
let temporary_channel_id = self.channel_id;
30032999

30043000
// Now that we're past error-generating stuff, update our local state:
3005-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number);
3001+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
30063002
self.channel_state = ChannelState::FundingCreated as u32;
30073003
self.channel_id = funding_txo.to_channel_id();
30083004
self.cur_remote_commitment_transaction_number -= 1;
@@ -3214,7 +3210,7 @@ impl Channel {
32143210
match self.send_commitment_no_state_update() {
32153211
Ok((res, remote_commitment_tx)) => {
32163212
// Update state now that we've passed all the can-fail calls...
3217-
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number);
3213+
self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
32183214
self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
32193215
Ok((res, self.channel_monitor.clone()))
32203216
},

src/ln/channelmonitor.rs

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -461,37 +461,11 @@ impl ChannelMonitor {
461461
Ok(())
462462
}
463463

464-
/// Tracks the next revocation point which may be required to claim HTLC outputs which we know
465-
/// the preimage of in case the remote end force-closes using their latest state. When called at
466-
/// channel opening revocation point is the CURRENT one used for first commitment tx. Needed in case of sizeable push_msat.
467-
pub(super) fn provide_their_next_revocation_point(&mut self, their_next_revocation_point: Option<(u64, PublicKey)>) {
468-
if let Some(new_revocation_point) = their_next_revocation_point {
469-
match self.their_cur_revocation_points {
470-
Some(old_points) => {
471-
if old_points.0 == new_revocation_point.0 + 1 {
472-
self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(new_revocation_point.1)));
473-
} else if old_points.0 == new_revocation_point.0 + 2 {
474-
if let Some(old_second_point) = old_points.2 {
475-
self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(new_revocation_point.1)));
476-
} else {
477-
self.their_cur_revocation_points = Some((new_revocation_point.0, new_revocation_point.1, None));
478-
}
479-
} else {
480-
self.their_cur_revocation_points = Some((new_revocation_point.0, new_revocation_point.1, None));
481-
}
482-
},
483-
None => {
484-
self.their_cur_revocation_points = Some((new_revocation_point.0, new_revocation_point.1, None));
485-
}
486-
}
487-
}
488-
}
489-
490464
/// Informs this monitor of the latest remote (ie non-broadcastable) commitment transaction.
491465
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
492466
/// possibly future revocation/preimage information) to claim outputs where possible.
493467
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
494-
pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<HTLCOutputInCommitment>, commitment_number: u64) {
468+
pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<HTLCOutputInCommitment>, commitment_number: u64, their_revocation_point: PublicKey) {
495469
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
496470
// so that a remote monitor doesn't learn anything unless there is a malicious close.
497471
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -501,6 +475,25 @@ impl ChannelMonitor {
501475
}
502476
self.remote_claimable_outpoints.insert(unsigned_commitment_tx.txid(), htlc_outputs);
503477
self.current_remote_commitment_number = commitment_number;
478+
//TODO: Merge this into the other per-remote-transaction output storage stuff
479+
match self.their_cur_revocation_points {
480+
Some(old_points) => {
481+
if old_points.0 == commitment_number + 1 {
482+
self.their_cur_revocation_points = Some((old_points.0, old_points.1, Some(their_revocation_point)));
483+
} else if old_points.0 == commitment_number + 2 {
484+
if let Some(old_second_point) = old_points.2 {
485+
self.their_cur_revocation_points = Some((old_points.0 - 1, old_second_point, Some(their_revocation_point)));
486+
} else {
487+
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
488+
}
489+
} else {
490+
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
491+
}
492+
},
493+
None => {
494+
self.their_cur_revocation_points = Some((commitment_number, their_revocation_point, None));
495+
}
496+
}
504497
}
505498

506499
/// Informs this monitor of the latest local (ie broadcastable) commitment transaction. The
@@ -896,16 +889,18 @@ impl ChannelMonitor {
896889
if commitment_number >= self.get_min_seen_secret() {
897890
let secret = self.get_secret(commitment_number).unwrap();
898891
let per_commitment_key = ignore_error!(SecretKey::from_slice(&self.secp_ctx, &secret));
899-
let (revocation_pubkey, b_htlc_key) = match self.key_storage {
900-
KeyStorage::PrivMode { ref revocation_base_key, ref htlc_base_key, .. } => {
892+
let (revocation_pubkey, b_htlc_key, local_payment_key) = match self.key_storage {
893+
KeyStorage::PrivMode { ref revocation_base_key, ref htlc_base_key, ref payment_base_key, .. } => {
901894
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
902895
(ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
903-
ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))))
896+
ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))),
897+
Some(ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key))))
904898
},
905899
KeyStorage::SigsMode { ref revocation_base_key, ref htlc_base_key, .. } => {
906900
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
907901
(ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key)),
908-
ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &htlc_base_key)))
902+
ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &htlc_base_key)),
903+
None)
909904
},
910905
};
911906
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.their_delayed_payment_base_key.unwrap()));
@@ -917,6 +912,13 @@ impl ChannelMonitor {
917912
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.our_to_self_delay, &delayed_key);
918913
let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
919914

915+
let local_payment_p2wpkh = if let Some(payment_key) = local_payment_key {
916+
// Note that the Network here is ignored as we immediately drop the address for the
917+
// script_pubkey version.
918+
let payment_hash160 = Hash160::from_data(&PublicKey::from_secret_key(&self.secp_ctx, &payment_key).serialize());
919+
Some(Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&payment_hash160[..]).into_script())
920+
} else { None };
921+
920922
let mut total_value = 0;
921923
let mut values = Vec::new();
922924
let mut inputs = Vec::new();
@@ -936,23 +938,12 @@ impl ChannelMonitor {
936938
htlc_idxs.push(None);
937939
values.push(outp.value);
938940
total_value += outp.value;
939-
} else if outp.script_pubkey.is_v0_p2wpkh() {
940-
match self.key_storage {
941-
KeyStorage::PrivMode { ref payment_base_key, .. } => {
942-
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
943-
if let Ok(local_key) = chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key) {
944-
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
945-
outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
946-
key: local_key,
947-
output: outp.clone(),
948-
});
949-
}
950-
}
951-
KeyStorage::SigsMode { .. } => {
952-
//TODO: we need to ensure an offline client will generate the event when it
953-
// cames back online after only the watchtower saw the transaction
954-
}
955-
}
941+
} else if Some(&outp.script_pubkey) == local_payment_p2wpkh.as_ref() {
942+
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
943+
outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
944+
key: local_payment_key.unwrap(),
945+
output: outp.clone(),
946+
});
956947
}
957948
}
958949

@@ -1090,7 +1081,6 @@ impl ChannelMonitor {
10901081
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
10911082
};
10921083

1093-
10941084
for (idx, outp) in tx.output.iter().enumerate() {
10951085
if outp.script_pubkey.is_v0_p2wpkh() {
10961086
match self.key_storage {
@@ -1102,11 +1092,8 @@ impl ChannelMonitor {
11021092
output: outp.clone(),
11031093
});
11041094
}
1105-
}
1106-
KeyStorage::SigsMode { .. } => {
1107-
//TODO: we need to ensure an offline client will generate the event when it
1108-
// cames back online after only the watchtower saw the transaction
1109-
}
1095+
},
1096+
KeyStorage::SigsMode { .. } => {}
11101097
}
11111098
break; // Only to_remote ouput is claimable
11121099
}
@@ -2161,10 +2148,10 @@ mod tests {
21612148
let logger = Arc::new(TestLogger::new());
21622149
let dummy_sig = Signature::from_der(&secp_ctx, &hex::decode("3045022100fa86fa9a36a8cd6a7bb8f06a541787d51371d067951a9461d5404de6b928782e02201c8b7c334c10aed8976a3a465be9a28abff4cb23acbf00022295b378ce1fa3cd").unwrap()[..]).unwrap();
21632150

2151+
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
21642152
macro_rules! dummy_keys {
21652153
() => {
21662154
{
2167-
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &[42; 32]).unwrap());
21682155
TxCreationKeys {
21692156
per_commitment_point: dummy_key.clone(),
21702157
revocation_key: dummy_key.clone(),
@@ -2233,10 +2220,10 @@ mod tests {
22332220
monitor.set_their_to_self_delay(10);
22342221

22352222
monitor.provide_latest_local_commitment_tx_info(dummy_tx.clone(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
2236-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655);
2237-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654);
2238-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653);
2239-
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652);
2223+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key);
2224+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key);
2225+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key);
2226+
monitor.provide_latest_remote_commitment_tx_info(&dummy_tx, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key);
22402227
for &(ref preimage, ref hash) in preimages.iter() {
22412228
monitor.provide_payment_preimage(hash, preimage);
22422229
}

0 commit comments

Comments
 (0)