-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2341 from riemannulus/merge/73/to/main
🔀 merge release/73 into development
- Loading branch information
Showing
15 changed files
with
286 additions
and
37 deletions.
There are no files selected for viewing
Submodule Lib9c
updated
20 files
15 changes: 15 additions & 0 deletions
15
...anet.Extensions.PluggedActionEvaluator/Libplanet.Extensions.PluggedActionEvaluator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Lib9c\.Libplanet\Libplanet\Libplanet.csproj" /> | ||
<ProjectReference Include="..\Lib9c\.Lib9c.Plugin.Shared\Lib9c.Plugin.Shared.csproj" /> | ||
<ProjectReference Include="..\Lib9c\.Libplanet.Extensions.ActionEvaluatorCommonComponents\Libplanet.Extensions.ActionEvaluatorCommonComponents.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
51 changes: 51 additions & 0 deletions
51
Libplanet.Extensions.PluggedActionEvaluator/PluggedActionEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Reflection; | ||
using System.Security.Cryptography; | ||
using Lib9c.Plugin.Shared; | ||
using Libplanet.Action; | ||
using Libplanet.Action.Loader; | ||
using Libplanet.Common; | ||
using Libplanet.Extensions.ActionEvaluatorCommonComponents; | ||
using Libplanet.Store.Trie; | ||
using Libplanet.Types.Blocks; | ||
|
||
namespace Libplanet.Extensions.PluggedActionEvaluator | ||
{ | ||
public class PluggedActionEvaluator : IActionEvaluator | ||
{ | ||
private readonly IPluginActionEvaluator _pluginActionEvaluator; | ||
|
||
public IActionLoader ActionLoader => throw new NotImplementedException(); | ||
|
||
public PluggedActionEvaluator(string pluginPath, string typeName, IKeyValueStore keyValueStore) | ||
{ | ||
_pluginActionEvaluator = CreateActionEvaluator(pluginPath, typeName, keyValueStore); | ||
} | ||
|
||
public static Assembly LoadPlugin(string absolutePath) | ||
{ | ||
PluginLoadContext loadContext = new PluginLoadContext(absolutePath); | ||
return loadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(absolutePath))); | ||
} | ||
|
||
public static IPluginActionEvaluator CreateActionEvaluator(Assembly assembly, string typeName, IPluginKeyValueStore keyValueStore) | ||
{ | ||
if (assembly.GetType(typeName) is Type type && | ||
Activator.CreateInstance(type, args: keyValueStore) as IPluginActionEvaluator | ||
is IPluginActionEvaluator pluginActionEvaluator) | ||
{ | ||
return pluginActionEvaluator; | ||
} | ||
|
||
throw new NullReferenceException("PluginActionEvaluator not found with given parameters"); | ||
} | ||
|
||
public static IPluginActionEvaluator CreateActionEvaluator(string pluginPath, string typeName, IKeyValueStore keyValueStore) | ||
=> CreateActionEvaluator(LoadPlugin(pluginPath), typeName, new PluginKeyValueStore(keyValueStore)); | ||
|
||
public IReadOnlyList<ICommittedActionEvaluation> Evaluate(IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash) | ||
=> _pluginActionEvaluator.Evaluate( | ||
PreEvaluationBlockMarshaller.Serialize(block), | ||
baseStateRootHash is { } srh ? srh.ToByteArray() : null) | ||
.Select(eval => ActionEvaluationMarshaller.Deserialize(eval)).ToList().AsReadOnly(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
Libplanet.Extensions.PluggedActionEvaluator/PluginKeyValueStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Immutable; | ||
using Lib9c.Plugin.Shared; | ||
using Libplanet.Store.Trie; | ||
|
||
namespace Libplanet.Extensions.PluggedActionEvaluator | ||
{ | ||
public class PluginKeyValueStore : IPluginKeyValueStore | ||
{ | ||
private readonly IKeyValueStore _keyValueStore; | ||
|
||
public PluginKeyValueStore(IKeyValueStore keyValueStore) | ||
{ | ||
_keyValueStore = keyValueStore; | ||
} | ||
public byte[] Get(in ImmutableArray<byte> key) => | ||
_keyValueStore.Get(new KeyBytes(key)); | ||
|
||
public void Set(in ImmutableArray<byte> key, byte[] value) => | ||
_keyValueStore.Set(new KeyBytes(key), value); | ||
|
||
public void Set(IDictionary<ImmutableArray<byte>, byte[]> values) => | ||
_keyValueStore.Set( | ||
values.ToDictionary(kv => | ||
new KeyBytes(kv.Key), kv => kv.Value)); | ||
|
||
public void Delete(in ImmutableArray<byte> key) => | ||
_keyValueStore.Delete(new KeyBytes(key)); | ||
|
||
public void Delete(IEnumerable<ImmutableArray<byte>> keys) => | ||
_keyValueStore.Delete( | ||
keys.Select(key => new KeyBytes(key))); | ||
|
||
public bool Exists(in ImmutableArray<byte> key) => | ||
_keyValueStore.Exists(new KeyBytes(key)); | ||
|
||
public IEnumerable<ImmutableArray<byte>> ListKeys() => | ||
_keyValueStore.ListKeys().Select(key => key.ByteArray); | ||
|
||
public void Dispose() => | ||
_keyValueStore.Dispose(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Libplanet.Extensions.PluggedActionEvaluator/PluginLoadContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace Libplanet.Extensions.PluggedActionEvaluator | ||
{ | ||
public class PluginLoadContext : AssemblyLoadContext | ||
{ | ||
private readonly AssemblyDependencyResolver _resolver; | ||
|
||
public PluginLoadContext(string pluginPath) | ||
{ | ||
_resolver = new AssemblyDependencyResolver(pluginPath); | ||
} | ||
|
||
protected override Assembly? Load(AssemblyName assemblyName) | ||
{ | ||
if (_resolver.ResolveAssemblyToPath(assemblyName) is { } assemblyPath) | ||
{ | ||
return LoadFromAssemblyPath(assemblyPath); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
{ | ||
if (_resolver.ResolveUnmanagedDllToPath(unmanagedDllName) is { } libraryPath) | ||
{ | ||
return LoadUnmanagedDllFromPath(libraryPath); | ||
} | ||
|
||
return IntPtr.Zero; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Libplanet.Headless/Hosting/PluggedActionEvaluatorConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Libplanet.Headless.Hosting; | ||
|
||
public class PluggedActionEvaluatorConfiguration : IActionEvaluatorConfiguration | ||
{ | ||
public ActionEvaluatorType Type => ActionEvaluatorType.PluggedActionEvaluator; | ||
|
||
public string PluginPath { get; init; } | ||
|
||
public string TypeName => "Lib9c.Plugin.PluginActionEvaluator"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.