Fix charge listener bug, and decouple charge from clightning in test docker compose

This commit is contained in:
nicolas.dorier 2018-03-17 19:26:30 +09:00
parent 81328b2667
commit 09f97915d6
6 changed files with 100 additions and 35 deletions

View File

@ -26,7 +26,7 @@ docker-compose down
If you want to stop, and remove all existing data If you want to stop, and remove all existing data
``` ```
docker-compose down -v docker-compose down --v
``` ```
You can run the tests inside a container by running You can run the tests inside a container by running
@ -35,7 +35,9 @@ You can run the tests inside a container by running
docker-compose run --rm tests docker-compose run --rm tests
``` ```
## Send commands to bitcoind ## How to manually test payments
### Using the test bitcoin-cli
You can call bitcoin-cli inside the container with `docker exec`, for example, if you want to send `0.23111090` to `mohu16LH66ptoWGEL1GtP6KHTBJYXMWhEf`: You can call bitcoin-cli inside the container with `docker exec`, for example, if you want to send `0.23111090` to `mohu16LH66ptoWGEL1GtP6KHTBJYXMWhEf`:
``` ```
@ -47,7 +49,28 @@ If you are using Powershell:
.\docker-bitcoin-cli.ps1 sendtoaddress "mohu16LH66ptoWGEL1GtP6KHTBJYXMWhEf" 0.23111090 .\docker-bitcoin-cli.ps1 sendtoaddress "mohu16LH66ptoWGEL1GtP6KHTBJYXMWhEf" 0.23111090
``` ```
For sending to litecoin, use .\docker-litecoin-cli.ps1 instead. ### Using the test litecoin-cli
Same as bitcoin-cli, but with `.\docker-litecoin-cli.ps1` instead.
### Using the test lightning-cli
```
docker exec -ti btcpayservertests_customer_lightningd_1 lightning-cli pay lnbcrt100u1pd2e6uspp5ajnadvhazjrz55twd5k6yeg9u87wpw0q2fdr7g960yl5asv5fmnqdq9d3hkccqpxmedyrk0ehw5ueqx5e0r4qrrv74cewddfcvsxaawqz7634cmjj39sqwy5tvhz0hasktkk6t9pqfdh3edmf3z09zst5y7khv3rvxh8ctqqw6mwhh
```
If you are using Powershell:
```
.\docker-customer-lightning-cli.ps1 pay lnbcrt100u1pd2e6uspp5ajnadvhazjrz55twd5k6yeg9u87wpw0q2fdr7g960yl5asv5fmnqdq9d3hkccqpxmedyrk0ehw5ueqx5e0r4qrrv74cewddfcvsxaawqz7634cmjj39sqwy5tvhz0hasktkk6t9pqfdh3edmf3z09zst5y7khv3rvxh8ctqqw6mwhh
```
If you get this message:
```
{ "code" : 205, "message" : "Could not find a route", "data" : { "getroute_tries" : 1, "sendpay_tries" : 0 } }
```
Please, run the test `CanSetLightningServer`, this will establish a channel between the customer and the merchant, then, retry.
## FAQ ## FAQ

View File

@ -102,6 +102,7 @@ namespace BTCPayServer.Tests
await ExplorerNode.SendToAddressAsync(address, Money.Coins(0.2m)); await ExplorerNode.SendToAddressAsync(address, Money.Coins(0.2m));
ExplorerNode.Generate(1); ExplorerNode.Generate(1);
await WaitLNSynched(); await WaitLNSynched();
await Task.Delay(1000);
await CustomerLightningD.FundChannelAsync(clightning, Money.Satoshis(16777215)); await CustomerLightningD.FundChannelAsync(clightning, Money.Satoshis(16777215));
break; break;
case "CHANNELD_AWAITING_LOCKIN": case "CHANNELD_AWAITING_LOCKIN":

View File

