Skip to content

Commit d3967e7

Browse files
author
Antoine Riard
committed
Move htlc_updated_waiting_threshold_conf to an OnchainEvent model
We need also to track claim tx until their maturation to know when we may safely remove them from could-be-bumped-txn buffer
1 parent c19a2be commit d3967e7

File tree

2 files changed

+141
-45
lines changed

2 files changed

+141
-45
lines changed

src/ln/channelmonitor.rs

Lines changed: 121 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ struct LocalSignedTx {
327327
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>,
328328
}
329329

330+
/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
331+
/// once they mature to enough confirmations (HTLC_FAIL_ANTI_REORG_DELAY)
332+
#[derive(Clone, PartialEq)]
333+
enum OnchainEvent {
334+
/// Outpoint under claim process by our own tx, at maturation we remote it from our_claim_txn_waiting_first_conf
335+
Claim {
336+
outpoint: BitcoinOutPoint,
337+
},
338+
/// HTLC output getting solved by a timeout, at maturation we pass upstream payment source information to solve
339+
/// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can
340+
/// only win from it, so it's never an OnchainEvent
341+
HTLCUpdate {
342+
htlc_update: (HTLCSource, PaymentHash),
343+
},
344+
}
345+
330346
const SERIALIZATION_VERSION: u8 = 1;
331347
const MIN_SERIALIZATION_VERSION: u8 = 1;
332348

