Skip to content

Commit

Permalink
Wallet: Start and stop node
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisreimann committed May 6, 2024
1 parent 8fbb48b commit d38dabf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 43 deletions.
79 changes: 43 additions & 36 deletions BTCPayApp.Core/Attempt2/LightningNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private set

public event AsyncEventHandler<(LightningNodeState Old, LightningNodeState New)>? StateChanged;


public LightningNodeManager(
IDbContextFactory<AppDbContext> dbContextFactory,
ILogger<LightningNodeManager> logger,
Expand All @@ -52,6 +51,35 @@ public LightningNodeManager(
_serviceScopeFactory = serviceScopeFactory;
}

public async Task StartNode()
{
if (_nodeScope is not null || State is LightningNodeState.Loaded)
return;
await _controlSemaphore.WaitAsync();

try
{
if (_nodeScope is null)
{
_nodeScope = _serviceScopeFactory.CreateScope();
_cancellationTokenSource = new CancellationTokenSource();
}
await Node.StartAsync(_cancellationTokenSource.Token);

State = LightningNodeState.Loaded;
}
catch (Exception e)
{
_nodeScope.Dispose();
_logger.LogError(e, "Error while starting lightning node");
_nodeScope = null;
State = LightningNodeState.Error;
}
finally
{
_controlSemaphore.Release();
}
}

public async Task StopNode()
{
Expand All @@ -67,36 +95,33 @@ public async Task StopNode()
finally
{
_controlSemaphore.Release();
State = LightningNodeState.Init;
State = LightningNodeState.Stopped;
}
}

public async Task CleanseTask()
{
await StopNode();
await StopNode();

if (_nodeScope is not null || State == LightningNodeState.NodeNotConfigured)
{
return;
}

await _controlSemaphore.WaitAsync();
try
{



await _onChainWalletManager.RemoveDerivation(WalletDerivation.LightningScripts);
await using var context = await _dbContextFactory.CreateDbContextAsync();
context.LightningPayments.RemoveRange(context.LightningPayments);
context.LightningChannels.RemoveRange(context.LightningChannels);
context.Settings.RemoveRange(context.Settings.Where(s => new string[]{"ChannelManager","NetworkGraph","Score","lightningconfig"}.Contains(s.Key)));
await context.SaveChangesAsync();
context.LightningPayments.RemoveRange(context.LightningPayments);
context.LightningChannels.RemoveRange(context.LightningChannels);
context.Settings.RemoveRange(context.Settings.Where(s => new string[]{"ChannelManager","NetworkGraph","Score","lightningconfig"}.Contains(s.Key)));
await context.SaveChangesAsync();
}
finally
{
_controlSemaphore.Release();

State = LightningNodeState.NodeNotConfigured;
}
}
Expand Down Expand Up @@ -148,11 +173,9 @@ private async Task OnChainWalletManagerOnStateChanged(object? sender,

private async Task OnOnStateChanged(object? sender, (LightningNodeState Old, LightningNodeState New) state)
{

LightningNodeState? newState = null;
try
{
await _controlSemaphore.WaitAsync();
switch (state.New)
{
case LightningNodeState.WaitingForConnection:
Expand Down Expand Up @@ -181,31 +204,16 @@ private async Task OnOnStateChanged(object? sender, (LightningNodeState Old, Lig
break;
}

if (_nodeScope is null)
{
_nodeScope = _serviceScopeFactory.CreateScope();
_cancellationTokenSource = new CancellationTokenSource();
}

try
{
await Node.StartAsync(_cancellationTokenSource.Token);
newState = LightningNodeState.Loaded;
}
catch (Exception e)
{
_nodeScope.Dispose();
_logger.LogError(e, "Error while starting lightning node");
_nodeScope = null;
State = LightningNodeState.Error;
}
await StartNode();

break;

case LightningNodeState.Loaded:
await _controlSemaphore.WaitAsync();

await _btcPayConnectionManager.HubProxy.MasterNodePong(_onChainWalletManager.WalletConfig
.Derivations[WalletDerivation.LightningScripts].Identifier, true);
_controlSemaphore.Release();
break;
// case LightningNodeState.Unloading:
// _nodeScope?.Dispose();
Expand All @@ -217,7 +225,6 @@ await _btcPayConnectionManager.HubProxy.MasterNodePong(_onChainWalletManager.Wal
}
finally
{
_controlSemaphore.Release();
if (newState is not null)
State = newState.Value;
}
Expand All @@ -241,4 +248,4 @@ protected override async Task ExecuteStopAsync(CancellationToken cancellationTok
StateChanged -= OnOnStateChanged;
_nodeScope?.Dispose();
}
}
}
1 change: 1 addition & 0 deletions BTCPayApp.Core/Attempt2/LightningNodeState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum LightningNodeState
WaitingForConnection,
Loading,
Loaded,
Stopped,
Error
}
8 changes: 3 additions & 5 deletions BTCPayApp.UI/Pages/DashboardPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,16 @@

private SetupState SetupStateOnchain() {
if (State.Value.Loading.Contains(RootState.LoadingHandles.LightningState) ||
State.Value.OnchainWalletState == OnChainWalletState.Init ||
State.Value.OnchainWalletState == OnChainWalletState.Loading ||
State.Value.OnchainWalletState is OnChainWalletState.Init or OnChainWalletState.Loading ||
State.Value.BTCPayServerConnectionState != HubConnectionState.Connected) return SetupState.Undetermined;
return State.Value.OnchainWalletState == OnChainWalletState.Loaded ? SetupState.Completed : SetupState.Pending;
}

private SetupState SetupStateLightning() {
if (State.Value.Loading.Contains(RootState.LoadingHandles.WalletState) ||
State.Value.LightningNodeState == LightningNodeState.Init ||
State.Value.LightningNodeState == LightningNodeState.Loading ||
State.Value.LightningNodeState is LightningNodeState.Init or LightningNodeState.Loading ||
State.Value.BTCPayServerConnectionState != HubConnectionState.Connected) return SetupState.Undetermined;
if (State.Value.LightningNodeState == LightningNodeState.Error) return SetupState.Failed;
if (State.Value.LightningNodeState is LightningNodeState.Error or LightningNodeState.Stopped) return SetupState.Failed;
return State.Value.LightningNodeState == LightningNodeState.Loaded ? SetupState.Completed : SetupState.Pending;
}

Expand Down
11 changes: 9 additions & 2 deletions BTCPayApp.UI/Pages/Wallet/IndexPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@
{
<button class="btn btn-primary" @onclick="ConfigureLNWallet">Configure Lightning Wallet</button>
}
else if(State.Value.LightningNodeState is LightningNodeState.Loaded or LightningNodeState.Error )
else if (State.Value.LightningNodeState is LightningNodeState.Loaded or LightningNodeState.Stopped or LightningNodeState.Error)
{
<button class="btn btn-danger" type="button" @onclick="LightningNodeManager.CleanseTask">DESTROY LN NODE</button>
<button class="btn btn-secondary" type="button" @onclick="LightningNodeManager.StopNode">STOP</button>
@if (State.Value.LightningNodeState is LightningNodeState.Stopped)
{
<button class="btn btn-secondary" type="button" @onclick="LightningNodeManager.StartNode">START</button>
}
else
{
<button class="btn btn-secondary" type="button" @onclick="LightningNodeManager.StopNode">STOP</button>
}
var groupId = OnChainWalletManager.WalletConfig?.Derivations[WalletDerivation.LightningScripts]?.Identifier;
<input class="form-control" type="text" value="type=app;group=@groupId" />
}
Expand Down

0 comments on commit d38dabf

Please sign in to comment.