Skip to content

Commit a5b7ed2

Browse files
author
Antoine Riard
committed
WIP
1 parent d7ea454 commit a5b7ed2

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

src/ln/channelmonitor.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,12 @@ pub struct ChannelMonitor {
461461
// for inputs spending this. If height timer (u32) is expired and claim tx hasn't reached enough confirmations
462462
// before, use TxMaterial to regenerate a new claim tx with a satoshis-per-1000-weight-units higher than last
463463
// one (u64), if timelock expiration (u32) is near, decrease height timer, the in-between bumps delay.
464-
// Last field cached (u32) is height of outpoint confirmation, which is needed to flush this tracker
464+
// We also cache height of outpoint confirmation (u32), which is needed to flush this tracker
465465
// in case of reorgs, given block timer are scaled on timer expiration we can't deduce from it original height.
466-
our_claim_txn_waiting_first_conf: HashMap<BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32)>,
466+
// Last field cached (Option<Sha256dHash>) is txid of lastest claim tx, it shouldn't be useful for single-input claim
467+
// tx but if we claimed outpoints at first in an aggregate one, they must stay claimed in an aggregate bumped
468+
// one too to respect RBF rules.
469+
our_claim_txn_waiting_first_conf: HashMap<BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32, Vec<BitcoinOutPoint>)>,
467470

468471
// Used to track onchain events, i.e transactions parts of channels confirmed on chain, on which
469472
// we have to take actions once they reach enough confs. Key is a block height timer, i.e we enforce
@@ -1142,6 +1145,10 @@ impl ChannelMonitor {
11421145
writer.write_all(&byte_utils::be64_to_array(claim_tx_data.2))?;
11431146
writer.write_all(&byte_utils::be32_to_array(claim_tx_data.3))?;
11441147
writer.write_all(&byte_utils::be32_to_array(claim_tx_data.4))?;
1148+
writer.write_all(&byte_utils::be64_to_array(claim_tx_data.5.len() as u64))?;
1149+
for inp in claim_tx_data.5.iter() {
1150+
inp.write(writer)?;
1151+
}
11451152
}
11461153

