Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Lib9cVersion>1.29.0-dev.fd558e739500c80e09fed936d1e5487016efb096</Lib9cVersion>
<Lib9cVersion>1.30.0-dev.c4bd7f8a1650132222cb6170c35660cd86c93a12</Lib9cVersion>
<LibplanetVersion>5.5.0</LibplanetVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Mimir.Initializer/Migrators/ProductMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task RunAsync(CancellationToken stoppingToken)

int? crystal = null;
int? crystalPerPrice = null;
int? combatPoint = null;
long? combatPoint = null;
try
{
(crystal, crystalPerPrice) = await _itemProductCalculationService.CalculateCrystalMetricsAsync(itemProduct);
Expand Down
4 changes: 2 additions & 2 deletions Mimir.MongoDB/Bson/AdventureCpDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ namespace Mimir.MongoDB.Bson;
public record AdventureCpDocument(
[property: BsonIgnore, JsonIgnore, GraphQLIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore, GraphQLIgnore] Address Address,
int Cp
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(1, StoredBlockIndex)), ICpDocument;
long Cp
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(2, StoredBlockIndex)), ICpDocument;
4 changes: 2 additions & 2 deletions Mimir.MongoDB/Bson/ArenaCpDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ namespace Mimir.MongoDB.Bson;
public record ArenaCpDocument(
[property: BsonIgnore, JsonIgnore, GraphQLIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore, GraphQLIgnore] Address Address,
int Cp
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(1, StoredBlockIndex)), ICpDocument;
long Cp
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(2, StoredBlockIndex)), ICpDocument;
2 changes: 1 addition & 1 deletion Mimir.MongoDB/Bson/ICpDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Mimir.MongoDB.Bson;

public interface ICpDocument
{
int Cp { get; }
long Cp { get; }
}
4 changes: 2 additions & 2 deletions Mimir.MongoDB/Bson/ProductDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public record ProductDocument : MimirBsonDocument
public Address AvatarAddress { get; init; }
public Address ProductsStateAddress { get; init; }
public Product Object { get; init; }
public int? CombatPoint { get; init; }
public long? CombatPoint { get; init; }
public decimal? UnitPrice { get; init; }
public int? Crystal { get; init; }
public int? CrystalPerPrice { get; init; }
Expand All @@ -36,7 +36,7 @@ public ProductDocument(
Address productsStateAddress,
Product product,
decimal unitPrice,
int? combatPoint,
long? combatPoint,
int? crystal,
int? crystalPerPrice
)
Expand Down
4 changes: 2 additions & 2 deletions Mimir.MongoDB/Bson/RaidCpDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ namespace Mimir.MongoDB.Bson;
public record RaidCpDocument(
[property: BsonIgnore, JsonIgnore, GraphQLIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore, GraphQLIgnore] Address Address,
int Cp
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(1, StoredBlockIndex)), ICpDocument;
long Cp
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(2, StoredBlockIndex)), ICpDocument;
1 change: 1 addition & 0 deletions Mimir.Tests/QueryTests/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Mimir.Services;
using Moq;
using Nekoyume;

Expand Down
26 changes: 0 additions & 26 deletions Mimir.Tests/QueryTests/BalanceQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,6 @@ public async Task GraphQL_Query_Balance_NCG_Returns_CorrectValue()
await Verify(result);
}

[Fact]
public async Task GraphQL_Query_Balance_NCG_Enqueues_Recovery_When_Not_Found()
{
var mockAddress = new Address("0x0000000000000000000000000000000000000000");
var mockRepo = new Mock<IBalanceRepository>();
var mockStateRecoveryService = new Mock<IStateRecoveryService>();

mockRepo
.Setup(repo => repo.GetByAddressAsync("NCG".ToCurrency(), mockAddress))
.ThrowsAsync(new DocumentNotFoundInMongoCollectionException("balance_ncg", mockAddress.ToHex()));

var serviceProvider = TestServices.Builder
.With(mockRepo.Object)
.With(mockStateRecoveryService.Object)
.Build();

var query = $$"""
query {
balance(currencyTicker: "NCG", address: "{{mockAddress}}")
}
""";

var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query));
await Verify(result);
}

