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

refactor: remove subscribe htlcs and complete_htlc #2161

Merged
merged 3 commits into from
Apr 12, 2023

Conversation

m1sterc001guy
Copy link
Contributor

Currently in the gateway, there are two RPCs needed for completing an HTLC: subscribe_htlcs and complete_htlc. Subscribe returns a stream which the actors use to determine if they need to take an action with the federation. If the gateway is able to get the preimage, it then calls complete_htlc to complete the payment.

However, it is not necessary to have two separate RPCs and this makes the code a bit more complicated. This refactor changes it so there is only one RPC called route_htlcs. This RPC takes a stream as input and returns a stream. This way, the actors can send messages using the input stream rather than needing to call a separate RPC.

This allows us to clean up the code a bit. In the LND implementation, we are able to remove the outcomes map.

Fixes: #1936

@m1sterc001guy m1sterc001guy marked this pull request as draft April 7, 2023 13:07
@m1sterc001guy m1sterc001guy force-pushed the remove_complete branch 2 times, most recently from c8be000 to f296b80 Compare April 7, 2023 16:17
@codecov
Copy link

codecov bot commented Apr 7, 2023

Codecov Report

Patch coverage: 11.56% and project coverage change: -0.07 ⚠️

Comparison is base (7bad9c1) 60.09% compared to head (75b47e3) 60.02%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2161      +/-   ##
==========================================
- Coverage   60.09%   60.02%   -0.07%     
==========================================
  Files         152      152              
  Lines       31054    31263     +209     
==========================================
+ Hits        18662    18766     +104     
- Misses      12392    12497     +105     
Impacted Files Coverage Δ
gateway/ln-gateway/src/bin/cln_extension.rs 0.22% <0.00%> (-0.03%) ⬇️
gateway/ln-gateway/src/lib.rs 46.30% <0.00%> (-0.34%) ⬇️
gateway/ln-gateway/src/lnd.rs 0.00% <0.00%> (ø)
gateway/ln-gateway/src/lnrpc_client.rs 0.00% <0.00%> (ø)
gateway/ln-gateway/src/actor.rs 44.83% <28.87%> (+0.77%) ⬆️
fedimint-testing/src/ln/fixtures.rs 93.82% <86.66%> (+20.37%) ⬆️

... and 18 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

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

Copy link
Contributor

@mxz42 mxz42 left a comment

Choose a reason for hiding this comment

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

started a review yesterday but didn't get quite through.

Submitting the comments now anyway hoping that they're not all out of date after the push

gateway/ln-gateway/src/bin/cln_extension.rs Outdated Show resolved Hide resolved
gateway/ln-gateway/src/actor.rs Outdated Show resolved Hide resolved
fedimint-testing/src/ln/fixtures.rs Outdated Show resolved Hide resolved
gateway/ln-gateway/src/actor.rs Outdated Show resolved Hide resolved
@m1sterc001guy m1sterc001guy marked this pull request as ready for review April 7, 2023 16:34
@m1sterc001guy m1sterc001guy requested a review from a team as a code owner April 7, 2023 16:34
@m1sterc001guy m1sterc001guy force-pushed the remove_complete branch 2 times, most recently from 0c7f409 to d065757 Compare April 7, 2023 17:07
Comment on lines 118 to 119
// The index of the incoming htlc in the incoming channel.
uint64 htlc_id = 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this abstract well over implementations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not currently, it is specific to LND. Let me see if there's an easy refactor so that it is generalized.

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 changed the gRPC spec to just include the incoming channel id and the htlc id in the SubscribeInterceptHtlcsResponse. This is generic across all implementations.

In LND we just pass these values onto LND to complete the HTLC. For our CLN implementation, we use it as the key into our outcomes map to lookup the channel sender needed to unblock the thread that will complete the HTLC.

gateway/ln-gateway/src/actor.rs Outdated Show resolved Hide resolved
@m1sterc001guy
Copy link
Contributor Author

(Adding @okjodom's comments that he sent offline while he didn't have much internet access)

@m1sterc001guy m1sterc001guy force-pushed the remove_complete branch 3 times, most recently from 74a8d9e to 8591246 Compare April 10, 2023 20:23
@m1sterc001guy m1sterc001guy added the waiting-on-review PR needs review label Apr 11, 2023
justinmoon
justinmoon previously approved these changes Apr 11, 2023
Copy link
Contributor

