Skip to content

Commit

Permalink
Fixed #14, created statistics for timed out nodes, Admin user is now …
Browse files Browse the repository at this point in the history
…created on startup, default timeouts changed to 30/30/120
  • Loading branch information
maxirmx committed May 28, 2024
1 parent d6e95b0 commit eb9d438
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 81 deletions.
8 changes: 8 additions & 0 deletions dkgCommon/Constants/NodeStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum NStatus
RunningStepFour = 27,
Finished = 30,
Failed = 40,
TimedOut = 41,
Unknown = 255
}
public sealed class NodeStatus
Expand Down Expand Up @@ -121,6 +122,12 @@ public static class NodeStatusConstants
Name = "Failed [no round result]",
};

public static readonly NodeStatus TimedOut = new()
{
NodeStatusId = NStatus.TimedOut,
Name = "Timed out [no round result]",
};

public static readonly NodeStatus[] NodeStatusArray = [
NotRegistered,
WaitingRoundStart,
Expand All @@ -131,6 +138,7 @@ public static class NodeStatusConstants
RunningStepThree,
Finished,
Failed,
TimedOut,
];
public static NodeStatus GetNodeStatusById(short id)
{
Expand Down
38 changes: 36 additions & 2 deletions dkgServiceNode/Controllers/DControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using dkgServiceNode.Services.RoundRunner;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using Microsoft.EntityFrameworkCore;

namespace dkgServiceNode.Controllers
{
Expand Down Expand Up @@ -116,7 +117,7 @@ protected ObjectResult _500MisssingStepOneData(int id, string status)
return _500($"Round [{id}] status is [{status}] but step one data is missing");
}

protected ObjectResult _500UnknownStateTransition(string rState, string nState )
protected ObjectResult _500UnknownStateTransition(string rState, string nState)
{
return _500($"Unknown state transition [rState = {rState}, nState = {nState}]");
}
Expand All @@ -131,5 +132,38 @@ protected DControllerBase(IHttpContextAccessor httpContextAccessor, UserContext
if (uid != null) curUserId = (int)uid;
}
}

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

if (node.RoundId != null)
{
node.RoundId = null;
needsUpdate = true;
}

if (needsUpdate)
{
dkgContext.Entry(node).State = EntityState.Modified;
await dkgContext.SaveChangesAsync();
}
}

