Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable jobs for disabled nodes on restart #394

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Data/Repositories/Interfaces/INodeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface INodeRepository

Task<List<Node>> GetAllManagedByUser(string userId);

Task<List<Node>> GetAllManagedByNodeGuard();
Task<List<Node>> GetAllManagedByNodeGuard(bool withDisabled = true);

Task<(bool, string?)> AddAsync(Node type);

Expand All @@ -45,4 +45,4 @@ public interface INodeRepository
(bool, string?) RemoveRange(List<Node> types);

(bool, string?) Update(Node type);
}
}
9 changes: 7 additions & 2 deletions src/Data/Repositories/NodeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task<List<Node>> GetAll()
.ToListAsync();
}

public async Task<List<Node>> GetAllManagedByNodeGuard()
public async Task<List<Node>> GetAllManagedByNodeGuard(bool withDisabled = true)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

Expand All @@ -119,6 +119,11 @@ public async Task<List<Node>> GetAllManagedByNodeGuard()
.ThenInclude(x => x.Keys)
.Include(x => x.ReturningFundsWallet)
.Where(node => node.Endpoint != null);
if (!withDisabled)
{
query = query.Where(node => !node.IsNodeDisabled);

}

var resultAsync = await query.ToListAsync();

Expand Down Expand Up @@ -182,4 +187,4 @@ public async Task<List<Node>> GetAllManagedByUser(string userId)
return _repository.Update(type, applicationDbContext);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Jobs/ChannelAcceptorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task Execute(IJobExecutionContext context)
_logger.LogInformation("Starting {JobName}... ", nameof(ChannelAcceptorJob));
try
{
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard();
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard(false);

var scheduler = await _schedulerFactory.GetScheduler();
foreach (var managedNode in managedNodes)
Expand All @@ -70,4 +70,4 @@ public async Task Execute(IJobExecutionContext context)

_logger.LogInformation("{JobName} ended", nameof(ChannelAcceptorJob));
}
}
}
4 changes: 2 additions & 2 deletions src/Jobs/MonitorChannelsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task Execute(IJobExecutionContext context)
_logger.LogInformation("Starting {JobName}... ", nameof(MonitorChannelsJob));
try
{
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard();
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard(false);

var scheduler = await _schedulerFactory.GetScheduler();

Expand Down Expand Up @@ -51,4 +51,4 @@ public async Task Execute(IJobExecutionContext context)

_logger.LogInformation("{JobName} ended", nameof(MonitorChannelsJob));
}
}
}
4 changes: 2 additions & 2 deletions src/Jobs/NodeChannelSubscribeJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task Execute(IJobExecutionContext context)
{
_logger.LogError(e, "Error while subscribing for the channel updates of node {NodeId}", nodeId);
//Sleep to avoid massive requests
await Task.Delay(1000);
await Task.Delay(5000);

throw new JobExecutionException(e, true);
}
Expand Down Expand Up @@ -176,4 +176,4 @@ public async Task NodeUpdateManagement(ChannelEventUpdate channelEventUpdate, No
break;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Jobs/NodeSubscriptorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task Execute(IJobExecutionContext context)
_logger.LogInformation("Starting {JobName}... ", nameof(NodeSubscriptorJob));
try
{
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard();
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard(false);

var scheduler = await _schedulerFactory.GetScheduler();

Expand All @@ -48,4 +48,4 @@ public async Task Execute(IJobExecutionContext context)

_logger.LogInformation("{JobName} ended", nameof(NodeSubscriptorJob));
}
}
}
4 changes: 2 additions & 2 deletions src/Jobs/ProcessNodeChannelAcceptorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ await AcceptChannelOpeningRequestWithUpfrontShutdown(_nBXplorerService,
_logger.LogError(e, "Error on {JobName}", nameof(ProcessNodeChannelAcceptorJob));

//Sleep to avoid massive requests
await Task.Delay(1000);
await Task.Delay(5000);

throw new JobExecutionException(e, true);

}
}
}
}
4 changes: 2 additions & 2 deletions src/Jobs/SweepAllNodesWalletsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Execute(IJobExecutionContext context)
_logger.LogInformation("Starting {JobName}... ", nameof(SweepAllNodesWalletsJob));
try
{
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard();
var managedNodes = await _nodeRepository.GetAllManagedByNodeGuard(false);

var scheduler = await _schedulerFactory.GetScheduler();
foreach (var managedNode in managedNodes.Where(managedNode => managedNode.ChannelAdminMacaroon != null && managedNode.AutosweepEnabled))
Expand All @@ -72,4 +72,4 @@ public async Task Execute(IJobExecutionContext context)
_logger.LogInformation("{JobName} ended", nameof(SweepAllNodesWalletsJob));
}
}
}
}
2 changes: 1 addition & 1 deletion src/Pages/Wallets.razor
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@
var withdrawalRequest = new WalletWithdrawalRequest
{
UserRequestorId = LoggedUser != null ? LoggedUser.Id : string.Empty,
Description = @$"Funds transferred from {_sourceWalletName} to {_targetWalletName}",
Description = $"Funds transferred from {_sourceWalletName} to {_targetWalletName}",
WithdrawAllFunds = _transferAllFunds,
DestinationAddress = targetBitcoinAddress.ToString(),
MempoolRecommendedFeesType = MempoolRecommendedFeesType.HourFee,
Expand Down
10 changes: 5 additions & 5 deletions test/NodeGuard.Tests/Rpc/NodeGuardServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ public async Task GetNodes_RequestIncludeUnmanaged_ReturnsUnmanagedNodes()
};

