Skip to content

Commit

Permalink
style(LightningClientService.cs): improve code readability by breakin…
Browse files Browse the repository at this point in the history
…g long method signatures into multiple lines

refactor(LightningClientService.cs): remove unnecessary lock around _clients dictionary access for better performance
style(LightningClientService.cs): adjust whitespace in dictionary initializations for consistency
  • Loading branch information
Jossec101 committed Dec 8, 2023
1 parent 82696d9 commit 1c12a45
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions src/Services/LightningClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@ public interface ILightningClientService
{
public Lightning.LightningClient GetLightningClient(string? endpoint);
public Task<ListChannelsResponse?> ListChannels(Node node, Lightning.LightningClient? client = null);
public AsyncServerStreamingCall<CloseStatusUpdate>? CloseChannel(Node node, Channel channel, bool forceClose = false, Lightning.LightningClient? client = null);
public AsyncServerStreamingCall<ChannelEventUpdate> SubscribeChannelEvents(Node node, Lightning.LightningClient? client = null);

public AsyncServerStreamingCall<CloseStatusUpdate>? CloseChannel(Node node, Channel channel,
bool forceClose = false, Lightning.LightningClient? client = null);

public AsyncServerStreamingCall<ChannelEventUpdate> SubscribeChannelEvents(Node node,
Lightning.LightningClient? client = null);

public Task<LightningNode?> GetNodeInfo(Node node, string pubKey, Lightning.LightningClient? client = null);
public Task ConnectToPeer(Node node, string peerPubKey, Lightning.LightningClient? client = null);
public AsyncServerStreamingCall<OpenStatusUpdate> OpenChannel(Node node, OpenChannelRequest openChannelRequest, Lightning.LightningClient? client = null);
public void FundingStateStepVerify(Node node, PSBT finalizedPSBT, byte[] pendingChannelId, Lightning.LightningClient? client = null);
public void FundingStateStepFinalize(Node node, PSBT finalizedPSBT, byte[] pendingChannelId, Lightning.LightningClient? client = null);

public AsyncServerStreamingCall<OpenStatusUpdate> OpenChannel(Node node, OpenChannelRequest openChannelRequest,
Lightning.LightningClient? client = null);

public void FundingStateStepVerify(Node node, PSBT finalizedPSBT, byte[] pendingChannelId,
Lightning.LightningClient? client = null);

public void FundingStateStepFinalize(Node node, PSBT finalizedPSBT, byte[] pendingChannelId,
Lightning.LightningClient? client = null);

public void FundingStateStepCancel(Node node, byte[] pendingChannelId, Lightning.LightningClient? client = null);
}

Expand All @@ -64,7 +76,7 @@ private static GrpcChannel CreateLightningClient(string? endpoint)
};

var grpcChannel = GrpcChannel.ForAddress($"https://{endpoint}",
new GrpcChannelOptions { HttpHandler = httpHandler });
new GrpcChannelOptions {HttpHandler = httpHandler});

return grpcChannel;
}
Expand All @@ -77,21 +89,19 @@ public Lightning.LightningClient GetLightningClient(string? endpoint)
}

// Atomic operation for TryGetValue and TryAdd
lock (_clients)
{
var found = _clients.TryGetValue(endpoint, out var client);
if (!found)
{
_logger.LogInformation("Client not found for endpoint {endpoint}, creating a new one", endpoint);
var newClient = CreateLightningClient(endpoint);
var added = _clients.TryAdd(endpoint, newClient);
_logger.LogDebug("Client for endpoint {endpoint} was added: {added}", endpoint, added ? "true" : "false");
return new Lightning.LightningClient(newClient);
}

_logger.LogInformation("Client found for endpoint {endpoint}", endpoint);
return new Lightning.LightningClient(client);
var found = _clients.TryGetValue(endpoint, out var client);
if (!found)
{
_logger.LogInformation("Client not found for endpoint {endpoint}, creating a new one", endpoint);
var newClient = CreateLightningClient(endpoint);
var added = _clients.TryAdd(endpoint, newClient);
_logger.LogDebug("Client for endpoint {endpoint} was added: {added}", endpoint, added ? "true" : "false");
return new Lightning.LightningClient(newClient);
}

_logger.LogInformation("Client found for endpoint {endpoint}", endpoint);
return new Lightning.LightningClient(client);
}

public async Task<ListChannelsResponse?> ListChannels(Node node, Lightning.LightningClient? client = null)
Expand All @@ -118,7 +128,8 @@ public Lightning.LightningClient GetLightningClient(string? endpoint)
return listChannelsResponse;
}

public AsyncServerStreamingCall<CloseStatusUpdate>? CloseChannel(Node node, Channel channel, bool forceClose = false, Lightning.LightningClient? client = null)
public AsyncServerStreamingCall<CloseStatusUpdate>? CloseChannel(Node node, Channel channel,
bool forceClose = false, Lightning.LightningClient? client = null)
{
//This method is here to avoid a circular dependency between the LightningService and the ChannelRepository
AsyncServerStreamingCall<CloseStatusUpdate>? closeChannelResponse = null;
Expand All @@ -133,7 +144,7 @@ public Lightning.LightningClient GetLightningClient(string? endpoint)
OutputIndex = channel.FundingTxOutputIndex
},
Force = forceClose,
}, new Metadata { { "macaroon", node.ChannelAdminMacaroon } });
}, new Metadata {{"macaroon", node.ChannelAdminMacaroon}});
}
catch (Exception e)
{
Expand All @@ -143,13 +154,14 @@ public Lightning.LightningClient GetLightningClient(string? endpoint)
return null;
}

