Skip to content

Commit 1c864ac

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 034c3e2 commit 1c864ac

File tree

2 files changed

+142
-45
lines changed

2 files changed

+142
-45
lines changed

src/ln/channelmonitor.rs

Lines changed: 122 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,23 @@ enum InputDescriptors {
352352
RevokedOutput, // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
353353
}
354354

355+
/// Upon discovering of some classes of onchain tx by ChannelMonitor, we may have to take actions on it
356+
/// once they mature to enough confirmations (HTLC_FAIL_ANTI_REORG_DELAY)
357+
#[derive(Clone, PartialEq)]
358+
enum OnchainEvent {
359+
/// Outpoint under claim process by our own tx, once this one get enough confirmations, we remove it from
360+
/// bump-txn candidate buffer.
361+
Claim {
362+
outpoint: BitcoinOutPoint,
363+
},
364+
/// HTLC output getting solved by a timeout, at maturation we pass upstream payment source information to solve
365+
/// inbound HTLC in backward channel. Note, in case of preimage, we pass info to upstream without delay as we can
366+
/// only win from it, so it's never an OnchainEvent
367+
HTLCUpdate {
368+
htlc_update: (HTLCSource, PaymentHash),
369+
},
370+
}
371+
355372
const SERIALIZATION_VERSION: u8 = 1;
356373
const MIN_SERIALIZATION_VERSION: u8 = 1;
357374