@justinmoon justinmoon left a comment

Choose a reason for hiding this comment

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

LGTM

continue;
Some(route_htlc_response::Action::CompleteResponse(_complete_response)) => {
// TODO: Might need to add some error handling here
info!("Successfully handled HTLC");
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to include some information about the HTLC that was handled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gonna leave this one as a todo since we don't actually know much about this HTLC (CompleteResponse doesn't contain any data currently).

gateway/ln-gateway/src/actor.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@mxz42 mxz42 left a comment

Choose a reason for hiding this comment

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

big change. left some comments and questions but these are better addressed in a new PR as this one is already rather big.

nice work! @m1sterc001guy

(also tested a bit in tmuxinator. seems to work so far 👍)

Comment on lines +48 to +52
#[derive(Clone)]
struct LightningSenderStream {
ln_sender: Sender<RouteHtlcRequest>,
lnrpc: Arc<RwLock<dyn ILnRpcClient>>,
}
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 add a doc string here?

for my understanding: the decision to use a RwLock instead of a tokio Mutex is that the tokio::RwLock is write-preferring, right?

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'll add a docstring. My understanding is RwLock is actually read-optimizied since it won't block other readers

Comment on lines +181 to +182
// Create a channel that will be used to shutdown the HTLC thread
let (sender, receiver) = mpsc::channel::<Arc<AtomicBool>>(100);
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 use a one shot channel or do we need to be able to receive the shutdown signal more than once?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is currently somewhat difficult because GatewayActor needs to be clonable. After this refactor though I think it should be possible

type HtlcOutcomeSender = oneshot::Sender<serde_json::Value>;

/// Functional structure to filter intercepted HTLCs into subscription streams.
/// Used as a CLN plugin
#[derive(Clone)]
pub struct ClnHtlcInterceptor {
subscriptions: Arc<Mutex<HashMap<u64, HtlcSubscriptionSender>>>,
pub outcomes: Arc<Mutex<HashMap<sha256::Hash, HtlcOutcomeSender>>>,
pub outcomes: Arc<Mutex<BTreeMap<(u64, u64), HtlcOutcomeSender>>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

(u64, u64) is what exactly? channel_id and hash "the index of the incoming htlc in the channel"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Probably should make a new data structure to encapsulate

for actor in actors.values() {
actor.write().await.subscribe_htlcs().await?;
let (sender, receiver) = mpsc::channel::<Arc<AtomicBool>>(100);
Copy link
Contributor

Choose a reason for hiding this comment

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

could this be a oneshot channel, too?

@mxz42
Copy link
Contributor

mxz42 commented Apr 12, 2023

CI audit:

fedimint-workspace-audit-audit> error: 1 vulnerability found!
error: builder for '/nix/store/p7qkfpa0mxn3nzz4y303g41d69vf895h-fedimint-workspace-audit-audit-0.0.1.drv' failed with exit code 1;
       last 10 log lines:
       > Dependency tree:
       > libsqlite3-sys 0.24.2
       > └── sqlx-core 0.6.2
       >     ├── sqlx-macros 0.6.2
       >     │   └── sqlx 0.6.2
       >     │       └── fedimint-sqlite 0.1.0
       >     │           └── fedimint-tests 0.1.0
       >     └── sqlx 0.6.2
       >
       > error: 1 vulnerability found!

we shouldn't forget about his, either.

@mxz42 mxz42 merged commit 60fbb75 into fedimint:master Apr 12, 2023
13 of 15 checks passed
@justinmoon
Copy link
Contributor

CI audit:

fedimint-workspace-audit-audit> error: 1 vulnerability found!
error: builder for '/nix/store/p7qkfpa0mxn3nzz4y303g41d69vf895h-fedimint-workspace-audit-audit-0.0.1.drv' failed with exit code 1;
       last 10 log lines:
       > Dependency tree:
       > libsqlite3-sys 0.24.2
       > └── sqlx-core 0.6.2
       >     ├── sqlx-macros 0.6.2
       >     │   └── sqlx 0.6.2
       >     │       └── fedimint-sqlite 0.1.0
       >     │           └── fedimint-tests 0.1.0
       >     └── sqlx 0.6.2
       >
       > error: 1 vulnerability found!

we shouldn't forget about his, either.

This got fixed here #2204

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-on-review PR needs review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove ILnRpcClient.complete_htlc
4 participants