11471154
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
@@ -1382,7 +1389,7 @@ impl ChannelMonitor {
13821389
log_trace!(self, "Outpoint {}:{} is under claiming process, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", single_htlc_tx.input[0].previous_output.vout, single_htlc_tx.input[0].previous_output.txid, height_timer);
13831390
match self.our_claim_txn_waiting_first_conf.entry(single_htlc_tx.input[0].previous_output.clone()) {
13841391
hash_map::Entry::Occupied(_) => {},
1385-
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
1392+
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: Some(revocation_pubkey), key: revocation_key, is_htlc: true, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height, Vec::new())); }
13861393
}
13871394
txn_to_broadcast.push(single_htlc_tx);
13881395
}
@@ -1457,13 +1464,17 @@ impl ChannelMonitor {
14571464

14581465
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
14591466

1467+
let mut buddy_inputs = Vec::new();
1468+
for inp in spend_tx.input.iter() {
1469+
buddy_inputs.push(inp.previous_output.clone());
1470+
}
14601471
for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
14611472
let (redeemscript, revocation_key) = sign_input!(sighash_parts, input, info.0, info.1);
14621473
let height_timer = Self::get_height_timer(height, info.2);
14631474
log_trace!(self, "Outpoint {}:{} is under claiming process, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", input.previous_output.vout, input.previous_output.txid, height_timer);
14641475
match self.our_claim_txn_waiting_first_conf.entry(input.previous_output.clone()) {
14651476
hash_map::Entry::Occupied(_) => {},
1466-
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: if info.0.is_some() { Some(revocation_pubkey) } else { None }, key: revocation_key, is_htlc: if info.0.is_some() { true } else { false }, amount: info.1 }, used_feerate, if !info.0.is_some() { height + info.2 } else { info.2 }, height)); }
1477+
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: if info.0.is_some() { Some(revocation_pubkey) } else { None }, key: revocation_key, is_htlc: if info.0.is_some() { true } else { false }, amount: info.1 }, used_feerate, if !info.0.is_some() { height + info.2 } else { info.2 }, height, buddy_inputs.clone())); }
14671478
}
14681479
}
14691480
assert!(predicted_weight >= spend_tx.get_weight());
@@ -1653,7 +1664,7 @@ impl ChannelMonitor {
16531664
log_trace!(self, "Outpoint {}:{} is under claiming process, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", single_htlc_tx.input[0].previous_output.vout, single_htlc_tx.input[0].previous_output.txid, height_timer);
16541665
match self.our_claim_txn_waiting_first_conf.entry(single_htlc_tx.input[0].previous_output.clone()) {
16551666
hash_map::Entry::Occupied(_) => {},
1656-
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
1667+
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height, Vec::new())); }
16571668
}
16581669
txn_to_broadcast.push(single_htlc_tx);
16591670
}
@@ -1691,7 +1702,7 @@ impl ChannelMonitor {
16911702
log_trace!(self, "Outpoint {}:{} is under claiming process, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", timeout_tx.input[0].previous_output.vout, timeout_tx.input[0].previous_output.txid, height_timer);
16921703
match self.our_claim_txn_waiting_first_conf.entry(timeout_tx.input[0].previous_output.clone()) {
16931704
hash_map::Entry::Occupied(_) => {},
1694-
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script : redeemscript, key: htlc_key, preimage: None, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height)); }
1705+
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script : redeemscript, key: htlc_key, preimage: None, amount: htlc.amount_msat / 1000 }, used_feerate, htlc.cltv_expiry, height, Vec::new())); }
16951706
}
16961707
}
16971708
txn_to_broadcast.push(timeout_tx);
@@ -1721,13 +1732,17 @@ impl ChannelMonitor {
17211732

17221733
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
17231734

1735+
let mut buddy_inputs = Vec::new();
1736+
for inp in spend_tx.input.iter() {
1737+
buddy_inputs.push(inp.previous_output.clone());
1738+
}
17241739
for (input, info) in spend_tx.input.iter_mut().zip(inputs_info.iter()) {
17251740
let (redeemscript, htlc_key) = sign_input!(sighash_parts, input, info.1, (info.0).0.to_vec());
17261741
let height_timer = Self::get_height_timer(height, info.2);
17271742
log_trace!(self, "Outpoint {}:{} is under claiming process, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", input.previous_output.vout, input.previous_output.txid, height_timer);
17281743
match self.our_claim_txn_waiting_first_conf.entry(input.previous_output.clone()) {
17291744
hash_map::Entry::Occupied(_) => {},
1730-
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*(info.0)), amount: info.1}, used_feerate, info.2, height)); }
1745+
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::RemoteHTLC { script: redeemscript, key: htlc_key, preimage: Some(*(info.0)), amount: info.1}, used_feerate, info.2, height, buddy_inputs.clone())); }
17311746
}
17321747
}
17331748
assert!(predicted_weight >= spend_tx.get_weight());
@@ -1835,13 +1850,13 @@ impl ChannelMonitor {
18351850
log_trace!(self, "Outpoint {}:{} is under claiming process, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", spend_tx.input[0].previous_output.vout, spend_tx.input[0].previous_output.txid, height_timer);
18361851
match self.our_claim_txn_waiting_first_conf.entry(spend_tx.input[0].previous_output.clone()) {
18371852
hash_map::Entry::Occupied(_) => {},
1838-
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: None, key: revocation_key, is_htlc: false, amount: tx.output[0].value }, used_feerate, height + self.our_to_self_delay as u32, height)); }
1853+
hash_map::Entry::Vacant(entry) => { entry.insert((height_timer, TxMaterial::Revoked { script: redeemscript, pubkey: None, key: revocation_key, is_htlc: false, amount: tx.output[0].value }, used_feerate, height + self.our_to_self_delay as u32, height, Vec::new())); }
18391854
}
18401855
(Some(spend_tx), Some(SpendableOutputDescriptor::StaticOutput { outpoint, output }))
18411856
} else { (None, None) }
18421857
}
18431858

