diff --git a/PatrolRewardService/PatrolRewardService/GraphqlTypes/NineChroniclesClient.cs b/PatrolRewardService/PatrolRewardService/GraphqlTypes/NineChroniclesClient.cs index 6407b39..d6a6a17 100644 --- a/PatrolRewardService/PatrolRewardService/GraphqlTypes/NineChroniclesClient.cs +++ b/PatrolRewardService/PatrolRewardService/GraphqlTypes/NineChroniclesClient.cs @@ -105,6 +105,12 @@ public async Task StageTx(Transaction tx) throw new GraphQLException(msg); } + /// + /// query transaction result by given + /// + /// + /// + /// public async Task Result(TxId txId) { var query = @" @@ -146,6 +152,54 @@ public async Task Result(TxId txId) throw new GraphQLException(msg); } + + /// + /// query transaction result by given transaction ids. the order of results is the same as the order of txIds. + /// + /// list of hex encoded tx ids + /// + /// + public async Task> Results(List txIds) + { + var query = @" +query($txIds: [TxId]!) { + transaction { + transactionResults(txIds: $txIds) { + txStatus + exceptionNames + blockIndex + } + } +} +"; + var variables = new + { + txIds, + }; + var request = new GraphQLHttpRequestWithAuth + { + Query = query, + Variables = variables, + Authentication = new AuthenticationHeaderValue("Bearer",Token()), + }; + + GraphQLResponse resp; + try + { + resp = await _client.SendQueryAsync(request); + } + catch (Exception e) + { + _logger.LogError("{Msg}", e.Message); + throw; + } + + if (resp.Errors is null) return resp.Data.Transaction.TransactionResults; + + var msg = resp.Errors.Aggregate("", (current, error) => current + error.Message + "\n"); + throw new GraphQLException(msg); + } + public async Task Tip() { var query = @"query { @@ -220,6 +274,16 @@ public class TransactionResultQuery public TransactionResult TransactionResult; } + public class TransactionResultsResponse + { + public TransactionResultsQuery Transaction; + } + + public class TransactionResultsQuery + { + public IReadOnlyList TransactionResults; + } + public class TransactionResult { public List? exceptionNames; diff --git a/PatrolRewardService/PatrolRewardService/TransactionStageWorker.cs b/PatrolRewardService/PatrolRewardService/TransactionStageWorker.cs index 6458c3b..c8d9710 100644 --- a/PatrolRewardService/PatrolRewardService/TransactionStageWorker.cs +++ b/PatrolRewardService/PatrolRewardService/TransactionStageWorker.cs @@ -45,11 +45,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } } + /// + /// Staging or transactions. + /// + /// + /// + /// public static async Task StageTx(RewardDbContext dbContext, NineChroniclesClient client, CancellationToken stoppingToken) { var transactions = dbContext.Transactions - .Where(p => p.Result == TransactionStatus.CREATED).ToList(); + .Where(p => p.Result == TransactionStatus.CREATED || p.Result == TransactionStatus.INVALID); foreach (var transaction in transactions) { var tx = Transaction.Deserialize(Convert.FromBase64String(transaction.Payload)); diff --git a/PatrolRewardService/PatrolRewardService/TransactionWorker.cs b/PatrolRewardService/PatrolRewardService/TransactionWorker.cs index dbf452e..6fcf4d3 100644 --- a/PatrolRewardService/PatrolRewardService/TransactionWorker.cs +++ b/PatrolRewardService/PatrolRewardService/TransactionWorker.cs @@ -1,4 +1,3 @@ -using Libplanet.Types.Tx; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using PatrolRewardService.GraphqlTypes; @@ -45,6 +44,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } } + /// + /// Update staged transactions result + /// + /// + /// + /// public static async Task UpdateTx(RewardDbContext dbContext, NineChroniclesClient client, CancellationToken stoppingToken) { @@ -53,9 +58,13 @@ public static async Task UpdateTx(RewardDbContext dbContext, NineChroniclesClien .OrderBy(p => p.Nonce) .Take(100) .ToList(); - foreach (var tx in transactions) + var txIds = transactions.Select(t => t.TxId.ToHex()).ToList(); + var results = await client.Results(txIds); + var count = transactions.Count; + for (int i = 0; i < count; i++) { - var result = await client.Result(tx.TxId); + var result = results[i]; + var tx = transactions[i]; tx.Result = result.txStatus; tx.ExceptionName = result.exceptionNames?.FirstOrDefault(); }