Skip to content

Commit

Permalink
Merge pull request #3588 from AElfProject/feature/plugin-modules
Browse files Browse the repository at this point in the history
Enable plugin modules
  • Loading branch information
chopin-fan authored Jul 18, 2024
2 parents 6be7f5e + 5069438 commit 542043d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public virtual IEnumerable<GenesisSmartContractDto> GetGenesisSmartContractDtos(
{
var contractCodes = GetContractCodes();
var deploymentList = ContractDeploymentListProvider.GetDeployContractNameList();
return ContractInitializationProviders
.Where(p => deploymentList.Contains(p.SystemSmartContractName))
.OrderBy(p => deploymentList.IndexOf(p.SystemSmartContractName))
.Select(p =>
return ContractInitializationProviders
.GroupBy(p => p.SystemSmartContractName)
.Where(g => deploymentList.Contains(g.Key))
.OrderBy(g => deploymentList.IndexOf(g.Key))
.Select(g =>
{
var p = g.Last();
var code = contractCodes[p.ContractCodeName];
var methodList = p.GetInitializeMethodList(code);
var genesisSmartContractDto = new GenesisSmartContractDto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using AElf.Blockchains.BasicBaseChain;
using AElf.ContractDeployer;
using AElf.Kernel.Plugin.Application;
using AElf.Kernel.SmartContract;
using AElf.Kernel.SmartContract.Application;
using Microsoft.Extensions.Options;
Expand All @@ -12,19 +14,23 @@ namespace AElf.Blockchains.MainChain;
/// </summary>
public class MainChainGenesisSmartContractDtoProvider : GenesisSmartContractDtoProviderBase
{
private readonly IEnumerable<IPluginContractProvider> _pluginContractProviders;
private readonly ContractOptions _contractOptions;

public MainChainGenesisSmartContractDtoProvider(IContractDeploymentListProvider contractDeploymentListProvider,
IEnumerable<IContractInitializationProvider> contractInitializationProviders,
IEnumerable<IPluginContractProvider> pluginContractProviders,
IOptionsSnapshot<ContractOptions> contractOptions)
: base(contractDeploymentListProvider, contractInitializationProviders)
{
_pluginContractProviders = pluginContractProviders;
_contractOptions = contractOptions.Value;
}

protected override IReadOnlyDictionary<string, byte[]> GetContractCodes()
{
return ContractsDeployer.GetContractCodes<MainChainGenesisSmartContractDtoProvider>(_contractOptions
.GenesisContractDir);
return ContractsDeployer.GetContractCodes<MainChainGenesisSmartContractDtoProvider>(
_contractOptions.GenesisContractDir,
pluginContractNames: _pluginContractProviders.Select(p => p.GetContractName()).ToList());
}
}
7 changes: 6 additions & 1 deletion src/AElf.ContractDeployer/ContractsDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ namespace AElf.ContractDeployer;
public static class ContractsDeployer
{
public static IReadOnlyDictionary<string, byte[]> GetContractCodes<T>(string contractDir = null,
bool isPatched = false)
bool isPatched = false, List<string> pluginContractNames = null)
{
var contractNames = GetContractNames(typeof(T).Assembly).ToList();
if (pluginContractNames is { Count: > 0 })
{
contractNames.AddRange(pluginContractNames);
}

if (contractNames.Count == 0) throw new NoContractDllFoundInManifestException();

return contractNames.Select(n => (n, GetCode(n, contractDir, isPatched)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AElf.Kernel.Plugin.Application;

public interface IPluginContractProvider
{
string GetContractName();
}
25 changes: 16 additions & 9 deletions src/AElf.Launcher/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using AElf.Blockchains.MainChain;
using AElf.Blockchains.SideChain;
Expand All @@ -9,7 +10,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp;
using Volo.Abp.Modularity.PlugIns;

namespace AElf.Launcher;

Expand All @@ -28,13 +30,23 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
var chainType = _configuration.GetValue("ChainType", ChainType.MainChain);
var pluginSourcesFolder = _configuration.GetValue("PluginSourcesFolder", Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"aelf", "plugins"));
Action<AbpApplicationCreationOptions> optionsAction = options =>
{
if (Directory.Exists(pluginSourcesFolder))
{
options.PlugInSources.AddFolder(pluginSourcesFolder);
}
};
switch (chainType)
{
case ChainType.SideChain:
AddApplication<SideChainAElfModule>(services);
services.AddApplication<SideChainAElfModule>(optionsAction);
break;
default:
AddApplication<MainChainAElfModule>(services);
case ChainType.MainChain:
services.AddApplication<MainChainAElfModule>(optionsAction);
break;
}

Expand All @@ -56,11 +68,6 @@ public void ConfigureServices(IServiceCollection services)
});
}

private static void AddApplication<T>(IServiceCollection services) where T : IAbpModule
{
services.AddApplication<T>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
// ReSharper disable once UnusedMember.Global
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down

0 comments on commit 542043d

Please sign in to comment.