Skip to content

Commit

Permalink
Remain backwards-compatible with SubChannels without channel_keys_id
Browse files Browse the repository at this point in the history
  • Loading branch information
luckysori committed Nov 3, 2023
1 parent 236c104 commit 4e104b4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
35 changes: 29 additions & 6 deletions dlc-manager/src/sub_channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ where
SubChannelState::OffChainClosed => {
s.is_offer = true;
s.update_idx -= 1;

// We do this to give older channels a chance to upgrade to the new version which
// expects this field to be set to `Some`.
s.channel_keys_id = Some(channel_details.channel_keys_id);

Some(s)
}
_ => return Err(Error::InvalidState(
Expand Down Expand Up @@ -411,7 +416,7 @@ where
counter_fund_pk: channel_details.counter_funding_pubkey.ok_or_else(|| {
Error::InvalidState("Counter funding PK is missing".to_string())
})?,
channel_keys_id: channel_details.channel_keys_id,
channel_keys_id: Some(channel_details.channel_keys_id),
}
}
};
Expand Down Expand Up @@ -1581,6 +1586,11 @@ where
let sub_channel = match sub_channel {
Some(mut s) => {
s.state = SubChannelState::Offered(offered_sub_channel);

// We do this to give older channels a chance to upgrade to the new version which
// expects this field to be set to `Some`.
s.channel_keys_id = Some(channel_details.channel_keys_id);

s
}
None => SubChannel {
Expand All @@ -1607,7 +1617,7 @@ where
counter_fund_pk: channel_details.counter_funding_pubkey.ok_or_else(|| {
Error::InvalidState("Counter funding PK is missing".to_string())
})?,
channel_keys_id: channel_details.channel_keys_id,
channel_keys_id: Some(channel_details.channel_keys_id),
},
};

Expand Down Expand Up @@ -3691,11 +3701,24 @@ where
split_tx: &Transaction,
) -> Result<Signature, Error> {
let mut signers = self.ln_channel_signers.lock().unwrap();

let channel_keys_id = match sub_channel.channel_keys_id {
Some(channel_keys_id) => channel_keys_id,
None => {
let channel_id = sub_channel.channel_id;
let channel_details = self
.ln_channel_manager
.get_channel_details(&channel_id)
.ok_or_else(|| {
Error::InvalidParameters(format!("Unknown LN channel {channel_id:02x?}"))
})?;
channel_details.channel_keys_id
}
};

let signer = signers.entry(sub_channel.channel_id).or_insert(
self.signer_provider.derive_ln_dlc_channel_signer(
sub_channel.fund_value_satoshis,
sub_channel.channel_keys_id,
),
self.signer_provider
.derive_ln_dlc_channel_signer(sub_channel.fund_value_satoshis, channel_keys_id),
);
signer.get_holder_split_tx_signature(
self.dlc_channel_manager.get_secp(),
Expand Down
5 changes: 4 additions & 1 deletion dlc-manager/src/subchannel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub struct SubChannel {
/// The revocation secrets from the remote party for already revoked split transactions.
pub counter_party_secrets: CounterpartyCommitmentSecrets,
/// The id used to derive the keys for the Lightning channel.
pub channel_keys_id: [u8; 32],
///
/// This is an [`Option`] to maintain backwards compatibility with versions of this struct which
/// were persisted without this field.
pub channel_keys_id: Option<[u8; 32]>,
}

impl std::fmt::Debug for SubChannel {
Expand Down
2 changes: 1 addition & 1 deletion dlc-manager/src/subchannel/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl_dlc_writeable!(SubChannel, {
(own_fund_pk, writeable),
(counter_fund_pk, writeable),
(counter_party_secrets, writeable),
(channel_keys_id, writeable)
(channel_keys_id, option)
});

impl_dlc_writeable_enum!(SubChannelState,
Expand Down

0 comments on commit 4e104b4

Please sign in to comment.