Skip to content

Commit b67d958

Browse files
committed
Re-add support for non-zero-fee-anchors to chan_utils
1 parent f0775f8 commit b67d958

File tree

7 files changed

+37
-15
lines changed

7 files changed

+37
-15
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ impl KeysInterface for KeyProvider {
207207
[id as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
208208
channel_value_satoshis,
209209
[0; 32],
210+
false,
210211
);
211212
let revoked_commitment = self.make_enforcement_state_cell(keys.commitment_seed);
212213
EnforcingSigner::new_with_revoked(keys, revoked_commitment, false)

fuzz/src/full_stack.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ impl KeysInterface for KeyProvider {
311311
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
312312
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
313313
channel_value_satoshis,
314-
[0; 32]
314+
[0; 32],
315+
false,
315316
)
316317
} else {
317318
InMemorySigner::new(
@@ -324,7 +325,8 @@ impl KeysInterface for KeyProvider {
324325
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
325326
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
326327
channel_value_satoshis,
327-
[0; 32]
328+
[0; 32],
329+
false,
328330
)
329331
})
330332
}

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4015,7 +4015,8 @@ mod tests {
40154015
SecretKey::from_slice(&[41; 32]).unwrap(),
40164016
[41; 32],
40174017
0,
4018-
[0; 32]
4018+
[0; 32],
4019+
false
40194020
);
40204021

40214022
let counterparty_pubkeys = ChannelPublicKeys {

lightning/src/chain/keysinterface.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ pub struct InMemorySigner {
519519
channel_value_satoshis: u64,
520520
/// Key derivation parameters
521521
channel_keys_id: [u8; 32],
522+
/// Whether non-zero-fee anchors are enabled (used in conjuction with channel_parameters.opt_anchors)
523+
use_non_zero_fee_anchors: bool,
522524
}
523525

524526
impl InMemorySigner {
@@ -533,7 +535,9 @@ impl InMemorySigner {
533535
htlc_base_key: SecretKey,
534536
commitment_seed: [u8; 32],
535537
channel_value_satoshis: u64,
536-
channel_keys_id: [u8; 32]) -> InMemorySigner {
538+
channel_keys_id: [u8; 32],
539+
use_non_zero_fee_anchors: bool,
540+
) -> InMemorySigner {
537541
let holder_channel_pubkeys =
538542
InMemorySigner::make_holder_keys(secp_ctx, &funding_key, &revocation_base_key,
539543
&payment_key, &delayed_payment_base_key,
@@ -550,6 +554,7 @@ impl InMemorySigner {
550554
holder_channel_pubkeys,
551555
channel_parameters: None,
552556
channel_keys_id,
557+
use_non_zero_fee_anchors
553558
}
554559
}
555560

@@ -704,7 +709,7 @@ impl BaseSign for InMemorySigner {
704709

705710
let mut htlc_sigs = Vec::with_capacity(commitment_tx.htlcs().len());
706711
for htlc in commitment_tx.htlcs() {
707-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
712+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, self.opt_anchors(), self.use_non_zero_fee_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
708713
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
709714
let htlc_sighashtype = if self.opt_anchors() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
710715
let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).segwit_signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype).unwrap()[..]);
@@ -870,6 +875,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
870875
holder_channel_pubkeys,
871876
channel_parameters: counterparty_channel_data,
872877
channel_keys_id: keys_id,
878+
use_non_zero_fee_anchors: false
873879
})
874880
}
875881
}
@@ -1035,7 +1041,8 @@ impl KeysManager {
10351041
htlc_base_key,
10361042
commitment_seed,
10371043
channel_value_satoshis,
1038-
params.clone()
1044+
params.clone(),
1045+
false,
10391046
)
10401047
}
10411048