@ -17,7 +17,7 @@ services:
TESTS_POSTGRES: User ID=postgres;Host=postgres;Port=5432;Database=btcpayserver TESTS_POSTGRES: User ID=postgres;Host=postgres;Port=5432;Database=btcpayserver
TESTS_PORT: 80 TESTS_PORT: 80
TESTS_HOSTNAME: tests TESTS_HOSTNAME: tests
TEST_CUSTOMERLIGHTNINGD: http://lightningd:9835/ TEST_CUSTOMERLIGHTNINGD: http://customer_lightningd:9835/
TEST_MERCHANTCHARGE: http://api-token:foiewnccewuify@lightning-charged:9112/ TEST_MERCHANTCHARGE: http://api-token:foiewnccewuify@lightning-charged:9112/
expose: expose:
- "80" - "80"
@ -36,7 +36,8 @@ services:
links: links:
- nbxplorer - nbxplorer
- postgres - postgres
- lightningd - customer_lightningd
- merchant_lightningd
- lightning-charged - lightning-charged
nbxplorer: nbxplorer:
@ -83,24 +84,7 @@ services:
volumes: volumes:
- "bitcoin_datadir:/data" - "bitcoin_datadir:/data"
lightning-charged: customer_lightningd:
image: shesek/lightning-charge:0.3.5
environment:
NETWORK: regtest
API_TOKEN: foiewnccewuify
SKIP_BITCOIND: 1
BITCOIND_RPCCONNECT: bitcoind
volumes:
- "bitcoin_datadir:/etc/bitcoin"
expose:
- "9112" # Charge
- "9735" # Lightning
ports:
- "54938:9112" # Charge
links:
- bitcoind
lightningd:
image: nicolasdorier/clightning image: nicolasdorier/clightning
environment: environment:
LIGHTNINGD_OPT: | LIGHTNINGD_OPT: |
@ -115,6 +99,46 @@ services:
- "9835" # api port - "9835" # api port
volumes: volumes:
- "bitcoin_datadir:/etc/bitcoin" - "bitcoin_datadir:/etc/bitcoin"
- "customer_lightningd_datadir:/root/.lightning"
links:
- bitcoind
lightning-charged:
image: shesek/lightning-charge:0.3.5
environment:
NETWORK: regtest
API_TOKEN: foiewnccewuify
SKIP_BITCOIND: 1
BITCOIND_RPCCONNECT: bitcoind
volumes:
- "bitcoin_datadir:/etc/bitcoin"
- "lightning_charge_datadir:/data"
- "merchant_lightningd_datadir:/etc/lightning"
expose:
- "9112" # Charge
- "9735" # Lightning
ports:
- "54938:9112" # Charge
links:
- bitcoind
- merchant_lightningd
merchant_lightningd:
image: nicolasdorier/clightning
environment:
LIGHTNINGD_OPT: |
bitcoin-datadir=/etc/bitcoin
bitcoin-rpcconnect=bitcoind
network=regtest
log-level=debug
ports:
- "30993:9835" # api port
expose:
- "9735" # server port
- "9835" # api port
volumes:
- "bitcoin_datadir:/etc/bitcoin"
- "merchant_lightningd_datadir:/root/.lightning"
links: links:
- bitcoind - bitcoind
@ -145,3 +169,6 @@ services:
volumes: volumes:
bitcoin_datadir: bitcoin_datadir:
customer_lightningd_datadir:
merchant_lightningd_datadir:
lightning_charge_datadir:

View File

@ -0,0 +1 @@
docker exec -ti btcpayservertests_customer_lightningd_1 lightning-cli $args

View File

