Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payment session and error handling on onion packet forwarding #191

Merged
merged 70 commits into from
Oct 24, 2024

Conversation

chenyukang
Copy link
Collaborator

@chenyukang chenyukang commented Sep 24, 2024

This PR contains these changes:

  1. Add payment session and payment status, and get_payment rpc will query the payment status.
  2. Add error handling on sending onion packet and TLC forwarding (without onion encryption right now)
  3. Some code clean up and refactor

@chenyukang chenyukang marked this pull request as draft September 24, 2024 15:34
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch 6 times, most recently from 8dbf8dd to 3dcae70 Compare September 26, 2024 12:59
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch 2 times, most recently from 0b18b5f to 9292ffa Compare September 29, 2024 04:07
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch from f144887 to abd1e46 Compare October 1, 2024 00:40
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch from 17f6f2c to 8cfb17f Compare October 13, 2024 09:42
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch from 9b595e3 to 118d9bc Compare October 13, 2024 16:37
@chenyukang chenyukang marked this pull request as ready for review October 14, 2024 07:56
src/fiber/channel.rs Outdated Show resolved Hide resolved
src/fiber/channel.rs Outdated Show resolved Hide resolved
.public_channel_info
.as_ref()
.map(|info| info.tlc_fee_proportional_millionths.unwrap_or_default())
.unwrap_or_default();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting a default fee value here effectively makes a non-public channel forwarding TLCs? Is my assumption correct? If it is, is this kind of behavior desirable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this node broadcasting a fee rate 100, then a update on fee rate is done, but the latest fee rate haven't been received by the graph before a payment is sent.

now there are two scenarios:

  1. the new fee is lower than previous one, but we don't report error here [maybe need to discussed here]
  2. the new fee is higher than previous one, we need to report error here.

if there is no configuration for the fee(means the node removed fee rate), I think it's same with scenario #1.

How can we find out a route with this node if this node is non-public? At least our current code will not do this. maybe we should add a check that only public node will forward a TLC right now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The public_channel_info is set while creating the channel, we deem every channel with this information as public channel https://github.com/contrun/cfn-node/blob/4033caaae18933ed922fa05543b6d686407628c0/src/fiber/channel.rs#L2224 .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I resolve this issue with 9fc0a35

src/fiber/channel.rs Outdated Show resolved Hide resolved
src/fiber/graph.rs Outdated Show resolved Hide resolved
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch from e372112 to 856a2ad Compare October 15, 2024 12:28
@chenyukang chenyukang force-pushed the yukang-more-on-payment-session branch from 1734bfb to 5ccb247 Compare October 16, 2024 09:34
}
}

async fn try_payment_session(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to extract two helper functions build_route and send_onion_packet to simplifying the main function.

and setting final error to `None`` outside the loop and then assigning it within the loop lead to unnecessary state management and logic complexity.

async fn try_payment_session(
    &self,
    state: &mut NetworkActorState<S>,
    mut payment_session: PaymentSession,
) -> Result<PaymentSession, Error> {
    let payment_data = payment_session.request.clone();
    let payment_hash = payment_data.payment_hash;

    while payment_session.can_retry() {
        payment_session.retried_times += 1;

        let hops_infos = self.build_route(payment_data.clone()).await?;
        let first_channel_outpoint = hops_infos[0]
            .channel_outpoint
            .clone()
            .expect("first hop channel outpoint");

        let tlc_id = self.send_onion_packet(state, hops_infos).await?;

        payment_session.set_status(PaymentSessionStatus::Inflight);
        payment_session.set_first_hop_info(first_channel_outpoint, tlc_id);
        self.store.insert_payment_session(payment_session.clone());

        return Ok(payment_session);
    }

    let error_msg = format!("Exhausted retries for payment: {:?}", payment_hash);
    payment_session.set_status(PaymentSessionStatus::Failed);
    payment_session.set_failed_status(&error_msg);
    self.store.insert_payment_session(payment_session);
    Err(Error::SendPaymentError(error_msg))
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there is any error happened in build_route or send_onion_packet, instead of just throwing an exception to upper level, we need to set payment session status and store into db.

let me think how to make the code simpler.

src/fiber/network.rs Outdated Show resolved Hide resolved
@chenyukang chenyukang changed the title Payment session and error handling on union packet forwarding Payment session and error handling on onion packet forwarding Oct 23, 2024
@chenyukang
Copy link
Collaborator Author

Will refactor on PR #228, merge now.

@chenyukang chenyukang merged commit a7e530a into main Oct 24, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants