From 3b344cdf66b7e819427c489e6af4da6c292e9e03 Mon Sep 17 00:00:00 2001 From: s2quake Date: Mon, 26 Aug 2024 13:42:12 +0900 Subject: [PATCH] feat: Add IPolicyActionsRegistry interface --- src/Libplanet.Action/ActionEvaluator.cs | 4 +-- .../IPolicyActionsRegistry.cs | 27 +++++++++++++++++++ src/Libplanet.Action/PolicyActionsRegistry.cs | 2 +- .../Transports/NetMQTransport.cs | 12 +++------ .../Blockchain/Policies/BlockPolicy.cs | 6 ++--- .../Blockchain/Policies/IBlockPolicy.cs | 2 +- .../Blockchain/Policies/NullBlockPolicy.cs | 2 +- .../RocksDBStoreBlockChainTest.cs | 2 +- .../RocksDBStoreFixture.cs | 2 +- .../Blockchain/BlockChainTest.cs | 2 +- .../Blockchain/DefaultStoreBlockChainTest.cs | 2 +- .../Store/DefaultStoreFixture.cs | 2 +- .../Store/MemoryStoreFixture.cs | 2 +- test/Libplanet.Tests/Store/StoreFixture.cs | 2 +- .../Libplanet.Explorer.Executable/Program.cs | 2 +- 15 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 src/Libplanet.Action/IPolicyActionsRegistry.cs diff --git a/src/Libplanet.Action/ActionEvaluator.cs b/src/Libplanet.Action/ActionEvaluator.cs index 8d6eda0bbb7..a27967b4ac4 100644 --- a/src/Libplanet.Action/ActionEvaluator.cs +++ b/src/Libplanet.Action/ActionEvaluator.cs @@ -24,7 +24,7 @@ namespace Libplanet.Action public class ActionEvaluator : IActionEvaluator { private readonly ILogger _logger; - private readonly PolicyActionsRegistry _policyActionsRegistry; + private readonly IPolicyActionsRegistry _policyActionsRegistry; private readonly IStateStore _stateStore; private readonly IActionLoader _actionLoader; @@ -40,7 +40,7 @@ public class ActionEvaluator : IActionEvaluator /// A implementation using /// action type lookup. public ActionEvaluator( - PolicyActionsRegistry policyActionsRegistry, + IPolicyActionsRegistry policyActionsRegistry, IStateStore stateStore, IActionLoader actionTypeLoader) { diff --git a/src/Libplanet.Action/IPolicyActionsRegistry.cs b/src/Libplanet.Action/IPolicyActionsRegistry.cs new file mode 100644 index 00000000000..5d551c0cdfe --- /dev/null +++ b/src/Libplanet.Action/IPolicyActionsRegistry.cs @@ -0,0 +1,27 @@ +using System.Collections.Immutable; + +namespace Libplanet.Action +{ + public interface IPolicyActionsRegistry + { + /// + /// An array of to execute and be rendered at the beginning + /// for every block, if any. + ImmutableArray BeginBlockActions { get; } + + /// + /// An array of to execute and be rendered at the end + /// for every block, if any. + ImmutableArray EndBlockActions { get; } + + /// + /// An array of to execute and be rendered at the beginning + /// for every transaction, if any. + ImmutableArray BeginTxActions { get; } + + /// + /// An array of to execute and be rendered at the end + /// for every transaction, if any. + ImmutableArray EndTxActions { get; } + } +} diff --git a/src/Libplanet.Action/PolicyActionsRegistry.cs b/src/Libplanet.Action/PolicyActionsRegistry.cs index e9793899bd6..fb25d957127 100644 --- a/src/Libplanet.Action/PolicyActionsRegistry.cs +++ b/src/Libplanet.Action/PolicyActionsRegistry.cs @@ -4,7 +4,7 @@ namespace Libplanet.Action { - public class PolicyActionsRegistry + public class PolicyActionsRegistry : IPolicyActionsRegistry { /// /// A class containing policy actions to evaluate at each situation. diff --git a/src/Libplanet.Net/Transports/NetMQTransport.cs b/src/Libplanet.Net/Transports/NetMQTransport.cs index cb70133db43..7b97ba83f8d 100644 --- a/src/Libplanet.Net/Transports/NetMQTransport.cs +++ b/src/Libplanet.Net/Transports/NetMQTransport.cs @@ -454,8 +454,7 @@ await _requests.Writer.WriteAsync( oce ), peer, - content, - reqId + content ); } catch (OperationCanceledException oce2) @@ -476,7 +475,7 @@ await _requests.Writer.WriteAsync( { a?.SetStatus(ActivityStatusCode.Error); a?.AddTag("Exception", nameof(ChannelClosedException)); - throw WrapCommunicationFailException(ce.InnerException ?? ce, peer, content, reqId); + throw WrapCommunicationFailException(ce.InnerException ?? ce, peer, content); } catch (Exception e) { @@ -964,14 +963,9 @@ await Task.Factory.StartNew( private CommunicationFailException WrapCommunicationFailException( Exception innerException, BoundPeer peer, - MessageContent message, - Guid reqId + MessageContent message ) { - const string errMsg = - "Failed to send and receive replies from {Peer} for request " + - "{Message} {RequestId}."; - _logger.Error(innerException, errMsg, peer, message, reqId); return new CommunicationFailException( $"Failed to send and receive replies from {peer} for request {message}.", message.Type, diff --git a/src/Libplanet/Blockchain/Policies/BlockPolicy.cs b/src/Libplanet/Blockchain/Policies/BlockPolicy.cs index 63366847532..63b1f24196a 100644 --- a/src/Libplanet/Blockchain/Policies/BlockPolicy.cs +++ b/src/Libplanet/Blockchain/Policies/BlockPolicy.cs @@ -22,7 +22,7 @@ public class BlockPolicy : IBlockPolicy private readonly Func _validateNextBlock; - private readonly PolicyActionsRegistry _policyActionsRegistry; + private readonly IPolicyActionsRegistry _policyActionsRegistry; private readonly Func _getMaxTransactionsBytes; private readonly Func _getMinTransactionsPerBlock; private readonly Func _getMaxTransactionsPerBlock; @@ -73,7 +73,7 @@ public class BlockPolicy : IBlockPolicy /// Goes to . Set to a constant function /// of 10 by default. public BlockPolicy( - PolicyActionsRegistry? policyActionsRegistry = null, + IPolicyActionsRegistry? policyActionsRegistry = null, TimeSpan? blockInterval = null, Func? validateNextBlockTx = null, @@ -169,7 +169,7 @@ public BlockPolicy( } } - public PolicyActionsRegistry PolicyActionsRegistry => _policyActionsRegistry; + public IPolicyActionsRegistry PolicyActionsRegistry => _policyActionsRegistry; /// /// Targeted time interval between two consecutive s. diff --git a/src/Libplanet/Blockchain/Policies/IBlockPolicy.cs b/src/Libplanet/Blockchain/Policies/IBlockPolicy.cs index 7de700e2680..ba97914b349 100644 --- a/src/Libplanet/Blockchain/Policies/IBlockPolicy.cs +++ b/src/Libplanet/Blockchain/Policies/IBlockPolicy.cs @@ -24,7 +24,7 @@ public interface IBlockPolicy /// /// A set of policy s to evaluate at each situation. /// - PolicyActionsRegistry PolicyActionsRegistry { get; } + IPolicyActionsRegistry PolicyActionsRegistry { get; } /// /// Checks if a can be included in a yet to be mined diff --git a/src/Libplanet/Blockchain/Policies/NullBlockPolicy.cs b/src/Libplanet/Blockchain/Policies/NullBlockPolicy.cs index 8e679648b36..19a28fea5d4 100644 --- a/src/Libplanet/Blockchain/Policies/NullBlockPolicy.cs +++ b/src/Libplanet/Blockchain/Policies/NullBlockPolicy.cs @@ -21,7 +21,7 @@ public NullBlockPolicy( public ISet
BlockedMiners { get; } = new HashSet
(); - public PolicyActionsRegistry PolicyActionsRegistry => new PolicyActionsRegistry(); + public IPolicyActionsRegistry PolicyActionsRegistry => new PolicyActionsRegistry(); public ImmutableArray BeginBlockActions => ImmutableArray.Empty; diff --git a/test/Libplanet.RocksDBStore.Tests/RocksDBStoreBlockChainTest.cs b/test/Libplanet.RocksDBStore.Tests/RocksDBStoreBlockChainTest.cs index 56936ac6b81..531c19a09c9 100644 --- a/test/Libplanet.RocksDBStore.Tests/RocksDBStoreBlockChainTest.cs +++ b/test/Libplanet.RocksDBStore.Tests/RocksDBStoreBlockChainTest.cs @@ -15,7 +15,7 @@ public RocksDBStoreBlockChainTest(ITestOutputHelper output) } protected override StoreFixture GetStoreFixture( - PolicyActionsRegistry policyActionsRegistry = null) + IPolicyActionsRegistry policyActionsRegistry = null) { try { diff --git a/test/Libplanet.RocksDBStore.Tests/RocksDBStoreFixture.cs b/test/Libplanet.RocksDBStore.Tests/RocksDBStoreFixture.cs index 3cebd2c96a6..d3f8771086d 100644 --- a/test/Libplanet.RocksDBStore.Tests/RocksDBStoreFixture.cs +++ b/test/Libplanet.RocksDBStore.Tests/RocksDBStoreFixture.cs @@ -10,7 +10,7 @@ namespace Libplanet.RocksDBStore.Tests public class RocksDBStoreFixture : StoreFixture { public RocksDBStoreFixture( - PolicyActionsRegistry policyActionsRegistry = null) + IPolicyActionsRegistry policyActionsRegistry = null) : base(policyActionsRegistry) { Path = System.IO.Path.Combine( diff --git a/test/Libplanet.Tests/Blockchain/BlockChainTest.cs b/test/Libplanet.Tests/Blockchain/BlockChainTest.cs index acccd29c3be..904b9a8bf33 100644 --- a/test/Libplanet.Tests/Blockchain/BlockChainTest.cs +++ b/test/Libplanet.Tests/Blockchain/BlockChainTest.cs @@ -1942,7 +1942,7 @@ void BuildIndex(Guid id, Block block) /// The policy block actions to use. /// The store fixture that every test in this class depends on. protected virtual StoreFixture GetStoreFixture( - PolicyActionsRegistry policyActionsRegistry = null) + IPolicyActionsRegistry policyActionsRegistry = null) => new MemoryStoreFixture(policyActionsRegistry); private (Address[], Transaction[]) MakeFixturesForAppendTests( diff --git a/test/Libplanet.Tests/Blockchain/DefaultStoreBlockChainTest.cs b/test/Libplanet.Tests/Blockchain/DefaultStoreBlockChainTest.cs index 928fb755172..d078cbb6cdc 100644 --- a/test/Libplanet.Tests/Blockchain/DefaultStoreBlockChainTest.cs +++ b/test/Libplanet.Tests/Blockchain/DefaultStoreBlockChainTest.cs @@ -12,7 +12,7 @@ public DefaultStoreBlockChainTest(ITestOutputHelper output) } protected override StoreFixture GetStoreFixture( - PolicyActionsRegistry policyActionsRegistry = null) => + IPolicyActionsRegistry policyActionsRegistry = null) => new DefaultStoreFixture(policyActionsRegistry: policyActionsRegistry); } } diff --git a/test/Libplanet.Tests/Store/DefaultStoreFixture.cs b/test/Libplanet.Tests/Store/DefaultStoreFixture.cs index 882bd6b4fb6..04a95ae4e38 100644 --- a/test/Libplanet.Tests/Store/DefaultStoreFixture.cs +++ b/test/Libplanet.Tests/Store/DefaultStoreFixture.cs @@ -10,7 +10,7 @@ public class DefaultStoreFixture : StoreFixture, IDisposable { public DefaultStoreFixture( bool memory = true, - PolicyActionsRegistry policyActionsRegistry = null) + IPolicyActionsRegistry policyActionsRegistry = null) : base(policyActionsRegistry) { if (memory) diff --git a/test/Libplanet.Tests/Store/MemoryStoreFixture.cs b/test/Libplanet.Tests/Store/MemoryStoreFixture.cs index 59b5908d8c3..2481f127be8 100644 --- a/test/Libplanet.Tests/Store/MemoryStoreFixture.cs +++ b/test/Libplanet.Tests/Store/MemoryStoreFixture.cs @@ -7,7 +7,7 @@ namespace Libplanet.Tests.Store public class MemoryStoreFixture : StoreFixture { public MemoryStoreFixture( - PolicyActionsRegistry policyActionsRegistry = null) + IPolicyActionsRegistry policyActionsRegistry = null) : base(policyActionsRegistry) { Store = new MemoryStore(); diff --git a/test/Libplanet.Tests/Store/StoreFixture.cs b/test/Libplanet.Tests/Store/StoreFixture.cs index 0c480826495..6f5f9d21b5c 100644 --- a/test/Libplanet.Tests/Store/StoreFixture.cs +++ b/test/Libplanet.Tests/Store/StoreFixture.cs @@ -17,7 +17,7 @@ namespace Libplanet.Tests.Store { public abstract class StoreFixture : IDisposable { - protected StoreFixture(PolicyActionsRegistry policyActionsRegistry = null) + protected StoreFixture(IPolicyActionsRegistry policyActionsRegistry = null) { Path = null; diff --git a/tools/Libplanet.Explorer.Executable/Program.cs b/tools/Libplanet.Explorer.Executable/Program.cs index 2c852312405..d3c2126f30a 100644 --- a/tools/Libplanet.Explorer.Executable/Program.cs +++ b/tools/Libplanet.Explorer.Executable/Program.cs @@ -378,7 +378,7 @@ public DumbBlockPolicy(BlockPolicy blockPolicy) _impl = blockPolicy; } - public PolicyActionsRegistry PolicyActionsRegistry => _impl.PolicyActionsRegistry; + public IPolicyActionsRegistry PolicyActionsRegistry => _impl.PolicyActionsRegistry; public int GetMinTransactionsPerBlock(long index) => _impl.GetMinTransactionsPerBlock(index);