protected async Task UpdateNodeState(DkgContext dkgContext, Node node, short nStatus, int? roundId)
{
if (node.StatusValue != nStatus || node.RoundId != roundId)
{
node.StatusValue = nStatus;
node.RoundId = roundId;
dkgContext.Entry(node).State = EntityState.Modified;
await dkgContext.SaveChangesAsync();
}
}
}
}
}
94 changes: 52 additions & 42 deletions dkgServiceNode/Controllers/NodesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task<ActionResult<Reference>> RegisterNode(Node node)
// Acknowledges that the status report has been received
internal async Task<ObjectResult> Accept(Round? round, Node node, StatusReport stReport)
{
await UpdateNodeState(node, (short)stReport.Status, round?.Id);
await UpdateNodeState(dkgContext, node, (short)stReport.Status, round?.Id);
if (round != null)
{
await UpdateRoundState(round);
Expand All @@ -141,7 +141,7 @@ internal async Task<ObjectResult> TrToNotRegistered(Round? round, Node node, Sta
runner.SetNoResult(round, node);
}

await ResetNodeState(node);
await ResetNodeState(dkgContext, node);
var response = new StatusResponse(0, NStatus.NotRegistered);
if (stReport.Status != NStatus.NotRegistered || stReport.RoundId != 0)
{
Expand All @@ -150,13 +150,23 @@ internal async Task<ObjectResult> TrToNotRegistered(Round? round, Node node, Sta
return Accepted(response);
}

internal async Task<ObjectResult> TrToRunningStepOne(Round? round, Node _node, StatusReport _stReport)
internal async Task<ObjectResult> TrToRunningStepOne(Round? round, Node node, StatusReport stReport)
{
if (round == null)
{
return _500UndefinedRound();
}

if (!runner.CheckNode(round, node))
{
await ResetNodeState(dkgContext, node);
var response = new StatusResponse(0, NStatus.NotRegistered);
if (stReport.Status != NStatus.NotRegistered || stReport.RoundId != 0)
{
return Ok(response);
}
}

await Task.Delay(0);
string[] data = runner.GetStepOneData(round!);
if (data.Length == 0)
Expand All @@ -171,9 +181,19 @@ internal async Task<ObjectResult> TrToRunningStepTwoConditional(Round? round, No
{
return _500UndefinedRound();
}


if (runner.CheckTimedOutNode(round, node))
{
await UpdateNodeState(dkgContext, node, (short)NStatus.TimedOut, round.Id);
var response = new StatusResponse(round.Id, NStatus.TimedOut);
if (stReport.Status != NStatus.TimedOut)
{
return Ok(response);
}
}

runner.SetStepTwoWaitingTime(round);
await UpdateNodeState(node, (short)stReport.Status, round?.Id);
await UpdateNodeState(dkgContext, node, (short)stReport.Status, round?.Id);

if (stReport.Data.Length != 0)
{
Expand All @@ -193,8 +213,18 @@ internal async Task<ObjectResult> TrToRunningStepThreeConditional(Round? round,
return _500UndefinedRound();
}

if (runner.CheckTimedOutNode(round, node))
{
await UpdateNodeState(dkgContext, node, (short)NStatus.TimedOut, round.Id);
var response = new StatusResponse(round.Id, NStatus.TimedOut);
if (stReport.Status != NStatus.TimedOut)
{
return Ok(response);
}
}

runner.SetStepThreeWaitingTime(round);
await UpdateNodeState(node, (short)stReport.Status, round?.Id);
await UpdateNodeState(dkgContext, node, (short)stReport.Status, round?.Id);

if (stReport.Data.Length != 0)
{
Expand All @@ -214,7 +244,7 @@ internal async Task<ObjectResult> WrongStatus(Round? round, Node node, StatusRep
runner.SetNoResult(round, node);
}

await ResetNodeState(node);
await ResetNodeState(dkgContext, node);
string rStatus = round == null ? "null" : GetRoundStatusById(round.StatusValue).ToString();
return _409Status(stReport.PublicKey, stReport.Name, GetNodeStatusById(stReport.Status).ToString(), rStatus);
}
Expand All @@ -230,14 +260,14 @@ internal async Task<ObjectResult> AcceptFinished(Round? round, Node node, Status
if (stReport.Data.Length != 0)
{
runner.SetResult(round, node, stReport.Data);
await UpdateNodeState(node, (short)stReport.Status, round.Id);
await UpdateNodeState(dkgContext, node, (short)stReport.Status, round.Id);
await UpdateRoundState(round);
return Accepted(new StatusResponse(round.Id, stReport.Status));
}
else
{
runner.SetNoResult(round, node);
await UpdateNodeState(node, (short)stReport.Status, round.Id);
await UpdateNodeState(dkgContext, node, (short)stReport.Status, round.Id);
await UpdateRoundState(round);
return _400NoResult(round.Id, node.Name, node.PublicKey);
}
Expand All @@ -252,43 +282,11 @@ internal async Task<ObjectResult> AcceptFailed(Round? round, Node node, StatusRe
runner.SetResultWaitingTime(round);

runner.SetNoResult(round, node);
await UpdateNodeState(node, (short)stReport.Status, round.Id);
await UpdateNodeState(dkgContext, node, (short)stReport.Status, round.Id);
await UpdateRoundState(round);

return Accepted(new StatusResponse(round.Id, stReport.Status));
}
internal async Task ResetNodeState(Node node)
{
bool needsUpdate = false;
if (node.StatusValue != (short)NStatus.NotRegistered)
{
node.StatusValue = (short)NStatus.NotRegistered;
needsUpdate = true;
}

if (node.RoundId != null)
{
node.RoundId = null;
needsUpdate = true;
}

if (needsUpdate)
{
dkgContext.Entry(node).State = EntityState.Modified;
await dkgContext.SaveChangesAsync();
}
}

internal async Task UpdateNodeState(Node node, short nStatus, int? roundId)
{
if (node.StatusValue != nStatus || node.RoundId != roundId)
{
node.StatusValue = nStatus;
node.RoundId = roundId;
dkgContext.Entry(node).State = EntityState.Modified;
await dkgContext.SaveChangesAsync();
}
}

// POST: api/Nodes/status
[HttpPost("status")]
Expand Down Expand Up @@ -399,6 +397,18 @@ public async Task<ActionResult<Reference>> Status(StatusReport statusReport)
{ (RStatus.Cancelled, NStatus.Failed), TrToNotRegistered },
{ (RStatus.Failed, NStatus.Failed), TrToNotRegistered },
{ (RStatus.Unknown, NStatus.Failed), TrToNotRegistered },

{ (null, NStatus.TimedOut), WrongStatus },
{ (RStatus.NotStarted, NStatus.TimedOut), WrongStatus },
{ (RStatus.Registration, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.CreatingDeals, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.ProcessingDeals, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.ProcessingResponses, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.Finished, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.Cancelled, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.Failed, NStatus.TimedOut), TrToNotRegistered },
{ (RStatus.Unknown, NStatus.TimedOut), TrToNotRegistered },

};

var node = await dkgContext.FindNodeByPublicKeyAsync(statusReport.PublicKey);
Expand Down
49 changes: 14 additions & 35 deletions dkgServiceNode/Controllers/RoundsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ public async Task<ActionResult<Round>> NextRoundStep(int id)
reNodes = rNodes.Skip(round.MaxNodes).ToList();
}
runner.RunRound(round, fiNodes);
foreach (Node node in reNodes ?? [])
{
await ResetNodeState(dkgContext, node);
}

break;
case (short)RStatus.ProcessingDeals:
runner.ProcessDeals(round);
Expand All @@ -201,38 +206,7 @@ public async Task<ActionResult<Round>> NextRoundStep(int id)
break;
}

dkgContext.Entry(round).State = EntityState.Modified;
try
{
await dkgContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!await dkgContext.RoundExistsAsync(id))
{
return _404Round(id);
}
else
{
throw;
}
}

foreach (var node in reNodes)
{
node.RoundId = null;
node.Status = NStatus.NotRegistered;
dkgContext.Entry(node).State = EntityState.Modified;
}
try
{
await dkgContext.SaveChangesAsync();
}
catch
{
}

return NoContent();
return await UpdateRoundState(dkgContext, round);
}

// POST: api/rounds/cancel/5
Expand All @@ -253,23 +227,28 @@ public async Task<ActionResult<Round>> CancelRound(int id)
round.CreatedOn = round.CreatedOn.ToUniversalTime();
round.Status = RoundStatusConstants.Cancelled;

runner.CancelRound(round);
return await UpdateRoundState(dkgContext, round);
}

internal async Task<ActionResult<Round>> UpdateRoundState(DkgContext dkgContext, Round round)
{
dkgContext.Entry(round).State = EntityState.Modified;
try
{
await dkgContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (! await dkgContext.RoundExistsAsync(id))
if (!await dkgContext.RoundExistsAsync(round.Id))
{
return _404Round(id);
return _404Round(round.Id);
}
else
{
throw;
}
}
runner.CancelRound(round);
return NoContent();
}
}
Expand Down
17 changes: 17 additions & 0 deletions dkgServiceNode/Data/DbEnsure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ FOR EACH ROW
INSERT INTO ""versions"" (""version"", ""date"") VALUES
('0.6.0', '" + DateTime.Now.ToString("yyyy-MM-dd") + @"');
COMMIT;
";

