Skip to content

Commit

Permalink
tests: add a test for closing a channel with LND
Browse files Browse the repository at this point in the history
Not sure if we should add this as the Dispose() method
of the test class actually:
https://stackoverflow.com/a/33516224/544947
  • Loading branch information
knocte committed Oct 6, 2020
1 parent 00a3fef commit aea9e9d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/BTCPayServer.Lightning.Common/OpenChannelResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ public OpenChannelResult Result
{
get; set;
}
public string FundingTxIdIfAvailable
{
get; set;
}
}
}
4 changes: 3 additions & 1 deletion src/BTCPayServer.Lightning.LND/LndClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ async Task<OpenChannelResponse> ILightningClient.OpenChannel(OpenChannelRequest
req.Sat_per_byte = ((int)openChannelRequest.FeeRate.SatoshiPerByte).ToString();
}
var result = await this.SwaggerClient.OpenChannelSyncAsync(req, cancellation);
return new OpenChannelResponse(OpenChannelResult.Ok);
var res = new OpenChannelResponse(OpenChannelResult.Ok);
res.FundingTxIdIfAvailable = result.Funding_txid_str;
return res;
}
catch(SwaggerException ex) when
(ex.AsLNDError() is LndError2 lndError &&
Expand Down
11 changes: 11 additions & 0 deletions tests/ChannelSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public static async Task OpenAll(RPCClient cashCow, IEnumerable<ILightningClient
}
}
}

private static Dictionary<(ILightningClient, ILightningClient), string> availableFundingTxIds
= new Dictionary<(ILightningClient, ILightningClient), string>();

internal static string GetFundingTxIdForChannel(ILightningClient sender, ILightningClient dest)
{
return availableFundingTxIds.GetValueOrDefault((sender, dest));
}

public static async Task OpenChannel(RPCClient cashCow, ILightningClient sender, ILightningClient dest)
{
var destInfo = await dest.GetInfo();
Expand Down Expand Up @@ -115,6 +124,8 @@ public static async Task OpenChannel(RPCClient cashCow, ILightningClient sender,
}
if(openChannel.Result == OpenChannelResult.Ok)
{
if (!String.IsNullOrEmpty(openChannel.FundingTxIdIfAvailable))
availableFundingTxIds[(sender, dest)] = openChannel.FundingTxIdIfAvailable;
// generate one block and a bit more time to confirm channel opening
await cashCow.GenerateAsync(1);
await WaitLNSynched(cashCow, sender);
Expand Down
25 changes: 25 additions & 0 deletions tests/CommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,5 +536,30 @@ public void CanParseLightningURL()
Assert.True(LightningConnectionString.TryParse("type=charge;server=http://api-token:[email protected]:54938/;allowinsecure=true", out conn));
Assert.Equal("type=charge;server=http://127.0.0.1:54938/;api-token=foiewnccewuify;allowinsecure=true", conn.ToString());
}

[Fact(Timeout = Timeout)]
public async Task CanCloseLndChannel()
{
// TODO: test in all LN implementations, not just LND
//foreach (var test in Tester.GetTestedPairs())
//{
var test = Tester.GetTestedLndPair();
await EnsureConnectedToDestinations(test);
var customer = (ILightningClient)test.Customer;
var senderChannel = (await customer.ListChannels()).First();

var senderInfo = await customer.GetInfo();
var fundingTxIdForChannel = ChannelSetup.GetFundingTxIdForChannel(customer, test.Merchant);
if (String.IsNullOrEmpty(fundingTxIdForChannel))
throw new InvalidOperationException("Could not find funding Tx ID of channel");
var closeChannelRequest = new CloseChannelRequest()
{
NodeInfo = senderInfo.NodeInfoList.First(),
ChannelPointOutputIndex = senderChannel.ChannelPoint.N,
ChannelPointFundingTxIdStr = fundingTxIdForChannel
};
await test.Merchant.CloseChannel(closeChannelRequest, CancellationToken.None);
//}
}
}
}
13 changes: 10 additions & 3 deletions tests/Tester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@ public static LndClient CreateLndClientDest()
yield return ("Eclair (Client)", CreateEclairClient());
}

private static (string Name, ILightningClient Customer, ILightningClient Merchant) GetTestedCLightningPair()
=> ("C-Lightning", CreateCLightningClient(), CreateCLightningClientDest());
internal static (string Name, LndClient Customer, LndClient Merchant) GetTestedLndPair()
=> ("LND", CreateLndClient(), CreateLndClientDest());
private static (string Name, ILightningClient Customer, ILightningClient Merchant) GetTestedEclairPair()
=> ("Eclair", CreateEclairClient(), CreateEclairClientDest());

public static IEnumerable<(string Name, ILightningClient Customer, ILightningClient Merchant)> GetTestedPairs()
{
yield return ("C-Lightning", CreateCLightningClient(), CreateCLightningClientDest());
yield return ("LND", CreateLndClient(), CreateLndClientDest());
yield return ("Eclair", CreateEclairClient(), CreateEclairClientDest());
yield return GetTestedCLightningPair();
yield return GetTestedLndPair();
yield return GetTestedEclairPair();
}
}
}

0 comments on commit aea9e9d

Please sign in to comment.