[Fact]
public async Task GraphQL_Query_Balance_Throws_When_No_Inputs_Provided()
{
Expand Down
16 changes: 12 additions & 4 deletions Mimir.Tests/TestServices.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Linq.Expressions;
using System.Numerics;
using System.Security.Cryptography;
using Hangfire;
using HotChocolate;
using HotChocolate.Execution;
using Lib9c.GraphQL.Types;
Expand All @@ -10,6 +12,7 @@
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Mimir.MongoDB.Services;
using Mimir.Services;
using MongoDB.Driver;
using Moq;

Expand All @@ -32,9 +35,13 @@ public ServiceProviderBuilder()
.BindRuntimeType(typeof(Address), typeof(AddressType))
.BindRuntimeType(typeof(BigInteger), typeof(BigIntegerType))
.BindRuntimeType(typeof(HashDigest<SHA256>), typeof(HashDigestSHA256Type));

var mockClient = new Mock<IBackgroundJobClient>();
_serviceCollection.AddSingleton<IBackgroundJobClient>(mockClient.Object);
}

public ServiceProviderBuilder With<T>(T service) where T : class
public ServiceProviderBuilder With<T>(T service)
where T : class
{
_serviceCollection.AddSingleton(service);
return this;
Expand All @@ -58,7 +65,8 @@ public IServiceProvider Build()
public static async Task<string> ExecuteRequestAsync(
IServiceProvider serviceProvider,
Action<OperationRequestBuilder> configureRequest,
CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default
)
{
await using var scope = serviceProvider.CreateAsyncScope();

Expand All @@ -67,8 +75,8 @@ public static async Task<string> ExecuteRequestAsync(
configureRequest(requestBuilder);
var request = requestBuilder.Build();

var executor = await scope.ServiceProvider
.GetRequiredService<IRequestExecutorResolver>()
var executor = await scope
.ServiceProvider.GetRequiredService<IRequestExecutorResolver>()
.GetRequestExecutorAsync(cancellationToken: cancellationToken);
await using var result = await executor.ExecuteAsync(request, cancellationToken);
result.ExpectOperationResult();
Expand Down
2 changes: 1 addition & 1 deletion Mimir.Worker/ActionHandler/ProductStateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Product product

int? crystal = null;
int? crystalPerPrice = null;
int? combatPoint = null;
long? combatPoint = null;
try
{
(crystal, crystalPerPrice) = await itemProductCalculationService.CalculateCrystalMetricsAsync(itemProduct);
Expand Down
2 changes: 1 addition & 1 deletion Mimir.Worker/Services/IItemProductCalculationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Mimir.Worker.Services;

public interface IItemProductCalculationService
{
Task<int?> CalculateCombatPointAsync(ItemProduct itemProduct);
Task<long?> CalculateCombatPointAsync(ItemProduct itemProduct);

Task<(int? crystal, int? crystalPerPrice)> CalculateCrystalMetricsAsync(ItemProduct itemProduct);
}
4 changes: 2 additions & 2 deletions Mimir.Worker/Services/ItemProductCalculationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public ItemProductCalculationService(IMongoDbService store)
_store = store;
}

public async Task<int?> CalculateCombatPointAsync(ItemProduct itemProduct)
public async Task<long?> CalculateCombatPointAsync(ItemProduct itemProduct)
{
var costumeStatSheet = await _store.GetSheetAsync<CostumeStatSheet>();

if (costumeStatSheet != null)
{
int? cp = itemProduct.TradableItem switch
long? cp = itemProduct.TradableItem switch
{
ItemUsable itemUsable => CPHelper.GetCP(
(Nekoyume.Model.Item.ItemUsable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MimirBsonDocument ConvertToDocument(AddressStatePair context)
$"{nameof(context.RawState)} Invalid state type. Expected {nameof(List)}, got {context.RawState.GetType().Name}."
);

var cp = list[0].ToInteger();
var cp = list[0].ToLong();

return new AdventureCpDocument(context.BlockIndex, context.Address, cp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MimirBsonDocument ConvertToDocument(AddressStatePair context)
$"{nameof(context.RawState)} Invalid state type. Expected {nameof(List)}, got {context.RawState.GetType().Name}."
);

var cp = list[0].ToInteger();
var cp = list[0].ToLong();

return new ArenaCpDocument(context.BlockIndex, context.Address, cp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MimirBsonDocument ConvertToDocument(AddressStatePair context)
$"{nameof(context.RawState)} Invalid state type. Expected {nameof(List)}, got {context.RawState.GetType().Name}."
);

var cp = list[0].ToInteger();
var cp = list[0].ToLong();

return new RaidCpDocument(context.BlockIndex, context.Address, cp);
}
Expand Down
32 changes: 25 additions & 7 deletions Mimir/GraphQL/Queries/Query.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Hangfire;
using HotChocolate.AspNetCore;
using Lib9c.GraphQL.Extensions;
using Lib9c.GraphQL.InputObjects;
Expand Down Expand Up @@ -46,21 +47,29 @@ [Service] IActionTypeRepository repo
/// </summary>
/// <param name="address">The address of the agent.</param>
/// <returns>The agent state</returns>
public async Task<AgentState> GetAgentAsync(Address address, [Service] IAgentRepository repo)
public async Task<AgentState> GetAgentAsync(
Address address,
[Service] IAgentRepository repo,
[Service] IBackgroundJobClient jobClient
)
{
try
{
var document = await repo.GetByAddressAsync(address);
if (document.Metadata.StoredBlockIndex != 0)
{
HangfireJobs.EnqueueAgentStateRecovery(address);
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverAgentStateAsync(address)
);
}

return document.Object;
}
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
{
HangfireJobs.EnqueueAgentStateRecovery(address);
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverAgentStateAsync(address)
);
throw;
}
}
Expand All @@ -70,15 +79,21 @@ public async Task<AgentState> GetAgentAsync(Address address, [Service] IAgentRep
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The avatar state</returns>
public async Task<AvatarState> GetAvatarAsync(Address address, [Service] IAvatarRepository repo)
public async Task<AvatarState> GetAvatarAsync(
Address address,
[Service] IAvatarRepository repo,
[Service] IBackgroundJobClient jobClient
)
{
try
{
return (await repo.GetByAddressAsync(address)).Object;
}
catch (Mimir.MongoDB.Exceptions.DocumentNotFoundInMongoCollectionException)
{
HangfireJobs.EnqueueAvatarStateRecovery(address);
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverAvatarStateAsync(address)
);
throw;
}
}
Expand All @@ -95,7 +110,8 @@ public async Task<string> GetBalanceAsync(
CurrencyInput? currency,
string? currencyTicker,
Address address,
[Service] IBalanceRepository repo
[Service] IBalanceRepository repo,
[Service] IBackgroundJobClient jobClient
)
{
try
Expand All @@ -118,7 +134,9 @@ [Service] IBalanceRepository repo
{
if (currencyTicker?.ToUpper() == "NCG")
{
HangfireJobs.EnqueueNCGBalanceRecovery(address);
jobClient.Enqueue<IStateRecoveryService>(service =>
service.TryRecoverNCGBalanceAsync(address)
);
}
throw;
}
Expand Down
1 change: 0 additions & 1 deletion Mimir/Options/HangfireOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class HangfireOption
{
public const string SectionName = "Hangfire";

public bool IsEnabled { get; set; } = false;
public string RedisConnectionString { get; set; } = string.Empty;
public string DashboardPath { get; set; } = "/hangfire";
public int WorkerCount { get; set; } = Environment.ProcessorCount;
Expand Down
30 changes: 12 additions & 18 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@
.Configuration.GetSection(HangfireOption.SectionName)
.Get<HangfireOption>();

if (hangfireOption?.IsEnabled == true)
{
builder.Services.AddHangfireServices(hangfireOption);
}
builder.Services.AddHangfireServices(hangfireOption);

// State recovery service
builder.Services.AddScoped<IStateRecoveryService, StateRecoveryService>();
Expand Down Expand Up @@ -210,21 +207,18 @@
var app = builder.Build();

// Hangfire dashboard
if (hangfireOption?.IsEnabled == true)
{
app.UseHangfireDashboard(
hangfireOption.DashboardPath,
new DashboardOptions
app.UseHangfireDashboard(
hangfireOption.DashboardPath,
new DashboardOptions
{
Authorization = new[]
{
Authorization = new[]
{
new BasicAuthDashboardAuthorizationFilter(
app.Services.GetRequiredService<IOptions<HangfireOption>>()
),
},
}
);
}
new BasicAuthDashboardAuthorizationFilter(
app.Services.GetRequiredService<IOptions<HangfireOption>>()
),
},
}
);

app.UseRouting();
app.MapGet("/", () => "Health Check").RequireRateLimiting("jwt");
Expand Down
29 changes: 0 additions & 29 deletions Mimir/Services/HangfireJobs.cs

This file was deleted.

Loading
Loading