Skip to content

Commit

Permalink
feat: Add IPolicyActionsRegistry interface
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Aug 27, 2024
1 parent 499c8fa commit 784fe40
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,7 +40,7 @@ public class ActionEvaluator : IActionEvaluator
/// <param name="actionTypeLoader"> A <see cref="IActionLoader"/> implementation using
/// action type lookup.</param>
public ActionEvaluator(
PolicyActionsRegistry policyActionsRegistry,
IPolicyActionsRegistry policyActionsRegistry,
IStateStore stateStore,
IActionLoader actionTypeLoader)
{
Expand Down
27 changes: 27 additions & 0 deletions src/Libplanet.Action/IPolicyActionsRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Immutable;

namespace Libplanet.Action
{
public interface IPolicyActionsRegistry
{
/// <summary>
/// An array of <see cref="IAction"/> to execute and be rendered at the beginning
/// for every block, if any.</summary>
ImmutableArray<IAction> BeginBlockActions { get; }

/// <summary>
/// An array of <see cref="IAction"/> to execute and be rendered at the end
/// for every block, if any.</summary>
ImmutableArray<IAction> EndBlockActions { get; }

/// <summary>
/// An array of <see cref="IAction"/> to execute and be rendered at the beginning
/// for every transaction, if any.</summary>
ImmutableArray<IAction> BeginTxActions { get; }

/// <summary>
/// An array of <see cref="IAction"/> to execute and be rendered at the end
/// for every transaction, if any.</summary>
ImmutableArray<IAction> EndTxActions { get; }
}
}
2 changes: 1 addition & 1 deletion src/Libplanet.Action/PolicyActionsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Libplanet.Action
{
public class PolicyActionsRegistry
public class PolicyActionsRegistry : IPolicyActionsRegistry
{
/// <summary>
/// A class containing policy actions to evaluate at each situation.
Expand Down
12 changes: 3 additions & 9 deletions src/Libplanet.Net/Transports/NetMQTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ await _requests.Writer.WriteAsync(
oce
),
peer,
content,
reqId
content
);
}
catch (OperationCanceledException oce2)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/Libplanet/Blockchain/Policies/BlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BlockPolicy : IBlockPolicy
private readonly Func<BlockChain, Block, BlockPolicyViolationException?>
_validateNextBlock;

private readonly PolicyActionsRegistry _policyActionsRegistry;
private readonly IPolicyActionsRegistry _policyActionsRegistry;
private readonly Func<long, long> _getMaxTransactionsBytes;
private readonly Func<long, int> _getMinTransactionsPerBlock;
private readonly Func<long, int> _getMaxTransactionsPerBlock;
Expand Down Expand Up @@ -73,7 +73,7 @@ public class BlockPolicy : IBlockPolicy
/// Goes to <see cref="GetMaxEvidencePendingDuration"/>. Set to a constant function
/// of <c>10</c> by default.</param>
public BlockPolicy(
PolicyActionsRegistry? policyActionsRegistry = null,
IPolicyActionsRegistry? policyActionsRegistry = null,
TimeSpan? blockInterval = null,
Func<BlockChain, Transaction, TxPolicyViolationException?>?
validateNextBlockTx = null,
Expand Down Expand Up @@ -169,7 +169,7 @@ public BlockPolicy(
}
}

public PolicyActionsRegistry PolicyActionsRegistry => _policyActionsRegistry;
public IPolicyActionsRegistry PolicyActionsRegistry => _policyActionsRegistry;

/// <summary>
/// Targeted time interval between two consecutive <see cref="Block"/>s.
Expand Down
2 changes: 1 addition & 1 deletion src/Libplanet/Blockchain/Policies/IBlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IBlockPolicy
/// <summary>
/// A set of policy <see cref="IAction"/>s to evaluate at each situation.
/// </summary>
PolicyActionsRegistry PolicyActionsRegistry { get; }
IPolicyActionsRegistry PolicyActionsRegistry { get; }

/// <summary>
/// Checks if a <see cref="Transaction"/> can be included in a yet to be mined
Expand Down
2 changes: 1 addition & 1 deletion src/Libplanet/Blockchain/Policies/NullBlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public NullBlockPolicy(

public ISet<Address> BlockedMiners { get; } = new HashSet<Address>();

public PolicyActionsRegistry PolicyActionsRegistry => new PolicyActionsRegistry();
public IPolicyActionsRegistry PolicyActionsRegistry => new PolicyActionsRegistry();

public ImmutableArray<IAction> BeginBlockActions => ImmutableArray<IAction>.Empty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public RocksDBStoreBlockChainTest(ITestOutputHelper output)
}

protected override StoreFixture GetStoreFixture(
PolicyActionsRegistry policyActionsRegistry = null)
IPolicyActionsRegistry policyActionsRegistry = null)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.RocksDBStore.Tests/RocksDBStoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,7 @@ void BuildIndex(Guid id, Block block)
/// <param name="policyActionsRegistry">The policy block actions to use.</param>
/// <returns>The store fixture that every test in this class depends on.</returns>
protected virtual StoreFixture GetStoreFixture(
PolicyActionsRegistry policyActionsRegistry = null)
IPolicyActionsRegistry policyActionsRegistry = null)
=> new MemoryStoreFixture(policyActionsRegistry);

private (Address[], Transaction[]) MakeFixturesForAppendTests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public DefaultStoreBlockChainTest(ITestOutputHelper output)
}

protected override StoreFixture GetStoreFixture(
PolicyActionsRegistry policyActionsRegistry = null) =>
IPolicyActionsRegistry policyActionsRegistry = null) =>
new DefaultStoreFixture(policyActionsRegistry: policyActionsRegistry);
}
}
2 changes: 1 addition & 1 deletion test/Libplanet.Tests/Store/DefaultStoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DefaultStoreFixture : StoreFixture, IDisposable
{
public DefaultStoreFixture(
bool memory = true,
PolicyActionsRegistry policyActionsRegistry = null)
IPolicyActionsRegistry policyActionsRegistry = null)
: base(policyActionsRegistry)
{
if (memory)
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Tests/Store/MemoryStoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Tests/Store/StoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tools/Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 784fe40

Please sign in to comment.