Skip to content

Commit

Permalink
feat: refactor registration sequence (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored Sep 22, 2024
1 parent 5a3fe02 commit 2c9c2bd
Show file tree
Hide file tree
Showing 28 changed files with 1,064 additions and 569 deletions.
2 changes: 1 addition & 1 deletion dkg-common
4 changes: 2 additions & 2 deletions dkgNodeLibrary/Services/DkgNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ public async Task Register(HttpClient httpClient)
}
else
{
Logger.LogInformation("'{Name}': registered with '{ServiceNodeUrl}' [Round {roundId}]",
Logger.LogInformation("'{Name}': waiting registration with '{ServiceNodeUrl}' [Round {roundId}]",
Name, ServiceNodeUrl, roundId);
SetStatusOnly(WaitingRoundStart);
// SetStatusOnly(WaitingRoundStart);
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions dkgNodesTests/NodeComparer.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void Compare_BothNodesNull_ReturnsZero()
[Test]
public void Compare_FirstNodeNull_ReturnsOne()
{
// Arrange

var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node = new Node() { Id = 100181 };
var node = new Node() { Address = "100181" };

var history1 = new NodesRoundHistory { NodeId = 100181, RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeId = 100182, RoundId = 1, NodeRandom = 3 };
var history1 = new NodesRoundHistory { NodeAddress = "100181", RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeAddress = "100182", RoundId = 1, NodeRandom = 3 };
nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history2);

Expand All @@ -61,7 +61,7 @@ public void Compare_FirstNodeNull_ReturnsOne()
public void Compare_SecondNodeNull_ReturnsMinusOne()
{
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node = new Node { Id = 1001810 };
var node = new Node { Address = "1001810" };
var result = comparer.Compare(node, null);
Assert.That(result, Is.EqualTo(0));
}
Expand All @@ -70,11 +70,11 @@ public void Compare_SecondNodeNull_ReturnsMinusOne()
public void Compare_BothNodesHaveSameNodeRandom_ReturnsZero()
{
var comparer = new NodeComparer(0, 1,nodesRoundHistoryCache);
var node1 = new Node { Id = 1001811 };
var node2 = new Node { Id = 1001812 };
var node1 = new Node { Address = "1001811" };
var node2 = new Node { Address = "1001812" };

var history1 = new NodesRoundHistory { NodeId = 1001811, RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeId = 1001812, RoundId = 1, NodeRandom = 5 };
var history1 = new NodesRoundHistory { NodeAddress = "1001811", RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeAddress = "1001812", RoundId = 1, NodeRandom = 5 };

nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history2);
Expand All @@ -87,11 +87,11 @@ public void Compare_BothNodesHaveSameNodeRandom_ReturnsZero()
public void Compare_FirstNodeHasSmallerNodeRandom_ReturnsMinusOne()
{
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node1 = new Node { Id = 1001821 };
var node2 = new Node { Id = 1001822 };
var node1 = new Node { Address = "1001821" };
var node2 = new Node { Address = "1001822" };

var history1 = new NodesRoundHistory { NodeId = 1001821, RoundId = 1, NodeRandom = 3 };
var history2 = new NodesRoundHistory { NodeId = 1001822, RoundId = 1, NodeRandom = 5 };
var history1 = new NodesRoundHistory { NodeAddress = "1001821", RoundId = 1, NodeRandom = 3 };
var history2 = new NodesRoundHistory { NodeAddress = "1001822", RoundId = 1, NodeRandom = 5 };

nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history2);
Expand All @@ -104,11 +104,11 @@ public void Compare_FirstNodeHasSmallerNodeRandom_ReturnsMinusOne()
public void Compare_SecondNodeHasSmallerNodeRandom_ReturnsOne()
{
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node1 = new Node { Id = 1001831 };
var node2 = new Node { Id = 1001832 };
var node1 = new Node { Address = "1001831" };
var node2 = new Node { Address = "1001832" };

var history1 = new NodesRoundHistory { NodeId = 1001831, RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeId = 1001832, RoundId = 1, NodeRandom = 3 };
var history1 = new NodesRoundHistory { NodeAddress = "1001831", RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeAddress = "1001832", RoundId = 1, NodeRandom = 3 };

nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.SaveNodesRoundHistoryToCache(history2);
Expand Down
42 changes: 16 additions & 26 deletions dkgServiceNode/Controllers/DControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using dkgCommon.Constants;
using Microsoft.EntityFrameworkCore;
using dkgServiceNode.Services.Cache;
using System.Xml.Linq;
using System.Net.NetworkInformation;

namespace dkgServiceNode.Controllers
{
Expand Down Expand Up @@ -140,9 +142,10 @@ protected ObjectResult _404CurrentVersion()
return StatusCode(StatusCodes.Status404NotFound,
new { message = "Failed to found current database version." });
}
protected ObjectResult _404Node(int id)
protected ObjectResult _404Node(string address)
{
return _404(id, "Node");
return StatusCode(StatusCodes.Status404NotFound,
new { message = $"Failed to find node [@{address}]." });
}
protected ObjectResult _404Node(string address, string name)
{
Expand Down Expand Up @@ -203,45 +206,32 @@ protected DControllerBase(IHttpContextAccessor httpContextAccessor, UserContext
}
}

protected async Task ResetNodeState(DkgContext dkgContext, Node node)
protected void ResetNodeState(NodeCompositeContext ncContext, Node node)
{
bool needsUpdate = false;
if (node.StatusValue != (short)NStatus.NotRegistered)
{
node.StatusValue = (short)NStatus.NotRegistered;
needsUpdate = true;
}

if (node.RoundId != null)
if (node.Status != NStatus.NotRegistered || node.RoundId != null)
{
node.Status = NStatus.NotRegistered;
node.RoundId = null;
needsUpdate = true;
}

if (needsUpdate)
{
await dkgContext.UpdateNodeAsync(node);
ncContext.UpdateNode(node);
}
}

protected async Task ResetNodeStates(DkgContext dkgContext, List<Node> nodes)
protected void ResetNodeStates(NodeCompositeContext ncContext, List<Node> nodes)
{
List<Task> tasks = [];
foreach (var node in nodes)
{
tasks.Add(ResetNodeState(dkgContext, node));
ResetNodeState(ncContext, node);
}
await Task.WhenAll(tasks);
}

protected async Task UpdateNodeState(DkgContext dkgContext, Node node, short nStatus, int? roundId)
protected void UpdateNodeState(NodeCompositeContext ncContext, Node node, NStatus nStatus, int? roundId)
{
if (node.StatusValue != nStatus || node.RoundId != roundId)
if (node.Status != nStatus || node.RoundId != roundId)
{
node.StatusValue = nStatus;
node.Status = nStatus;
node.RoundId = roundId;
await dkgContext.UpdateNodeAsync(node);
ncContext.UpdateNode(node);
}
}
}
}
}
67 changes: 32 additions & 35 deletions dkgServiceNode/Controllers/NodesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ namespace dkgServiceNode.Controllers

