From cd70f9fb3c9b59817aab814026c7368e0e8c82f0 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Mon, 25 Sep 2023 13:27:24 +0900 Subject: [PATCH] Accommodate libplanet API changes --- Lib9c | 2 +- Libplanet.Headless/ReducedStore.cs | 16 +------- .../Commands/MarketCommand.cs | 4 +- .../GraphQLServiceExtensions.cs | 3 -- .../GraphTypes/StandaloneQuery.cs | 39 +++++++++---------- .../GraphTypes/StandaloneSubscription.cs | 26 ++++--------- .../GraphTypes/TransactionHeadlessQuery.cs | 32 +++++---------- 7 files changed, 40 insertions(+), 82 deletions(-) diff --git a/Lib9c b/Lib9c index 3fe68024d..ff30ed736 160000 --- a/Lib9c +++ b/Lib9c @@ -1 +1 @@ -Subproject commit 3fe68024d26000ef3ff17c8a2ad6be56bcc05d59 +Subproject commit ff30ed736aa7f03c1cb67690c1ce10f46035dea5 diff --git a/Libplanet.Headless/ReducedStore.cs b/Libplanet.Headless/ReducedStore.cs index 389597d0b..851fb6347 100644 --- a/Libplanet.Headless/ReducedStore.cs +++ b/Libplanet.Headless/ReducedStore.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; -using Bencodex.Types; using Libplanet.Crypto; using Libplanet.Store; using Libplanet.Types.Blocks; @@ -106,21 +104,11 @@ public void PutBlock(Block block) => public void PutTransaction(Transaction tx) => InternalStore.PutTransaction(tx); - public void PutTxExecution(TxSuccess txSuccess) + public void PutTxExecution(TxExecution txExecution) { - // Omit TxSuccess.UpdatedStates as it is unused by Nine Chronicles and too big. - TxSuccess reducedTxSuccess = new TxSuccess( - txSuccess.BlockHash, - txSuccess.TxId, - updatedStates: txSuccess.UpdatedStates.ToImmutableDictionary(pair => pair.Key, _ => (IValue)Null.Value), - updatedFungibleAssets: txSuccess.UpdatedFungibleAssets - ); - InternalStore.PutTxExecution(reducedTxSuccess); + InternalStore.PutTxExecution(txExecution); } - public void PutTxExecution(TxFailure txFailure) => - InternalStore.PutTxExecution(txFailure); - public void SetCanonicalChainId(Guid chainId) => InternalStore.SetCanonicalChainId(chainId); diff --git a/NineChronicles.Headless.Executable/Commands/MarketCommand.cs b/NineChronicles.Headless.Executable/Commands/MarketCommand.cs index a7ea80af1..8aaa5f649 100644 --- a/NineChronicles.Headless.Executable/Commands/MarketCommand.cs +++ b/NineChronicles.Headless.Executable/Commands/MarketCommand.cs @@ -108,8 +108,8 @@ public void Query( IEnumerable<(Transaction, ActionBase)> actions = block.Transactions .Reverse() .Where(tx => includeFails || - !(chain.GetTxExecution(block.Hash, tx.Id) is { } e) || - e is TxSuccess) + !(chain.GetTxExecution(block.Hash, tx.Id) is { } e) || + !e.Fail) .SelectMany(tx => tx.Actions is { } ca ? ca.Reverse().Select(a => (tx, ToAction(a))) : Enumerable.Empty<(Transaction, ActionBase)>()); diff --git a/NineChronicles.Headless/GraphQLServiceExtensions.cs b/NineChronicles.Headless/GraphQLServiceExtensions.cs index 9820b3d93..cad7d67af 100644 --- a/NineChronicles.Headless/GraphQLServiceExtensions.cs +++ b/NineChronicles.Headless/GraphQLServiceExtensions.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Reflection; using GraphQL.Types; -using Libplanet.Action; using Libplanet.Explorer.GraphTypes; using Libplanet.Explorer.Interfaces; using Libplanet.Explorer.Queries; @@ -36,8 +35,6 @@ public static IServiceCollection AddLibplanetScalarTypes(this IServiceCollection services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs index 6823c4ea4..3bb21bea1 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs @@ -115,30 +115,27 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi var recipient = context.GetArgument("recipient"); - IEnumerable txs = digest.TxIds + IEnumerable blockTxs = digest.TxIds .Select(b => new TxId(b.ToBuilder().ToArray())) .Select(store.GetTransaction); - var filteredTransactions = txs.Where(tx => - tx.Actions!.Count == 1 && - ToAction(tx.Actions.First()) is ITransferAsset transferAsset && - (!recipient.HasValue || transferAsset.Recipient == recipient) && - transferAsset.Amount.Currency.Ticker == "NCG" && - store.GetTxExecution(blockHash, tx.Id) is TxSuccess); - - TransferNCGHistory ToTransferNCGHistory(TxSuccess txSuccess, string? memo) - { - return new TransferNCGHistory( - txSuccess.BlockHash, - txSuccess.TxId, - default, - default, - new FungibleAssetValue(), - memo); - } - var histories = filteredTransactions.Select(tx => - ToTransferNCGHistory((TxSuccess)store.GetTxExecution(blockHash, tx.Id), - ((ITransferAsset)ToAction(tx.Actions!.Single())).Memo)); + var filtered = blockTxs + .Where(tx => tx.Actions.Count == 1) + .Select(tx => (store.GetTxExecution(blockHash, tx.Id), ToAction(tx.Actions[0]))) + .Where(pair => pair.Item1 is { } && pair.Item2 is ITransferAsset) + .Select(pair => (pair.Item1, (ITransferAsset)pair.Item2)) + .Where(pair => !pair.Item1.Fail && + (!recipient.HasValue || pair.Item2.Recipient == recipient) && + pair.Item2.Amount.Currency.Ticker == "NCG"); + + var histories = filtered.Select(pair => + new TransferNCGHistory( + pair.Item1.BlockHash, + pair.Item1.TxId, + pair.Item2.Sender, + pair.Item2.Recipient, + pair.Item2.Amount, + pair.Item2.Memo)); return histories; }); diff --git a/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs b/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs index 2bce5e686..f32eeb1f8 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs @@ -321,29 +321,17 @@ private IObservable SubscribeTx(IResolveFieldContext context) } var txExecution = store.GetTxExecution(blockHash, transaction.Id); var txExecutedBlock = chain[blockHash]; - - return txExecution switch - { - TxSuccess success => new TxResult( - TxStatus.SUCCESS, + return txExecution.Fail + ? new TxResult( + TxStatus.FAILURE, txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), - null, - success.UpdatedStates - .Select(kv => new KeyValuePair( - kv.Key, - kv.Value)) - .ToImmutableDictionary(), - success.UpdatedFungibleAssets), - TxFailure failure => new TxResult( - TxStatus.FAILURE, + txExecution.ExceptionNames) + : new TxResult( + TxStatus.SUCCESS, txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), - failure.ExceptionName, - null, - null), - _ => null - }; + txExecution.ExceptionNames); } private void RenderBlock((Block OldTip, Block NewTip) pair) diff --git a/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs b/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs index ecaa294ee..dc037448f 100644 --- a/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs +++ b/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs @@ -207,41 +207,29 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext) if (!(store.GetFirstTxIdBlockHashIndex(txId) is { } txExecutedBlockHash)) { return blockChain.GetStagedTransactionIds().Contains(txId) - ? new TxResult(TxStatus.STAGING, null, null, null, null, null) - : new TxResult(TxStatus.INVALID, null, null, null, null, null); + ? new TxResult(TxStatus.STAGING, null, null, null) + : new TxResult(TxStatus.INVALID, null, null, null); } try { TxExecution execution = blockChain.GetTxExecution(txExecutedBlockHash, txId); Block txExecutedBlock = blockChain[txExecutedBlockHash]; - return execution switch - { - TxSuccess txSuccess => new TxResult( - TxStatus.SUCCESS, + return execution.Fail + ? new TxResult( + TxStatus.FAILURE, txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), - null, - txSuccess.UpdatedStates - .Select(kv => new KeyValuePair( - kv.Key, - kv.Value)) - .ToImmutableDictionary(), - txSuccess.UpdatedFungibleAssets), - TxFailure txFailure => new TxResult( - TxStatus.FAILURE, + execution.ExceptionNames) + : new TxResult( + TxStatus.SUCCESS, txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), - txFailure.ExceptionName, - null, - null), - _ => throw new NotImplementedException( - $"{nameof(execution)} is not expected concrete class.") - }; + execution.ExceptionNames); } catch (Exception) { - return new TxResult(TxStatus.INVALID, null, null, null, null, null); + return new TxResult(TxStatus.INVALID, null, null, null); } } );