@@ -402,7 +419,10 @@ pub struct ChannelMonitor {
402419

403420
destination_script: Script,
404421

405-
htlc_updated_waiting_threshold_conf: HashMap<u32, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>>,
422+
// Used to track onchain events, i.e transactions parts of channels confirmed on chain, on which
423+
// we have to take actions once they reach enough confs. Key is a block height timer, i.e we enforce
424+
// actions when we receive a block with given height. Actions depend on OnchainEvent type.
425+
onchain_events_waiting_threshold_conf: HashMap<u32, Vec<OnchainEvent>>,
406426

407427
// We simply modify last_block_hash in Channel's block_connected so that serialization is
408428
// consistent but hopefully the users' copy handles block_connected in a consistent way.
@@ -466,7 +486,7 @@ impl PartialEq for ChannelMonitor {
466486
self.current_local_signed_commitment_tx != other.current_local_signed_commitment_tx ||
467487
self.payment_preimages != other.payment_preimages ||
468488
self.destination_script != other.destination_script ||
469-
self.htlc_updated_waiting_threshold_conf != other.htlc_updated_waiting_threshold_conf
489+
self.onchain_events_waiting_threshold_conf != other.onchain_events_waiting_threshold_conf
470490
{
471491
false
472492
} else {
@@ -516,7 +536,7 @@ impl ChannelMonitor {
516536
payment_preimages: HashMap::new(),
517537
destination_script: destination_script,
518538

519-
htlc_updated_waiting_threshold_conf: HashMap::new(),
539+
onchain_events_waiting_threshold_conf: HashMap::new(),
520540

521541
last_block_hash: Default::default(),
522542
secp_ctx: Secp256k1::new(),
@@ -1025,14 +1045,22 @@ impl ChannelMonitor {
10251045
self.last_block_hash.write(writer)?;
10261046
self.destination_script.write(writer)?;
10271047

1028-
writer.write_all(&byte_utils::be64_to_array(self.htlc_updated_waiting_threshold_conf.len() as u64))?;
1029-
for (ref target, ref updates) in self.htlc_updated_waiting_threshold_conf.iter() {
1048+
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
1049+
for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
10301050
writer.write_all(&byte_utils::be32_to_array(**target))?;
1031-
writer.write_all(&byte_utils::be64_to_array(updates.len() as u64))?;
1032-
for ref update in updates.iter() {
1033-
update.0.write(writer)?;
1034-
update.1.write(writer)?;
1035-
update.2.write(writer)?;
1051+
writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
1052+
for ev in events.iter() {
1053+
match *ev {
1054+
OnchainEvent::Claim { ref outpoint } => {
1055+
writer.write_all(&[0; 1])?;
1056+
outpoint.write(writer)?;
1057+
},
1058+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1059+
writer.write_all(&[1; 1])?;
1060+
htlc_update.0.write(writer)?;
1061+
htlc_update.1.write(writer)?;
1062+
}
1063+
}
10361064
}
10371065
}
10381066

@@ -1271,14 +1299,21 @@ impl ChannelMonitor {
12711299
for &(ref htlc, ref source_option) in outpoints.iter() {
12721300
if let &Some(ref source) = source_option {
12731301
log_info!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction, waiting for confirmation (at height {})", log_bytes!(htlc.payment_hash.0), $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
1274-
match self.htlc_updated_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1302+
match self.onchain_events_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
12751303
hash_map::Entry::Occupied(mut entry) => {
12761304
let e = entry.get_mut();
1277-
e.retain(|ref update| update.0 != **source);
1278-
e.push(((**source).clone(), None, htlc.payment_hash.clone()));
1305+
e.retain(|ref event| {
1306+
match **event {
1307+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1308+
return htlc_update.0 != **source
1309+
},
1310+
_ => return true
1311+
}
1312+
});
1313+
e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
12791314
}
12801315
hash_map::Entry::Vacant(entry) => {
1281-
entry.insert(vec![((**source).clone(), None, htlc.payment_hash.clone())]);
1316+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
12821317
}
12831318
}
12841319
}
@@ -1361,14 +1396,21 @@ impl ChannelMonitor {
13611396
}
13621397
}
13631398
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);
1364-
match self.htlc_updated_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1399+
match self.onchain_events_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
13651400
hash_map::Entry::Occupied(mut entry) => {
13661401
let e = entry.get_mut();
1367-
e.retain(|ref update| update.0 != **source);
1368-
e.push(((**source).clone(), None, htlc.payment_hash.clone()));
1402+
e.retain(|ref event| {
1403+
match **event {
1404+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1405+
return htlc_update.0 != **source
1406+
},
1407+
_ => return true
1408+
}
1409+
});
1410+
e.push(OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())});
13691411
}
13701412
hash_map::Entry::Vacant(entry) => {
1371-
entry.insert(vec![((**source).clone(), None, htlc.payment_hash.clone())]);
1413+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ((**source).clone(), htlc.payment_hash.clone())}]);
13721414
}
13731415
}
13741416
}
@@ -1745,16 +1787,23 @@ impl ChannelMonitor {
17451787
let mut watch_outputs = Vec::new();
17461788

17471789
macro_rules! wait_threshold_conf {
1748-
($height: expr, $source: expr, $update: expr, $commitment_tx: expr, $payment_hash: expr) => {
1749-
log_info!(self, "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation (at height{})", log_bytes!($payment_hash.0), $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
1750-
match self.htlc_updated_waiting_threshold_conf.entry($height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
1790+
($height: expr, $source: expr, $commitment_tx: expr, $payment_hash: expr) => {
1791+
log_trace!(self, "Failing HTLC with payment_hash {} from {} local commitment tx due to broadcast of transaction, waiting confirmation (at height{})", log_bytes!($payment_hash.0), $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
1792+
match self.onchain_events_waiting_threshold_conf.entry($height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
17511793
hash_map::Entry::Occupied(mut entry) => {
17521794
let e = entry.get_mut();
1753-
e.retain(|ref update| update.0 != $source);
1754-
e.push(($source, $update, $payment_hash));
1795+
e.retain(|ref event| {
1796+
match **event {
1797+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
1798+
return htlc_update.0 != $source
1799+
},
1800+
_ => return true
1801+
}
1802+
});
1803+
e.push(OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)});
17551804
}
17561805
hash_map::Entry::Vacant(entry) => {
1757-
entry.insert(vec![($source, $update, $payment_hash)]);
1806+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: ($source, $payment_hash)}]);
17581807
}
17591808
}
17601809
}
@@ -1805,7 +1854,7 @@ impl ChannelMonitor {
18051854
for &(ref htlc, _, ref source) in &$local_tx.htlc_outputs {
18061855
if htlc.transaction_output_index.is_none() {
18071856
if let &Some(ref source) = source {
1808-
wait_threshold_conf!(height, source.clone(), None, "lastest", htlc.payment_hash.clone());
1857+
wait_threshold_conf!(height, source.clone(), "lastest", htlc.payment_hash.clone());
18091858
}
18101859
}
18111860
}
@@ -1956,19 +2005,27 @@ impl ChannelMonitor {
19562005
}
19572006
}
19582007
}
1959-
if let Some(updates) = self.htlc_updated_waiting_threshold_conf.remove(&height) {
1960-
for update in updates {
1961-
log_trace!(self, "HTLC {} failure update has get enough confirmation to be pass upstream", log_bytes!((update.2).0));
1962-
htlc_updated.push(update);
2008+
if let Some(events) = self.onchain_events_waiting_threshold_conf.remove(&height) {
2009+
for ev in events {
2010+
match ev {
2011+
OnchainEvent::Claim { outpoint: _ } => {
2012+
},
2013+
OnchainEvent::HTLCUpdate { htlc_update } => {
2014+
log_trace!(self, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
2015+
htlc_updated.push((htlc_update.0, None, htlc_update.1));
2016+
},
2017+
}
19632018
}
19642019
}
19652020
self.last_block_hash = block_hash.clone();
19662021
(watch_outputs, spendable_outputs, htlc_updated)
19672022
}
19682023

19692024
fn block_disconnected(&mut self, height: u32, block_hash: &Sha256dHash) {
1970-
if let Some(_) = self.htlc_updated_waiting_threshold_conf.remove(&(height + HTLC_FAIL_ANTI_REORG_DELAY - 1)) {
1971-
//We discard htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
2025+
if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + HTLC_FAIL_ANTI_REORG_DELAY - 1)) {
2026+
//We may discard:
2027+
//- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
2028+
//- our claim tx on a commitment tx output
19722029
}
19732030
self.last_block_hash = block_hash.clone();
19742031
}
@@ -2150,14 +2207,21 @@ impl ChannelMonitor {
21502207
htlc_updated.push((source, Some(payment_preimage), payment_hash));
21512208
} else {
21522209
log_info!(self, "Failing HTLC with payment_hash {} timeout by a spend tx, waiting for confirmation (at height{})", log_bytes!(payment_hash.0), height + HTLC_FAIL_ANTI_REORG_DELAY - 1);
2153-
match self.htlc_updated_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
2210+
match self.onchain_events_waiting_threshold_conf.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
21542211
hash_map::Entry::Occupied(mut entry) => {
21552212
let e = entry.get_mut();
2156-
e.retain(|ref update| update.0 != source);
2157-
e.push((source, None, payment_hash.clone()));
2213+
e.retain(|ref event| {
2214+
match **event {
2215+
OnchainEvent::HTLCUpdate { ref htlc_update } => {
2216+
return htlc_update.0 != source
2217+
},
2218+
_ => return true
2219+
}
2220+
});
2221+
e.push(OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)});
21582222
}
21592223
hash_map::Entry::Vacant(entry) => {
2160-
entry.insert(vec![(source, None, payment_hash)]);
2224+
entry.insert(vec![OnchainEvent::HTLCUpdate { htlc_update: (source, payment_hash)}]);
21612225
}
21622226
}
21632227
}
@@ -2380,18 +2444,31 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
23802444
let destination_script = Readable::read(reader)?;
23812445

