From 05e3886b9d337be80321c6b8363f6376204ec697 Mon Sep 17 00:00:00 2001 From: area363 Date: Thu, 31 Aug 2023 23:38:29 +0900 Subject: [PATCH 1/2] bump lib9c bump: Lib9c --- Lib9c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib9c b/Lib9c index f537ee791..a15e2151f 160000 --- a/Lib9c +++ b/Lib9c @@ -1 +1 @@ -Subproject commit f537ee791eb552350d0bb35a77cad5e89c46fd6d +Subproject commit a15e2151f93ede2da1a6402b097a833b348a89d7 From 28f36d2b93cfb56230702ef868477df450fd702f Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Tue, 5 Sep 2023 14:56:57 +0900 Subject: [PATCH 2/2] Accommodate API changes --- .../Hosting/LibplanetNodeServiceTest.cs | 2 +- Libplanet.Headless/ReducedStore.cs | 1 - .../Commands/ReplayCommand.Privates.cs | 36 +++++++------- .../Commands/ReplayCommand.cs | 2 +- .../Commands/StateCommand.cs | 2 +- .../Common/Actions/EmptyAction.cs | 6 +-- .../GraphTypes/StandaloneQuery.cs | 49 ++++++++----------- .../GraphTypes/StandaloneSubscription.cs | 4 -- .../GraphTypes/TransactionHeadlessQuery.cs | 10 ++-- 9 files changed, 48 insertions(+), 64 deletions(-) diff --git a/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs b/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs index 85d414fb5..938e5e305 100644 --- a/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs +++ b/Libplanet.Headless.Tests/Hosting/LibplanetNodeServiceTest.cs @@ -88,7 +88,7 @@ private class DummyAction : IAction { IValue IAction.PlainValue => Dictionary.Empty; - IAccountStateDelta IAction.Execute(IActionContext context) + IAccount IAction.Execute(IActionContext context) { return context.PreviousState; } diff --git a/Libplanet.Headless/ReducedStore.cs b/Libplanet.Headless/ReducedStore.cs index e55be9d65..389597d0b 100644 --- a/Libplanet.Headless/ReducedStore.cs +++ b/Libplanet.Headless/ReducedStore.cs @@ -113,7 +113,6 @@ public void PutTxExecution(TxSuccess txSuccess) txSuccess.BlockHash, txSuccess.TxId, updatedStates: txSuccess.UpdatedStates.ToImmutableDictionary(pair => pair.Key, _ => (IValue)Null.Value), - fungibleAssetsDelta: txSuccess.FungibleAssetsDelta, updatedFungibleAssets: txSuccess.UpdatedFungibleAssets ); InternalStore.PutTxExecution(reducedTxSuccess); diff --git a/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs b/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs index 05046254b..24e371342 100644 --- a/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs +++ b/NineChronicles.Headless.Executable/Commands/ReplayCommand.Privates.cs @@ -29,7 +29,7 @@ public partial class ReplayCommand : CoconaLiteConsoleAppBase /// Almost duplicate https://github.com/planetarium/libplanet/blob/main/Libplanet/State/AccountStateDelta.cs. /// [Pure] - private sealed class AccountStateDelta : IAccountStateDelta + private sealed class AccountStateDelta : IAccount { private readonly IAccountState _baseState; @@ -97,7 +97,7 @@ private AccountStateDelta(IAccountState baseState, IAccountDelta delta) /// [Pure] - public IAccountStateDelta SetState(Address address, IValue state) => + public IAccount SetState(Address address, IValue state) => UpdateStates(Delta.States.SetItem(address, state)); /// @@ -132,7 +132,7 @@ public ValidatorSet GetValidatorSet() => /// [Pure] - public IAccountStateDelta MintAsset( + public IAccount MintAsset( IActionContext context, Address recipient, FungibleAssetValue value) { if (value.Sign <= 0) @@ -183,7 +183,7 @@ public IAccountStateDelta MintAsset( /// [Pure] - public IAccountStateDelta TransferAsset( + public IAccount TransferAsset( IActionContext context, Address sender, Address recipient, @@ -194,7 +194,7 @@ public IAccountStateDelta TransferAsset( /// [Pure] - public IAccountStateDelta BurnAsset( + public IAccount BurnAsset( IActionContext context, Address owner, FungibleAssetValue value) { string msg; @@ -245,7 +245,7 @@ public IAccountStateDelta BurnAsset( /// [Pure] - public IAccountStateDelta SetValidator(Validator validator) + public IAccount SetValidator(Validator validator) { return UpdateValidatorSet(GetValidatorSet().Update(validator)); } @@ -257,14 +257,14 @@ public IAccountStateDelta SetValidator(Validator validator) /// a basis. /// A null state delta created from . /// - internal static IAccountStateDelta Create(IAccountState previousState) => + internal static IAccount Create(IAccountState previousState) => new AccountStateDelta(previousState); /// /// Creates a null state delta while inheriting s /// total updated fungibles. /// - /// The previous to use. + /// The previous to use. /// A null state delta that is of the same type as . /// /// Thrown if given @@ -272,9 +272,9 @@ internal static IAccountStateDelta Create(IAccountState previousState) => /// /// /// This inherits 's - /// . + /// . /// - internal static IAccountStateDelta Flush(IAccountStateDelta stateDelta) => + internal static IAccount Flush(IAccount stateDelta) => stateDelta is AccountStateDelta impl ? new AccountStateDelta(stateDelta) { @@ -348,7 +348,7 @@ private AccountStateDelta UpdateValidatorSet( }; [Pure] - private IAccountStateDelta TransferAssetV0( + private IAccount TransferAssetV0( Address sender, Address recipient, FungibleAssetValue value, @@ -384,7 +384,7 @@ private IAccountStateDelta TransferAssetV0( } [Pure] - private IAccountStateDelta TransferAssetV1( + private IAccount TransferAssetV1( Address sender, Address recipient, FungibleAssetValue value, @@ -501,7 +501,7 @@ public ActionContext( Address miner, long blockIndex, int blockProtocolVersion, - IAccountStateDelta previousState, + IAccount previousState, int randomSeed, bool rehearsal = false) { @@ -528,7 +528,7 @@ public ActionContext( public bool Rehearsal { get; } - public IAccountStateDelta PreviousState { get; } + public IAccount PreviousState { get; } public IRandom Random { get; } @@ -746,7 +746,7 @@ private static IEnumerable EvaluateActions( long blockIndex, int blockProtocolVersion, TxId? txid, - IAccountStateDelta previousStates, + IAccount previousStates, Address miner, Address signer, byte[] signature, @@ -754,7 +754,7 @@ private static IEnumerable EvaluateActions( ILogger? logger = null) { ActionContext CreateActionContext( - IAccountStateDelta prevState, + IAccount prevState, int randomSeed) { return new ActionContext( @@ -776,11 +776,11 @@ ActionContext CreateActionContext( byte[] preEvaluationHashBytes = preEvaluationHash.ToByteArray(); int seed = ActionEvaluator.GenerateRandomSeed(preEvaluationHashBytes, hashedSignature, signature, 0); - IAccountStateDelta states = previousStates; + IAccount states = previousStates; foreach (IAction action in actions) { Exception? exc = null; - IAccountStateDelta nextStates = states; + IAccount nextStates = states; ActionContext context = CreateActionContext(nextStates, seed); try diff --git a/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs b/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs index 02a7f1218..0f8f70f6e 100644 --- a/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs +++ b/NineChronicles.Headless.Executable/Commands/ReplayCommand.cs @@ -101,7 +101,7 @@ public int Tx( // Evaluate tx. IAccountState previousBlockStates = blockChain.GetBlockState(previousBlock.Hash); - IAccountStateDelta previousStates = AccountStateDelta.Create(previousBlockStates); + IAccount previousStates = AccountStateDelta.Create(previousBlockStates); var actions = tx.Actions.Select(a => ToAction(a)); var actionEvaluations = EvaluateActions( preEvaluationHash: targetBlock.PreEvaluationHash, diff --git a/NineChronicles.Headless.Executable/Commands/StateCommand.cs b/NineChronicles.Headless.Executable/Commands/StateCommand.cs index 513a7e90b..235e7bbd8 100644 --- a/NineChronicles.Headless.Executable/Commands/StateCommand.cs +++ b/NineChronicles.Headless.Executable/Commands/StateCommand.cs @@ -461,7 +461,7 @@ private static ImmutableDictionary GetTotalDelta( return ImmutableDictionary.Empty; } - IAccountStateDelta lastStates = actionEvaluations[actionEvaluations.Count - 1].OutputState; + IAccount lastStates = actionEvaluations[actionEvaluations.Count - 1].OutputState; ImmutableDictionary totalDelta = stateUpdatedAddresses.ToImmutableDictionary( diff --git a/NineChronicles.Headless.Tests/Common/Actions/EmptyAction.cs b/NineChronicles.Headless.Tests/Common/Actions/EmptyAction.cs index de3b21288..a33c0ecb2 100644 --- a/NineChronicles.Headless.Tests/Common/Actions/EmptyAction.cs +++ b/NineChronicles.Headless.Tests/Common/Actions/EmptyAction.cs @@ -11,12 +11,12 @@ public void LoadPlainValue(IValue plainValue) { } - public IAccountStateDelta Execute(IActionContext context) + public IAccount Execute(IActionContext context) { return context.PreviousState; } - public void Render(IActionContext context, IAccountStateDelta nextStates) + public void Render(IActionContext context, IAccount nextStates) { } @@ -24,7 +24,7 @@ public void RenderError(IActionContext context, Exception exception) { } - public void Unrender(IActionContext context, IAccountStateDelta nextStates) + public void Unrender(IActionContext context, IAccount nextStates) { } diff --git a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs index 87667f91f..95ad54498 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneQuery.cs @@ -116,39 +116,32 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi var recipient = context.GetArgument("recipient"); IEnumerable txs = digest.TxIds - .Select(b => new TxId(b.ToBuilder().ToArray())) + .Select(bytes => new TxId(bytes)) .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) + + var pairs = txs + .Where(tx => + tx.Actions!.Count == 1 && + store.GetTxExecution(blockHash, tx.Id) is TxSuccess) + .Select(tx => (tx.Id, ToAction(tx.Actions.First()))) + .Where(pair => + pair.Item2 is ITransferAsset transferAssset && + transferAssset.Amount.Currency.Ticker == "NCG") + .Select(pair => (pair.Item1, (ITransferAsset)pair.Item2)) + .Where(pair => (!(recipient is { } r) || pair.Item2.Recipient == r)); + + TransferNCGHistory ToTransferNCGHistory((TxId TxId, ITransferAsset Transfer) pair) { - var rawTransferNcgHistories = txSuccess.FungibleAssetsDelta - .Where(pair => pair.Value.Values.Any(fav => fav.Currency.Ticker == "NCG")) - .Select(pair => - (pair.Key, pair.Value.Values.First(fav => fav.Currency.Ticker == "NCG"))) - .ToArray(); - var ((senderAddress, _), (recipientAddress, amount)) = - rawTransferNcgHistories[0].Item2.RawValue > rawTransferNcgHistories[1].Item2.RawValue - ? (rawTransferNcgHistories[1], rawTransferNcgHistories[0]) - : (rawTransferNcgHistories[0], rawTransferNcgHistories[1]); return new TransferNCGHistory( - txSuccess.BlockHash, - txSuccess.TxId, - senderAddress, - recipientAddress, - amount, - memo); + blockHash, + pair.TxId, + pair.Transfer.Sender, + pair.Transfer.Recipient, + pair.Transfer.Amount, + pair.Transfer.Memo); } - var histories = filteredTransactions.Select(tx => - ToTransferNCGHistory((TxSuccess)store.GetTxExecution(blockHash, tx.Id), - ((ITransferAsset)ToAction(tx.Actions!.Single())).Memo)); - + var histories = pairs.Select(ToTransferNCGHistory); return histories; }); diff --git a/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs b/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs index 4b699c65e..4fdf34bac 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs @@ -329,21 +329,17 @@ private IObservable SubscribeTx(IResolveFieldContext context) txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), null, - null, success.UpdatedStates .Select(kv => new KeyValuePair( kv.Key, kv.Value)) .ToImmutableDictionary(), - success.FungibleAssetsDelta, success.UpdatedFungibleAssets), TxFailure failure => new TxResult( TxStatus.FAILURE, txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), failure.ExceptionName, - failure.ExceptionMetadata, - null, null, null), _ => null diff --git a/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs b/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs index fda11309a..ecaa294ee 100644 --- a/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs +++ b/NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs @@ -207,8 +207,8 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext) if (!(store.GetFirstTxIdBlockHashIndex(txId) is { } txExecutedBlockHash)) { return blockChain.GetStagedTransactionIds().Contains(txId) - ? new TxResult(TxStatus.STAGING, null, null, null, null, null, null, null) - : new TxResult(TxStatus.INVALID, null, null, null, null, null, null, null); + ? new TxResult(TxStatus.STAGING, null, null, null, null, null) + : new TxResult(TxStatus.INVALID, null, null, null, null, null); } try @@ -222,21 +222,17 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext) txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), null, - null, txSuccess.UpdatedStates .Select(kv => new KeyValuePair( kv.Key, kv.Value)) .ToImmutableDictionary(), - txSuccess.FungibleAssetsDelta, txSuccess.UpdatedFungibleAssets), TxFailure txFailure => new TxResult( TxStatus.FAILURE, txExecutedBlock.Index, txExecutedBlock.Hash.ToString(), txFailure.ExceptionName, - txFailure.ExceptionMetadata, - null, null, null), _ => throw new NotImplementedException( @@ -245,7 +241,7 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext) } catch (Exception) { - return new TxResult(TxStatus.INVALID, null, null, null, null, null, null, null); + return new TxResult(TxStatus.INVALID, null, null, null, null, null); } } );