@@ -377,7 +393,8 @@ pub struct ChannelMonitor {
377393

378394
destination_script: Script,
379395

380-
htlc_updated_waiting_threshold_conf: HashMap<u32, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>>,
396+
397+
onchain_events_waiting_threshold_conf: HashMap<u32, Vec<OnchainEvent>>,
381398

382399
// We simply modify last_block_hash in Channel's block_connected so that serialization is
383400
// consistent but hopefully the users' copy handles block_connected in a consistent way.
@@ -409,7 +426,7 @@ impl PartialEq for ChannelMonitor {
409426
self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
410427
self.payment_preimages != other.payment_preimages ||
411428
self.destination_script != other.destination_script ||
412-
self.htlc_updated_waiting_threshold_conf != other.htlc_updated_waiting_threshold_conf
429+
self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf
413430
{
414431
false
415432
} else {
@@ -459,7 +476,7 @@ impl ChannelMonitor {
459476
payment_preimages: HashMap::new(),
460477
destination_script: destination_script,
461478

462-
htlc_updated_waiting_threshold_conf: HashMap::new(),
479+
onchain_events_waiting_threshold_conf: HashMap::new(),
463480

464481
last_block_hash: Default::default(),
465482
secp_ctx: Secp256k1::new(),
@@ -938,14 +955,22 @@ impl ChannelMonitor {
938955
self.last_block_hash.write(writer)?;
939956
self.destination_script.write(writer)?;
940957

941-
writer.write_all(&byte_utils::be64_to_array(self.htlc_updated_waiting_threshold_conf.len() as u64))?;
942-
for (ref target, ref updates) in self.htlc_updated_waiting_threshold_conf.iter() {
958+
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
959+
for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
943960
writer.write_all(&byte_utils::be32_to_array(**target))?;
944-
writer.write_all(&byte_utils::be64_to_array(updates.len() as u64))?;
945-
for ref update in updates.iter() {
946-
update.0.write(writer)?;
947-
update.1.write(writer)?;
948-
update.2.write(writer)?;
961+
writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
962+
for ev in events.iter() {
963+
match *ev {
964+
OnchainEvent::Claim { ref outpoint } => {
965+
writer.write_all(&[0; 1])?;
966+
outpoint.write(writer)?;
967+
},
968+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
969+
writer.write_all(&[1; 1])?;
970+
htlc_update.0.write(writer)?;
971+
htlc_update.1.write(writer)?;
972+
}
973+
}
949974
}
950975
}
951976

@@ -1177,14 +1202,21 @@ impl ChannelMonitor {
11771202
for &(ref htlc, ref source_option) in outpoints.iter() {
11781203
if let &Some(ref source) = source_option {
11791204
log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction, waiting confirmation until {} height", log_bytes!(htlc.payment_hash.0), $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
1180-
match self.htlc_updated_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1205+
match self.onchain_events_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
11811206
hash_map::Entry::Occupied(mut entry) => {
11821207
let e = entry.get_mut();
1183-
e.retain(|ref update| update.0 != **source);
1184-
e.push(((**source).clone(), None, htlc.payment_hash.clone()));
1208+
e.retain(|ref event| {
1209+
match **event {
1210+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1211+
return htlc_update.0 != **source
1212+
},
1213+
_ => return true
1214+
}
1215+
});
1216+
e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
11851217
}
11861218
hash_map::Entry::Vacant(entry) => {
1187-
entry.insert(vec![((**source).clone(), None, htlc.payment_hash.clone())]);
1219+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
11881220
}
11891221
}
11901222
}
@@ -1261,14 +1293,21 @@ impl ChannelMonitor {
12611293
}
12621294
}
12631295
log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
1264-
match self.htlc_updated_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1296+
match self.onchain_events_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
12651297
hash_map::Entry::Occupied(mut entry) => {
12661298
let e = entry.get_mut();
1267-
e.retain(|ref update| update.0 != **source);
1268-
e.push(((**source).clone(), None, htlc.payment_hash.clone()));
1299+
e.retain(|ref event| {
1300+
match **event {
1301+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1302+
return htlc_update.0 != **source
1303+
},
1304+
_ => return true
1305+
}
1306+
});
1307+
e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
12691308
}
12701309
hash_map::Entry::Vacant(entry) => {
1271-
entry.insert(vec![((**source).clone(), None, htlc.payment_hash.clone())]);
1310+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
12721311
}
12731312
}
12741313
}
@@ -1629,16 +1668,23 @@ impl ChannelMonitor {
16291668
let mut watch_outputs = Vec::new();
16301669

16311670
macro_rules! wait_threshold_conf {
1632-
($height: expr, $source: expr, $update: expr, $commitment_tx: expr, $payment_hash: expr) => {
1671+
($height: expr, $source: expr, $commitment_tx: expr, $payment_hash: expr) => {
16331672
log_trace!(self, "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation until {} height", log_bytes!($payment_hash.0), $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
1634-
match self.htlc_updated_waiting_threshold_conf.entry($height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1673+
match self.onchain_events_waiting_threshold_conf.entry($height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
16351674
hash_map::Entry::Occupied(mut entry) => {
16361675
let e = entry.get_mut();
1637-
e.retain(|ref update| update.0 != $source);
1638-
e.push(($source, $update, $payment_hash));
1676+
e.retain(|ref event| {
1677+
match **event {
1678+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1679+
return htlc_update.0 != $source
1680+
},
1681+
_ => return true
1682+
}
1683+
});
1684+
e.push(OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)});
16391685
}
16401686
hash_map::Entry::Vacant(entry) => {
1641-
entry.insert(vec![($source, $update, $payment_hash)]);
1687+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)}]);
16421688
}
16431689
}
16441690
}
@@ -1656,7 +1702,7 @@ impl ChannelMonitor {
16561702
for &(ref htlc, _, ref source) in &local_tx.htlc_outputs {
16571703
if htlc.transaction_output_index.is_none() {
16581704
if let &Some(ref source) = source {
1659-
wait_threshold_conf!(height, source.clone(), None, "lastest", htlc.payment_hash.clone());
1705+
wait_threshold_conf!(height, source.clone(), "lastest", htlc.payment_hash.clone());
16601706
}
16611707
}
16621708
}
@@ -1676,7 +1722,7 @@ impl ChannelMonitor {
16761722
for &(ref htlc, _, ref source) in &local_tx.htlc_outputs {
16771723
if htlc.transaction_output_index.is_none() {
16781724
if let &Some(ref source) = source {
1679-
wait_threshold_conf!(height, source.clone(), None, "previous", htlc.payment_hash.clone());
1725+
wait_threshold_conf!(height, source.clone(), "previous", htlc.payment_hash.clone());
16801726
}
16811727
}
16821728
}
@@ -1827,19 +1873,29 @@ impl ChannelMonitor {
18271873
}
18281874
}
18291875
}
1830-
if let Some(updates) = self.htlc_updated_waiting_threshold_conf.remove(&height) {
1831-
for update in updates {
1832-
log_trace!(self, "HTLC {} failure update has get enough confirmation to be pass upstream", log_bytes!((update.2).0));
1833-
htlc_updated.push(update);
1876+
if let Some(events) = self.onchain_events_waiting_threshold_conf.remove(&height) {
1877+
for ev in events {
1878+
match ev {
1879+
OnchainEvent::Claim { outpoint } => {
1880+
},
1881+
OnchainEvent::HTLCUpdate { htlc_update } => {
1882+
log_trace!(self, "HTLC {} failure update has get enough confirmation to be pass upstream", log_bytes!((htlc_update.1).0));
1883+
htlc_updated.push((htlc_update.0, None, htlc_update.1));
1884+
},
1885+
}
1886+
}
1887+
}
18341888
}
18351889
}
18361890
self.last_block_hash = block_hash.clone();
18371891
(watch_outputs, spendable_outputs, htlc_updated)
18381892
}
18391893

