Skip to content

Commit

Permalink
Process states via fixed block index
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed May 16, 2024
1 parent 0dcba91 commit e1427fd
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 64 deletions.
9 changes: 7 additions & 2 deletions Mimir.Worker/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ public Initializer(
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var started = DateTime.UtcNow;
await _tableSheetScrapper.ExecuteAsync(stoppingToken);
await _arenaScrapper.ExecuteAsync(stoppingToken);
var latestBlockIndex = await _stateService.GetLatestIndex();

await _tableSheetScrapper.ExecuteAsync(latestBlockIndex, stoppingToken);
await _arenaScrapper.ExecuteAsync(latestBlockIndex, stoppingToken);

await _store.UpdateLatestBlockIndex(latestBlockIndex);

_logger.LogInformation(
"Finished Initializer background service. Elapsed {TotalElapsedMinutes} minutes",
DateTime.UtcNow.Subtract(started).Minutes);
Expand Down
4 changes: 2 additions & 2 deletions Mimir.Worker/Queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ query GetTip {
}
}

query GetState($accountAddress: Address! $address: Address!) {
state(accountAddress: $accountAddress, address: $address)
query GetState($accountAddress: Address! $address: Address!, $index: Long) {
state(accountAddress: $accountAddress, address: $address, index: $index)
}

query GetBattleArenaTransactions($blockIndex: Long!) {
Expand Down
9 changes: 3 additions & 6 deletions Mimir.Worker/Scrapper/ArenaScrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ public class ArenaScrapper(ILogger<ArenaScrapper> logger, IStateService service,
private readonly IStateService _stateService = service;
private readonly MongoDbStore _store = store;

public async Task ExecuteAsync(CancellationToken cancellationToken)
public async Task ExecuteAsync(long blockIndex, CancellationToken cancellationToken)
{
var latestBlockIndex = await service.GetLatestIndex();
var stateGetter = _stateService.At();
var roundData = await stateGetter.GetArenaRoundData(latestBlockIndex);
var stateGetter = _stateService.At(blockIndex);
var roundData = await stateGetter.GetArenaRoundData(blockIndex);
var arenaParticipants = await stateGetter.GetArenaParticipantsState(roundData.ChampionshipId, roundData.Round);

var buffer = new List<(Address AvatarAddress, ArenaData Arena, AvatarData Avatar)>();
Expand All @@ -31,8 +30,6 @@ async Task FlushBufferAsync()

buffer.Clear();
}

await _store.UpdateLatestBlockIndex(latestBlockIndex);

foreach (var avatarAddress in arenaParticipants.AvatarAddresses)
{
Expand Down
26 changes: 14 additions & 12 deletions Mimir.Worker/Scrapper/StateGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ public class StateGetter
{
private readonly ILogger<StateGetter> _logger;
private readonly IStateService _service;
private readonly long? _index;

public StateGetter(IStateService service)
public StateGetter(IStateService service, long? index=null)
{
_logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<StateGetter>();
_service = service;
_index = index;
}

public async Task<T> GetSheet<T>()
where T : ISheet, new()
{
var sheetState = await _service.GetState(Addresses.TableSheet.Derive(typeof(T).Name));
var sheetState = await _service.GetState(Addresses.TableSheet.Derive(typeof(T).Name), _index);
if (sheetState is not Text sheetValue)
{
throw new ArgumentException(nameof(T));
Expand All @@ -41,7 +43,7 @@ public async Task<ArenaParticipants> GetArenaParticipantsState(int championshipI
{
var arenaParticipantsAddress =
ArenaParticipants.DeriveAddress(championshipId, roundId);
var state = await _service.GetState(arenaParticipantsAddress);
var state = await _service.GetState(arenaParticipantsAddress, _index);
return state switch
{
List list => new ArenaParticipants(list),
Expand All @@ -53,7 +55,7 @@ public async Task<ArenaScore> GetArenaScoreState(Address avatarAddress, int cham
{
var arenaScoreAddress =
ArenaScore.DeriveAddress(avatarAddress, championshipId, roundId);
var state = await _service.GetState(arenaScoreAddress);
var state = await _service.GetState(arenaScoreAddress, _index);
return state switch
{
List list => new ArenaScore(list),
Expand All @@ -65,7 +67,7 @@ public async Task<ArenaInformation> GetArenaInfoState(Address avatarAddress, int
{
var arenaInfoAddress =
ArenaInformation.DeriveAddress(avatarAddress, championshipId, roundId);
var state = await _service.GetState(arenaInfoAddress);
var state = await _service.GetState(arenaInfoAddress, _index);
return state switch
{
List list => new ArenaInformation(list),
Expand Down Expand Up @@ -129,7 +131,7 @@ public async Task<Inventory> GetInventoryState(Address avatarAddress)
public async Task<ItemSlotState> GetItemSlotState(Address avatarAddress)
{
var state = await _service.GetState(
ItemSlotState.DeriveAddress(avatarAddress, BattleType.Arena));
ItemSlotState.DeriveAddress(avatarAddress, BattleType.Arena), _index);
return state switch
{
List list => new ItemSlotState(list),
Expand All @@ -141,7 +143,7 @@ public async Task<ItemSlotState> GetItemSlotState(Address avatarAddress)
public async Task<List<RuneState>> GetRuneStates(Address avatarAddress)
{
var state = await _service.GetState(
RuneSlotState.DeriveAddress(avatarAddress, BattleType.Arena));
RuneSlotState.DeriveAddress(avatarAddress, BattleType.Arena), _index);
var runeSlotState = state switch
{
List list => new RuneSlotState(list),
Expand All @@ -152,7 +154,7 @@ public async Task<List<RuneState>> GetRuneStates(Address avatarAddress)
var runes = new List<RuneState>();
foreach (var runeStateAddress in runeSlotState.GetEquippedRuneSlotInfos().Select(info => RuneState.DeriveAddress(avatarAddress, info.RuneId)))
{
if (await _service.GetState(runeStateAddress) is List list)
if (await _service.GetState(runeStateAddress, _index) is List list)
{
runes.Add(new RuneState(list));
}
Expand All @@ -163,22 +165,22 @@ public async Task<List<RuneState>> GetRuneStates(Address avatarAddress)

public async Task<IValue?> GetStateWithLegacyAccount(Address address, Address accountAddress)
{
var state = await _service.GetState(address, accountAddress);
var state = await _service.GetState(address, accountAddress, _index);

if (state == null)
{
state = await _service.GetState(address);
state = await _service.GetState(address, _index);
}
return state;
}

public async Task<IValue?> GetAvatarStateWithLegacyAccount(Address avatarAddress, Address accountAddress, Address legacyAddress)
{
var state = await _service.GetState(avatarAddress, accountAddress);
var state = await _service.GetState(avatarAddress, accountAddress, _index);

if (state == null)
{
state = await _service.GetState(legacyAddress);
state = await _service.GetState(legacyAddress, _index);
}
return state;
}
Expand Down
7 changes: 3 additions & 4 deletions Mimir.Worker/Scrapper/TableSheetScrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ MongoDbStore store
private readonly IStateService _stateService = service;
private readonly MongoDbStore _store = store;

public async Task ExecuteAsync(CancellationToken cancellationToken)
public async Task ExecuteAsync(long blockIndex, CancellationToken cancellationToken)
{
var latestBlockIndex = await service.GetLatestIndex();
var stateGetter = _stateService.At();
var stateGetter = _stateService.At(blockIndex);

var sheetTypes = typeof(ISheet)
.Assembly.GetTypes()
Expand All @@ -49,7 +48,7 @@ type.Namespace is { } @namespace
}

var sheetAddress = Addresses.TableSheet.Derive(sheetType.Name);
var sheetState = await _stateService.GetState(sheetAddress);
var sheetState = await _stateService.GetState(sheetAddress, blockIndex);
if (sheetState is not Text sheetValue)
{
throw new ArgumentException(nameof(sheetType));
Expand Down
16 changes: 8 additions & 8 deletions Mimir.Worker/Services/HeadlessStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public class HeadlessStateService(IHeadlessGQLClient client) : IStateService

public long TipIndex => TipInfo.Item1;

public Task<IValue?[]> GetStates(Address[] addresses)
public Task<IValue?[]> GetStates(Address[] addresses, long? index)
{
return Task.WhenAll(addresses.Select(GetState));
return Task.WhenAll(addresses.Select(addr => GetState(addr, index)));
}

public Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress)
public Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress, long? index)
{
return Task.WhenAll(addresses.Select(addr => GetState(addr, accountAddress)));
return Task.WhenAll(addresses.Select(addr => GetState(addr, accountAddress, index)));
}

public async Task<IValue?> GetState(Address address)
public async Task<IValue?> GetState(Address address, long? index)
{
return await GetState(address, ReservedAddresses.LegacyAccount);
return await GetState(address, ReservedAddresses.LegacyAccount, index);
}

public async Task<IValue?> GetState(Address address, Address accountAddress)
public async Task<IValue?> GetState(Address address, Address accountAddress, long? index)
{
var result = await client.GetState.ExecuteAsync(accountAddress.ToString(), address.ToString());
var result = await client.GetState.ExecuteAsync(accountAddress.ToString(), address.ToString(), index);
result.EnsureNoErrors();

if (result.Data?.State is null)
Expand Down
8 changes: 4 additions & 4 deletions Mimir.Worker/Services/IStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Mimir.Worker.Services;
public interface IStateService
{
Task<int> GetLatestIndex();
Task<IValue?> GetState(Address address);
Task<IValue?> GetState(Address address, Address accountAddress);
Task<IValue?[]> GetStates(Address[] addresses);
Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress);
Task<IValue?> GetState(Address address, long? index);
Task<IValue?> GetState(Address address, Address accountAddress, long? index);
Task<IValue?[]> GetStates(Address[] addresses, long? index);
Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress, long? index);
}
4 changes: 2 additions & 2 deletions Mimir.Worker/Services/StateServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Mimir.Worker.Services;

public static class StateServiceExtensions
{
public static StateGetter At(this IStateService service)
public static StateGetter At(this IStateService service, long index)
{
return new StateGetter(service);
return new StateGetter(service, index);
}
}
4 changes: 2 additions & 2 deletions Mimir/Queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ query GetTip {
}
}

query GetState($accountAddress: Address! $address: Address!) {
state(accountAddress: $accountAddress, address: $address)
query GetState($accountAddress: Address! $address: Address!, $index: Long) {
state(accountAddress: $accountAddress, address: $address, index: $index)
}
16 changes: 8 additions & 8 deletions Mimir/Services/HeadlessStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public class HeadlessStateService(IHeadlessGQLClient client) : IStateService

public long TipIndex => TipInfo.Item1;

public Task<IValue?[]> GetStates(Address[] addresses)
public Task<IValue?[]> GetStates(Address[] addresses, long? index=null)
{
return Task.WhenAll(addresses.Select(GetState));
return Task.WhenAll(addresses.Select(addr => GetState(addr, index)));
}

public Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress)
public Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress, long? index)
{
return Task.WhenAll(addresses.Select(addr => GetState(addr, accountAddress)));
return Task.WhenAll(addresses.Select(addr => GetState(addr, accountAddress, index)));
}

public async Task<IValue?> GetState(Address address)
public async Task<IValue?> GetState(Address address, long? index)
{
return await GetState(address, ReservedAddresses.LegacyAccount);
return await GetState(address, ReservedAddresses.LegacyAccount, index);
}

public async Task<IValue?> GetState(Address address, Address accountAddress)
public async Task<IValue?> GetState(Address address, Address accountAddress, long? index)
{
var result = await client.GetState.ExecuteAsync(accountAddress.ToString(), address.ToString());
var result = await client.GetState.ExecuteAsync(accountAddress.ToString(), address.ToString(), index);
result.EnsureNoErrors();

if (result.Data?.State is null)
Expand Down
8 changes: 4 additions & 4 deletions Mimir/Services/IStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mimir.Services;

public interface IStateService
{
Task<IValue?> GetState(Address address);
Task<IValue?> GetState(Address address, Address accountAddress);
Task<IValue?[]> GetStates(Address[] addresses);
Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress);
Task<IValue?> GetState(Address address, long? index);
Task<IValue?> GetState(Address address, Address accountAddress, long? index);
Task<IValue?[]> GetStates(Address[] addresses, long? index);
Task<IValue?[]> GetStates(Address[] addresses, Address accountAddress, long? index);
}
21 changes: 11 additions & 10 deletions Mimir/Util/StateGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@

namespace Mimir.Util;

public class StateGetter(IStateService stateService)
public class StateGetter(IStateService stateService, long? index=null)
{
public async Task<IValue?> GetStateAsync(Address address, Address accountAddress) =>
await stateService.GetState(address, accountAddress) ??
await stateService.GetState(address);
await stateService.GetState(address, accountAddress, index) ??
await stateService.GetState(address, index);

public async Task<IValue?> GetStateWithLegacyAccountAsync(
Address address,
Address accountAddress,
Address legacyAddress) =>
await stateService.GetState(address, accountAddress) ??
await stateService.GetState(legacyAddress);
await stateService.GetState(address, accountAddress, index) ??
await stateService.GetState(legacyAddress, index);

public async Task<T?> GetSheetAsync<T>()
where T : ISheet, new()
{
var sheetState = await stateService.GetState(Addresses.GetSheetAddress<T>());
var sheetState = await stateService.GetState(Addresses.GetSheetAddress<T>(), index);
if (sheetState is not Text sheetValue)
{
throw new ArgumentException(nameof(T));
Expand All @@ -44,8 +44,8 @@ await stateService.GetState(address, accountAddress) ??
Address agentAddress,
bool withInventory = true)
{
var rawState = await stateService.GetState(agentAddress, Addresses.Agent) ??
await stateService.GetState(agentAddress);
var rawState = await stateService.GetState(agentAddress, Addresses.Agent, index) ??
await stateService.GetState(agentAddress, index);
var agentState = rawState switch
{
List agentStateList => new AgentState(agentStateList),
Expand Down Expand Up @@ -124,7 +124,7 @@ await stateService.GetState(address, accountAddress) ??
public async Task<ItemSlotState?> GetItemSlotStateAsync(Address avatarAddress)
{
var state = await stateService.GetState(
ItemSlotState.DeriveAddress(avatarAddress, BattleType.Arena));
ItemSlotState.DeriveAddress(avatarAddress, BattleType.Arena), index);
return state switch
{
List list => new ItemSlotState(list),
Expand Down Expand Up @@ -176,7 +176,8 @@ public async Task<Dictionary<Address, CollectionState>> GetCollectionStatesAsync
var result = new Dictionary<Address, CollectionState>();
var values = await stateService.GetStates(
addresses.ToArray(),
Addresses.Collection
Addresses.Collection,
index
);
for (var i = 0; i < addresses.Count; i++)
{
Expand Down

0 comments on commit e1427fd

Please sign in to comment.