Skip to content

Commit 8665110

Browse files
committed
Change package ID computation for HTLC claims on anchor channels
While the previous way of computing the identifier was safe, it wouldn't have been in certain scenarios if we considered splitting aggregated packages. While this type of splitting has yet to be implemented, it may come in the near future. To ensure we're prepared to handle such, we opt to instead commit to all of the HTLCs to claim in the request.
1 parent 6775b95 commit 8665110

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lightning/src/chain/onchaintx.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ use bitcoin::PackedLockTime;
1717
use bitcoin::blockdata::transaction::Transaction;
1818
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
1919
use bitcoin::blockdata::script::Script;
20-
20+
use bitcoin::hashes::Hash;
21+
#[cfg(anchors)]
22+
use bitcoin::hashes::HashEngine;
23+
#[cfg(anchors)]
24+
use bitcoin::hashes::sha256::Hash as Sha256;
2125
use bitcoin::hash_types::{Txid, BlockHash};
22-
2326
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
2427
use bitcoin::secp256k1;
2528

@@ -48,7 +51,6 @@ use core::ops::Deref;
4851
use core::mem::replace;
4952
#[cfg(anchors)]
5053
use core::mem::swap;
51-
use bitcoin::hashes::Hash;
5254

5355
const MAX_ALLOC_SIZE: usize = 64*1024;
5456

@@ -774,19 +776,24 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
774776
OnchainClaim::Event(claim_event) => {
775777
log_info!(logger, "Yielding onchain event to spend inputs {:?}", req.outpoints());
776778
let package_id = match claim_event {
777-
ClaimEvent::BumpCommitment { ref commitment_tx, .. } => commitment_tx.txid().into_inner(),
779+
ClaimEvent::BumpCommitment { ref commitment_tx, .. } =>
780+
// For commitment claims, we can just use their txid as it should
781+
// already be unique.
782+
commitment_tx.txid().into_inner(),
778783
ClaimEvent::BumpHTLC { ref htlcs, .. } => {
779-
// Use the same construction as a lightning channel id to generate
780-
// the package id for this request based on the first HTLC. It
781-
// doesn't matter what we use as long as it's unique per request.
782-
let mut package_id = [0; 32];
783-
package_id[..].copy_from_slice(&htlcs[0].commitment_txid[..]);
784-
let htlc_output_index = htlcs[0].htlc.transaction_output_index.unwrap();
785-
package_id[30] ^= ((htlc_output_index >> 8) & 0xff) as u8;
786-
package_id[31] ^= ((htlc_output_index >> 0) & 0xff) as u8;
787-
package_id
784+
// For HTLC claims, commit to the entire set of HTLC outputs to
785+
// claim, which will always be unique per request. Note that, even
786+
// if the request has HTLCs removed due to the counterparty claiming
787+
// it instead, the identifier returned here remains unchanged.
788+
let mut engine = Sha256::engine();
789+
for htlc in htlcs {
790+
engine.input(&htlc.commitment_txid.into_inner());
791+
engine.input(&htlc.htlc.transaction_output_index.unwrap().to_be_bytes());
792+
}
793+
Sha256::from_engine(engine).into_inner()
788794
},
789795
};
796+
debug_assert!(self.pending_claim_requests.get(&package_id).is_none());
790797
debug_assert_eq!(self.pending_claim_events.iter().filter(|entry| entry.0 == package_id).count(), 0);
791798
self.pending_claim_events.push((package_id, claim_event));
792799
package_id

0 commit comments

Comments
 (0)