Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IActionLoaderProvider #54

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/common/LibplanetConsole.Common/BlockChainUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Numerics;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Action.Sys;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
Expand All @@ -27,15 +28,15 @@ public static readonly AppProtocolVersion AppProtocolVersion
= AppProtocolVersion.Sign(AppProtocolKey, 1);

public static BlockChain CreateBlockChain(
GenesisOptions genesisOptions, string storePath, IRenderer renderer)
GenesisOptions genesisOptions,
string storePath,
IRenderer renderer,
IActionLoader[] actionLoaders)
{
var genesisKey = (PrivateKey)genesisOptions.GenesisKey;
var isNew = storePath == string.Empty || Directory.Exists(storePath) != true;
var (store, stateStore) = GetStore(storePath);
var actionLoader = new AggregateTypedActionLoader
{
new AssemblyActionLoader(typeof(AssemblyActionLoader).Assembly),
};
var actionLoader = new AggregateTypedActionLoader(actionLoaders);
var actionEvaluator = new ActionEvaluator(
policyBlockActionGetter: _ => null,
stateStore,
Expand Down
2 changes: 1 addition & 1 deletion src/node/LibplanetConsole.Nodes/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected ApplicationBase(ApplicationOptions options)
_logger.Debug(Environment.CommandLine);
_logger.Debug("Application initializing...");
_isAutoStart = options.ManualStart != true;
_node = new Node(options, _logger);
_node = new Node(this, options, _logger);
_container = new(this);
_container.ComposeExportedValue<ILogger>(_logger);
_container.ComposeExportedValue<IApplication>(this);
Expand Down
8 changes: 8 additions & 0 deletions src/node/LibplanetConsole.Nodes/IActionLoaderProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Libplanet.Action.Loader;

namespace LibplanetConsole.Nodes;

public interface IActionLoaderProvider
{
IActionLoader GetActionLoader();
}
21 changes: 18 additions & 3 deletions src/node/LibplanetConsole.Nodes/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Renderers;
using Libplanet.Common;
Expand All @@ -15,7 +16,9 @@
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Actions;
using LibplanetConsole.Common.Exceptions;
using LibplanetConsole.Common.Extensions;
using LibplanetConsole.Frameworks;
using LibplanetConsole.Nodes.Serializations;
using LibplanetConsole.Seeds;
Expand All @@ -30,6 +33,7 @@
private readonly SynchronizationContext _synchronizationContext
= SynchronizationContext.Current!;

private readonly IServiceProvider _serviceProvider;
private readonly AppPrivateKey _seedNodePrivateKey = new();
private readonly ConcurrentDictionary<TxId, ManualResetEvent> _eventByTxId = [];
private readonly ConcurrentDictionary<IValue, Exception> _exceptionByAction = [];
Expand All @@ -46,8 +50,9 @@
private SeedNode? _consensusSeedNode;
private NodeOptions _nodeOptions;

public Node(ApplicationOptions options, ILogger logger)
public Node(IServiceProvider serviceProvider, ApplicationOptions options, ILogger logger)
{
_serviceProvider = serviceProvider;
_seedEndPoint = options.NodeEndPoint;
_privateKey = options.PrivateKey.ToSecureString();
_storePath = options.StorePath;
Expand Down Expand Up @@ -137,7 +142,7 @@
return BlockChain;
}

return null;
return _serviceProvider.GetService(serviceType);
}

public bool Verify(object obj, byte[] signature) => PublicKey.Verify(obj, signature);
Expand Down Expand Up @@ -243,10 +248,12 @@
TargetBlockInterval = TimeSpan.FromSeconds(2),
ContextTimeoutOptions = new(),
};
var actionLoaders = CollectActionLoaders(_serviceProvider);
var blockChain = BlockChainUtility.CreateBlockChain(
genesisOptions: nodeOptions.GenesisOptions,
storePath: storePath,
renderer: this);
renderer: this,
actionLoaders: actionLoaders);

if (nodeOptions.BlocksyncSeedPeer is null)
{
Expand Down Expand Up @@ -420,6 +427,14 @@
{
}

private static IActionLoader[] CollectActionLoaders(IServiceProvider serviceProvider)
{
var actionLoaderProviders = serviceProvider.GetService<IEnumerable<IActionLoaderProvider>>();

Check warning on line 432 in src/node/LibplanetConsole.Nodes/Node.cs

View workflow job for this annotation

GitHub Actions / build

Line must be no longer than 100 characters (now 101).

Check warning on line 432 in src/node/LibplanetConsole.Nodes/Node.cs

View workflow job for this annotation

GitHub Actions / build

Line must be no longer than 100 characters (now 101).
var actionLoaderList = actionLoaderProviders.Select(item => item.GetActionLoader()).ToList();

Check warning on line 433 in src/node/LibplanetConsole.Nodes/Node.cs

View workflow job for this annotation

GitHub Actions / build

Line must be no longer than 100 characters (now 101).

Check warning on line 433 in src/node/LibplanetConsole.Nodes/Node.cs

View workflow job for this annotation

GitHub Actions / build

Line must be no longer than 100 characters (now 101).
actionLoaderList.Add(new AssemblyActionLoader(typeof(AssemblyActionLoader).Assembly));
return [.. actionLoaderList];
}

private static async Task<NetMQTransport> CreateTransport(
PrivateKey privateKey, AppEndPoint endPoint)
{
Expand Down