From 25a5cf7fb0aa5210efdb5de2111effc56f233216 Mon Sep 17 00:00:00 2001 From: s2quake Date: Fri, 12 Jul 2024 14:25:10 +0900 Subject: [PATCH] feat: Add IActionLoaderProvider --- .../BlockChainUtility.cs | 11 +++++----- .../LibplanetConsole.Nodes/ApplicationBase.cs | 2 +- .../IActionLoaderProvider.cs | 8 +++++++ src/node/LibplanetConsole.Nodes/Node.cs | 21 ++++++++++++++++--- 4 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 src/node/LibplanetConsole.Nodes/IActionLoaderProvider.cs diff --git a/src/common/LibplanetConsole.Common/BlockChainUtility.cs b/src/common/LibplanetConsole.Common/BlockChainUtility.cs index a997cfad..f2076fee 100644 --- a/src/common/LibplanetConsole.Common/BlockChainUtility.cs +++ b/src/common/LibplanetConsole.Common/BlockChainUtility.cs @@ -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; @@ -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, diff --git a/src/node/LibplanetConsole.Nodes/ApplicationBase.cs b/src/node/LibplanetConsole.Nodes/ApplicationBase.cs index fd269daf..8e2f86a2 100644 --- a/src/node/LibplanetConsole.Nodes/ApplicationBase.cs +++ b/src/node/LibplanetConsole.Nodes/ApplicationBase.cs @@ -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(_logger); _container.ComposeExportedValue(this); diff --git a/src/node/LibplanetConsole.Nodes/IActionLoaderProvider.cs b/src/node/LibplanetConsole.Nodes/IActionLoaderProvider.cs new file mode 100644 index 00000000..1bd55d96 --- /dev/null +++ b/src/node/LibplanetConsole.Nodes/IActionLoaderProvider.cs @@ -0,0 +1,8 @@ +using Libplanet.Action.Loader; + +namespace LibplanetConsole.Nodes; + +public interface IActionLoaderProvider +{ + IActionLoader GetActionLoader(); +} diff --git a/src/node/LibplanetConsole.Nodes/Node.cs b/src/node/LibplanetConsole.Nodes/Node.cs index 79630d64..2f67088e 100644 --- a/src/node/LibplanetConsole.Nodes/Node.cs +++ b/src/node/LibplanetConsole.Nodes/Node.cs @@ -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; @@ -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; @@ -30,6 +33,7 @@ internal sealed class Node : IActionRenderer, INode, IApplicationService private readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current!; + private readonly IServiceProvider _serviceProvider; private readonly AppPrivateKey _seedNodePrivateKey = new(); private readonly ConcurrentDictionary _eventByTxId = []; private readonly ConcurrentDictionary _exceptionByAction = []; @@ -46,8 +50,9 @@ private readonly SynchronizationContext _synchronizationContext 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; @@ -137,7 +142,7 @@ public AppPeer ConsensusSeedPeer return BlockChain; } - return null; + return _serviceProvider.GetService(serviceType); } public bool Verify(object obj, byte[] signature) => PublicKey.Verify(obj, signature); @@ -243,10 +248,12 @@ var swarmTransport 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) { @@ -420,6 +427,14 @@ void IActionRenderer.RenderBlockEnd(Block oldTip, Block newTip) { } + private static IActionLoader[] CollectActionLoaders(IServiceProvider serviceProvider) + { + var actionLoaderProviders = serviceProvider.GetService>(); + var actionLoaderList = actionLoaderProviders.Select(item => item.GetActionLoader()).ToList(); + actionLoaderList.Add(new AssemblyActionLoader(typeof(AssemblyActionLoader).Assembly)); + return [.. actionLoaderList]; + } + private static async Task CreateTransport( PrivateKey privateKey, AppEndPoint endPoint) {