Skip to content

Commit

Permalink
Merge pull request #2370 from planetarium/release/91
Browse files Browse the repository at this point in the history
main merge release/91
  • Loading branch information
ipdae authored Jan 3, 2024
2 parents d1d2ccb + 2cb7300 commit e1c8d5b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Lib9c
3 changes: 2 additions & 1 deletion NineChronicles.Headless/GraphQLService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using AspNetCoreRateLimit;
using GraphQL.Server;
Expand Down Expand Up @@ -200,7 +201,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// Capture requests
if (Convert.ToBoolean(Configuration.GetSection("MultiAccountManaging")["EnableManaging"]))
{
Dictionary<string, HashSet<Address>> ipSignerList = new();
ConcurrentDictionary<string, HashSet<Address>> ipSignerList = new();
app.UseMiddleware<HttpMultiAccountManagementMiddleware>(
StandaloneContext,
ipSignerList,
Expand Down
6 changes: 1 addition & 5 deletions NineChronicles.Headless/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NineChronicles.Headless.Properties;
using System.Net;
using Lib9c.Formatters;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Headless.Hosting;
using MessagePack;
using MessagePack.Resolvers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Nekoyume.Action;
using NineChronicles.Headless.Middleware;
using NineChronicles.Headless.Services;
using Sentry;

namespace NineChronicles.Headless
{
Expand Down Expand Up @@ -82,7 +78,7 @@ IConfiguration configuration
options.MaxReceiveMessageSize = null;
if (Convert.ToBoolean(configuration.GetSection("MultiAccountManaging")["EnableManaging"]))
{
Dictionary<string, HashSet<Address>> ipSignerList = new();
ConcurrentDictionary<string, HashSet<Address>> ipSignerList = new();
options.Interceptors.Add<GrpcMultiAccountManagementMiddleware>(
standaloneContext,
ipSignerList,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Grpc.Core;
using Grpc.Core.Interceptors;
Expand All @@ -13,17 +14,17 @@ namespace NineChronicles.Headless.Middleware
{
public class GrpcMultiAccountManagementMiddleware : Interceptor
{
private static readonly Dictionary<Address, DateTimeOffset> MultiAccountTxIntervalTracker = new();
private static readonly Dictionary<Address, DateTimeOffset> MultiAccountManagementList = new();
private static readonly ConcurrentDictionary<Address, DateTimeOffset> MultiAccountTxIntervalTracker = new();
private static readonly ConcurrentDictionary<Address, DateTimeOffset> MultiAccountManagementList = new();
private readonly ILogger _logger;
private StandaloneContext _standaloneContext;
private readonly Dictionary<string, HashSet<Address>> _ipSignerList;
private readonly ConcurrentDictionary<string, HashSet<Address>> _ipSignerList;
private readonly ActionEvaluationPublisher _actionEvaluationPublisher;
private readonly IOptions<MultiAccountManagerProperties> _options;

public GrpcMultiAccountManagementMiddleware(
StandaloneContext standaloneContext,
Dictionary<string, HashSet<Address>> ipSignerList,
ConcurrentDictionary<string, HashSet<Address>> ipSignerList,
ActionEvaluationPublisher actionEvaluationPublisher,
IOptions<MultiAccountManagerProperties> options)
{
Expand All @@ -36,12 +37,12 @@ public GrpcMultiAccountManagementMiddleware(

private static void ManageMultiAccount(Address agent)
{
MultiAccountManagementList.Add(agent, DateTimeOffset.Now);
MultiAccountManagementList.TryAdd(agent, DateTimeOffset.Now);
}

private static void RestoreMultiAccount(Address agent)
{
MultiAccountManagementList.Remove(agent);
MultiAccountManagementList.TryRemove(agent, out _);
}

public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
Expand Down Expand Up @@ -95,7 +96,7 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
{
_logger.Information(
$"[GRPC-MULTI-ACCOUNT-MANAGER] Adding agent {agent} to the agent tracker.");
MultiAccountTxIntervalTracker.Add(agent, DateTimeOffset.Now);
MultiAccountTxIntervalTracker.TryAdd(agent, DateTimeOffset.Now);
}
else
{
Expand All @@ -105,7 +106,7 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
_logger.Information(
$"[GRPC-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;
MultiAccountTxIntervalTracker.TryUpdate(agent, DateTimeOffset.Now, MultiAccountTxIntervalTracker[agent]);
}
else
{
Expand All @@ -114,7 +115,7 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
$"{_options.Value.ManagementTimeMinutes} minutes due to " +
$"{_ipSignerList[remoteIp].Count} associated accounts.");
ManageMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
MultiAccountTxIntervalTracker.TryUpdate(agent, DateTimeOffset.Now, MultiAccountTxIntervalTracker[agent]);
throw new RpcException(new Status(StatusCode.Cancelled, "Request cancelled."));
}
}
Expand All @@ -127,8 +128,7 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
_logger.Information(
$"[GRPC-MULTI-ACCOUNT-MANAGER] Restoring Agent {agent} after {_options.Value.ManagementTimeMinutes} minutes.");
RestoreMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] =
DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes);
MultiAccountTxIntervalTracker.TryUpdate(agent, DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes), MultiAccountTxIntervalTracker[agent]);
_logger.Information(
$"[GRPC-MULTI-ACCOUNT-MANAGER] Current time: {DateTimeOffset.Now} Added time: {DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes)}.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -18,19 +19,19 @@ namespace NineChronicles.Headless.Middleware
{
public class HttpMultiAccountManagementMiddleware
{
private static readonly Dictionary<Address, DateTimeOffset> MultiAccountTxIntervalTracker = new();
private static readonly Dictionary<Address, DateTimeOffset> MultiAccountManagementList = new();
private static readonly ConcurrentDictionary<Address, DateTimeOffset> MultiAccountTxIntervalTracker = new();
private static readonly ConcurrentDictionary<Address, DateTimeOffset> MultiAccountManagementList = new();
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private StandaloneContext _standaloneContext;
private readonly Dictionary<string, HashSet<Address>> _ipSignerList;
private readonly ConcurrentDictionary<string, HashSet<Address>> _ipSignerList;
private readonly IOptions<MultiAccountManagerProperties> _options;
private ActionEvaluationPublisher _publisher;

public HttpMultiAccountManagementMiddleware(
RequestDelegate next,
StandaloneContext standaloneContext,
Dictionary<string, HashSet<Address>> ipSignerList,
ConcurrentDictionary<string, HashSet<Address>> ipSignerList,
IOptions<MultiAccountManagerProperties> options,
ActionEvaluationPublisher publisher)
{
Expand All @@ -44,12 +45,12 @@ public HttpMultiAccountManagementMiddleware(

private static void ManageMultiAccount(Address agent)
{
MultiAccountManagementList.Add(agent, DateTimeOffset.Now);
MultiAccountManagementList.TryAdd(agent, DateTimeOffset.Now);
}

private static void RestoreMultiAccount(Address agent)
{
MultiAccountManagementList.Remove(agent);
MultiAccountManagementList.TryRemove(agent, out _);
}

public async Task InvokeAsync(HttpContext context)
Expand Down Expand Up @@ -92,20 +93,20 @@ and not ClaimStakeReward
if (!MultiAccountTxIntervalTracker.ContainsKey(agent))
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Adding agent {agent} to the agent tracker.");
MultiAccountTxIntervalTracker.Add(agent, DateTimeOffset.Now);
MultiAccountTxIntervalTracker.TryAdd(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;
MultiAccountTxIntervalTracker.TryUpdate(agent, DateTimeOffset.Now, MultiAccountTxIntervalTracker[agent]);
}
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;
MultiAccountTxIntervalTracker.TryUpdate(agent, DateTimeOffset.Now, MultiAccountTxIntervalTracker[agent]);
await CancelRequestAsync(context);
return;
}
Expand All @@ -118,7 +119,7 @@ and not ClaimStakeReward
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Restoring Agent {agent} after {_options.Value.ManagementTimeMinutes} minutes.");
RestoreMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes);
MultiAccountTxIntervalTracker.TryUpdate(agent, DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes), MultiAccountTxIntervalTracker[agent]);
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Current time: {DateTimeOffset.Now} Added time: {DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes)}.");
}
else
Expand Down
8 changes: 4 additions & 4 deletions NineChronicles.Headless/Middleware/IpBanMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
Expand All @@ -11,7 +11,7 @@ namespace NineChronicles.Headless.Middleware
{
public class IpBanMiddleware
{
private static Dictionary<string, DateTimeOffset> _bannedIps = new();
private static ConcurrentDictionary<string, DateTimeOffset> _bannedIps = new();
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private readonly IOptions<CustomIpRateLimitOptions> _options;
Expand All @@ -27,15 +27,15 @@ public static void BanIp(string ip)
{
if (!_bannedIps.ContainsKey(ip))
{
_bannedIps.Add(ip, DateTimeOffset.Now);
_bannedIps.TryAdd(ip, DateTimeOffset.Now);
}
}

public static void UnbanIp(string ip)
{
if (_bannedIps.ContainsKey(ip))
{
_bannedIps.Remove(ip);
_bannedIps.TryRemove(ip, out _);
}
}

Expand Down

0 comments on commit e1c8d5b

Please sign in to comment.