public class NodesController : DControllerBase
{
protected readonly DkgContext dkgContext;
protected readonly NodeCompositeContext ncContext;
protected readonly NodeContext nodeContext;
protected readonly Runner runner;
protected readonly ILogger logger;

public NodesController(IHttpContextAccessor httpContextAccessor,
UserContext uContext, DkgContext dContext,
Runner rnner, ILogger<NodesController> lgger) :
UserContext uContext,
NodeContext noContext,
NodeCompositeContext nContext,
Runner rnner,
ILogger<NodesController> lgger) :
base(httpContextAccessor, uContext)
{
dkgContext = dContext;
nodeContext = noContext;
ncContext = nContext;
runner = rnner;
logger = lgger;
}
Expand All @@ -66,8 +71,8 @@ public ActionResult<NodesFrameResult> FetchNodes(NodesFrame nodesFrame)
Stopwatch stopwatch = new();
stopwatch.Start();

var sf = dkgContext.GetNodeCount();
var nf = dkgContext.GetFilteredNodes(nodesFrame.Search);
var sf = ncContext.GetNodeCount();
var nf = ncContext.GetFilteredNodes(nodesFrame.Search);

if (nodesFrame.SortBy != null && nodesFrame.SortBy.Length > 0)
{
Expand All @@ -80,9 +85,6 @@ public ActionResult<NodesFrameResult> FetchNodes(NodesFrame nodesFrame)
case "name":
nf = sortOrder == "asc" ? [.. nf.OrderBy(n => n.Name)] : [.. nf.OrderByDescending(n => n.Name)];
break;
case "id":
nf = sortOrder == "asc" ? [.. nf.OrderBy(n => n.Id)] : [.. nf.OrderByDescending(n => n.Id)];
break;
case "address":
nf = sortOrder == "asc" ? [.. nf.OrderBy(n => n.Address)] : [.. nf.OrderByDescending(n => n.Address)];
break;
Expand Down Expand Up @@ -112,32 +114,29 @@ public ActionResult<NodesFrameResult> FetchNodes(NodesFrame nodesFrame)
return res;
}

