Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce migrateActivateCollections #695

Merged
merged 2 commits into from
Jun 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public NineChroniclesSummarySchema(IServiceProvider serviceProvider)
: base(serviceProvider)
{
Query = serviceProvider.GetRequiredService<NineChroniclesSummaryQuery>();
Mutation = serviceProvider.GetRequiredService<NineChroniclesSummaryMutation>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace NineChronicles.DataProvider.Queries
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphQL;
using GraphQL.Types;
using Libplanet.Crypto;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using NineChronicles.DataProvider.Store;
using NineChronicles.DataProvider.Store.Models;
using NineChronicles.Headless.GraphTypes.States;
using Serilog;

public class NineChroniclesSummaryMutation : ObjectGraphType
{
public NineChroniclesSummaryMutation(MySqlStore store, StateContext stateContext)
{
Store = store;
StateContext = stateContext;

Field<NonNullGraphType<StringGraphType>>(
name: "migrateActivateCollections",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<ListGraphType<StringGraphType>>>
{
Name = "signers",
}
),
resolve: context =>
{
var signers = context.GetArgument<List<string>>("signers");
var collectionSheet = StateContext.WorldState.GetSheet<CollectionSheet>();
var blockIndex = Store.GetTip();
var result = new Dictionary<string, (string, int, int)>();
foreach (var signer in signers)
{
var avatars = Store.GetAvatarsFromSigner(signer);
foreach (var avatar in avatars)
{
try
{
var collectionState = stateContext.WorldState.GetCollectionState(new Address(avatar.Address!));
var previous = RenderSubscriber.MigrateActivateCollections(Store, collectionSheet, blockIndex, collectionState, avatar, "Migrate from worker");
result[avatar.Address!] = (signer, previous, avatar.ActivateCollections.Count);
}
catch (Exception e)
{
Log.Error(e, "[MigrateActivateCollections] Unexpected exception occurred during MocaWorker: {Exc}", e);
}
}
}

if (result.Any())
{
StringBuilder sb = new StringBuilder("[MigrateActivateCollections]migration result");
sb.AppendLine("signer,avatar,previous,migrated");
foreach (var kv in result)
{
var value = kv.Value;
var log = $"{value.Item1},{kv.Key},{value.Item2},{kv.Value.Item3}";
sb.AppendLine(log);
}

return sb.ToString();
}

return "[MigrateActivateCollections]no required migrations";
}
);
}

private MySqlStore Store { get; }

private StateContext StateContext { get; }
}
}
98 changes: 69 additions & 29 deletions NineChronicles.DataProvider/RenderSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,57 @@ public static string GetSignerAndOtherAddressesHex(Address signer, params Addres
return sb.ToString();
}

public static int MigrateActivateCollections(
MySqlStore mySqlStore,
CollectionSheet collectionSheet,
long blockIndex,
CollectionState collectionState,
AvatarModel avatar,
string actionId
)
{
Log.Information("[MigrateActivateCollections] avatar: {Signer}, {Address}, {Collections}", avatar.AgentAddress, avatar.Address, avatar.ActivateCollections.Count);

// check chain state ids to fill in missing collection data
var existIds = avatar.ActivateCollections.Select(i => i.CollectionId).ToList();
var targetIds = collectionState.Ids.Except(existIds).ToList();
Log.Information("[MigrateActivateCollections] migration targets: {Address}, [{ExistIds}]/[{TargetIds}]", avatar.Address, string.Join(",", existIds), string.Join(",", targetIds));
var previous = avatar.ActivateCollections.Count;
foreach (var collectionId in targetIds)
{
var row = collectionSheet[collectionId];
var options = new List<CollectionOptionModel>();
foreach (var modifier in row.StatModifiers)
{
var option = new CollectionOptionModel
{
StatType = modifier.StatType.ToString(),
OperationType = modifier.Operation.ToString(),
Value = modifier.Value,
};
options.Add(option);
}

var collectionModel = new ActivateCollectionModel
{
ActionId = actionId,
Avatar = avatar,
BlockIndex = blockIndex,
CollectionId = collectionId,
Options = options,
};
avatar.ActivateCollections.Add(collectionModel);
}

if (targetIds.Any())
{
mySqlStore.UpdateAvatar(avatar);
Log.Information("[MigrateActivateCollections] Update Avatar: {Address}, [{Previous}]/[{New}]", avatar.Address, previous, avatar.ActivateCollections.Count);
}

return previous;
}

protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_blockRenderer.BlockSubject.Subscribe(b =>
Expand Down Expand Up @@ -1430,38 +1481,27 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)

_actionRenderer.EveryRender<ActivateCollection>().Subscribe(ev =>
{
if (ev.Exception is null && ev.Action is { } activateCollection)
try
{
var outputState = new World(_blockChainStates.GetWorldState(ev.OutputState));
var collectionSheet = outputState.GetSheet<CollectionSheet>();
var avatar = MySqlStore.GetAvatar(activateCollection.AvatarAddress, true);
foreach (var (collectionId, materials) in activateCollection.CollectionData)
if (ev.Exception is null && ev.Action is { } activateCollection)
{
var row = collectionSheet[collectionId];
var options = new List<CollectionOptionModel>();
foreach (var modifier in row.StatModifiers)
{
var option = new CollectionOptionModel
{
StatType = modifier.StatType.ToString(),
OperationType = modifier.Operation.ToString(),
Value = modifier.Value,
};
options.Add(option);
}

var collectionModel = new ActivateCollectionModel
{
ActionId = activateCollection.Id.ToString(),
Avatar = avatar,
BlockIndex = ev.BlockIndex,
CollectionId = collectionId,
Options = options,
};
avatar.ActivateCollections.Add(collectionModel);
var outputState = new World(_blockChainStates.GetWorldState(ev.OutputState));
var collectionSheet = outputState.GetSheet<CollectionSheet>();
var avatar = MySqlStore.GetAvatar(activateCollection.AvatarAddress, true);
var collectionState = outputState.GetCollectionState(activateCollection.AvatarAddress);
MigrateActivateCollections(
MySqlStore,
collectionSheet,
ev.BlockIndex,
collectionState,
avatar,
activateCollection.Id.ToString()
);
}

MySqlStore.UpdateAvatar(avatar);
}
catch (Exception ex)
{
Log.Error(ex, "RenderSubscriber Error: {ErrorMessage}, StackTrace: {StackTrace}", ex.Message, ex.StackTrace);
}
});

Expand Down
7 changes: 7 additions & 0 deletions NineChronicles.DataProvider/Store/MySqlStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2373,5 +2373,12 @@ public void UpdateAvatar(AvatarModel avatar)
ctx.Avatars!.Update(avatar);
ctx.SaveChanges();
}

public ICollection<AvatarModel> GetAvatarsFromSigner(string signer)
{
using NineChroniclesContext ctx = _dbContextFactory.CreateDbContext();
var avatars = ctx.Avatars!;
return avatars.Include(a => a.ActivateCollections).Where(a => signer == a.AgentAddress).ToList();
}
}
}
Loading