18401894
fn block_disconnected(&mut self, height: u32, block_hash: &Sha256dHash) {
1841-
if let Some(_) = self.htlc_updated_waiting_threshold_conf.remove(&(height + HTLC_FAIL_ANTI_REORG_DELAY - 1)) {
1842-
//We discard htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
1895+
if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + HTLC_FAIL_ANTI_REORG_DELAY - 1)) {
1896+
//We may discard:
1897+
//- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
1898+
//- our claim tx on a commitment tx output
18431899
}
18441900
self.last_block_hash = block_hash.clone();
18451901
}
@@ -2021,14 +2077,21 @@ impl ChannelMonitor {
20212077
htlc_updated.push((source, Some(payment_preimage), payment_hash));
20222078
} else {
20232079
log_trace!(self, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting confirmation until {} height", log_bytes!(payment_hash.0), height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
2024-
match self.htlc_updated_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
2080+
match self.onchain_events_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
20252081
hash_map::Entry::Occupied(mut entry) => {
20262082
let e = entry.get_mut();
2027-
e.retain(|ref update| update.0 != source);
2028-
e.push((source, None, payment_hash.clone()));
2083+
e.retain(|ref event| {
2084+
match **event {
2085+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
2086+
return htlc_update.0 != source
2087+
},
2088+
_ => return true
2089+
}
2090+
});
2091+
e.push(OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)});
20292092
}
20302093
hash_map::Entry::Vacant(entry) => {
2031-
entry.insert(vec![(source, None, payment_hash)]);
2094+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)}]);
20322095
}
20332096
}
20342097
}
@@ -2251,18 +2314,31 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
22512314
let destination_script = Readable::read(reader)?;
22522315

22532316
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
2254-
let mut htlc_updated_waiting_threshold_conf = HashMap::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
2317+
let mut onchain_events_waiting_threshold_conf = HashMap::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
22552318
for _ in 0..waiting_threshold_conf_len {
22562319
let height_target = Readable::read(reader)?;
2257-
let updates_len: u64 = Readable::read(reader)?;
2258-
let mut updates = Vec::with_capacity(cmp::min(updates_len as usize, MAX_ALLOC_SIZE / 128));
2259-
for _ in 0..updates_len {
2260-
let htlc_source = Readable::read(reader)?;
2261-
let preimage = Readable::read(reader)?;
2262-
let hash = Readable::read(reader)?;
2263-
updates.push((htlc_source, preimage, hash));
2320+
let events_len: u64 = Readable::read(reader)?;
2321+
let mut events = Vec::with_capacity(cmp::min(events_len as usize, MAX_ALLOC_SIZE / 128));
2322+
for _ in 0..events_len {
2323+
let ev = match <u8 as Readable<R>>::read(reader)? {
2324+
0 => {
2325+
let outpoint = Readable::read(reader)?;
2326+
OnchainEvent::Claim {
2327+
outpoint
2328+
}
2329+
},
2330+
1 => {
2331+
let htlc_source = Readable::read(reader)?;
2332+
let hash = Readable::read(reader)?;
2333+
OnchainEvent::HTLCUpdate {
2334+
htlc_update: (htlc_source, hash)
2335+
}
2336+
},
2337+
_ => return Err(DecodeError::InvalidValue),
2338+
};
2339+
events.push(ev);
22642340
}
2265-
htlc_updated_waiting_threshold_conf.insert(height_target, updates);
2341+
onchain_events_waiting_threshold_conf.insert(height_target, events);
22662342
}
22672343

22682344
Ok((last_block_hash.clone(), ChannelMonitor {
@@ -2289,7 +2365,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
22892365

22902366
destination_script,
22912367

2292-
htlc_updated_waiting_threshold_conf,
2368+
onchain_events_waiting_threshold_conf,
22932369

22942370
last_block_hash,
22952371
secp_ctx,

src/util/ser.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::hash::Hash;
99
use secp256k1::Signature;
1010
use secp256k1::key::{PublicKey, SecretKey};
1111
use bitcoin::blockdata::script::Script;
12+
use bitcoin::blockdata::transaction::OutPoint;
1213
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1314
use std::marker::Sized;
1415
use ln::msgs::DecodeError;
@@ -422,3 +423,22 @@ impl<R, T> Readable<R> for Option<T>
422423
}
423424
}
424425
}
426+
427+
impl Writeable for OutPoint {
428+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
429+
self.txid.write(w)?;
430+
self.vout.write(w)?;
431+
Ok(())
432+
}
433+
}
434+
435+
impl<R: Read> Readable<R> for OutPoint {
436+
fn read(r: &mut R) -> Result<Self, DecodeError> {
437+
let txid = Readable::read(r)?;
438+
let vout = Readable::read(r)?;
439+
Ok(OutPoint {
440+
txid,
441+
vout,
442+
})
443+
}
444+
}

0 commit comments

Comments
 (0)