Skip to content

Commit 356908b

Browse files
author
Antoine Riard
committed
Remove write_option in ChannelMonitor serializer
Drop Box usage in remote_claimabe_outpoint
1 parent f5a5729 commit 356908b

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

src/ln/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3359,7 +3359,7 @@ impl Channel {
33593359
let (res, remote_commitment_tx, htlcs) = match self.send_commitment_no_state_update() {
33603360
Ok((res, (remote_commitment_tx, mut htlcs))) => {
33613361
// Update state now that we've passed all the can-fail calls...
3362-
let htlcs_no_ref = htlcs.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| Box::new(source_ref.clone())))).collect();
3362+
let htlcs_no_ref = htlcs.drain(..).map(|(htlc, htlc_source)| (htlc, htlc_source.map(|source_ref| source_ref.clone()))).collect();
33633363
(res, remote_commitment_tx, htlcs_no_ref)
33643364
},
33653365
Err(e) => return Err(e),

src/ln/channelmonitor.rs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ pub struct ChannelMonitor {
362362
their_to_self_delay: Option<u16>,
363363

364364
old_secrets: [([u8; 32], u64); 49],
365-
remote_claimable_outpoints: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
365+
remote_claimable_outpoints: HashMap<Sha256dHash, Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>>,
366366
/// We cannot identify HTLC-Success or HTLC-Timeout transactions by themselves on the chain.
367367
/// Nor can we figure out their commitment numbers without the commitment transaction they are
368368
/// spending. Thus, in order to claim them via revocation key, we track all the remote
@@ -564,7 +564,7 @@ impl ChannelMonitor {
564564
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
565565
/// possibly future revocation/preimage information) to claim outputs where possible.
566566
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
567-
pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>, commitment_number: u64, their_revocation_point: PublicKey) {
567+
pub(super) fn provide_latest_remote_commitment_tx_info(&mut self, unsigned_commitment_tx: &Transaction, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>, commitment_number: u64, their_revocation_point: PublicKey) {
568568
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
569569
// so that a remote monitor doesn't learn anything unless there is a malicious close.
570570
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -781,18 +781,6 @@ impl ChannelMonitor {
781781
// Set in initial Channel-object creation, so should always be set by now:
782782
U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
783783

784-
macro_rules! write_option {
785-
($thing: expr) => {
786-
match $thing {
787-
&Some(ref t) => {
788-
1u8.write(writer)?;
789-
t.write(writer)?;
790-
},
791-
&None => 0u8.write(writer)?,
792-
}
793-
}
794-
}
795-
796784
match self.key_storage {
797785
Storage::Local { ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref prev_latest_per_commitment_point, ref latest_per_commitment_point, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
798786
writer.write_all(&[0; 1])?;
@@ -801,18 +789,8 @@ impl ChannelMonitor {
801789
writer.write_all(&delayed_payment_base_key[..])?;
802790
writer.write_all(&payment_base_key[..])?;
803791
writer.write_all(&shutdown_pubkey.serialize())?;
804-
if let Some(ref prev_latest_per_commitment_point) = *prev_latest_per_commitment_point {
805-
writer.write_all(&[1; 1])?;
806-
writer.write_all(&prev_latest_per_commitment_point.serialize())?;
807-
} else {
808-
writer.write_all(&[0; 1])?;
809-
}
810-
if let Some(ref latest_per_commitment_point) = *latest_per_commitment_point {
811-
writer.write_all(&[1; 1])?;
812-
writer.write_all(&latest_per_commitment_point.serialize())?;
813-
} else {
814-
writer.write_all(&[0; 1])?;
815-
}
792+
prev_latest_per_commitment_point.write(writer)?;
793+
latest_per_commitment_point.write(writer)?;
816794
match funding_info {
817795
&Some((ref outpoint, ref script)) => {
818796
writer.write_all(&outpoint.txid[..])?;
@@ -823,8 +801,8 @@ impl ChannelMonitor {
823801
debug_assert!(false, "Try to serialize a useless Local monitor !");
824802
},
825803
}
826-
write_option!(current_remote_commitment_txid);
827-
write_option!(prev_remote_commitment_txid);
804+
current_remote_commitment_txid.write(writer)?;
805+
prev_remote_commitment_txid.write(writer)?;
828806
},
829807
Storage::Watchtower { .. } => unimplemented!(),
830808
}
@@ -864,7 +842,7 @@ impl ChannelMonitor {
864842
writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
865843
writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
866844
writer.write_all(&$htlc_output.payment_hash.0[..])?;
867-
write_option!(&$htlc_output.transaction_output_index);
845+
$htlc_output.transaction_output_index.write(writer)?;
868846
}
869847
}
870848

@@ -874,7 +852,7 @@ impl ChannelMonitor {
874852
writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
875853
for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
876854
serialize_htlc_in_commitment!(htlc_output);
877-
write_option!(htlc_source);
855+
htlc_source.write(writer)?;
878856
}
879857
}
880858

@@ -923,7 +901,8 @@ impl ChannelMonitor {
923901
} else {
924902
0u8.write(writer)?;
925903
}
926-
write_option!(htlc_source);
904+
htlc_source.write(writer)?;
905+
//write_option!(htlc_source);
927906
}
928907
}
929908
}
@@ -1188,7 +1167,7 @@ impl ChannelMonitor {
11881167
for &(ref htlc, ref source_option) in outpoints.iter() {
11891168
if let &Some(ref source) = source_option {
11901169
log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx);
1191-
htlc_updated.push(((**source).clone(), None, htlc.payment_hash.clone()));
1170+
htlc_updated.push(((*source).clone(), None, htlc.payment_hash.clone()));
11921171
}
11931172
}
11941173
}
@@ -1266,7 +1245,7 @@ impl ChannelMonitor {
12661245
}
12671246
}
12681247
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);
1269-
htlc_updated.push(((**source).clone(), None, htlc.payment_hash.clone()));
1248+
htlc_updated.push(((*source).clone(), None, htlc.payment_hash.clone()));
12701249
}
12711250
}
12721251
}
@@ -1903,7 +1882,7 @@ impl ChannelMonitor {
19031882
if pending_htlc.payment_hash == $htlc_output.payment_hash && pending_htlc.amount_msat == $htlc_output.amount_msat {
19041883
if let &Some(ref source) = pending_source {
19051884
log_claim!("revoked remote commitment tx", false, pending_htlc, true);
1906-
payment_data = Some(((**source).clone(), $htlc_output.payment_hash));
1885+
payment_data = Some(((*source).clone(), $htlc_output.payment_hash));
19071886
break;
19081887
}
19091888
}
@@ -1956,7 +1935,7 @@ impl ChannelMonitor {
19561935
}
19571936
}
19581937
if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
1959-
scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()).map(|boxed| &**boxed))),
1938+
scan_commitment!(htlc_outputs.iter().map(|&(ref a, ref b)| (a, (b.as_ref().clone()))),
19601939
"remote commitment tx", false);
19611940
}
19621941

@@ -2085,7 +2064,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20852064
let htlcs_count: u64 = Readable::read(reader)?;
20862065
let mut htlcs = Vec::with_capacity(cmp::min(htlcs_count as usize, MAX_ALLOC_SIZE / 32));
20872066
for _ in 0..htlcs_count {
2088-
htlcs.push((read_htlc_in_commitment!(), <Option<HTLCSource> as Readable<R>>::read(reader)?.map(|o: HTLCSource| Box::new(o))));
2067+
htlcs.push((read_htlc_in_commitment!(), <Option<HTLCSource> as Readable<R>>::read(reader)?));
20892068
}
20902069
if let Some(_) = remote_claimable_outpoints.insert(txid, htlcs) {
20912070
return Err(DecodeError::InvalidValue);

0 commit comments

Comments
 (0)