@ -60,6 +60,8 @@ namespace BTCPayServer.Payments.Lightning
private async Task EnsureListening(string invoiceId, bool poll) private async Task EnsureListening(string invoiceId, bool poll)
{ {
if (Listening(invoiceId))
return;
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId); var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
foreach (var paymentMethod in invoice.GetPaymentMethods(_NetworkProvider) foreach (var paymentMethod in invoice.GetPaymentMethods(_NetworkProvider)
.Where(c => c.GetId().PaymentType == PaymentTypes.LightningLike)) .Where(c => c.GetId().PaymentType == PaymentTypes.LightningLike))
@ -95,12 +97,9 @@ namespace BTCPayServer.Payments.Lightning
continue; continue;
} }
if (!Listening(invoiceId))
{
StartListening(listenedInvoice); StartListening(listenedInvoice);
} }
} }
}
TimeSpan _PollInterval = TimeSpan.FromMinutes(1.0); TimeSpan _PollInterval = TimeSpan.FromMinutes(1.0);
public TimeSpan PollInterval public TimeSpan PollInterval
@ -157,6 +156,7 @@ namespace BTCPayServer.Payments.Lightning
catch (Exception ex) catch (Exception ex)
{ {
Logs.PayServer.LogError(ex, $"{supportedPaymentMethod.CryptoCode} (Lightning): Error while contacting {supportedPaymentMethod.GetLightningChargeUrl(false)}"); Logs.PayServer.LogError(ex, $"{supportedPaymentMethod.CryptoCode} (Lightning): Error while contacting {supportedPaymentMethod.GetLightningChargeUrl(false)}");
DoneListening(supportedPaymentMethod.GetLightningChargeUrl(false));
} }
Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Stop listening {supportedPaymentMethod.GetLightningChargeUrl(false)}"); Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Stop listening {supportedPaymentMethod.GetLightningChargeUrl(false)}");
} }
@ -202,6 +202,23 @@ namespace BTCPayServer.Payments.Lightning
return false; return false;
} }
/// <summary>
/// Stop listening all invoices on this server
/// </summary>
/// <param name="uri"></param>
private void DoneListening(Uri uri)
{
lock (_ListenedInvoiceByChargeInvoiceId)
{
foreach (var listenedInvoice in _ListenedInvoiceByLightningUrl[uri.AbsoluteUri])
{
_ListenedInvoiceByChargeInvoiceId.Remove(listenedInvoice.PaymentMethodDetails.InvoiceId);
_InvoiceIds.Remove(listenedInvoice.InvoiceId);
}
_ListenedInvoiceByLightningUrl.Remove(uri.AbsoluteUri);
}
}
bool Listening(string invoiceId) bool Listening(string invoiceId)
{ {
lock (_ListenedInvoiceByLightningUrl) lock (_ListenedInvoiceByLightningUrl)
@ -230,10 +247,6 @@ namespace BTCPayServer.Payments.Lightning
{ {
var listen = Listen(listenedInvoice.SupportedPaymentMethod, listenedInvoice.Network); var listen = Listen(listenedInvoice.SupportedPaymentMethod, listenedInvoice.Network);
_ListeningLightning.Add(listen); _ListeningLightning.Add(listen);
listen.ContinueWith(_ =>
{
DoneListening(listenedInvoice);
}, TaskScheduler.Default);
} }
_ListenedInvoiceByLightningUrl.Add(listenedInvoice.Uri, listenedInvoice); _ListenedInvoiceByLightningUrl.Add(listenedInvoice.Uri, listenedInvoice);
_ListenedInvoiceByChargeInvoiceId.Add(listenedInvoice.PaymentMethodDetails.InvoiceId, listenedInvoice); _ListenedInvoiceByChargeInvoiceId.Add(listenedInvoice.PaymentMethodDetails.InvoiceId, listenedInvoice);

View File

@ -126,13 +126,13 @@ namespace BTCPayServer.Services.Wallets
} }
catch catch
{ {
Logs.PayServer.LogError("Call to NBXplorer GetUTXOsAsync timed out, this should never happen, please report this issue to NBXplorer developers"); Logs.PayServer.LogError($"{Network.CryptoCode}: Call to NBXplorer GetUTXOsAsync timed out, this should never happen, please report this issue to NBXplorer developers");
throw; throw;
} }
var spentTime = DateTimeOffset.UtcNow - now; var spentTime = DateTimeOffset.UtcNow - now;
if (spentTime.TotalSeconds > 30) if (spentTime.TotalSeconds > 30)
{ {
Logs.PayServer.LogWarning($"NBXplorer took {(int)spentTime.TotalSeconds} seconds to reply, there is something wrong, please report this issue to NBXplorer developers"); Logs.PayServer.LogWarning($"{Network.CryptoCode}: NBXplorer took {(int)spentTime.TotalSeconds} seconds to reply, there is something wrong, please report this issue to NBXplorer developers");
} }
entry.AbsoluteExpiration = DateTimeOffset.UtcNow + CacheSpan; entry.AbsoluteExpiration = DateTimeOffset.UtcNow + CacheSpan;
return result; return result;