23822446
let waiting_threshold_conf_len: u64 = Readable::read(reader)?;
2383-
let mut htlc_updated_waiting_threshold_conf = HashMap::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
2447+
let mut onchain_events_waiting_threshold_conf = HashMap::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128));
23842448
for _ in 0..waiting_threshold_conf_len {
23852449
let height_target = Readable::read(reader)?;
2386-
let updates_len: u64 = Readable::read(reader)?;
2387-
let mut updates = Vec::with_capacity(cmp::min(updates_len as usize, MAX_ALLOC_SIZE / 128));
2388-
for _ in 0..updates_len {
2389-
let htlc_source = Readable::read(reader)?;
2390-
let preimage = Readable::read(reader)?;
2391-
let hash = Readable::read(reader)?;
2392-
updates.push((htlc_source, preimage, hash));
2450+
let events_len: u64 = Readable::read(reader)?;
2451+
let mut events = Vec::with_capacity(cmp::min(events_len as usize, MAX_ALLOC_SIZE / 128));
2452+
for _ in 0..events_len {
2453+
let ev = match <u8 as Readable<R>>::read(reader)? {
2454+
0 => {
2455+
let outpoint = Readable::read(reader)?;
2456+
OnchainEvent::Claim {
2457+
outpoint
2458+
}
2459+
},
2460+
1 => {
2461+
let htlc_source = Readable::read(reader)?;
2462+
let hash = Readable::read(reader)?;
2463+
OnchainEvent::HTLCUpdate {
2464+
htlc_update: (htlc_source, hash)
2465+
}
2466+
},
2467+
_ => return Err(DecodeError::InvalidValue),
2468+
};
2469+
events.push(ev);
23932470
}
2394-
htlc_updated_waiting_threshold_conf.insert(height_target, updates);
2471+
onchain_events_waiting_threshold_conf.insert(height_target, events);
23952472
}
23962473

23972474
Ok((last_block_hash.clone(), ChannelMonitor {
@@ -2418,7 +2495,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
24182495

24192496
destination_script,
24202497

2421-
htlc_updated_waiting_threshold_conf,
2498+
onchain_events_waiting_threshold_conf,
24222499

24232500
last_block_hash,
24242501
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)