Skip to content

Commit

Permalink
update middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Nov 3, 2023
1 parent 6e3daac commit 4968eb5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 82 deletions.
8 changes: 4 additions & 4 deletions NineChronicles.Headless.Executable/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@
}
},
"MultiAccountManaging": {
"EnableManaging": false,
"ManagementTimeMinutes": 10,
"TxIntervalMinutes": 10,
"ThresholdCount": 29
"EnableManaging": true,
"ManagementTimeMinutes": 60,
"TxIntervalMinutes": 60,
"ThresholdCount": 19
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,107 +61,89 @@ public async Task InvokeAsync(HttpContext context)
var remoteIp = context.Connection.RemoteIpAddress!.ToString();
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
context.Request.Body.Seek(0, SeekOrigin.Begin);
if (_options.Value.EnableManaging)
if (_options.Value.EnableManaging && body.Contains("stageTransaction"))
{
if (body.Contains("agent(address:\\\"") || body.Contains("agent(address: \\\""))
try
{
try
{
var agent = new Address(body.Split("\\\"")[1].Split("0x")[1]);
UpdateIpSignerList(remoteIp, agent);
}
catch (Exception ex)
{
_logger.Error(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Error message: {message} Stacktrace: {stackTrace}",
ex.Message,
ex.StackTrace);
}
}
var pattern = "64313.*6565";
var txPayload = Regex.Match(body, pattern).ToString();
byte[] bytes = ByteUtil.ParseHex(txPayload);
Transaction tx = Transaction.Deserialize(bytes);
var agent = tx.Signer;
var action = NCActionUtils.ToAction(tx.Actions.Actions.First());
UpdateIpSignerList(remoteIp, agent);

if (body.Contains("stageTransaction"))
{
try
// Only monitoring actions not used in the launcher
if (action is not Stake
and not ClaimStakeReward
and not TransferAsset)
{
var pattern = "64313.*6565";
var txPayload = Regex.Match(body, pattern).ToString();
byte[] bytes = ByteUtil.ParseHex(txPayload);
Transaction tx = Transaction.Deserialize(bytes);
var agent = tx.Signer;
var action = NCActionUtils.ToAction(tx.Actions.Actions.First());

// Only monitoring actions not used in the launcher
if (action is not Stake
and not ClaimStakeReward
and not TransferAsset)
if (_ipSignerList.ContainsKey(remoteIp))
{
if (_ipSignerList.ContainsKey(remoteIp))
if (_ipSignerList[remoteIp].Count > _options.Value.ThresholdCount)
{
if (_ipSignerList[remoteIp].Count > _options.Value.ThresholdCount)
{
_logger.Information(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] IP: {IP} List Count: {Count}, AgentAddresses: {Agent}",
remoteIp,
_ipSignerList[remoteIp].Count,
_ipSignerList[remoteIp]);
_logger.Information(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] IP: {IP} List Count: {Count}, AgentAddresses: {Agent}",
remoteIp,
_ipSignerList[remoteIp].Count,
_ipSignerList[remoteIp]);

if (!MultiAccountManagementList.ContainsKey(agent))
if (!MultiAccountManagementList.ContainsKey(agent))
{
if (!MultiAccountTxIntervalTracker.ContainsKey(agent))
{
if (!MultiAccountTxIntervalTracker.ContainsKey(agent))
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Adding agent {agent} to the agent tracker.");
MultiAccountTxIntervalTracker.Add(agent, DateTimeOffset.Now);
}
else
{
if ((DateTimeOffset.Now - MultiAccountTxIntervalTracker[agent]).Minutes >= _options.Value.TxIntervalMinutes)
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Resetting Agent {agent}'s time because it has been more than {_options.Value.TxIntervalMinutes} minutes since the last transaction.");
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
}
else
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Managing Agent {agent} for {_options.Value.ManagementTimeMinutes} minutes due to {_ipSignerList[remoteIp].Count} associated accounts.");
ManageMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
await CancelRequestAsync(context);
return;
}
}
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Adding agent {agent} to the agent tracker.");
MultiAccountTxIntervalTracker.Add(agent, DateTimeOffset.Now);
}
else
{
var currentManagedTime = (DateTimeOffset.Now - MultiAccountManagementList[agent]).Minutes;
if (currentManagedTime > _options.Value.ManagementTimeMinutes)
if ((DateTimeOffset.Now - MultiAccountTxIntervalTracker[agent]).Minutes >= _options.Value.TxIntervalMinutes)
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Restoring Agent {agent} after {_options.Value.ManagementTimeMinutes} minutes.");
RestoreMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes);
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Current time: {DateTimeOffset.Now} Added time: {DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes)}.");
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Resetting Agent {agent}'s time because it has been more than {_options.Value.TxIntervalMinutes} minutes since the last transaction.");
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
}
else
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Agent {agent} is in managed status for the next {_options.Value.ManagementTimeMinutes - currentManagedTime} minutes.");
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Managing Agent {agent} for {_options.Value.ManagementTimeMinutes} minutes due to {_ipSignerList[remoteIp].Count} associated accounts.");
ManageMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
await CancelRequestAsync(context);
return;
}
}
}
}
else
{
UpdateIpSignerList(remoteIp, agent);
else
{
var currentManagedTime = (DateTimeOffset.Now - MultiAccountManagementList[agent]).Minutes;
if (currentManagedTime > _options.Value.ManagementTimeMinutes)
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Restoring Agent {agent} after {_options.Value.ManagementTimeMinutes} minutes.");
RestoreMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes);
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Current time: {DateTimeOffset.Now} Added time: {DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes)}.");
}
else
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Agent {agent} is in managed status for the next {_options.Value.ManagementTimeMinutes - currentManagedTime} minutes.");
await CancelRequestAsync(context);
return;
}
}
}
}

}
catch (Exception ex)
{
_logger.Error(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Error message: {message} Stacktrace: {stackTrace}",
ex.Message,
ex.StackTrace);
else
{
UpdateIpSignerList(remoteIp, agent);
}
}

}
catch (Exception ex)
{
_logger.Error(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Error message: {message} Stacktrace: {stackTrace}",
ex.Message,
ex.StackTrace);
}
}
}
Expand Down

0 comments on commit 4968eb5

Please sign in to comment.