From 4968eb54150df980952684dbb4f456ee0341b0c8 Mon Sep 17 00:00:00 2001 From: area363 Date: Thu, 5 Oct 2023 09:29:02 +0900 Subject: [PATCH] update middleware --- .../appsettings.json | 8 +- .../HttpMultiAccountManagementMiddleware.cs | 138 ++++++++---------- 2 files changed, 64 insertions(+), 82 deletions(-) diff --git a/NineChronicles.Headless.Executable/appsettings.json b/NineChronicles.Headless.Executable/appsettings.json index 87b56cfff..a32c40dc8 100644 --- a/NineChronicles.Headless.Executable/appsettings.json +++ b/NineChronicles.Headless.Executable/appsettings.json @@ -124,9 +124,9 @@ } }, "MultiAccountManaging": { - "EnableManaging": false, - "ManagementTimeMinutes": 10, - "TxIntervalMinutes": 10, - "ThresholdCount": 29 + "EnableManaging": true, + "ManagementTimeMinutes": 60, + "TxIntervalMinutes": 60, + "ThresholdCount": 19 } } diff --git a/NineChronicles.Headless/Middleware/HttpMultiAccountManagementMiddleware.cs b/NineChronicles.Headless/Middleware/HttpMultiAccountManagementMiddleware.cs index 977c8dfb1..7fcf0e2b9 100644 --- a/NineChronicles.Headless/Middleware/HttpMultiAccountManagementMiddleware.cs +++ b/NineChronicles.Headless/Middleware/HttpMultiAccountManagementMiddleware.cs @@ -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); } } }