struct MsgHandleErrInternal {
err: msgs::LightningError,
- chan_id: Option<([u8; 32], u64)>, // If Some a channel of ours has been closed
+ chan_id: Option<([u8; 32], u128)>, // If Some a channel of ours has been closed
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
}
impl MsgHandleErrInternal {
Self { err, chan_id: None, shutdown_finish: None }
}
#[inline]
- fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u64, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
+ fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
Self {
err: LightningError {
err: err.clone(),
///
/// [`outbound_capacity_msat`]: ChannelDetails::outbound_capacity_msat
pub unspendable_punishment_reserve: Option<u64>,
- /// The `user_channel_id` passed in to create_channel, or 0 if the channel was inbound.
- pub user_channel_id: u64,
+ /// The `user_channel_id` passed in to create_channel, or a random value if the channel was
+ /// inbound. This may be zero for inbound channels serialized with LDK versions prior to
+ /// 0.0.113.
+ pub user_channel_id: u128,
/// Our total balance. This is the amount we would get if we close the channel.
/// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
/// amount is not likely to be recoverable on close.
#[derive(Clone, Debug)]
pub enum PaymentSendFailure {
/// A parameter which was passed to send_payment was invalid, preventing us from attempting to
- /// send the payment at all. No channel state has been changed or messages sent to peers, and
- /// once you've changed the parameter at error, you can freely retry the payment in full.
+ /// send the payment at all.
+ ///
+ /// You can freely resend the payment in full (with the parameter error fixed).
+ ///
+ /// Because the payment failed outright, no payment tracking is done, you do not need to call
+ /// [`ChannelManager::abandon_payment`] and [`ChannelManager::retry_payment`] will *not* work
+ /// for this payment.
ParameterError(APIError),
/// A parameter in a single path which was passed to send_payment was invalid, preventing us
- /// from attempting to send the payment at all. No channel state has been changed or messages
- /// sent to peers, and once you've changed the parameter at error, you can freely retry the
- /// payment in full.
+ /// from attempting to send the payment at all.
+ ///
+ /// You can freely resend the payment in full (with the parameter error fixed).
///
/// The results here are ordered the same as the paths in the route object which was passed to
/// send_payment.
+ ///
+ /// Because the payment failed outright, no payment tracking is done, you do not need to call
+ /// [`ChannelManager::abandon_payment`] and [`ChannelManager::retry_payment`] will *not* work
+ /// for this payment.
PathParameterError(Vec<Result<(), APIError>>),
/// All paths which were attempted failed to send, with no channel state change taking place.
- /// You can freely retry the payment in full (though you probably want to do so over different
+ /// You can freely resend the payment in full (though you probably want to do so over different
/// paths than the ones selected).
///
- /// [`ChannelManager::abandon_payment`] does *not* need to be called for this payment and
- /// [`ChannelManager::retry_payment`] will *not* work for this payment.
- AllFailedRetrySafe(Vec<APIError>),
+ /// Because the payment failed outright, no payment tracking is done, you do not need to call
+ /// [`ChannelManager::abandon_payment`] and [`ChannelManager::retry_payment`] will *not* work
+ /// for this payment.
+ AllFailedResendSafe(Vec<APIError>),
+ /// Indicates that a payment for the provided [`PaymentId`] is already in-flight and has not
+ /// yet completed (i.e. generated an [`Event::PaymentSent`]) or been abandoned (via
+ /// [`ChannelManager::abandon_payment`]).
+ ///
+ /// [`Event::PaymentSent`]: events::Event::PaymentSent
+ DuplicatePayment,
/// Some paths which were attempted failed to send, though possibly not all. At least some
/// paths have irrevocably committed to the HTLC and retrying the payment in full would result
/// in over-/re-payment.
///
/// `user_channel_id` will be provided back as in
/// [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
- /// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to 0
- /// for inbound channels, so you may wish to avoid using 0 for `user_channel_id` here.
- /// `user_channel_id` has no meaning inside of LDK, it is simply copied to events and otherwise
- /// ignored.
+ /// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to a
+ /// randomized value for inbound channels. `user_channel_id` has no meaning inside of LDK, it
+ /// is simply copied to events and otherwise ignored.
///
/// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
/// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
/// [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
/// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
/// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
- pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u64, override_config: Option<UserConfig>) -> Result<[u8; 32], APIError> {
+ pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u128, override_config: Option<UserConfig>) -> Result<[u8; 32], APIError> {
if channel_value_satoshis < 1000 {
return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
}
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
match pending_outbounds.entry(payment_id) {
- hash_map::Entry::Occupied(_) => Err(PaymentSendFailure::ParameterError(APIError::RouteError {
- err: "Payment already in progress"
- })),
+ hash_map::Entry::Occupied(_) => Err(PaymentSendFailure::DuplicatePayment),
hash_map::Entry::Vacant(entry) => {
let payment = entry.insert(PendingOutboundPayment::Retryable {
session_privs: HashSet::new(),
// `pending_outbound_payments` map, as the user isn't expected to `abandon_payment`.
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
debug_assert!(removed, "We should always have a pending payment to remove here");
- Err(PaymentSendFailure::AllFailedRetrySafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
+ Err(PaymentSendFailure::AllFailedResendSafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
} else {
Ok(())
}
///
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
- pub fn accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u64) -> Result<(), APIError> {
+ pub fn accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u128) -> Result<(), APIError> {
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id)
}
///
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
- pub fn accept_inbound_channel_from_trusted_peer_0conf(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u64) -> Result<(), APIError> {
+ pub fn accept_inbound_channel_from_trusted_peer_0conf(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u128) -> Result<(), APIError> {
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, true, user_channel_id)
}
- fn do_accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, accept_0conf: bool, user_channel_id: u64) -> Result<(), APIError> {
+ fn do_accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, accept_0conf: bool, user_channel_id: u128) -> Result<(), APIError> {
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
let mut channel_state_lock = self.channel_state.lock().unwrap();
return Err(MsgHandleErrInternal::send_err_msg_no_close("No inbound channels accepted".to_owned(), msg.temporary_channel_id.clone()));
}
+ let mut random_bytes = [0u8; 16];
+ random_bytes.copy_from_slice(&self.keys_manager.get_secure_random_bytes()[..16]);
+ let user_channel_id = u128::from_be_bytes(random_bytes);
+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
let mut channel = match Channel::new_from_req(&self.fee_estimator, &self.keys_manager,
- counterparty_node_id.clone(), &their_features, msg, 0, &self.default_configuration,
+ counterparty_node_id.clone(), &their_features, msg, user_channel_id, &self.default_configuration,
self.best_block.read().unwrap().height(), &self.logger, outbound_scid_alias)
{
Err(e) => {
}
channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
node_id: counterparty_node_id.clone(),
- msg: channel.accept_inbound_channel(0),
+ msg: channel.accept_inbound_channel(user_channel_id),
});
} else {
let mut pending_events = self.pending_events.lock().unwrap();
(11, outbound_htlc_maximum_msat, option),
});
-impl_writeable_tlv_based!(ChannelDetails, {
- (1, inbound_scid_alias, option),
- (2, channel_id, required),
- (3, channel_type, option),
- (4, counterparty, required),
- (5, outbound_scid_alias, option),
- (6, funding_txo, option),
- (7, config, option),
- (8, short_channel_id, option),
- (10, channel_value_satoshis, required),
- (12, unspendable_punishment_reserve, option),
- (14, user_channel_id, required),
- (16, balance_msat, required),
- (18, outbound_capacity_msat, required),
- // Note that by the time we get past the required read above, outbound_capacity_msat will be
- // filled in, so we can safely unwrap it here.
- (19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
- (20, inbound_capacity_msat, required),
- (22, confirmations_required, option),
- (24, force_close_spend_delay, option),
- (26, is_outbound, required),
- (28, is_channel_ready, required),
- (30, is_usable, required),
- (32, is_public, required),
- (33, inbound_htlc_minimum_msat, option),
- (35, inbound_htlc_maximum_msat, option),
-});
+impl Writeable for ChannelDetails {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+ // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
+ // versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
+ let user_channel_id_low = self.user_channel_id as u64;
+ let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
+ write_tlv_fields!(writer, {
+ (1, self.inbound_scid_alias, option),
+ (2, self.channel_id, required),
+ (3, self.channel_type, option),
+ (4, self.counterparty, required),
+ (5, self.outbound_scid_alias, option),
+ (6, self.funding_txo, option),
+ (7, self.config, option),
+ (8, self.short_channel_id, option),
+ (10, self.channel_value_satoshis, required),
+ (12, self.unspendable_punishment_reserve, option),
+ (14, user_channel_id_low, required),
+ (16, self.balance_msat, required),
+ (18, self.outbound_capacity_msat, required),
+ // Note that by the time we get past the required read above, outbound_capacity_msat will be
+ // filled in, so we can safely unwrap it here.
+ (19, self.next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
+ (20, self.inbound_capacity_msat, required),
+ (22, self.confirmations_required, option),
+ (24, self.force_close_spend_delay, option),
+ (26, self.is_outbound, required),
+ (28, self.is_channel_ready, required),
+ (30, self.is_usable, required),
+ (32, self.is_public, required),
+ (33, self.inbound_htlc_minimum_msat, option),
+ (35, self.inbound_htlc_maximum_msat, option),
+ (37, user_channel_id_high_opt, option),
+ });
+ Ok(())
+ }
+}
+
+impl Readable for ChannelDetails {
+ fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
+ init_and_read_tlv_fields!(reader, {
+ (1, inbound_scid_alias, option),
+ (2, channel_id, required),
+ (3, channel_type, option),
+ (4, counterparty, required),
+ (5, outbound_scid_alias, option),
+ (6, funding_txo, option),
+ (7, config, option),
+ (8, short_channel_id, option),
+ (10, channel_value_satoshis, required),
+ (12, unspendable_punishment_reserve, option),
+ (14, user_channel_id_low, required),
+ (16, balance_msat, required),
+ (18, outbound_capacity_msat, required),
+ // Note that by the time we get past the required read above, outbound_capacity_msat will be
+ // filled in, so we can safely unwrap it here.
+ (19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
+ (20, inbound_capacity_msat, required),
+ (22, confirmations_required, option),
+ (24, force_close_spend_delay, option),
+ (26, is_outbound, required),
+ (28, is_channel_ready, required),
+ (30, is_usable, required),
+ (32, is_public, required),
+ (33, inbound_htlc_minimum_msat, option),
+ (35, inbound_htlc_maximum_msat, option),
+ (37, user_channel_id_high_opt, option),
+ });
+
+ // `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
+ // versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
+ let user_channel_id_low: u64 = user_channel_id_low.0.unwrap();
+ let user_channel_id = user_channel_id_low as u128 +
+ ((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
+
+ Ok(Self {
+ inbound_scid_alias,
+ channel_id: channel_id.0.unwrap(),
+ channel_type,
+ counterparty: counterparty.0.unwrap(),
+ outbound_scid_alias,
+ funding_txo,
+ config,
+ short_channel_id,
+ channel_value_satoshis: channel_value_satoshis.0.unwrap(),
+ unspendable_punishment_reserve,
+ user_channel_id,
+ balance_msat: balance_msat.0.unwrap(),
+ outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
+ next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
+ inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
+ confirmations_required,
+ force_close_spend_delay,
+ is_outbound: is_outbound.0.unwrap(),
+ is_channel_ready: is_channel_ready.0.unwrap(),
+ is_usable: is_usable.0.unwrap(),
+ is_public: is_public.0.unwrap(),
+ inbound_htlc_minimum_msat,
+ inbound_htlc_maximum_msat,
+ })
+ }
+}
impl_writeable_tlv_based!(PhantomRouteHints, {
(2, channels, vec_type),