readonly static string sqlScript_0_6_2 = @"
START TRANSACTION;
ALTER TABLE ""rounds"" ALTER COLUMN ""timeout2"" SET DEFAULT 30;
ALTER TABLE ""rounds"" ALTER COLUMN ""timeout3"" SET DEFAULT 30;
INSERT INTO ""users"" (""name"", ""email"", ""password"", ""is_enabled"", ""is_admin"")
SELECT 'Admin', 'admin@example.com', '$2a$11$YygO9mUKjDioWY0CPj35LeCGY4SRnVHNdT2cFdVAGTSRwSpYHhytu', TRUE, TRUE
WHERE NOT EXISTS(SELECT 1 FROM ""users"" WHERE ""email"" = 'admin@example.com');
INSERT INTO ""versions"" (""version"", ""date"") VALUES
('0.6.2', '" + DateTime.Now.ToString("yyyy-MM-dd") + @"');
COMMIT;
";
private static string PuVersionUpdateQuery(string v)
Expand Down Expand Up @@ -279,6 +295,7 @@ public static void Ensure(string connectionString)
PuVersionUpdate("0.5.2", connection);
EnsureVersion("0.6.0", sqlScript_0_6_0, connection);
PuVersionUpdate("0.6.1", connection);
EnsureVersion("0.6.2", sqlScript_0_6_2, connection);
}
}

Expand Down
4 changes: 2 additions & 2 deletions dkgServiceNode/Models/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class Round
public int MaxNodes { get; set; } = 256;

[Column("timeout2")]
public int Timeout2 { get; set; } = 120;
public int Timeout2 { get; set; } = 30;

[Column("timeout3")]
public int Timeout3 { get; set; } = 120;
public int Timeout3 { get; set; } = 30;

[Column("timeoutr")]
public int TimeoutR { get; set; } = 120;
Expand Down
Loading

0 comments on commit eb9d438

Please sign in to comment.