nodeRepositoryMock.Setup(repo => repo.GetAll()).ReturnsAsync(sampleNodes);
nodeRepositoryMock.Setup(repo => repo.GetAllManagedByNodeGuard()).ReturnsAsync(new List<Node>());
nodeRepositoryMock.Setup(repo => repo.GetAllManagedByNodeGuard(It.IsAny<bool>())).ReturnsAsync(new List<Node>());

var service = new NodeGuardService(
_logger.Object, new Mock<ILiquidityRuleRepository>().Object,
Expand Down Expand Up @@ -1135,7 +1135,7 @@ public async Task GetNodes_RequestIncludeUnmanaged_ReturnsUnmanagedNodes()
// Assert
response.Nodes.Count.Should().Be(sampleNodes.Count);
nodeRepositoryMock.Verify(repo => repo.GetAll(), Times.Once);
nodeRepositoryMock.Verify(repo => repo.GetAllManagedByNodeGuard(), Times.Never);
nodeRepositoryMock.Verify(repo => repo.GetAllManagedByNodeGuard(It.IsAny<bool>()), Times.Never);
}

[Fact]
Expand All @@ -1150,7 +1150,7 @@ public async Task GetNodes_RequestNotIncludeUnmanaged_ReturnsManagedNodes()
};

nodeRepositoryMock.Setup(repo => repo.GetAll()).ReturnsAsync(new List<Node>());
nodeRepositoryMock.Setup(repo => repo.GetAllManagedByNodeGuard()).ReturnsAsync(sampleNodes);
nodeRepositoryMock.Setup(repo => repo.GetAllManagedByNodeGuard(It.IsAny<bool>())).ReturnsAsync(sampleNodes);

var service = new NodeGuardService(
_logger.Object, new Mock<ILiquidityRuleRepository>().Object,
Expand Down Expand Up @@ -1178,7 +1178,7 @@ public async Task GetNodes_RequestNotIncludeUnmanaged_ReturnsManagedNodes()
// Assert
response.Nodes.Count.Should().Be(sampleNodes.Count);
nodeRepositoryMock.Verify(repo => repo.GetAll(), Times.Never);
nodeRepositoryMock.Verify(repo => repo.GetAllManagedByNodeGuard(), Times.Once);
nodeRepositoryMock.Verify(repo => repo.GetAllManagedByNodeGuard(It.IsAny<bool>()), Times.Once);
}


Expand Down Expand Up @@ -1307,4 +1307,4 @@ await act
"Status(StatusCode=\"Internal\", Detail=\"You can't select wallets by type and id at the same time\")");
}
}
}
}
8 changes: 4 additions & 4 deletions test/NodeGuard.Tests/Services/BitcoinServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ async Task PerformWithdrawal_SingleSigSucceeds()
.Setup(x => x.BroadcastAsync(It.IsAny<Transaction>(), default, default))
.ReturnsAsync(new BroadcastResult() { Success = true });
nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(new List<Node>() {node}));
var bitcoinService = new BitcoinService(_logger, null, walletWithdrawalRequestRepository.Object, null, nodeRepository.Object, null, nbXplorerService.Object, null);

Expand Down Expand Up @@ -651,7 +651,7 @@ async Task PerformWithdrawal_MultiSigSucceeds()
.Setup(x => x.BroadcastAsync(It.IsAny<Transaction>(), default, default))
.ReturnsAsync(new BroadcastResult() { Success = true });
nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(new List<Node>() {node}));
var bitcoinService = new BitcoinService(_logger, null, walletWithdrawalRequestRepository.Object, null, nodeRepository.Object, null, nbXplorerService.Object, null);