1844-
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx, per_commitment_point: &Option<PublicKey>, delayed_payment_base_key: &Option<SecretKey>, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, Vec<TxOut>, Vec<(BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32))>) {
1859+
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx, per_commitment_point: &Option<PublicKey>, delayed_payment_base_key: &Option<SecretKey>, height: u32) -> (Vec<Transaction>, Vec<SpendableOutputDescriptor>, Vec<TxOut>, Vec<(BitcoinOutPoint, (u32, TxMaterial, u64, u32, u32, Vec<BitcoinOutPoint>))>) {
18451860
let mut res = Vec::with_capacity(local_tx.htlc_outputs.len());
18461861
let mut spendable_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
18471862
let mut watch_outputs = Vec::with_capacity(local_tx.htlc_outputs.len());
@@ -1895,7 +1910,7 @@ impl ChannelMonitor {
18951910

18961911
add_dynamic_output!(htlc_timeout_tx, 0);
18971912
let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
1898-
pending_claims.push((htlc_timeout_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: None, amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height)));
1913+
pending_claims.push((htlc_timeout_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: None, amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height, Vec::new())));
18991914
res.push(htlc_timeout_tx);
19001915
} else {
19011916
if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
@@ -1915,7 +1930,7 @@ impl ChannelMonitor {
19151930

19161931
add_dynamic_output!(htlc_success_tx, 0);
19171932
let height_timer = Self::get_height_timer(height, htlc.cltv_expiry);
1918-
pending_claims.push((htlc_success_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height)));
1933+
pending_claims.push((htlc_success_tx.input[0].previous_output.clone(), (height_timer, TxMaterial::LocalHTLC { script: htlc_script, sigs: (*their_sig, *our_sig), preimage: Some(*payment_preimage), amount: htlc.amount_msat / 1000}, 0, htlc.cltv_expiry, height, Vec::new())));
19191934
res.push(htlc_success_tx);
19201935
}
19211936
}
@@ -2210,9 +2225,10 @@ impl ChannelMonitor {
22102225
bump_candidates.push((claimed_outpoint, claim_tx_data));
22112226
}
22122227
}
2213-
for candidate in bump_candidates {
2214-
if let Some((new_timer, bumped_tx, feerate)) = self.bump_claim_tx(candidate.0, (candidate.1).0, &(candidate.1).1, (candidate.1).2, fee_estimator) {
2215-
pending_claims.push((*candidate.0, (new_timer, (candidate.1).1.clone() , feerate, (candidate.1).3, (candidate.1).4)));
2228+
// TODO: retrieve and sort aggregate claim outpoints based on last_txid
2229+
for (outpoint, (height_timer, tx_material, old_feerate, timelock_expiration, seen_height, buddy_inputs)) in bump_candidates.drain(..) {
2230+
if let Some((new_timer, bumped_tx, feerate)) = self.bump_claim_tx(outpoint, *height_timer, &tx_material, *old_feerate, fee_estimator) {
2231+
pending_claims.push((*outpoint, (new_timer, *tx_material, feerate, *timelock_expiration, *seen_height, *buddy_inputs)));
22162232
bumped_txn.append(&mut vec![bumped_tx]);
22172233
}
22182234
}
@@ -2799,7 +2815,12 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
27992815
let last_fee = Readable::read(reader)?;
28002816
let timelock_expiration = Readable::read(reader)?;
28012817
let height = Readable::read(reader)?;
2802-
our_claim_txn_waiting_first_conf.insert(outpoint, (height_target, tx_material, last_fee, timelock_expiration, height));
2818+
let buddy_len: u64 = Readable::read(reader)?;
2819+
let mut buddy_inputs = Vec::new();
2820+
for _ in 0..buddy_len {
2821+
buddy_inputs.push(Readable::read(reader)?);
2822+
}
2823+
our_claim_txn_waiting_first_conf.insert(outpoint, (height_target, tx_material, last_fee, timelock_expiration, height, buddy_inputs));
28032824
}
28042825

28052826
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;

0 commit comments

Comments
 (0)