lightning/src/ln/chan_utils.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ pub fn make_funding_redeemscript(broadcaster: &PublicKey, countersignatory: &Pub
660660
///
661661
/// Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the
662662
/// commitment transaction).
663-
pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, opt_anchors: bool, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
663+
pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, opt_anchors: bool, use_non_zero_fee_anchors: bool, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
664664
let mut txins: Vec<TxIn> = Vec::new();
665665
txins.push(TxIn {
666666
previous_output: OutPoint {
@@ -677,7 +677,7 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
677677
} else {
678678
htlc_success_tx_weight(opt_anchors)
679679
};
680-
let output_value = if opt_anchors {
680+
let output_value = if opt_anchors && !use_non_zero_fee_anchors {
681681
htlc.amount_msat / 1000
682682
} else {
683683
let total_fee = feerate_per_kw as u64 * weight / 1000;
@@ -1160,6 +1160,8 @@ pub struct CommitmentTransaction {
11601160
htlcs: Vec<HTLCOutputInCommitment>,
11611161
// A boolean that is serialization backwards-compatible
11621162
opt_anchors: Option<()>,
1163+
// Whether non-zero-fee anchors should be used
1164+
opt_non_zero_fee_anchors: Option<()>,
11631165
// A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
11641166
keys: TxCreationKeys,
11651167
// For access to the pre-built transaction, see doc for trust()
@@ -1193,6 +1195,7 @@ impl_writeable_tlv_based!(CommitmentTransaction, {
11931195
(10, built, required),
11941196
(12, htlcs, vec_type),
11951197
(14, opt_anchors, option),
1198+
(16, opt_non_zero_fee_anchors, option),
11961199
});
11971200

11981201
impl CommitmentTransaction {
@@ -1225,9 +1228,16 @@ impl CommitmentTransaction {
12251228
transaction,
12261229
txid
12271230
},
1231+
opt_non_zero_fee_anchors: None,
12281232
}
12291233
}
12301234

1235+
/// use non-zero fee anchors
1236+
pub fn with_non_zero_fee_anchors(mut self) -> Self {
1237+
self.opt_non_zero_fee_anchors = Some(());
1238+
self
1239+
}
1240+
12311241
fn internal_rebuild_transaction(&self, keys: &TxCreationKeys, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_funding_key: &PublicKey, countersignatory_funding_key: &PublicKey) -> Result<BuiltCommitmentTransaction, ()> {
12321242
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);
12331243

@@ -1492,7 +1502,7 @@ impl<'a> TrustedCommitmentTransaction<'a> {
14921502

14931503
for this_htlc in inner.htlcs.iter() {
14941504
assert!(this_htlc.transaction_output_index.is_some());
1495-
let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
1505+
let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), self.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
14961506

14971507
let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
14981508

@@ -1514,7 +1524,7 @@ impl<'a> TrustedCommitmentTransaction<'a> {
15141524
// Further, we should never be provided the preimage for an HTLC-Timeout transaction.
15151525
if this_htlc.offered && preimage.is_some() { unreachable!(); }
15161526

1517-
let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
1527+
let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), self.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
15181528

15191529
let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
15201530

lightning/src/ln/channel.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,7 +3002,7 @@ impl<Signer: Sign> Channel<Signer> {
30023002
if let Some(_) = htlc.transaction_output_index {
30033003
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
30043004
self.get_counterparty_selected_contest_delay().unwrap(), &htlc, self.opt_anchors(),
3005-
&keys.broadcaster_delayed_payment_key, &keys.revocation_key);
3005+
false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
30063006

30073007
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
30083008
let htlc_sighashtype = if self.opt_anchors() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
@@ -5753,7 +5753,7 @@ impl<Signer: Sign> Channel<Signer> {
57535753

57545754
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
57555755
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
5756-
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, self.opt_anchors(), &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
5756+
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.get_holder_selected_contest_delay(), htlc, self.opt_anchors(), false, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
57575757
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &counterparty_keys)),
57585758
log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
57595759
log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.channel_id()));
@@ -7220,7 +7220,8 @@ mod tests {
72207220
// These aren't set in the test vectors:
72217221
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
72227222
10_000_000,
7223-
[0; 32]
7223+
[0; 32],
7224+
false,
72247225
);
72257226

72267227
assert_eq!(signer.pubkeys().funding_pubkey.serialize()[..],
@@ -7338,7 +7339,7 @@ mod tests {
73387339
let ref htlc = htlcs[$htlc_idx];
73397340
let htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.feerate_per_kw,
73407341
chan.get_counterparty_selected_contest_delay().unwrap(),
7341-
&htlc, $opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
7342+
&htlc, $opt_anchors, false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
73427343
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, $opt_anchors, &keys);
73437344
let htlc_sighashtype = if $opt_anchors { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
73447345
let htlc_sighash = Message::from_slice(&sighash::SighashCache::new(&htlc_tx).segwit_signature_hash(0, &htlc_redeemscript, htlc.amount_msat / 1000, htlc_sighashtype).unwrap()[..]).unwrap();

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl BaseSign for EnforcingSigner {
157157
for (this_htlc, sig) in trusted_tx.htlcs().iter().zip(&commitment_tx.counterparty_htlc_sigs) {
158158
assert!(this_htlc.transaction_output_index.is_some());
159159
let keys = trusted_tx.keys();
160-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
160+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.opt_anchors(), false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
161161

162162
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc, self.opt_anchors(), &keys);
163163

0 commit comments

Comments
 (0)