Skip to content

Commit

Permalink
feat: get status of pending CLN invoices on startup (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Nov 7, 2024
1 parent bb1a42f commit 776613d
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/VersionCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class VersionCheck {
maximal: '24.08.2',
},
[ClnClient.serviceNameHold]: {
minimal: '0.1.0',
maximal: '0.1.2',
minimal: '0.2.0',
maximal: '0.2.0',
},
[MpayClient.serviceName]: {
minimal: '0.1.0',
Expand Down
32 changes: 17 additions & 15 deletions lib/lightning/cln/ClnClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ClnClient
private readonly holdMeta = new Metadata();

private trackAllSubscription?: ClientReadableStream<holdrpc.TrackAllResponse>;
private holdInvoicesToSubscribe: Set<Uint8Array> = new Set<Uint8Array>();

constructor(
logger: Logger,
Expand Down Expand Up @@ -138,7 +139,7 @@ class ClnClient

public useMpay = () => this.mpay !== undefined && this.mpay.isConnected();

public connect = async (startSubscriptions = true): Promise<boolean> => {
public connect = async (): Promise<boolean> => {
if (!this.isConnected()) {
this.nodeClient = new NodeClient(this.nodeUri, this.nodeCreds, {
...grpcOptions,
Expand All @@ -154,11 +155,6 @@ class ClnClient

try {
await this.getInfo();

if (startSubscriptions) {
this.subscribeTrackHoldInvoices();
}

this.setClientStatus(ClientStatus.Connected);
} catch (error) {
this.setClientStatus(ClientStatus.Disconnected);
Expand Down Expand Up @@ -620,11 +616,14 @@ class ClnClient
};
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public subscribeSingleInvoice = (_: Buffer): void => {
// Just here for interface compatibility;
// with CLN we can subscribe to all hold invoices with one gRPC subscription
return;
public subscribeSingleInvoice = (preimageHash: Buffer): void => {
// That call is only used to get the last update of relevant invoices
// when starting the subscription for *all* hold invoices
if (this.trackAllSubscription !== undefined) {
return;
}

this.holdInvoicesToSubscribe.add(preimageHash);
};

private static routingHintsToGrpc = (
Expand Down Expand Up @@ -771,14 +770,17 @@ class ClnClient
return undefined;
};

private subscribeTrackHoldInvoices = () => {
public subscribeTrackHoldInvoices = () => {
if (this.trackAllSubscription) {
this.trackAllSubscription.cancel();
}

this.trackAllSubscription = this.holdClient!.trackAll(
new holdrpc.TrackAllRequest(),
);
const req = new holdrpc.TrackAllRequest();

req.setPaymentHashesList(Array.from(this.holdInvoicesToSubscribe.values()));
this.holdInvoicesToSubscribe.clear();

this.trackAllSubscription = this.holdClient!.trackAll(req);

this.trackAllSubscription.on('data', (update: holdrpc.TrackAllResponse) => {
switch (update.getState()) {
Expand Down
17 changes: 17 additions & 0 deletions lib/proto/hold/hold_grpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions lib/proto/hold/hold_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions lib/proto/hold/hold_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 776613d

Please sign in to comment.