Merge pull request #555 from ariard/2020-03-begin-dryup-chanmon-keys

Begin dry-up ChannelMonitor key access
This commit is contained in:
Matt Corallo 2020-03-21 22:21:38 +00:00 committed by GitHub
commit e5bedc43d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 174 additions and 338 deletions

View file

@ -290,22 +290,15 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given key is already present")),
hash_map::Entry::Vacant(e) => e,
};
match monitor.key_storage {
Storage::Local { ref funding_info, .. } => {
match funding_info {
&None => {
return Err(MonitorUpdateError("Try to update a useless monitor without funding_txo !"));
},
&Some((ref outpoint, ref script)) => {
log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..]));
self.chain_monitor.install_watch_tx(&outpoint.txid, script);
self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
},
}
match monitor.onchain_detection.funding_info {
None => {
return Err(MonitorUpdateError("Try to update a useless monitor without funding_txo !"));
},
Some((ref outpoint, ref script)) => {
log_trace!(self, "Got new Channel Monitor for channel {}", log_bytes!(outpoint.to_channel_id()[..]));
self.chain_monitor.install_watch_tx(&outpoint.txid, script);
self.chain_monitor.install_watch_outpoint((outpoint.txid, outpoint.index as u32), script);
},
Storage::Watchtower { .. } => {
self.chain_monitor.watch_all_txn();
}
}
for (txid, outputs) in monitor.get_outputs_to_watch().iter() {
for (idx, script) in outputs.iter().enumerate() {
@ -321,7 +314,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
let mut monitors = self.monitors.lock().unwrap();
match monitors.get_mut(&key) {
Some(orig_monitor) => {
log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor.key_storage));
log_trace!(self, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor.onchain_detection));
orig_monitor.update_monitor(update, &self.broadcaster)
},
None => Err(MonitorUpdateError("No such monitor registered"))
@ -398,44 +391,17 @@ pub(crate) const LATENCY_GRACE_PERIOD_BLOCKS: u32 = 3;
/// keeping bumping another claim tx to solve the outpoint.
pub(crate) const ANTI_REORG_DELAY: u32 = 6;
enum Storage<ChanSigner: ChannelKeys> {
Local {
keys: ChanSigner,
funding_key: SecretKey,
revocation_base_key: SecretKey,
htlc_base_key: SecretKey,
delayed_payment_base_key: SecretKey,
payment_base_key: SecretKey,
funding_info: Option<(OutPoint, Script)>,
current_remote_commitment_txid: Option<Sha256dHash>,
prev_remote_commitment_txid: Option<Sha256dHash>,
},
Watchtower {
revocation_base_key: PublicKey,
htlc_base_key: PublicKey,
}
struct OnchainDetection<ChanSigner: ChannelKeys> {
keys: ChanSigner,
funding_info: Option<(OutPoint, Script)>,
current_remote_commitment_txid: Option<Sha256dHash>,
prev_remote_commitment_txid: Option<Sha256dHash>,
}
#[cfg(any(test, feature = "fuzztarget"))]
impl<ChanSigner: ChannelKeys> PartialEq for Storage<ChanSigner> {
impl<ChanSigner: ChannelKeys> PartialEq for OnchainDetection<ChanSigner> {
fn eq(&self, other: &Self) -> bool {
match *self {
Storage::Local { ref keys, .. } => {
let k = keys;
match *other {
Storage::Local { ref keys, .. } => keys.pubkeys() == k.pubkeys(),
Storage::Watchtower { .. } => false,
}
},
Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => {
let (rbk, hbk) = (revocation_base_key, htlc_base_key);
match *other {
Storage::Local { .. } => false,
Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} =>
revocation_base_key == rbk && htlc_base_key == hbk,
}
},
}
self.keys.pubkeys() == other.keys.pubkeys()
}
}
@ -766,7 +732,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
broadcasted_remote_payment_script: Option<(Script, SecretKey)>,
shutdown_script: Script,
key_storage: Storage<ChanSigner>,
onchain_detection: OnchainDetection<ChanSigner>,
their_htlc_base_key: Option<PublicKey>,
their_delayed_payment_base_key: Option<PublicKey>,
funding_redeemscript: Option<Script>,
@ -819,9 +785,9 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
outputs_to_watch: HashMap<Sha256dHash, Vec<Script>>,
#[cfg(test)]
pub onchain_tx_handler: OnchainTxHandler,
pub onchain_tx_handler: OnchainTxHandler<ChanSigner>,
#[cfg(not(test))]
onchain_tx_handler: OnchainTxHandler,
onchain_tx_handler: OnchainTxHandler<ChanSigner>,
// We simply modify last_block_hash in Channel's block_connected so that serialization is
// consistent but hopefully the users' copy handles block_connected in a consistent way.
@ -843,7 +809,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
self.destination_script != other.destination_script ||
self.broadcasted_local_revokable_script != other.broadcasted_local_revokable_script ||
self.broadcasted_remote_payment_script != other.broadcasted_remote_payment_script ||
self.key_storage != other.key_storage ||
self.onchain_detection != other.onchain_detection ||
self.their_htlc_base_key != other.their_htlc_base_key ||
self.their_delayed_payment_base_key != other.their_delayed_payment_base_key ||
self.funding_redeemscript != other.funding_redeemscript ||
@ -903,30 +869,19 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
}
self.shutdown_script.write(writer)?;
match self.key_storage {
Storage::Local { ref keys, ref funding_key, ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
writer.write_all(&[0; 1])?;
keys.write(writer)?;
writer.write_all(&funding_key[..])?;
writer.write_all(&revocation_base_key[..])?;
writer.write_all(&htlc_base_key[..])?;
writer.write_all(&delayed_payment_base_key[..])?;
writer.write_all(&payment_base_key[..])?;
match funding_info {
&Some((ref outpoint, ref script)) => {
writer.write_all(&outpoint.txid[..])?;
writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
script.write(writer)?;
},
&None => {
debug_assert!(false, "Try to serialize a useless Local monitor !");
},
}
current_remote_commitment_txid.write(writer)?;
prev_remote_commitment_txid.write(writer)?;
self.onchain_detection.keys.write(writer)?;
match self.onchain_detection.funding_info {
Some((ref outpoint, ref script)) => {
writer.write_all(&outpoint.txid[..])?;
writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
script.write(writer)?;
},
None => {
debug_assert!(false, "Try to serialize a useless Local monitor !");
},
Storage::Watchtower { .. } => unimplemented!(),
}
self.onchain_detection.current_remote_commitment_txid.write(writer)?;
self.onchain_detection.prev_remote_commitment_txid.write(writer)?;
writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
@ -1121,13 +1076,16 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
logger: Arc<Logger>) -> ChannelMonitor<ChanSigner> {
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
let funding_key = keys.funding_key().clone();
let revocation_base_key = keys.revocation_base_key().clone();
let htlc_base_key = keys.htlc_base_key().clone();
let delayed_payment_base_key = keys.delayed_payment_base_key().clone();
let payment_base_key = keys.payment_base_key().clone();
let our_channel_close_key_hash = Hash160::hash(&shutdown_pubkey.serialize());
let shutdown_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_close_key_hash[..]).into_script();
let onchain_detection = OnchainDetection {
keys: keys.clone(),
funding_info: Some(funding_info.clone()),
current_remote_commitment_txid: None,
prev_remote_commitment_txid: None,
};
ChannelMonitor {
latest_update_id: 0,
commitment_transaction_number_obscure_factor,
@ -1137,17 +1095,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
broadcasted_remote_payment_script: None,
shutdown_script,
key_storage: Storage::Local {
keys,
funding_key,
revocation_base_key,
htlc_base_key,
delayed_payment_base_key,
payment_base_key,
funding_info: Some(funding_info),
current_remote_commitment_txid: None,
prev_remote_commitment_txid: None,
},
onchain_detection: onchain_detection,
their_htlc_base_key: Some(their_htlc_base_key.clone()),
their_delayed_payment_base_key: Some(their_delayed_payment_base_key.clone()),
funding_redeemscript: Some(funding_redeemscript),
@ -1173,7 +1121,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
onchain_events_waiting_threshold_conf: HashMap::new(),
outputs_to_watch: HashMap::new(),
onchain_tx_handler: OnchainTxHandler::new(destination_script.clone(), logger.clone()),
onchain_tx_handler: OnchainTxHandler::new(destination_script.clone(), keys, logger.clone()),
last_block_hash: Default::default(),
secp_ctx: Secp256k1::new(),
@ -1191,11 +1139,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
// Prune HTLCs from the previous remote commitment tx so we don't generate failure/fulfill
// events for now-revoked/fulfilled HTLCs.
if let Storage::Local { ref mut prev_remote_commitment_txid, .. } = self.key_storage {
if let Some(txid) = prev_remote_commitment_txid.take() {
for &mut (_, ref mut source) in self.remote_claimable_outpoints.get_mut(&txid).unwrap() {
*source = None;
}
if let Some(txid) = self.onchain_detection.prev_remote_commitment_txid.take() {
for &mut (_, ref mut source) in self.remote_claimable_outpoints.get_mut(&txid).unwrap() {
*source = None;
}
}
@ -1250,10 +1196,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
let new_txid = unsigned_commitment_tx.txid();
log_trace!(self, "Tracking new remote commitment transaction with txid {} at commitment number {} with {} HTLC outputs", new_txid, commitment_number, htlc_outputs.len());
log_trace!(self, "New potential remote commitment transaction: {}", encode::serialize_hex(unsigned_commitment_tx));
if let Storage::Local { ref mut current_remote_commitment_txid, ref mut prev_remote_commitment_txid, .. } = self.key_storage {
*prev_remote_commitment_txid = current_remote_commitment_txid.take();
*current_remote_commitment_txid = Some(new_txid);
}
self.onchain_detection.prev_remote_commitment_txid = self.onchain_detection.current_remote_commitment_txid.take();
self.onchain_detection.current_remote_commitment_txid = Some(new_txid);
self.remote_claimable_outpoints.insert(new_txid, htlc_outputs);
self.current_remote_commitment_number = commitment_number;
//TODO: Merge this into the other per-remote-transaction output storage stuff
@ -1278,18 +1222,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
}
pub(super) fn provide_rescue_remote_commitment_tx_info(&mut self, their_revocation_point: PublicKey) {
match self.key_storage {
Storage::Local { ref payment_base_key, ref keys, .. } => {
if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &keys.pubkeys().payment_basepoint) {
let to_remote_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
.push_slice(&Hash160::hash(&payment_key.serialize())[..])
.into_script();
if let Ok(to_remote_key) = chan_utils::derive_private_key(&self.secp_ctx, &their_revocation_point, &payment_base_key) {
self.broadcasted_remote_payment_script = Some((to_remote_script, to_remote_key));
}
}
},
Storage::Watchtower { .. } => {}
if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &self.onchain_detection.keys.pubkeys().payment_basepoint) {
let to_remote_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
.push_slice(&Hash160::hash(&payment_key.serialize())[..])
.into_script();
if let Ok(to_remote_key) = chan_utils::derive_private_key(&self.secp_ctx, &their_revocation_point, &self.onchain_detection.keys.payment_base_key()) {
self.broadcasted_remote_payment_script = Some((to_remote_script, to_remote_key));
}
}
}
@ -1395,17 +1334,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
/// Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
pub fn get_funding_txo(&self) -> Option<OutPoint> {
match self.key_storage {
Storage::Local { ref funding_info, .. } => {
match funding_info {
&Some((outpoint, _)) => Some(outpoint),
&None => None
}
},
Storage::Watchtower { .. } => {
return None;
}
if let Some((outp, _)) = self.onchain_detection.funding_info {
return Some(outp)
}
None
}
/// Gets a list of txids, with their output scripts (in the order they appear in the
@ -1495,18 +1427,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
if commitment_number >= self.get_min_seen_secret() {
let secret = self.get_secret(commitment_number).unwrap();
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
let (revocation_pubkey, revocation_key, b_htlc_key, local_payment_key) = match self.key_storage {
Storage::Local { ref keys, ref revocation_base_key, ref payment_base_key, .. } => {
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
(ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().revocation_basepoint)),
ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &revocation_base_key)),
ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().htlc_basepoint)),
ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key)))
},
Storage::Watchtower { .. } => {
unimplemented!()
},
};
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.onchain_detection.keys.revocation_base_key()));
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.pubkeys().htlc_basepoint));
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.payment_base_key()));
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()));
let a_htlc_key = match self.their_htlc_base_key {
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
@ -1582,13 +1507,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
}
}
}
if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
if let &Some(ref txid) = current_remote_commitment_txid {
check_htlc_fails!(txid, "current");
}
if let &Some(ref txid) = prev_remote_commitment_txid {
check_htlc_fails!(txid, "remote");
}
if let Some(ref txid) = self.onchain_detection.current_remote_commitment_txid {
check_htlc_fails!(txid, "current");
}
if let Some(ref txid) = self.onchain_detection.prev_remote_commitment_txid {
check_htlc_fails!(txid, "remote");
}
// No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
}
@ -1647,13 +1570,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
}
}
}
if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
if let &Some(ref txid) = current_remote_commitment_txid {
check_htlc_fails!(txid, "current", 'current_loop);
}
if let &Some(ref txid) = prev_remote_commitment_txid {
check_htlc_fails!(txid, "previous", 'prev_loop);
}
if let Some(ref txid) = self.onchain_detection.current_remote_commitment_txid {
check_htlc_fails!(txid, "current", 'current_loop);
}
if let Some(ref txid) = self.onchain_detection.prev_remote_commitment_txid {
check_htlc_fails!(txid, "previous", 'prev_loop);
}
if let Some(revocation_points) = self.their_cur_revocation_points {
@ -1663,19 +1584,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
if revocation_points.0 == commitment_number + 1 { Some(point) } else { None }
} else { None };
if let Some(revocation_point) = revocation_point_option {
let (revocation_pubkey, b_htlc_key, htlc_privkey, local_payment_key) = match self.key_storage {
Storage::Local { ref keys, ref htlc_base_key, ref payment_base_key, .. } => {
(ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &keys.pubkeys().revocation_basepoint)),
ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &keys.pubkeys().htlc_basepoint)),
ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &htlc_base_key)),
ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &payment_base_key)))
},
Storage::Watchtower { .. } => { unimplemented!() }
};
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
let b_htlc_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.pubkeys().htlc_basepoint));
let htlc_privkey = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.htlc_base_key()));
let a_htlc_key = match self.their_htlc_base_key {
None => return (claimable_outpoints, (commitment_txid, watch_outputs)),
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
};
let local_payment_key = ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, revocation_point, &self.onchain_detection.keys.payment_base_key()));
self.broadcasted_remote_payment_script = {
// Note that the Network here is ignored as we immediately drop the address for the
@ -1726,13 +1642,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
let secret = if let Some(secret) = self.get_secret(commitment_number) { secret } else { return (Vec::new(), None); };
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
let (revocation_pubkey, revocation_key) = match self.key_storage {
Storage::Local { ref keys, ref revocation_base_key, .. } => {
(ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().revocation_basepoint)),
ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, revocation_base_key)))
},
Storage::Watchtower { .. } => { unimplemented!() }
};
let revocation_pubkey = ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &self.onchain_detection.keys.pubkeys().revocation_basepoint));
let revocation_key = ignore_error!(chan_utils::derive_private_revocation_key(&self.secp_ctx, &per_commitment_key, &self.onchain_detection.keys.revocation_base_key()));
let delayed_key = match self.their_delayed_payment_base_key {
None => return (Vec::new(), None),
Some(their_delayed_payment_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &their_delayed_payment_base_key)),
@ -1745,53 +1656,51 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
(claimable_outpoints, Some((htlc_txid, tx.output.clone())))
}
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx, delayed_payment_base_key: &SecretKey) -> (Vec<Transaction>, Vec<TxOut>, Option<(Script, SecretKey, Script)>) {
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx) -> (Vec<Transaction>, Vec<TxOut>, Option<(Script, SecretKey, Script)>) {
let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
let redeemscript = chan_utils::get_revokeable_redeemscript(&local_tx.revocation_key, self.their_to_self_delay.unwrap(), &local_tx.delayed_payment_key);
let broadcasted_local_revokable_script = if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, &local_tx.per_commitment_point, delayed_payment_base_key) {
let broadcasted_local_revokable_script = if let Ok(local_delayedkey) = chan_utils::derive_private_key(&self.secp_ctx, &local_tx.per_commitment_point, self.onchain_detection.keys.delayed_payment_base_key()) {
Some((redeemscript.to_v0_p2wsh(), local_delayedkey, redeemscript))
} else { None };
if let &Storage::Local { ref htlc_base_key, .. } = &self.key_storage {
for &(ref htlc, ref sigs, _) in local_tx.htlc_outputs.iter() {
if let Some(transaction_output_index) = htlc.transaction_output_index {
if let &Some(ref their_sig) = sigs {
if htlc.offered {
log_trace!(self, "Broadcasting HTLC-Timeout transaction against local commitment transactions");
let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
for &(ref htlc, ref sigs, _) in local_tx.htlc_outputs.iter() {
if let Some(transaction_output_index) = htlc.transaction_output_index {
if let &Some(ref their_sig) = sigs {
if htlc.offered {
log_trace!(self, "Broadcasting HTLC-Timeout transaction against local commitment transactions");
let mut htlc_timeout_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
let (our_sig, htlc_script) = match
chan_utils::sign_htlc_transaction(&mut htlc_timeout_tx, their_sig, &None, htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, &self.onchain_detection.keys.htlc_base_key(), &self.secp_ctx) {
Ok(res) => res,
Err(_) => continue,
};
let mut per_input_material = HashMap::with_capacity(1);
per_input_material.insert(htlc_timeout_tx.input[0].previous_output, InputMaterial::LocalHTLC { witness_script: htlc_script, sigs: (*their_sig, our_sig), preimage: None, amount: htlc.amount_msat / 1000});
//TODO: with option_simplified_commitment track outpoint too
log_trace!(self, "Outpoint {}:{} is being being claimed", htlc_timeout_tx.input[0].previous_output.vout, htlc_timeout_tx.input[0].previous_output.txid);
res.push(htlc_timeout_tx);
} else {
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
log_trace!(self, "Broadcasting HTLC-Success transaction against local commitment transactions");
let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
let (our_sig, htlc_script) = match
chan_utils::sign_htlc_transaction(&mut htlc_timeout_tx, their_sig, &None, htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, htlc_base_key, &self.secp_ctx) {
chan_utils::sign_htlc_transaction(&mut htlc_success_tx, their_sig, &Some(*payment_preimage), htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, &self.onchain_detection.keys.htlc_base_key(), &self.secp_ctx) {
Ok(res) => res,
Err(_) => continue,
};
let mut per_input_material = HashMap::with_capacity(1);
per_input_material.insert(htlc_timeout_tx.input[0].previous_output, InputMaterial::LocalHTLC { witness_script: htlc_script, sigs: (*their_sig, our_sig), preimage: None, amount: htlc.amount_msat / 1000});
per_input_material.insert(htlc_success_tx.input[0].previous_output, InputMaterial::LocalHTLC { witness_script: htlc_script, sigs: (*their_sig, our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000});
//TODO: with option_simplified_commitment track outpoint too
log_trace!(self, "Outpoint {}:{} is being being claimed", htlc_timeout_tx.input[0].previous_output.vout, htlc_timeout_tx.input[0].previous_output.txid);
res.push(htlc_timeout_tx);
} else {
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
log_trace!(self, "Broadcasting HTLC-Success transaction against local commitment transactions");
let mut htlc_success_tx = chan_utils::build_htlc_transaction(&local_tx.txid, local_tx.feerate_per_kw, self.their_to_self_delay.unwrap(), htlc, &local_tx.delayed_payment_key, &local_tx.revocation_key);
let (our_sig, htlc_script) = match
chan_utils::sign_htlc_transaction(&mut htlc_success_tx, their_sig, &Some(*payment_preimage), htlc, &local_tx.a_htlc_key, &local_tx.b_htlc_key, &local_tx.revocation_key, &local_tx.per_commitment_point, htlc_base_key, &self.secp_ctx) {
Ok(res) => res,
Err(_) => continue,
};
let mut per_input_material = HashMap::with_capacity(1);
per_input_material.insert(htlc_success_tx.input[0].previous_output, InputMaterial::LocalHTLC { witness_script: htlc_script, sigs: (*their_sig, our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000});
//TODO: with option_simplified_commitment track outpoint too
log_trace!(self, "Outpoint {}:{} is being being claimed", htlc_success_tx.input[0].previous_output.vout, htlc_success_tx.input[0].previous_output.txid);
res.push(htlc_success_tx);
}
log_trace!(self, "Outpoint {}:{} is being being claimed", htlc_success_tx.input[0].previous_output.vout, htlc_success_tx.input[0].previous_output.txid);
res.push(htlc_success_tx);
}
watch_outputs.push(local_tx.tx.without_valid_witness().output[transaction_output_index as usize].clone());
} else { panic!("Should have sigs for non-dust local tx outputs!") }
}
}
watch_outputs.push(local_tx.tx.without_valid_witness().output[transaction_output_index as usize].clone());
} else { panic!("Should have sigs for non-dust local tx outputs!") }
}
}
@ -1840,38 +1749,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
// HTLCs set may differ between last and previous local commitment txn, in case of one them hitting chain, ensure we cancel all HTLCs backward
let mut is_local_tx = false;
if let &mut Some(ref mut local_tx) = &mut self.current_local_signed_commitment_tx {
if local_tx.txid == commitment_txid {
match self.key_storage {
Storage::Local { ref funding_key, .. } => {
local_tx.tx.add_local_sig(funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
},
_ => {},
}
}
}
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
if local_tx.txid == commitment_txid {
is_local_tx = true;
log_trace!(self, "Got latest local commitment tx broadcast, searching for available HTLCs to claim");
assert!(local_tx.tx.has_local_sig());
match self.key_storage {
Storage::Local { ref delayed_payment_base_key, .. } => {
let mut res = self.broadcast_by_local_state(local_tx, delayed_payment_base_key);
append_onchain_update!(res);
},
Storage::Watchtower { .. } => { }
}
}
}
if let &mut Some(ref mut local_tx) = &mut self.prev_local_signed_commitment_tx {
if local_tx.txid == commitment_txid {
match self.key_storage {
Storage::Local { ref funding_key, .. } => {
local_tx.tx.add_local_sig(funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
},
_ => {},
}
let mut res = self.broadcast_by_local_state(local_tx);
append_onchain_update!(res);
}
}
if let &Some(ref local_tx) = &self.prev_local_signed_commitment_tx {
@ -1879,13 +1762,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
is_local_tx = true;
log_trace!(self, "Got previous local commitment tx broadcast, searching for available HTLCs to claim");
assert!(local_tx.tx.has_local_sig());
match self.key_storage {
Storage::Local { ref delayed_payment_base_key, .. } => {
let mut res = self.broadcast_by_local_state(local_tx, delayed_payment_base_key);
append_onchain_update!(res);
},
Storage::Watchtower { .. } => { }
}
let mut res = self.broadcast_by_local_state(local_tx);
append_onchain_update!(res);
}
}
@ -1928,23 +1806,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
// tracking state and panic!()ing if we get an update after force-closure/local-tx signing.
log_trace!(self, "Getting signed latest local commitment transaction!");
if let &mut Some(ref mut local_tx) = &mut self.current_local_signed_commitment_tx {
match self.key_storage {
Storage::Local { ref funding_key, .. } => {
local_tx.tx.add_local_sig(funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
},
_ => {},
}
local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
}
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
let mut res = vec![local_tx.tx.with_valid_witness().clone()];
match self.key_storage {
Storage::Local { ref delayed_payment_base_key, .. } => {
res.append(&mut self.broadcast_by_local_state(local_tx, delayed_payment_base_key).0);
// We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
// The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
},
_ => panic!("Can only broadcast by local channelmonitor"),
};
res.append(&mut self.broadcast_by_local_state(local_tx).0);
// We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
// The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
res
} else {
Vec::new()
@ -1979,14 +1847,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
// which is an easy way to filter out any potential non-matching txn for lazy
// filters.
let prevout = &tx.input[0].previous_output;
let funding_txo = match self.key_storage {
Storage::Local { ref funding_info, .. } => {
funding_info.clone()
}
Storage::Watchtower { .. } => {
unimplemented!();
}
};
let funding_txo = self.onchain_detection.funding_info.clone();
if funding_txo.is_none() || (prevout.txid == funding_txo.as_ref().unwrap().0.txid && prevout.vout == funding_txo.as_ref().unwrap().0.index as u32) {
if (tx.input[0].sequence >> 8*3) as u8 == 0x80 && (tx.lock_time >> 8*3) as u8 == 0x20 {
let (mut new_outpoints, new_outputs) = self.check_spend_remote_transaction(&tx, height);
@ -2027,30 +1888,20 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
} else { false };
if let Some(ref mut cur_local_tx) = self.current_local_signed_commitment_tx {
if should_broadcast {
match self.key_storage {
Storage::Local { ref funding_key, .. } => {
cur_local_tx.tx.add_local_sig(funding_key, self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
},
_ => {}
}
cur_local_tx.tx.add_local_sig(&self.onchain_detection.keys.funding_key(), self.funding_redeemscript.as_ref().unwrap(), self.channel_value_satoshis.unwrap(), &self.secp_ctx);
}
}
if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
if should_broadcast {
log_trace!(self, "Broadcast onchain {}", log_tx!(cur_local_tx.tx.with_valid_witness()));
broadcaster.broadcast_transaction(&cur_local_tx.tx.with_valid_witness());
match self.key_storage {
Storage::Local { ref delayed_payment_base_key, .. } => {
let (txs, new_outputs, _) = self.broadcast_by_local_state(&cur_local_tx, delayed_payment_base_key);
if !new_outputs.is_empty() {
watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
}
for tx in txs {
log_trace!(self, "Broadcast onchain {}", log_tx!(tx));
broadcaster.broadcast_transaction(&tx);
}
},
Storage::Watchtower { .. } => { },
let (txs, new_outputs, _) = self.broadcast_by_local_state(&cur_local_tx);
if !new_outputs.is_empty() {
watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
}
for tx in txs {
log_trace!(self, "Broadcast onchain {}", log_tx!(tx));
broadcaster.broadcast_transaction(&tx);
}
}
}
@ -2151,16 +2002,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
scan_commitment!(cur_local_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), true);
}
if let Storage::Local { ref current_remote_commitment_txid, ref prev_remote_commitment_txid, .. } = self.key_storage {
if let &Some(ref txid) = current_remote_commitment_txid {
if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
}
if let Some(ref txid) = self.onchain_detection.current_remote_commitment_txid {
if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
}
if let &Some(ref txid) = prev_remote_commitment_txid {
if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
}
}
if let Some(ref txid) = self.onchain_detection.prev_remote_commitment_txid {
if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
scan_commitment!(htlc_outputs.iter().map(|&(ref a, _)| a), false);
}
}
@ -2201,7 +2050,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
macro_rules! check_htlc_valid_remote {
($remote_txid: expr, $htlc_output: expr) => {
if let &Some(txid) = $remote_txid {
if let Some(txid) = $remote_txid {
for &(ref pending_htlc, ref pending_source) in self.remote_claimable_outpoints.get(&txid).unwrap() {
if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
if let &Some(ref source) = pending_source {
@ -2228,13 +2077,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
// resolve the source HTLC with the original sender.
payment_data = Some(((*source).clone(), htlc_output.payment_hash));
} else if !$local_tx {
if let Storage::Local { ref current_remote_commitment_txid, .. } = self.key_storage {
check_htlc_valid_remote!(current_remote_commitment_txid, htlc_output);
}
check_htlc_valid_remote!(self.onchain_detection.current_remote_commitment_txid, htlc_output);
if payment_data.is_none() {
if let Storage::Local { ref prev_remote_commitment_txid, .. } = self.key_storage {
check_htlc_valid_remote!(prev_remote_commitment_txid, htlc_output);
}
check_htlc_valid_remote!(self.onchain_detection.prev_remote_commitment_txid, htlc_output);
}
}
if payment_data.is_none() {
@ -2405,36 +2250,23 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
};
let shutdown_script = Readable::read(reader)?;
let key_storage = match <u8 as Readable>::read(reader)? {
0 => {
let keys = Readable::read(reader)?;
let funding_key = Readable::read(reader)?;
let revocation_base_key = Readable::read(reader)?;
let htlc_base_key = Readable::read(reader)?;
let delayed_payment_base_key = Readable::read(reader)?;
let payment_base_key = Readable::read(reader)?;
// Technically this can fail and serialize fail a round-trip, but only for serialization of
// barely-init'd ChannelMonitors that we can't do anything with.
let outpoint = OutPoint {
txid: Readable::read(reader)?,
index: Readable::read(reader)?,
};
let funding_info = Some((outpoint, Readable::read(reader)?));
let current_remote_commitment_txid = Readable::read(reader)?;
let prev_remote_commitment_txid = Readable::read(reader)?;
Storage::Local {
keys,
funding_key,
revocation_base_key,
htlc_base_key,
delayed_payment_base_key,
payment_base_key,
funding_info,
current_remote_commitment_txid,
prev_remote_commitment_txid,
}
},
_ => return Err(DecodeError::InvalidValue),
let onchain_detection = {
let keys = Readable::read(reader)?;
// Technically this can fail and serialize fail a round-trip, but only for serialization of
// barely-init'd ChannelMonitors that we can't do anything with.
let outpoint = OutPoint {
txid: Readable::read(reader)?,
index: Readable::read(reader)?,
};
let funding_info = Some((outpoint, Readable::read(reader)?));
let current_remote_commitment_txid = Readable::read(reader)?;
let prev_remote_commitment_txid = Readable::read(reader)?;
OnchainDetection {
keys,
funding_info,
current_remote_commitment_txid,
prev_remote_commitment_txid,
}
};
let their_htlc_base_key = Some(Readable::read(reader)?);
@ -2645,7 +2477,7 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for (Sha256dH
broadcasted_remote_payment_script,
shutdown_script,
key_storage,
onchain_detection,
their_htlc_base_key,
their_delayed_payment_base_key,
funding_redeemscript,
@ -2903,7 +2735,7 @@ mod tests {
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
}
assert_eq!(base_weight + OnchainTxHandler::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
// Claim tx with 1 offered HTLCs, 3 received HTLCs
claim_tx.input.clear();
@ -2925,7 +2757,7 @@ mod tests {
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
}
assert_eq!(base_weight + OnchainTxHandler::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
// Justice tx with 1 revoked HTLC-Success tx output
claim_tx.input.clear();
@ -2945,7 +2777,7 @@ mod tests {
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
}
assert_eq!(base_weight + OnchainTxHandler::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
assert_eq!(base_weight + OnchainTxHandler::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
}
// Further testing is done in the ChannelManager integration tests.

View file

@ -17,6 +17,7 @@ use ln::msgs::DecodeError;
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
use ln::chan_utils::HTLCType;
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
use chain::keysinterface::ChannelKeys;
use util::logger::Logger;
use util::ser::{ReadableArgs, Readable, Writer, Writeable};
use util::byte_utils;
@ -138,10 +139,11 @@ macro_rules! subtract_high_prio_fee {
/// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
/// do RBF bumping if possible.
#[derive(Clone)]
pub struct OnchainTxHandler {
pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
destination_script: Script,
key_storage: ChanSigner,
// Used to track claiming requests. If claim tx doesn't confirm before height timer expiration we need to bump
// it (RBF or CPFP). If an input has been part of an aggregate tx at first claim try, we need to keep it within
// another bumped aggregate tx to comply with RBF rules. We may have multiple claiming txn in the flight for the
@ -175,10 +177,12 @@ pub struct OnchainTxHandler {
logger: Arc<Logger>
}
impl Writeable for OnchainTxHandler {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
pub(crate) fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
self.destination_script.write(writer)?;
self.key_storage.write(writer)?;
writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
for (ref ancestor_claim_txid, claim_tx_data) in self.pending_claim_requests.iter() {
ancestor_claim_txid.write(writer)?;
@ -214,10 +218,12 @@ impl Writeable for OnchainTxHandler {
}
}
impl ReadableArgs<Arc<Logger>> for OnchainTxHandler {
impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTxHandler<ChanSigner> {
fn read<R: ::std::io::Read>(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
let destination_script = Readable::read(reader)?;
let key_storage = Readable::read(reader)?;
let pending_claim_requests_len: u64 = Readable::read(reader)?;
let mut pending_claim_requests = HashMap::with_capacity(cmp::min(pending_claim_requests_len as usize, MAX_ALLOC_SIZE / 128));
for _ in 0..pending_claim_requests_len {
@ -263,6 +269,7 @@ impl ReadableArgs<Arc<Logger>> for OnchainTxHandler {
Ok(OnchainTxHandler {
destination_script,
key_storage,
claimable_outpoints,
pending_claim_requests,
onchain_events_waiting_threshold_conf,
@ -272,10 +279,14 @@ impl ReadableArgs<Arc<Logger>> for OnchainTxHandler {
}
}
impl OnchainTxHandler {
pub(super) fn new(destination_script: Script, logger: Arc<Logger>) -> Self {
impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
pub(super) fn new(destination_script: Script, keys: ChanSigner, logger: Arc<Logger>) -> Self {
let key_storage = keys;
OnchainTxHandler {
destination_script,
key_storage,
pending_claim_requests: HashMap::new(),
claimable_outpoints: HashMap::new(),
onchain_events_waiting_threshold_conf: HashMap::new(),

View file

@ -66,14 +66,7 @@ impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
}
macro_rules! log_funding_info {
($key_storage: expr) => {
match $key_storage {
Storage::Local { ref funding_info, .. } => {
::util::macro_logger::DebugFundingInfo(&funding_info)
},
Storage::Watchtower { .. } => {
::util::macro_logger::DebugFundingInfo(&None)
}
}
::util::macro_logger::DebugFundingInfo(&$key_storage.funding_info)
}
}