// RESET: api/nodes/reset/5
[HttpPost("reset/{id}")]
// RESET: api/nodes/reset/123a5
[HttpPost("reset/{address}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> ResetNode(int id)
public async Task<IActionResult> ResetNode(string address)
{
Stopwatch stopwatch = new();
stopwatch.Start();
IActionResult res;
var ch = await userContext.CheckAdmin(curUserId);
var ch = await userContext.CheckAdminAsync(curUserId);
if (ch == null || !ch.Value)
{
res = _403();
}
else
{
var node = dkgContext.GetNodeById(id);
var node = ncContext.GetNodeByAddress(address);
if (node == null)
{
res = _404Node(id);
res = _404Node(address);
}
else
{
node.StatusValue = (short)NStatus.NotRegistered;
node.RoundId = null;
await dkgContext.UpdateNodeAsync(node);

ResetNodeState(ncContext, node);
res = NoContent();
}
}
Expand All @@ -146,33 +145,31 @@ public async Task<IActionResult> ResetNode(int id)
return res;
}

// DELETE: api/nodes/5
[HttpDelete("{id}")]
// DELETE: api/nodes/123a5
[HttpDelete("{address}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> DeleteNode(int id)
public async Task<IActionResult> DeleteNode(string address)
{
Stopwatch stopwatch = new();
stopwatch.Start();

IActionResult res;

var ch = await userContext.CheckAdmin(curUserId);
var ch = await userContext.CheckAdminAsync(curUserId);
if (ch == null || !ch.Value)
{
res = _403();
}
else
{


var node = dkgContext.GetNodeById(id);
var node = ncContext.GetNodeByAddress(address);
if (node == null)
{
res = _404Node(id);
res = _404Node(address);
}
else
{
await dkgContext.DeleteNodeAsync(node);
await nodeContext.DeleteAsync(node);
res = NoContent();
}
}
Expand All @@ -189,23 +186,23 @@ public ActionResult<IEnumerable<Node>> GetNodes()
Stopwatch stopwatch = new();
stopwatch.Start();

var res = dkgContext.GetAllNodesSortedById();
var res = ncContext.GetAllNodesSortedById();

stopwatch.Stop();
UpdateE2GetAll(stopwatch.Elapsed);
return Ok(res);
}

// GET: api/nodes/5
[HttpGet("{id}")]
// GET: api/nodes/123a5
[HttpGet("{address}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Node))]
public ActionResult<Node> GetNode(int id)
public ActionResult<Node> GetNode(string address)
{
Stopwatch stopwatch = new();
stopwatch.Start();

var node = dkgContext.GetNodeById(id);
if (node == null) return _404Node(id);
var node = ncContext.GetNodeByAddress(address);
if (node == null) return _404Node(address);

stopwatch.Stop();
UpdateE2Get(stopwatch.Elapsed);
Expand All @@ -222,7 +219,7 @@ public async Task<ActionResult<IEnumerable<TimingResult>>> GetStatistics()
stopwatch.Start();

ActionResult<IEnumerable<TimingResult>> res;
var ch = await userContext.CheckAdmin(curUserId);
var ch = await userContext.CheckAdminAsync(curUserId);
if (ch == null || !ch.Value)
{
res = _403();
Expand Down
Loading

0 comments on commit 2c9c2bd

Please sign in to comment.