Expand Down Expand Up @@ -731,7 +731,7 @@ async Task PerformWithdrawal_LegacyMultiSigSucceeds()
.Setup(x => x.BroadcastAsync(It.IsAny<Transaction>(), default, default))
.ReturnsAsync(new BroadcastResult() { Success = true });
nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(new List<Node>() {node}));
var bitcoinService = new BitcoinService(_logger, null, walletWithdrawalRequestRepository.Object, null, nodeRepository.Object, null, nbXplorerService.Object, null);

Expand All @@ -741,4 +741,4 @@ async Task PerformWithdrawal_LegacyMultiSigSucceeds()
// Assert
await act.Should().NotThrowAsync();
}
}
}
20 changes: 10 additions & 10 deletions test/NodeGuard.Tests/Services/LightningServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public async Task OpenChannel_SuccessLegacyMultiSig()
var nodes = new List<Node> { destinationNode };

nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(nodes));

var lightningClientService = new Mock<ILightningClientService>();
Expand Down Expand Up @@ -597,7 +597,7 @@ public async Task OpenChannel_SuccessMultiSig()
var nodes = new List<Node> { destinationNode };

nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(nodes));

var lightningClient = new Mock<ILightningClientService>();
Expand Down Expand Up @@ -810,7 +810,7 @@ public async Task OpenChannel_SuccessSingleSigBip39()
var nodes = new List<Node> { destinationNode };

nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(nodes));

var lightningClient = new Mock<ILightningClientService>();
Expand Down Expand Up @@ -1029,7 +1029,7 @@ public async Task OpenChannel_FailedIncorrectNumberOfHumanSigs()
var nodes = new List<Node> { destinationNode };

nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(nodes));

var lightningClient = new Mock<ILightningClientService>();
Expand Down Expand Up @@ -1242,7 +1242,7 @@ public async Task OpenChannel_SuccessSingleSig()
var nodes = new List<Node> { destinationNode };

nodeRepository
.Setup(x => x.GetAllManagedByNodeGuard())
.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>()))
.Returns(Task.FromResult(nodes));

var lightningClient = new Mock<ILightningClientService>();
Expand Down Expand Up @@ -1588,7 +1588,7 @@ public async Task GetChannelsStatus_SourceNodeIsManaged_SourceIsInitiator()
var nodeRepository = new Mock<INodeRepository>();
var lightningClientService = new Mock<ILightningClientService>();

nodeRepository.Setup(x => x.GetAllManagedByNodeGuard()).ReturnsAsync(
nodeRepository.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>())).ReturnsAsync(
new List<Node>()
{
new()
Expand Down Expand Up @@ -1632,7 +1632,7 @@ public async Task GetChannelsStatus_SourceNodeIsManaged_SourceIsNotInitiator()
var nodeRepository = new Mock<INodeRepository>();
var lightningClientService = new Mock<ILightningClientService>();

nodeRepository.Setup(x => x.GetAllManagedByNodeGuard()).ReturnsAsync(
nodeRepository.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>())).ReturnsAsync(
new List<Node>()
{
new()
Expand Down Expand Up @@ -1676,7 +1676,7 @@ public async Task GetChannelsStatus_BothNodesAreManaged_SourceIsInitiator()
var nodeRepository = new Mock<INodeRepository>();
var lightningClientService = new Mock<ILightningClientService>();

nodeRepository.Setup(x => x.GetAllManagedByNodeGuard()).ReturnsAsync(
nodeRepository.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>())).ReturnsAsync(
new List<Node>()
{
new()
Expand Down Expand Up @@ -1743,7 +1743,7 @@ public async Task GetChannelsStatus_BothNodesAreManaged_SourceIsNotInitiator()
var nodeRepository = new Mock<INodeRepository>();
var lightningClientService = new Mock<ILightningClientService>();

nodeRepository.Setup(x => x.GetAllManagedByNodeGuard()).ReturnsAsync(
nodeRepository.Setup(x => x.GetAllManagedByNodeGuard(It.IsAny<bool>())).ReturnsAsync(
new List<Node>()
{
new()
Expand Down Expand Up @@ -1803,4 +1803,4 @@ public async Task GetChannelsStatus_BothNodesAreManaged_SourceIsNotInitiator()
channelStatus[0].RemoteBalance.Should().Be(0);
}
}
}
}
Loading