Skip to content

Commit fdd4dc5

Browse files
author
Noé Comte
committed
Initial commit
1 parent bbebccc commit fdd4dc5

17 files changed

Lines changed: 570 additions & 0 deletions

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.swp
2+
*.*~
3+
project.lock.json
4+
.DS_Store
5+
*.pyc
6+
nupkg/
7+
8+
# Visual Studio Code
9+
.vscode
10+
11+
# Rider
12+
.idea
13+
14+
# User-specific files
15+
*.suo
16+
*.user
17+
*.userosscache
18+
*.sln.docstates
19+
20+
# Build results
21+
[Dd]ebug/
22+
[Dd]ebugPublic/
23+
[Rr]elease/
24+
[Rr]eleases/
25+
x64/
26+
x86/
27+
build/
28+
bld/
29+
[Bb]in/
30+
[Oo]bj/
31+
[Oo]ut/
32+
msbuild.log
33+
msbuild.err
34+
msbuild.wrn
35+
36+
# Visual Studio 2015
37+
.vs/

Constants/Constants.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TradeHub.MissedBlocksAlerter
6+
{
7+
public static class Constants
8+
{
9+
public const string PUSHBULLET_BASE_URL = "https://api.pushbullet.com";
10+
public const string PUSHBULLET_ENDPOINT_DEVICES = "v2/devices";
11+
public const string PUSHBULLET_ENDPOINT_PUSH = "v2/pushes";
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TradeHub.MissedBlocksAlerter.Models.Pushbullet.Responses
6+
{
7+
public class DevicesListResponse
8+
{
9+
public List<DevicesListResponseResult> Devices { get; set; }
10+
}
11+
12+
public class DevicesListResponseResult
13+
{
14+
public string Iden { get; set; }
15+
public string Nickname { get; set; }
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TradeHub.MissedBlocksAlerter.Models.Tradescan.Responses
6+
{
7+
public class SigningInfosResponse
8+
{
9+
public long Height { get; set; }
10+
public List<SigningInfosResponseResult> Result { get; set; }
11+
}
12+
13+
public class SigningInfosResponseResult
14+
{
15+
public SigningInfosResponseResult() { }
16+
17+
public string Address { get; set; }
18+
public long MissedBlocksCounter { get; set; }
19+
}
20+
}

Program.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.IO;
7+
using System.Threading.Tasks;
8+
using TradeHub.MissedBlocksAlerter.Services;
9+
using TradeHub.MissedBlocksAlerter.Settings;
10+
11+
namespace TradeHub.MissedBlocksAlerter
12+
{
13+
class Program
14+
{
15+
static async Task Main(string[] args)
16+
{
17+
await new HostBuilder()
18+
.ConfigureServices((hostContext, services) =>
19+
{
20+
services.AddLogging(cfg => cfg.AddConsole().SetMinimumLevel(LogLevel.Debug));
21+
22+
var config = BuildConfiguration();
23+
services.Configure<AppSettings>(config);
24+
25+
services.AddScoped<PushbulletService>();
26+
services.AddScoped<TradescanService>();
27+
28+
services.AddHostedService<AppWorkerService>();
29+
})
30+
.RunConsoleAsync();
31+
}
32+
33+
static IConfigurationRoot BuildConfiguration() => new ConfigurationBuilder()
34+
.SetBasePath(Directory.GetCurrentDirectory())
35+
.AddJsonFile("appsettings.json", false, true)
36+
.Build();
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<Configuration>Release</Configuration>
8+
<Platform>Any CPU</Platform>
9+
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
10+
<PublishProtocol>FileSystem</PublishProtocol>
11+
<TargetFramework>netcoreapp3.1</TargetFramework>
12+
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
13+
<SelfContained>true</SelfContained>
14+
<PublishSingleFile>True</PublishSingleFile>
15+
<PublishTrimmed>False</PublishTrimmed>
16+
</PropertyGroup>
17+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<Configuration>Release</Configuration>
8+
<Platform>Any CPU</Platform>
9+
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
10+
<PublishProtocol>FileSystem</PublishProtocol>
11+
<TargetFramework>netcoreapp3.1</TargetFramework>
12+
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
13+
<SelfContained>true</SelfContained>
14+
<PublishSingleFile>True</PublishSingleFile>
15+
<PublishReadyToRun>True</PublishReadyToRun>
16+
<PublishTrimmed>False</PublishTrimmed>
17+
</PropertyGroup>
18+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<Configuration>Release</Configuration>
8+
<Platform>Any CPU</Platform>
9+
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
10+
<PublishProtocol>FileSystem</PublishProtocol>
11+
<TargetFramework>netcoreapp3.1</TargetFramework>
12+
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
13+
<SelfContained>true</SelfContained>
14+
<PublishSingleFile>True</PublishSingleFile>
15+
<PublishReadyToRun>True</PublishReadyToRun>
16+
<PublishTrimmed>False</PublishTrimmed>
17+
</PropertyGroup>
18+
</Project>

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# TradeHub.MissedBlocksAlerter
2+
3+
TradeHub.MissedBlocksAlerter is a missed blocks alerter for Switcheo TradeHub.
4+
It notify you on any device (phone, tablet or desktop) through Pushbullet if your configured thresholds are reached.
5+
6+
## Requirements
7+
- Your validator consensus address *(swthvalcons[...])*.
8+
- A [Pushbullet](https://www.pushbullet.com/) account with an access token.
9+
10+
## Contributing
11+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
12+
13+
## License
14+
[MIT](https://choosealicense.com/licenses/mit/)

Services/AppWorkerService.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.Options;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using TradeHub.MissedBlocksAlerter.Services;
10+
using TradeHub.MissedBlocksAlerter.Settings;
11+
12+
namespace TradeHub.MissedBlocksAlerter.Services
13+
{
14+
public class AppWorkerService : BackgroundService, IDisposable
15+
{
16+
private readonly ILogger<AppWorkerService> _logger;
17+
private readonly IOptionsSnapshot<AppSettings> _appSettings;
18+
19+
private readonly PushbulletService _pushbulletService;
20+
private readonly TradescanService _tradescanService;
21+
22+
private Timer _timer;
23+
24+
private DateTime? _lastError = null;
25+
private long _missedBlocks = 0;
26+
27+
public AppWorkerService(ILogger<AppWorkerService> logger, IOptionsSnapshot<AppSettings> appSettings, PushbulletService pushbulletService, TradescanService tradescanService)
28+
{
29+
_logger = logger;
30+
_appSettings = appSettings;
31+
_pushbulletService = pushbulletService;
32+
_tradescanService = tradescanService;
33+
}
34+
35+
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
36+
{
37+
var deviceName = _appSettings.Value.Pushbullet.Target;
38+
var deviceIden = await _pushbulletService.GetDeviceIdenByDeviceName(deviceName);
39+
if (!string.IsNullOrEmpty(deviceIden))
40+
{
41+
_logger.LogInformation($"Target: {deviceName}, Identifier: {deviceIden}");
42+
43+
await _pushbulletService.CreatePush(deviceIden, "Started successfully");
44+
45+
_timer = new Timer((state) => DoWorkAsync(deviceIden), null, TimeSpan.Zero, TimeSpan.FromSeconds(_appSettings.Value.Supervision.SecondsBetween));
46+
while (!cancellationToken.IsCancellationRequested) { }
47+
48+
await _pushbulletService.CreatePush(deviceIden, "Stopping");
49+
}
50+
else
51+
{
52+
_logger.LogError("An error occured while retrieving your target device identifier from Pushbullet API");
53+
}
54+
55+
await Task.CompletedTask;
56+
}
57+
58+
public async void DoWorkAsync(string deviceIden)
59+
{
60+
var blockHeightAndMissedBlocks = await _tradescanService.GetBlockHeightAndMissedBlocksByConsensusAddr(_appSettings.Value.Validator.ConsensusAddr);
61+
62+
if (!blockHeightAndMissedBlocks.Key.HasValue || !blockHeightAndMissedBlocks.Value.HasValue)
63+
{
64+
if (_lastError.HasValue && (DateTime.Now - _lastError.Value).TotalMinutes < _appSettings.Value.ErrorReports.MinutesBetween)
65+
return;
66+
67+
if (_appSettings.Value.ErrorReports.Enabled)
68+
{
69+
var errorMessage = "An error occurred while retrieving signing informations from Tradescan API";
70+
_logger.LogError(errorMessage);
71+
await _pushbulletService.CreatePush(deviceIden, errorMessage);
72+
73+
_lastError = DateTime.Now;
74+
}
75+
}
76+
else
77+
{
78+
var blockHeight = blockHeightAndMissedBlocks.Key.Value;
79+
var missedBlocks = blockHeightAndMissedBlocks.Value.Value;
80+
81+
_logger.LogDebug($"Height: {blockHeight}, MissedBlocks: {missedBlocks}");
82+
83+
if (missedBlocks >= _appSettings.Value.Thresholds.MaxBlocksMissed && _missedBlocks != missedBlocks)
84+
{
85+
var warningMessage = $"Your validator missed {missedBlocks} block(s)";
86+
_logger.LogWarning(warningMessage);
87+
await _pushbulletService.CreatePush(deviceIden, warningMessage);
88+
89+
_missedBlocks = missedBlocks;
90+
}
91+
}
92+
}
93+
94+
public new void Dispose()
95+
{
96+
_timer?.Dispose();
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)