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

feat: Lightning Client NG Pay Invoice #2279

Merged
merged 1 commit into from
Apr 27, 2023

Conversation

m1sterc001guy
Copy link
Contributor

@m1sterc001guy m1sterc001guy commented Apr 20, 2023

Lightning Client NG paying a bolt 11 invoice. Adds the Pay state machine.

@codecov
Copy link

codecov bot commented Apr 20, 2023

Codecov Report

Patch coverage: 5.57% and project coverage change: -1.30 ⚠️

Comparison is base (88bb9c3) 57.46% compared to head (acc5a9b) 56.16%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2279       /-   ##
==========================================
- Coverage   57.46%   56.16%   -1.30%     
==========================================
  Files         163      167        4     
  Lines       33738    34551      813     
==========================================
  Hits        19386    19405       19     
- Misses      14352    15146      794     
Impacted Files Coverage Δ
fedimint-cli/src/lib.rs 5.24% <0.00%> (ø)
fedimint-cli/src/ng.rs 0.00% <0.00%> (ø)
fedimint-client-legacy/src/lib.rs 74.52% <0.00%> (ø)
gateway/ln-gateway/src/actor.rs 46.48% <0.00%> (ø)
gateway/ln-gateway/src/lib.rs 48.67% <0.00%> (ø)
gateway/ln-gateway/src/rpc/mod.rs 80.51% <ø> (ø)
gateway/ln-gateway/src/rpc/rpc_server.rs 63.38% <ø> (ø)
modules/fedimint-ln-client/src/api.rs 0.00% <0.00%> (ø)
modules/fedimint-ln-client/src/db.rs 0.00% <0.00%> (ø)
modules/fedimint-ln-client/src/pay.rs 0.00% <0.00%> (ø)
... and 10 more

... and 9 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@elsirion elsirion left a comment

Choose a reason for hiding this comment

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

Great progress! Just a few high-level comments about the API, will go through the actual state machines more thoroughly tomorrow :)

fedimint-cli/src/ng.rs Outdated Show resolved Hide resolved
fedimint-cli/src/ng.rs Outdated Show resolved Hide resolved
fedimint-cli/src/ng.rs Outdated Show resolved Hide resolved
@justinmoon
Copy link
Contributor

The CLI commands all worked for me 🎉

@m1sterc001guy m1sterc001guy force-pushed the lightning_pay_sm branch 5 times, most recently from a4014fe to 6eeb381 Compare April 26, 2023 19:39
old_state: LightningPayStateMachine,
contract_id: ContractId,
) -> LightningPayStateMachine {
// TODO: Retry contacting gateway
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't implement this yet. What do we want the policy to be here? Try 5 times then go to Refundable? Try forever until we contact the gateway?

let rejected_common = funded_common.clone();
let success_context = global_context.clone();
let reject_context = global_context.clone();
let gateway = self.gateway.clone();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For simplicity's sake while implementing this I just cloned everything. I don't think that's too expensive but its probably not necessary.

@m1sterc001guy m1sterc001guy marked this pull request as ready for review April 26, 2023 19:57
@m1sterc001guy m1sterc001guy requested review from a team as code owners April 26, 2023 19:57
elsirion
elsirion previously approved these changes Apr 27, 2023
Copy link
Contributor

@elsirion elsirion left a comment

Choose a reason for hiding this comment

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

LGTM, some nits but it works and we can improve incrementally

Comment on lines 106 to 113

// Wait for the refund transaction to complete after a lightning invoice
// failed to be paid.
async fn await_lightning_refund(
&self,
operation_id: OperationId,
refund_txid: TransactionId,
) -> anyhow::Result<()>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just wait for the transaction to be accepted? I know I have to add a "await output finalization" function to the PrimaryModule API, but that doesn't seem high prio since just awaiting tx acceptance is sufficient to guarantee we will get the refund eventually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this is just a quirk with the CLI that I pinged you about offline. The CLI just needs to be alive long enough for the output to be finalized so the notes can be retrieved. The Pay state machine doesn't actually wait on this, it just waits for the refund tx to be accepted then exits. We can refactor this if needed

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see, was missing that context. I'll implement an abstraction that removes the dependency on the mint module, but good to go for now.

Comment on lines 46 to 47
Refunded(TransactionId),
Failure(TransactionId),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could remove these and instead query tx acceptance in LnClientExt::subscribe_ln_pay_updates, a lot like I did here (definitely not your fault, the API for that just became available 😆 we can patch it out later):

Ok(Box::pin(stream! {
yield SpendOOBState::Created;
let refund = refund_future.await;
if refund.user_triggered {
yield SpendOOBState::UserCanceledProcessing;
match tx_subscription.await_tx_accepted(refund.transaction_id).await {
Ok(()) => {
yield SpendOOBState::UserCanceledSuccess;
},
Err(()) => {
yield SpendOOBState::UserCanceledFailure;
}
}
} else {
match tx_subscription.await_tx_accepted(refund.transaction_id).await {
Ok(()) => {
yield SpendOOBState::Refunded;
},
Err(()) => {
yield SpendOOBState::Success;
}
}
}
}))

Comment on lines 160 to 153
gateway: LightningGateway,
) -> LightningPayStateMachine {
assert!(matches!(
old_state.state,
LightningPayStates::CreatedOutgoingLnContract(_)
));
Copy link
Contributor

Choose a reason for hiding this comment

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

If you were using the data from inside old_state.state you wouldn't need to pass in the gateway, potentially saving some clones.

modules/fedimint-ln-client/src/pay.rs Outdated Show resolved Hide resolved
@elsirion elsirion merged commit 15830ac into fedimint:master Apr 27, 2023
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.

3 participants