Skip to content

Commit 6e2c9b1

Browse files
committed
Ensure similar formatting is applied to same code block
The formatting was different sometimes `0x4000 | 15`, sometimes `0x4000|15` making it difficult to search for all occurrences of this specific message failure. I went for using rustfmt instead of doing more manual adjustments.
1 parent 236887d commit 6e2c9b1

File tree

1 file changed

+146
-98
lines changed

1 file changed

+146
-98
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 146 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,67 +1713,84 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
17131713
incoming_packet_shared_secret: incoming_shared_secret,
17141714
};
17151715

1716-
let mut total_value = 0;
1717-
let payment_secret_opt =
1718-
if let &Some(ref data) = &payment_data { Some(data.payment_secret.clone()) } else { None };
1719-
let htlcs = channel_state.claimable_htlcs.entry((payment_hash, payment_secret_opt))
1720-
.or_insert(Vec::new());
1721-
htlcs.push(ClaimableHTLC {
1722-
prev_hop,
1723-
value: amt_to_forward,
1724-
payment_data: payment_data.clone(),
1725-
});
1726-
if let &Some(ref data) = &payment_data {
1727-
for htlc in htlcs.iter() {
1728-
total_value += htlc.value;
1729-
if htlc.payment_data.as_ref().unwrap().total_msat != data.total_msat {
1730-
total_value = msgs::MAX_VALUE_MSAT;
1731-
}
1716+
let mut total_value = 0;
1717+
let payment_secret_opt = if let &Some(ref data) = &payment_data {
1718+
Some(data.payment_secret.clone())
1719+
} else {
1720+
None
1721+
};
1722+
let htlcs = channel_state
1723+
.claimable_htlcs
1724+
.entry((payment_hash, payment_secret_opt))
1725+
.or_insert(Vec::new());
1726+
htlcs.push(ClaimableHTLC {
1727+
prev_hop,
1728+
value: amt_to_forward,
1729+
payment_data: payment_data.clone(),
1730+
});
1731+
if let &Some(ref data) = &payment_data {
1732+
for htlc in htlcs.iter() {
1733+
total_value += htlc.value;
1734+
if htlc.payment_data.as_ref().unwrap().total_msat
1735+
!= data.total_msat
1736+
{
1737+
total_value = msgs::MAX_VALUE_MSAT;
1738+
}
17321739
if total_value >= msgs::MAX_VALUE_MSAT { break; }
1733-
}
1740+
}
17341741
if total_value >= msgs::MAX_VALUE_MSAT || total_value > data.total_msat {
1735-
for htlc in htlcs.iter() {
1736-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1737-
htlc_msat_height_data.extend_from_slice(
1738-
&byte_utils::be32_to_array(
1739-
self.latest_block_height.load(Ordering::Acquire)
1740-
as u32,
1741-
),
1742-
);
1743-
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
1744-
short_channel_id: htlc.prev_hop.short_channel_id,
1745-
htlc_id: htlc.prev_hop.htlc_id,
1746-
incoming_packet_shared_secret: htlc.prev_hop.incoming_packet_shared_secret,
1747-
}), payment_hash,
1748-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data }
1749-
));
1750-
}
1751-
} else if total_value == data.total_msat {
1752-
new_events.push(events::Event::PaymentReceived {
1753-
payment_hash: payment_hash,
1754-
payment_secret: Some(data.payment_secret),
1755-
amt: total_value,
1756-
});
1757-
}
1758-
} else {
1759-
new_events.push(events::Event::PaymentReceived {
1760-
payment_hash: payment_hash,
1761-
payment_secret: None,
1762-
amt: amt_to_forward,
1763-
});
1764-
}
1742+
for htlc in htlcs.iter() {
1743+
let mut htlc_msat_height_data =
1744+
byte_utils::be64_to_array(htlc.value).to_vec();
1745+
htlc_msat_height_data.extend_from_slice(
1746+
&byte_utils::be32_to_array(
1747+
self.latest_block_height.load(Ordering::Acquire)
1748+
as u32,
1749+
),
1750+
);
1751+
failed_forwards.push((
1752+
HTLCSource::PreviousHopData(HTLCPreviousHopData {
1753+
short_channel_id: htlc
1754+
.prev_hop
1755+
.short_channel_id,
1756+
htlc_id: htlc.prev_hop.htlc_id,
1757+
incoming_packet_shared_secret: htlc
1758+
.prev_hop
1759+
.incoming_packet_shared_secret,
1760+
}),
1761+
payment_hash,
1762+
HTLCFailReason::Reason {
1763+
failure_code: 0x4000 | 15,
1764+
data: htlc_msat_height_data,
1765+
},
1766+
));
1767+
}
1768+
} else if total_value == data.total_msat {
1769+
new_events.push(events::Event::PaymentReceived {
1770+
payment_hash: payment_hash,
1771+
payment_secret: Some(data.payment_secret),
1772+
amt: total_value,
1773+
});
1774+
}
1775+
} else {
1776+
new_events.push(events::Event::PaymentReceived {
1777+
payment_hash: payment_hash,
1778+
payment_secret: None,
1779+
amt: amt_to_forward,
1780+
});
1781+
}
17651782
},
1766-
HTLCForwardInfo::AddHTLC { .. } => {
1767-
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
1783+
HTLCForwardInfo::AddHTLC { .. } => {
1784+
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
17681785
},
1769-
HTLCForwardInfo::FailHTLC { .. } => {
1770-
panic!("Got pending fail of our own HTLC");
1771-
}
1772-
}
1773-
}
1774-
}
1775-
}
1776-
}
1786+
HTLCForwardInfo::FailHTLC { .. } => {
1787+
panic!("Got pending fail of our own HTLC");
1788+
}
1789+
}
1790+
}
1791+
}
1792+
}
1793+
}
17771794

17781795
for (htlc_source, payment_hash, failure_reason) in failed_forwards.drain(..) {
17791796
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment_hash, failure_reason);
@@ -1821,22 +1838,36 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
18211838
pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash, payment_secret: &Option<PaymentSecret>) -> bool {
18221839
let _ = self.total_consistency_lock.read().unwrap();
18231840

1824-
let mut channel_state = Some(self.channel_state.lock().unwrap());
1825-
let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(&(*payment_hash, *payment_secret));
1826-
if let Some(mut sources) = removed_source {
1827-
for htlc in sources.drain(..) {
1828-
if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
1829-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1830-
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
1831-
self.latest_block_height.load(Ordering::Acquire) as u32,
1832-
));
1833-
self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
1834-
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
1835-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data });
1836-
}
1837-
true
1838-
} else { false }
1839-
}
1841+
let mut channel_state = Some(self.channel_state.lock().unwrap());
1842+
let removed_source = channel_state
1843+
.as_mut()
1844+
.unwrap()
1845+
.claimable_htlcs
1846+
.remove(&(*payment_hash, *payment_secret));
1847+
if let Some(mut sources) = removed_source {
1848+
for htlc in sources.drain(..) {
1849+
if channel_state.is_none() {
1850+
channel_state = Some(self.channel_state.lock().unwrap());
1851+
}
1852+
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1853+
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
1854+
self.latest_block_height.load(Ordering::Acquire) as u32,
1855+
));
1856+
self.fail_htlc_backwards_internal(
1857+
channel_state.take().unwrap(),
1858+
HTLCSource::PreviousHopData(htlc.prev_hop),
1859+
payment_hash,
1860+
HTLCFailReason::Reason {
1861+
failure_code: 0x4000 | 15,
1862+
data: htlc_msat_height_data,
1863+
},
1864+
);
1865+
}
1866+
true
1867+
} else {
1868+
false
1869+
}
1870+
}
18401871

18411872
/// Fails an HTLC backwards to the sender of it to us.
18421873
/// Note that while we take a channel_state lock as input, we do *not* assume consistency here.
@@ -1994,36 +2025,53 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
19942025
}
19952026
}
19962027

1997-
let mut errs = Vec::new();
1998-
let mut claimed_any_htlcs = false;
1999-
for htlc in sources.drain(..) {
2000-
if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
2001-
if (is_mpp && !valid_mpp) || (!is_mpp && (htlc.value < expected_amount || htlc.value > expected_amount * 2)) {
2002-
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
2003-
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
2004-
self.latest_block_height.load(Ordering::Acquire) as u32,
2005-
));
2006-
self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
2007-
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
2008-
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data });
2009-
} else {
2028+
let mut errs = Vec::new();
2029+
let mut claimed_any_htlcs = false;
2030+
for htlc in sources.drain(..) {
2031+
if channel_state.is_none() {
2032+
channel_state = Some(self.channel_state.lock().unwrap());
2033+
}
2034+
if (is_mpp && !valid_mpp)
2035+
|| (!is_mpp
2036+
&& (htlc.value < expected_amount || htlc.value > expected_amount * 2))
2037+
{
2038+
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
2039+
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
2040+
self.latest_block_height.load(Ordering::Acquire) as u32,
2041+
));
2042+
self.fail_htlc_backwards_internal(
2043+
channel_state.take().unwrap(),
2044+
HTLCSource::PreviousHopData(htlc.prev_hop),
2045+
&payment_hash,
2046+
HTLCFailReason::Reason {
2047+
failure_code: 0x4000 | 15,
2048+
data: htlc_msat_height_data,
2049+
},
2050+
);
2051+
} else {
20102052
match self.claim_funds_from_hop(channel_state.as_mut().unwrap(), htlc.prev_hop, payment_preimage) {
2011-
Err(Some(e)) => {
2012-
if let msgs::ErrorAction::IgnoreError = e.1.err.action {
2013-
// We got a temporary failure updating monitor, but will claim the
2014-
// HTLC when the monitor updating is restored (or on chain).
2015-
log_error!(self, "Temporary failure claiming HTLC, treating as success: {}", e.1.err.err);
2016-
claimed_any_htlcs = true;
2053+
Err(Some(e)) => {
2054+
if let msgs::ErrorAction::IgnoreError = e.1.err.action {
2055+
// We got a temporary failure updating monitor, but will claim the
2056+
// HTLC when the monitor updating is restored (or on chain).
2057+
log_error!(
2058+
self,
2059+
"Temporary failure claiming HTLC, treating as success: {}",
2060+
e.1.err.err
2061+
);
2062+
claimed_any_htlcs = true;
20172063
} else { errs.push(e); }
20182064
},
2019-
Err(None) if is_mpp => unreachable!("We already checked for channel existence, we can't fail here!"),
2020-
Err(None) => {
2065+
Err(None) if is_mpp => unreachable!(
2066+
"We already checked for channel existence, we can't fail here!"
2067+
),
2068+
Err(None) => {
20212069
log_warn!(self, "Channel we expected to claim an HTLC from was closed.");
20222070
},
2023-
Ok(()) => claimed_any_htlcs = true,
2024-
}
2025-
}
2026-
}
2071+
Ok(()) => claimed_any_htlcs = true,
2072+
}
2073+
}
2074+
}
20272075

20282076
// Now that we've done the entire above loop in one lock, we can handle any errors
20292077
// which were generated.

0 commit comments

Comments
 (0)