public AsyncServerStreamingCall<ChannelEventUpdate> SubscribeChannelEvents(Node node, Lightning.LightningClient? client = null)
public AsyncServerStreamingCall<ChannelEventUpdate> SubscribeChannelEvents(Node node,
Lightning.LightningClient? client = null)
{
client ??= GetLightningClient(node.Endpoint);
return client.SubscribeChannelEvents(new ChannelEventSubscription(),
new Metadata
{
{ "macaroon", node.ChannelAdminMacaroon }
{"macaroon", node.ChannelAdminMacaroon}
});
}

Expand All @@ -167,7 +179,7 @@ public AsyncServerStreamingCall<ChannelEventUpdate> SubscribeChannelEvents(Node
{
PubKey = pubKey,
IncludeChannels = false
}, new Metadata { { "macaroon", node.ChannelAdminMacaroon } });
}, new Metadata {{"macaroon", node.ChannelAdminMacaroon}});

return nodeInfo?.Node;
}
Expand Down Expand Up @@ -195,11 +207,11 @@ public async Task ConnectToPeer(Node node, string peerPubKey, Lightning.Lightnin

connectPeerResponse = await client.ConnectPeerAsync(new ConnectPeerRequest
{
Addr = new LightningAddress { Host = addr, Pubkey = nodeInfo.PubKey },
Addr = new LightningAddress {Host = addr, Pubkey = nodeInfo.PubKey},
Perm = true
}, new Metadata
{
{ "macaroon", node.ChannelAdminMacaroon }
{"macaroon", node.ChannelAdminMacaroon}
});
}
//We avoid to stop the method if the peer is already connected
Expand Down Expand Up @@ -236,15 +248,17 @@ public async Task ConnectToPeer(Node node, string peerPubKey, Lightning.Lightnin
}
}

public AsyncServerStreamingCall<OpenStatusUpdate> OpenChannel(Node node, OpenChannelRequest openChannelRequest, Lightning.LightningClient? client = null)
public AsyncServerStreamingCall<OpenStatusUpdate> OpenChannel(Node node, OpenChannelRequest openChannelRequest,
Lightning.LightningClient? client = null)
{
client ??= GetLightningClient(node.Endpoint);
return client.OpenChannel(openChannelRequest,
new Metadata { { "macaroon", node.ChannelAdminMacaroon } }
new Metadata {{"macaroon", node.ChannelAdminMacaroon}}
);
}

public void FundingStateStepVerify(Node node, PSBT finalizedPSBT, byte[] pendingChannelId, Lightning.LightningClient? client = null)
public void FundingStateStepVerify(Node node, PSBT finalizedPSBT, byte[] pendingChannelId,
Lightning.LightningClient? client = null)
{
client ??= GetLightningClient(node.Endpoint);
client.FundingStateStep(
Expand All @@ -257,10 +271,11 @@ public void FundingStateStepVerify(Node node, PSBT finalizedPSBT, byte[] pending
Convert.FromHexString(finalizedPSBT.ToHex())),
PendingChanId = ByteString.CopyFrom(pendingChannelId)
}
}, new Metadata { { "macaroon", node.ChannelAdminMacaroon } });
}, new Metadata {{"macaroon", node.ChannelAdminMacaroon}});
}

public void FundingStateStepFinalize(Node node, PSBT finalizedPSBT, byte[] pendingChannelId, Lightning.LightningClient? client = null)
public void FundingStateStepFinalize(Node node, PSBT finalizedPSBT, byte[] pendingChannelId,
Lightning.LightningClient? client = null)
{
client ??= GetLightningClient(node.Endpoint);
client.FundingStateStep(
Expand All @@ -273,7 +288,7 @@ public void FundingStateStepFinalize(Node node, PSBT finalizedPSBT, byte[] pendi
Convert.FromHexString(finalizedPSBT.ToHex())),
PendingChanId = ByteString.CopyFrom(pendingChannelId)
}
}, new Metadata { { "macaroon", node.ChannelAdminMacaroon } });
}, new Metadata {{"macaroon", node.ChannelAdminMacaroon}});
}

public void FundingStateStepCancel(Node node, byte[] pendingChannelId, Lightning.LightningClient? client = null)
Expand All @@ -286,6 +301,6 @@ public void FundingStateStepCancel(Node node, byte[] pendingChannelId, Lightning
{
PendingChanId = ByteString.CopyFrom(pendingChannelId)
}
}, new Metadata { { "macaroon", node.ChannelAdminMacaroon } });
}, new Metadata {{"macaroon", node.ChannelAdminMacaroon}});
}
}

0 comments on commit 1c12a45

Please sign in to comment.