Skip to content

Commit

Permalink
Merge pull request #467 from moreal/bugfix/validate-graphql
Browse files Browse the repository at this point in the history
fix: correct the logic to fetch transactions
  • Loading branch information
moreal authored Oct 29, 2024
2 parents 6ec46f0 + 2f4e8e4 commit e5b0f8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
33 changes: 26 additions & 7 deletions Mimir.Worker/Client/HeadlessGQLClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using BitFaster.Caching;
using BitFaster.Caching.Lru;
using Libplanet.Crypto;
using Microsoft.IdentityModel.Tokens;
Expand All @@ -17,7 +18,13 @@ public class HeadlessGQLClient : IHeadlessGQLClient
private readonly Uri[] _urls;
private readonly string? _issuer;
private readonly string? _secret;
private readonly ConcurrentLru<long, GetTransactionsResponse> _transactionCache = new(10);

private readonly IAsyncCache<long, GetTransactionsResponse> _transactionCache =
new ConcurrentLruBuilder<long, GetTransactionsResponse>()
.AsAsyncCache()
.WithExpireAfterWrite(TimeSpan.FromSeconds(30))
.WithCapacity(10)
.Build();
private const int RetryAttempts = 3;
private const int DelayInSeconds = 5;

Expand Down Expand Up @@ -94,7 +101,7 @@ private async Task<T> PostGraphQLRequestAsync<T>(
jsonResponse
);

if (graphQLResponse is null || graphQLResponse.Data is null)
if (graphQLResponse is null || graphQLResponse.Data is null || graphQLResponse.Errors is not null)
{
throw new HttpRequestException("Response data is null.");
}
Expand Down Expand Up @@ -211,10 +218,22 @@ public async Task<GetTransactionsResponse> GetTransactionsAsync(
{
return await _transactionCache.GetOrAddAsync(
blockIndex,
async (index) => await PostGraphQLRequestAsync<GetTransactionsResponse>(
GraphQLQueries.GetTransactions,
new { blockIndex=index, limit },
stoppingToken
));
async (index) =>
{
var response = await PostGraphQLRequestAsync<GetTransactionsResponse>(
GraphQLQueries.GetTransactions,
new { blockIndex = index, limit },
stoppingToken
);

// Validate `GetTransactionsResponse` is valid.
if (response.Transaction?.NCTransactions is null ||
response.Transaction.NCTransactions.Any(t => t is null || t.Actions.Any(a => a is null)))
{
throw new InvalidOperationException("Invalid transactions response.");
}

return response;
});
}
}
9 changes: 6 additions & 3 deletions Mimir.Worker/Client/Models.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Mimir.Worker.Client;
Expand All @@ -14,7 +15,9 @@ public class GraphQLRequest
public class GraphQLResponse<T>
{
[JsonPropertyName("data")]
public T Data { get; set; }
public T? Data { get; set; }

public JsonElement[]? Errors { get; set; }
}

public class GetAccountDiffsResponse
Expand Down Expand Up @@ -62,13 +65,13 @@ public class GetStateResponse
public class GetTransactionsResponse
{
[JsonPropertyName("transaction")]
public TransactionResponse Transaction { get; set; }
public TransactionResponse? Transaction { get; set; }
}

public class TransactionResponse
{
[JsonPropertyName("ncTransactions")]
public List<NcTransaction?> NCTransactions { get; set; }
public List<NcTransaction?>? NCTransactions { get; set; }
}

public class NcTransaction
Expand Down

0 comments on commit e5b0f8e

Please sign in to comment.