Expose the timelock of transaction packages
authorMatt Corallo <git@bluematt.me>
Fri, 21 May 2021 21:02:03 +0000 (21:02 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 28 May 2021 23:56:44 +0000 (23:56 +0000)
lightning/src/chain/package.rs

index bb44604317f630f97e911d5cd158b6802851e8b4..a7339468991f93f462ab132ee6104b7860a4fb70 100644 (file)
@@ -387,6 +387,21 @@ impl PackageSolvingData {
                        _ => { panic!("API Error!"); }
                }
        }
+       fn absolute_tx_timelock(&self, output_conf_height: u32) -> u32 {
+               // Get the absolute timelock at which this output can be spent given the height at which
+               // this output was confirmed. We use `output_conf_height + 1` as a safe default as we can
+               // be confirmed in the next block and transactions with time lock `current_height + 1`
+               // always propagate.
+               let absolute_timelock = match self {
+                       PackageSolvingData::RevokedOutput(_) => output_conf_height + 1,
+                       PackageSolvingData::RevokedHTLCOutput(_) => output_conf_height + 1,
+                       PackageSolvingData::CounterpartyOfferedHTLCOutput(_) => output_conf_height + 1,
+                       PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => std::cmp::max(outp.htlc.cltv_expiry, output_conf_height + 1),
+                       PackageSolvingData::HolderHTLCOutput(ref outp) => std::cmp::max(outp.cltv_expiry, output_conf_height + 1),
+                       PackageSolvingData::HolderFundingOutput(_) => output_conf_height + 1,
+               };
+               absolute_timelock
+       }
 }
 
 impl Writeable for PackageSolvingData {
@@ -598,6 +613,10 @@ impl PackageTemplate {
                }
                amounts
        }
+       pub(crate) fn package_timelock(&self) -> u32 {
+               self.inputs.iter().map(|(_, outp)| outp.absolute_tx_timelock(self.height_original))
+                       .max().expect("There must always be at least one output to spend in a PackageTemplate")
+       }
        pub(crate) fn package_weight(&self, destination_script: &Script) -> usize {
                let mut inputs_weight = 0;
                let mut witnesses_weight = 2; // count segwit flags