From b41933f460854e7bb2c85a78b4f424caa71a2c8a Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Mon, 4 Dec 2023 14:59:38 +0900 Subject: [PATCH 1/5] Revert "Propagate TxContext usage inside ActionEvaluator" This reverts commit 6df8806639dc56ce3c1e5179abc5f961f73c7bf4. --- Libplanet.Action/ActionEvaluator.cs | 77 ++++++++++++++++------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/Libplanet.Action/ActionEvaluator.cs b/Libplanet.Action/ActionEvaluator.cs index 856d880b7a2..05fc1f4fe33 100644 --- a/Libplanet.Action/ActionEvaluator.cs +++ b/Libplanet.Action/ActionEvaluator.cs @@ -183,28 +183,37 @@ internal static IEnumerable EvaluateActions( IImmutableList actions, ILogger? logger = null) { - ITxContext txContext = new TxContext( - signer: tx?.Signer ?? blockHeader.Miner, - txId: tx?.Id ?? null, - miner: blockHeader.Miner, - blockIndex: blockHeader.Index, - blockProtocolVersion: blockHeader.ProtocolVersion); + IActionContext CreateActionContext( + IAccount prevState, + int randomSeed, + long actionGasLimit) + { + return new ActionContext( + new TxContext( + signer: tx?.Signer ?? blockHeader.Miner, + txId: tx?.Id ?? null, + miner: blockHeader.Miner, + blockIndex: blockHeader.Index, + blockProtocolVersion: blockHeader.ProtocolVersion), + previousState: prevState, + randomSeed: randomSeed, + gasLimit: actionGasLimit); + } + + long gasLimit = tx?.GasLimit ?? long.MaxValue; byte[] preEvaluationHashBytes = blockHeader.PreEvaluationHash.ToByteArray(); byte[] signature = tx?.Signature ?? Array.Empty(); int seed = GenerateRandomSeed(preEvaluationHashBytes, signature, 0); - long gasLimit = tx?.GasLimit ?? long.MaxValue; IAccount state = previousState; foreach (IAction action in actions) { + IActionContext context = CreateActionContext(state, seed, gasLimit); (ActionEvaluation Evaluation, long NextGasLimit) result = EvaluateAction( blockHeader, tx, - txContext, - state, - seed, - gasLimit, + context, action, logger); @@ -220,39 +229,39 @@ internal static IEnumerable EvaluateActions( } } - // FIXME: Argument blockHeader is passed on purely for logging. internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction( IPreEvaluationBlockHeader blockHeader, ITransaction? tx, - ITxContext txContext, - IAccount prevState, - int randomSeed, - long gasLimit, + IActionContext context, IAction action, ILogger? logger = null) { - IActionContext inputContext = new ActionContext( - txContext, - prevState, - randomSeed, - gasLimit); - IActionContext context = inputContext; - + IActionContext inputContext = context; IAccount state = inputContext.PreviousState; Exception? exc = null; IFeeCollector feeCollector = new FeeCollector(context, tx?.MaxGasPrice); + IActionContext CreateActionContext(IAccount newPrevState) + { + return new ActionContext( + new TxContext( + signer: inputContext.Signer, + txId: inputContext.TxId, + miner: inputContext.Miner, + blockIndex: inputContext.BlockIndex, + blockProtocolVersion: inputContext.BlockProtocolVersion), + previousState: newPrevState, + randomSeed: inputContext.RandomSeed, + gasLimit: inputContext.GasLimit()); + } + try { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); AccountMetrics.Initialize(); state = feeCollector.Mortgage(state); - context = new ActionContext( - txContext: txContext, - previousState: state, - randomSeed: randomSeed, - gasLimit: gasLimit); + context = CreateActionContext(state); feeCollector = feeCollector.Next(context); state = action.Execute(context); logger? @@ -279,8 +288,8 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction( e, message, action, - txContext.TxId, - txContext.BlockIndex, + tx?.Id, + blockHeader.Index, ByteUtil.Hex(blockHeader.PreEvaluationHash.ByteArray)); throw; } @@ -294,8 +303,8 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction( e, message, action, - txContext.TxId, - txContext.BlockIndex, + tx?.Id, + blockHeader.Index, ByteUtil.Hex(blockHeader.PreEvaluationHash.ByteArray)); var innerMessage = $"The action {action} (block #{blockHeader.Index}, " + @@ -308,8 +317,8 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction( exc = new UnexpectedlyTerminatedActionException( innerMessage, blockHeader.PreEvaluationHash, - txContext.BlockIndex, - txContext.TxId, + blockHeader.Index, + tx?.Id, null, action, e); From 686eb002881c538f022639dbee4145b521efa2f6 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Mon, 4 Dec 2023 14:59:48 +0900 Subject: [PATCH 2/5] Revert "Changelog" This reverts commit 945df40952c9f9c6e38cf5e702e98c4ac1b473a5. --- CHANGES.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a30a6247e7d..5008a7da254 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,13 +20,6 @@ be compatible with this version, specifically, those that ran with - Changed `TxInvoice` to no longer allow having the null-ness of `MaxGasPrice` and `GasLimit` to be different, i.e. either both should be null or both should not be null at the same time. [[#3529]] - - (Libplanet.Action) Changed `IActionContext` to inherit `ITxContext`. - The following properties of `IActionContext` have moved to `ITxContext`: - `Signer`, `TxId`, `Miner`, `BlockIndex`, and `BlockProtocolVersion`. - [[#3530]] - - (Libplanet.Action) Changed `IAccount.Mint()`, `IAccount.Burn()`, and - `IAccount.Transfer()` to accept `ITxContext` instead of `IActionContext` - as its parameter. [[#3530]] ### Backward-incompatible network protocol changes @@ -34,8 +27,6 @@ be compatible with this version, specifically, those that ran with ### Added APIs - - (Libplanet.Action) Added `ITxContext` interface. [[#3530]] - ### Behavioral changes ### Bug fixes @@ -46,7 +37,6 @@ be compatible with this version, specifically, those that ran with [#3523]: https://github.com/planetarium/libplanet/pull/3523 [#3529]: https://github.com/planetarium/libplanet/pull/3529 -[#3530]: https://github.com/planetarium/libplanet/pull/3530 [Libplanet 2.0.0]: https://www.nuget.org/packages/Libplanet/2.0.0 From 68e6d09e62bcf3299ba3299d9648430ff43f7e14 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Mon, 4 Dec 2023 14:59:56 +0900 Subject: [PATCH 3/5] Revert "Update docs" This reverts commit 1b9912297951a7170757607f7f06ea97111ed236. --- Libplanet.Action/ActionContext.cs | 3 --- Libplanet.Action/IActionContext.cs | 9 +-------- Libplanet.Action/ITxContext.cs | 7 ++----- Libplanet.Action/State/IAccount.cs | 14 +++++++------- Libplanet.Action/TxContext.cs | 4 +--- 5 files changed, 11 insertions(+), 26 deletions(-) diff --git a/Libplanet.Action/ActionContext.cs b/Libplanet.Action/ActionContext.cs index 1f6e0776174..6b66ba6ac05 100644 --- a/Libplanet.Action/ActionContext.cs +++ b/Libplanet.Action/ActionContext.cs @@ -6,9 +6,6 @@ namespace Libplanet.Action { - /// - /// Implements . - /// internal class ActionContext : IActionContext { public static readonly AsyncLocal GetGasMeter = new AsyncLocal(); diff --git a/Libplanet.Action/IActionContext.cs b/Libplanet.Action/IActionContext.cs index ef6114fdfc5..884cd4a47ea 100644 --- a/Libplanet.Action/IActionContext.cs +++ b/Libplanet.Action/IActionContext.cs @@ -1,19 +1,12 @@ using System.Diagnostics.Contracts; using Libplanet.Action.State; -using Libplanet.Types.Blocks; -using Libplanet.Types.Tx; namespace Libplanet.Action { /// - /// - /// Contextual data determined by a , - /// a , and - /// (in particular, the 's offset within the - /// ). + /// Contextual data determined by a transaction and a block. /// Passed to method. /// - /// public interface IActionContext : ITxContext { /// diff --git a/Libplanet.Action/ITxContext.cs b/Libplanet.Action/ITxContext.cs index 942e6f57099..b5c37aee49a 100644 --- a/Libplanet.Action/ITxContext.cs +++ b/Libplanet.Action/ITxContext.cs @@ -6,12 +6,9 @@ namespace Libplanet.Action { /// - /// Contextual data determined by a and - /// a . - /// Passed to method as part of the - /// argument. + /// Contextual data determined by a transaction and a block. + /// Passed to method. /// - /// public interface ITxContext { /// diff --git a/Libplanet.Action/State/IAccount.cs b/Libplanet.Action/State/IAccount.cs index a317b8283a0..15936408444 100644 --- a/Libplanet.Action/State/IAccount.cs +++ b/Libplanet.Action/State/IAccount.cs @@ -78,8 +78,8 @@ public interface IAccount : IAccountState /// Mints the fungible asset (i.e., in-game monetary), /// and give it to the . /// - /// The of the - /// executing this method to validate the authority. + /// The of the + /// executing this method. /// The address who receives the minted asset. /// The asset value to mint. /// A new instance that the given (i.e., in-game monetary) /// from the to the . /// - /// The of the - /// executing this method to determine its behavior for backward compatibility. + /// The of the + /// executing this method. /// The address who sends the fungible asset to /// the . /// The address who receives the fungible asset from @@ -120,7 +120,7 @@ IAccount MintAsset( /// the option is turned off. /// /// The behavior is different depending on 's - /// . There is a bug for version 0 + /// . There is a bug for version 0 /// where this may not act as intended. Such behavior is left intact for backward /// compatibility. /// @@ -137,8 +137,8 @@ IAccount TransferAsset( /// Burns the fungible asset (i.e., in-game monetary) from /// 's balance. /// - /// The of the - /// executing this method to validate the authority. + /// The of the + /// executing this method. /// The address who owns the fungible asset to burn. /// The fungible asset to burn. /// A new instance that the given - /// Implements . - /// internal class TxContext : ITxContext { public TxContext( From 52b9e5d404879ab6c6f6456791db4248581df14b Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Mon, 4 Dec 2023 15:00:04 +0900 Subject: [PATCH 4/5] Revert "Fix tests and align API" This reverts commit ff08922055373756efc0b110ad6b4d99aa300caf. --- Libplanet.Action.Tests/ActionContextTest.cs | 58 +++++++++---------- .../ActionEvaluationTest.cs | 11 ++-- Libplanet.Action.Tests/Sys/InitializeTest.cs | 22 ++++--- Libplanet.Action/ActionContext.cs | 24 +++++--- Libplanet.Action/ActionEvaluator.cs | 22 ++++--- Libplanet.Action/TxContext.cs | 17 +++--- Libplanet.Tests/Action/AccountDiffTest.cs | 11 ++-- Libplanet.Tests/Action/AccountV0Test.cs | 11 ++-- Libplanet.Tests/Action/AccountV1Test.cs | 11 ++-- .../Renderers/AnonymousActionRendererTest.cs | 11 ++-- .../Renderers/LoggedActionRendererTest.cs | 11 ++-- 11 files changed, 99 insertions(+), 110 deletions(-) diff --git a/Libplanet.Action.Tests/ActionContextTest.cs b/Libplanet.Action.Tests/ActionContextTest.cs index 8f9c2b38902..4044de57780 100644 --- a/Libplanet.Action.Tests/ActionContextTest.cs +++ b/Libplanet.Action.Tests/ActionContextTest.cs @@ -31,12 +31,11 @@ public void RandomShouldBeDeterministic() foreach (var (seed, expected) in testCases) { var context = new ActionContext( - new TxContext( - signer: _address, - txId: _txid, - miner: _address, - blockIndex: 1, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: _address, + txid: _txid, + miner: _address, + blockIndex: 1, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: new Account(MockAccountState.Empty), randomSeed: seed, gasLimit: 0 @@ -50,35 +49,33 @@ public void RandomShouldBeDeterministic() public void GuidShouldBeDeterministic() { var context1 = new ActionContext( - new TxContext( - signer: _address, - txId: _txid, - miner: _address, - blockIndex: 1, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: _address, + txid: _txid, + miner: _address, + blockIndex: 1, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: new Account(MockAccountState.Empty), randomSeed: 0, - gasLimit: 0); + gasLimit: 0 + ); var context2 = new ActionContext( - new TxContext( - signer: _address, - txId: _txid, - miner: _address, - blockIndex: 1, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: _address, + txid: _txid, + miner: _address, + blockIndex: 1, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: new Account(MockAccountState.Empty), randomSeed: 0, gasLimit: 0 ); var context3 = new ActionContext( - new TxContext( - signer: _address, - txId: _txid, - miner: _address, - blockIndex: 1, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: _address, + txid: _txid, + miner: _address, + blockIndex: 1, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: new Account(MockAccountState.Empty), randomSeed: 1, gasLimit: 0 @@ -113,12 +110,11 @@ public void GuidVersionAndVariant() for (var i = 0; i < 100; i++) { var context = new ActionContext( - new TxContext( - signer: _address, - txId: _txid, - miner: _address, - blockIndex: 1, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: _address, + txid: _txid, + miner: _address, + blockIndex: 1, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: new Account(MockAccountState.Empty), randomSeed: i, gasLimit: 0 diff --git a/Libplanet.Action.Tests/ActionEvaluationTest.cs b/Libplanet.Action.Tests/ActionEvaluationTest.cs index 2b730881eb8..01696114acf 100644 --- a/Libplanet.Action.Tests/ActionEvaluationTest.cs +++ b/Libplanet.Action.Tests/ActionEvaluationTest.cs @@ -32,12 +32,11 @@ public void Constructor() var evaluation = new ActionEvaluation( new DumbAction(address, "item"), new ActionContext( - new TxContext( - address, - txid, - address, - 1, - Block.CurrentProtocolVersion), + address, + txid, + address, + 1, + Block.CurrentProtocolVersion, new Account(MockAccountState.Empty), 123, 0), diff --git a/Libplanet.Action.Tests/Sys/InitializeTest.cs b/Libplanet.Action.Tests/Sys/InitializeTest.cs index ca3501fc0e9..cd078a912c6 100644 --- a/Libplanet.Action.Tests/Sys/InitializeTest.cs +++ b/Libplanet.Action.Tests/Sys/InitializeTest.cs @@ -45,12 +45,11 @@ public void Execute() var prevState = new Account(MockAccountState.Empty); BlockHash genesisHash = random.NextBlockHash(); var context = new ActionContext( - new TxContext( - signer: signer, - txId: random.NextTxId(), - miner: random.NextAddress(), - blockIndex: 0, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: signer, + txid: random.NextTxId(), + miner: random.NextAddress(), + blockIndex: 0, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: prevState, randomSeed: 123, gasLimit: 0); @@ -73,12 +72,11 @@ public void ExecuteInNonGenesis() var prevState = new Account(MockAccountState.Empty); BlockHash genesisHash = random.NextBlockHash(); var context = new ActionContext( - new TxContext( - signer: signer, - txId: random.NextTxId(), - miner: random.NextAddress(), - blockIndex: 10, - blockProtocolVersion: Block.CurrentProtocolVersion), + signer: signer, + txid: random.NextTxId(), + miner: random.NextAddress(), + blockIndex: 10, + blockProtocolVersion: Block.CurrentProtocolVersion, previousState: prevState, randomSeed: 123, gasLimit: long.MaxValue); diff --git a/Libplanet.Action/ActionContext.cs b/Libplanet.Action/ActionContext.cs index 6b66ba6ac05..c0ee1f4e38d 100644 --- a/Libplanet.Action/ActionContext.cs +++ b/Libplanet.Action/ActionContext.cs @@ -10,17 +10,23 @@ internal class ActionContext : IActionContext { public static readonly AsyncLocal GetGasMeter = new AsyncLocal(); - private readonly ITxContext _txContext; - private readonly long _gasLimit; public ActionContext( - ITxContext txContext, + Address signer, + TxId? txid, + Address miner, + long blockIndex, + int blockProtocolVersion, IAccount previousState, int randomSeed, long gasLimit) { - _txContext = txContext; + Signer = signer; + TxId = txid; + Miner = miner; + BlockIndex = blockIndex; + BlockProtocolVersion = blockProtocolVersion; PreviousState = previousState; RandomSeed = randomSeed; _gasLimit = gasLimit; @@ -29,19 +35,19 @@ public ActionContext( } /// - public Address Signer => _txContext.Signer; + public Address Signer { get; } /// - public TxId? TxId => _txContext.TxId; + public TxId? TxId { get; } /// - public Address Miner => _txContext.Miner; + public Address Miner { get; } /// - public long BlockIndex => _txContext.BlockIndex; + public long BlockIndex { get; } /// - public int BlockProtocolVersion => _txContext.BlockProtocolVersion; + public int BlockProtocolVersion { get; } /// public IAccount PreviousState { get; } diff --git a/Libplanet.Action/ActionEvaluator.cs b/Libplanet.Action/ActionEvaluator.cs index 05fc1f4fe33..158d7380651 100644 --- a/Libplanet.Action/ActionEvaluator.cs +++ b/Libplanet.Action/ActionEvaluator.cs @@ -189,12 +189,11 @@ IActionContext CreateActionContext( long actionGasLimit) { return new ActionContext( - new TxContext( - signer: tx?.Signer ?? blockHeader.Miner, - txId: tx?.Id ?? null, - miner: blockHeader.Miner, - blockIndex: blockHeader.Index, - blockProtocolVersion: blockHeader.ProtocolVersion), + signer: tx?.Signer ?? blockHeader.Miner, + txid: tx?.Id ?? null, + miner: blockHeader.Miner, + blockIndex: blockHeader.Index, + blockProtocolVersion: blockHeader.ProtocolVersion, previousState: prevState, randomSeed: randomSeed, gasLimit: actionGasLimit); @@ -244,12 +243,11 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction( IActionContext CreateActionContext(IAccount newPrevState) { return new ActionContext( - new TxContext( - signer: inputContext.Signer, - txId: inputContext.TxId, - miner: inputContext.Miner, - blockIndex: inputContext.BlockIndex, - blockProtocolVersion: inputContext.BlockProtocolVersion), + signer: inputContext.Signer, + txid: inputContext.TxId, + miner: inputContext.Miner, + blockIndex: inputContext.BlockIndex, + blockProtocolVersion: inputContext.BlockProtocolVersion, previousState: newPrevState, randomSeed: inputContext.RandomSeed, gasLimit: inputContext.GasLimit()); diff --git a/Libplanet.Action/TxContext.cs b/Libplanet.Action/TxContext.cs index da595ce2faa..6260145c854 100644 --- a/Libplanet.Action/TxContext.cs +++ b/Libplanet.Action/TxContext.cs @@ -7,17 +7,14 @@ namespace Libplanet.Action internal class TxContext : ITxContext { public TxContext( - Address signer, - TxId? txId, - Address miner, - long blockIndex, - int blockProtocolVersion) + IPreEvaluationBlockHeader blockHeader, + ITransaction? transaction) { - Signer = signer; - TxId = txId; - Miner = miner; - BlockIndex = blockIndex; - BlockProtocolVersion = blockProtocolVersion; + Signer = transaction?.Signer ?? blockHeader.Miner; + TxId = transaction?.Id; + Miner = blockHeader.Miner; + BlockIndex = blockHeader.Index; + BlockProtocolVersion = blockHeader.ProtocolVersion; } /// diff --git a/Libplanet.Tests/Action/AccountDiffTest.cs b/Libplanet.Tests/Action/AccountDiffTest.cs index a6c0c804c86..cf0224f1f69 100644 --- a/Libplanet.Tests/Action/AccountDiffTest.cs +++ b/Libplanet.Tests/Action/AccountDiffTest.cs @@ -143,12 +143,11 @@ public void Diff() public IActionContext CreateActionContext(Address signer, ITrie trie) => new ActionContext( - new TxContext( - signer, - null, - signer, - 0, - Block.CurrentProtocolVersion), + signer, + null, + signer, + 0, + Block.CurrentProtocolVersion, new Account(new AccountState(trie)), 0, 0); diff --git a/Libplanet.Tests/Action/AccountV0Test.cs b/Libplanet.Tests/Action/AccountV0Test.cs index 67f7ffc4abd..b17714c7fce 100644 --- a/Libplanet.Tests/Action/AccountV0Test.cs +++ b/Libplanet.Tests/Action/AccountV0Test.cs @@ -22,12 +22,11 @@ public AccountV0Test(ITestOutputHelper output) public override IActionContext CreateContext( IAccount delta, Address signer) => new ActionContext( - new TxContext( - signer, - null, - signer, - 0, - ProtocolVersion), + signer, + null, + signer, + 0, + ProtocolVersion, delta, 0, 0); diff --git a/Libplanet.Tests/Action/AccountV1Test.cs b/Libplanet.Tests/Action/AccountV1Test.cs index 2b6fe416a32..c646dcb7f51 100644 --- a/Libplanet.Tests/Action/AccountV1Test.cs +++ b/Libplanet.Tests/Action/AccountV1Test.cs @@ -25,12 +25,11 @@ public AccountV1Test(ITestOutputHelper output) public override IActionContext CreateContext( IAccount delta, Address signer) => new ActionContext( - new TxContext( - signer, - null, - signer, - 0, - ProtocolVersion), + signer, + null, + signer, + 0, + ProtocolVersion, delta, 0, 0); diff --git a/Libplanet.Tests/Blockchain/Renderers/AnonymousActionRendererTest.cs b/Libplanet.Tests/Blockchain/Renderers/AnonymousActionRendererTest.cs index bbdab44403c..1966eb0cf09 100644 --- a/Libplanet.Tests/Blockchain/Renderers/AnonymousActionRendererTest.cs +++ b/Libplanet.Tests/Blockchain/Renderers/AnonymousActionRendererTest.cs @@ -20,12 +20,11 @@ public class AnonymousActionRendererTest private static ICommittedActionContext _actionContext = new CommittedActionContext(new ActionContext( - new TxContext( - default, - default, - default, - default, - Block.CurrentProtocolVersion), + default, + default, + default, + Block.CurrentProtocolVersion, + default, _account, default, 0)); diff --git a/Libplanet.Tests/Blockchain/Renderers/LoggedActionRendererTest.cs b/Libplanet.Tests/Blockchain/Renderers/LoggedActionRendererTest.cs index 35dc57b9610..1dfe9db95cf 100644 --- a/Libplanet.Tests/Blockchain/Renderers/LoggedActionRendererTest.cs +++ b/Libplanet.Tests/Blockchain/Renderers/LoggedActionRendererTest.cs @@ -66,12 +66,11 @@ public void ActionRenderings(bool error, bool exception) LogEvent firstLog = null; ICommittedActionContext actionContext = new CommittedActionContext(new ActionContext( - new TxContext( - default, - default, - default, - 123, - Block.CurrentProtocolVersion), + default, + default, + default, + Block.CurrentProtocolVersion, + 123, _account, default, 0)); From 4b0b11cf5148ac89930386f63f1a7fdc40d6fdf8 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Mon, 4 Dec 2023 15:00:12 +0900 Subject: [PATCH 5/5] Revert "Introduce ITxContext" This reverts commit c871f59f57582d9c91d4f04cd2a036b51420a91f. --- Libplanet.Action/ActionContext.cs | 1 + Libplanet.Action/IActionContext.cs | 44 ++++++++++++++- Libplanet.Action/ITxContext.cs | 53 ------------------- Libplanet.Action/State/Account.cs | 6 +-- Libplanet.Action/State/IAccount.cs | 6 +-- Libplanet.Action/TxContext.cs | 35 ------------ .../Queries/StateQueryTest.cs | 14 ++--- 7 files changed, 57 insertions(+), 102 deletions(-) delete mode 100644 Libplanet.Action/ITxContext.cs delete mode 100644 Libplanet.Action/TxContext.cs diff --git a/Libplanet.Action/ActionContext.cs b/Libplanet.Action/ActionContext.cs index c0ee1f4e38d..8b702f9fb50 100644 --- a/Libplanet.Action/ActionContext.cs +++ b/Libplanet.Action/ActionContext.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.Contracts; using System.Threading; using Libplanet.Action.State; using Libplanet.Crypto; diff --git a/Libplanet.Action/IActionContext.cs b/Libplanet.Action/IActionContext.cs index 884cd4a47ea..0bd1b938830 100644 --- a/Libplanet.Action/IActionContext.cs +++ b/Libplanet.Action/IActionContext.cs @@ -1,5 +1,8 @@ using System.Diagnostics.Contracts; using Libplanet.Action.State; +using Libplanet.Crypto; +using Libplanet.Types.Blocks; +using Libplanet.Types.Tx; namespace Libplanet.Action { @@ -7,8 +10,47 @@ namespace Libplanet.Action /// Contextual data determined by a transaction and a block. /// Passed to method. /// - public interface IActionContext : ITxContext + public interface IActionContext { + /// + /// The of the that contains + /// the to be executed. If the is + /// not part of a , e.g. , + /// this is set to instead. + /// + [Pure] + Address Signer { get; } + + /// + /// The of the that contains + /// the . If the is not part of + /// a , e.g. , + /// this is set to . + /// + [Pure] + TxId? TxId { get; } + + /// + /// The of the that contains + /// the . + /// + [Pure] + Address Miner { get; } + + /// + /// The of the that contains + /// the . + /// + [Pure] + long BlockIndex { get; } + + /// + /// The of the that contains + /// the . + /// + [Pure] + int BlockProtocolVersion { get; } + /// /// A null delta of states, which means it represents the states /// before executes. diff --git a/Libplanet.Action/ITxContext.cs b/Libplanet.Action/ITxContext.cs deleted file mode 100644 index b5c37aee49a..00000000000 --- a/Libplanet.Action/ITxContext.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Diagnostics.Contracts; -using Libplanet.Crypto; -using Libplanet.Types.Blocks; -using Libplanet.Types.Tx; - -namespace Libplanet.Action -{ - /// - /// Contextual data determined by a transaction and a block. - /// Passed to method. - /// - public interface ITxContext - { - /// - /// The of the that contains - /// the to be executed. If the is - /// not part of a , e.g. , - /// this is set to instead. - /// - [Pure] - Address Signer { get; } - - /// - /// The of the that contains - /// the . If the is not part of - /// a , e.g. , - /// this is set to . - /// - [Pure] - TxId? TxId { get; } - - /// - /// The of the that contains - /// the . - /// - [Pure] - Address Miner { get; } - - /// - /// The of the that contains - /// the . - /// - [Pure] - long BlockIndex { get; } - - /// - /// The of the that contains - /// the . - /// - [Pure] - int BlockProtocolVersion { get; } - } -} diff --git a/Libplanet.Action/State/Account.cs b/Libplanet.Action/State/Account.cs index f011905e4d4..b0c33ac0ff3 100644 --- a/Libplanet.Action/State/Account.cs +++ b/Libplanet.Action/State/Account.cs @@ -112,7 +112,7 @@ public ValidatorSet GetValidatorSet() => /// [Pure] public IAccount MintAsset( - ITxContext context, Address recipient, FungibleAssetValue value) + IActionContext context, Address recipient, FungibleAssetValue value) { if (value.Sign <= 0) { @@ -161,7 +161,7 @@ public IAccount MintAsset( /// [Pure] public IAccount TransferAsset( - ITxContext context, + IActionContext context, Address sender, Address recipient, FungibleAssetValue value, @@ -172,7 +172,7 @@ public IAccount TransferAsset( /// [Pure] public IAccount BurnAsset( - ITxContext context, Address owner, FungibleAssetValue value) + IActionContext context, Address owner, FungibleAssetValue value) { string msg; diff --git a/Libplanet.Action/State/IAccount.cs b/Libplanet.Action/State/IAccount.cs index 15936408444..7ffa24284fa 100644 --- a/Libplanet.Action/State/IAccount.cs +++ b/Libplanet.Action/State/IAccount.cs @@ -95,7 +95,7 @@ public interface IAccount : IAccountState /// . [Pure] IAccount MintAsset( - ITxContext context, Address recipient, FungibleAssetValue value); + IActionContext context, Address recipient, FungibleAssetValue value); /// /// Transfers the fungible asset (i.e., in-game monetary) @@ -126,7 +126,7 @@ IAccount MintAsset( /// [Pure] IAccount TransferAsset( - ITxContext context, + IActionContext context, Address sender, Address recipient, FungibleAssetValue value, @@ -152,7 +152,7 @@ IAccount TransferAsset( /// has insufficient balance than to burn. [Pure] IAccount BurnAsset( - ITxContext context, Address owner, FungibleAssetValue value); + IActionContext context, Address owner, FungibleAssetValue value); /// /// Sets to the stored . diff --git a/Libplanet.Action/TxContext.cs b/Libplanet.Action/TxContext.cs deleted file mode 100644 index 6260145c854..00000000000 --- a/Libplanet.Action/TxContext.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Libplanet.Crypto; -using Libplanet.Types.Blocks; -using Libplanet.Types.Tx; - -namespace Libplanet.Action -{ - internal class TxContext : ITxContext - { - public TxContext( - IPreEvaluationBlockHeader blockHeader, - ITransaction? transaction) - { - Signer = transaction?.Signer ?? blockHeader.Miner; - TxId = transaction?.Id; - Miner = blockHeader.Miner; - BlockIndex = blockHeader.Index; - BlockProtocolVersion = blockHeader.ProtocolVersion; - } - - /// - public Address Signer { get; } - - /// - public TxId? TxId { get; } - - /// - public Address Miner { get; } - - /// - public long BlockIndex { get; } - - /// - public int BlockProtocolVersion { get; } - } -} diff --git a/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs b/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs index 6f09cdf4735..2a8d24bd448 100644 --- a/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs +++ b/Libplanet.Explorer.Tests/Queries/StateQueryTest.cs @@ -174,7 +174,7 @@ public async Task Validators() Assert.Equal("032038e153d344773986c039ba5dbff12ae70cfdf6ea8beb7c5ea9b361a72a9233", validatorDict["publicKey"]); Assert.Equal(new BigInteger(1), validatorDict["power"]); } - + [Fact] public async Task ThrowExecutionErrorIfViolateMutualExclusive() { @@ -194,7 +194,7 @@ public async Task ThrowExecutionErrorIfViolateMutualExclusive() ", source: source); Assert.IsType(result.Errors); } - + [Fact] public async Task StatesBySrh() { @@ -345,7 +345,7 @@ public async Task ValidatorsBySrh() Assert.Equal("032038e153d344773986c039ba5dbff12ae70cfdf6ea8beb7c5ea9b361a72a9233", validatorDict["publicKey"]); Assert.Equal(new BigInteger(1), validatorDict["power"]); } - + private class MockChainStates : IBlockChainStates { @@ -366,7 +366,7 @@ public FungibleAssetValue GetTotalSupply(Currency currency, BlockHash? offset) = public ValidatorSet GetValidatorSet(BlockHash? offset) => GetAccountState(offset).GetValidatorSet(); - public IAccountState GetAccountState(BlockHash? offset) => + public IAccountState GetAccountState(BlockHash? offset) => new MockAccount(offset, null); public IAccountState GetAccountState(HashDigest? hash) => @@ -400,18 +400,18 @@ public IAccount SetState(Address address, IValue state) throw new System.NotImplementedException(); } - public IAccount MintAsset(ITxContext context, Address recipient, FungibleAssetValue value) + public IAccount MintAsset(IActionContext context, Address recipient, FungibleAssetValue value) { throw new System.NotImplementedException(); } - public IAccount TransferAsset(ITxContext context, Address sender, Address recipient, + public IAccount TransferAsset(IActionContext context, Address sender, Address recipient, FungibleAssetValue value, bool allowNegativeBalance = false) { throw new System.NotImplementedException(); } - public IAccount BurnAsset(ITxContext context, Address owner, FungibleAssetValue value) + public IAccount BurnAsset(IActionContext context, Address owner, FungibleAssetValue value) { throw new System.NotImplementedException(); }