Skip to content

Commit

Permalink
Merge pull request #2265 from planetarium/feature/plugin-aev
Browse files Browse the repository at this point in the history
✨  introduce: Pluggable ActionEvaluator
  • Loading branch information
riemannulus authored Dec 4, 2023
2 parents 275f319 + 31d5f0c commit bce0756
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .Lib9c.Plugin.Shared/IPluginActionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Lib9c.Plugin.Shared
{
public interface IPluginActionEvaluator
{
byte[][] Evaluate(byte[] blockBytes, byte[]? baseStateRootHashBytes);
}
}
23 changes: 23 additions & 0 deletions .Lib9c.Plugin.Shared/IPluginKeyValueStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Immutable;

namespace Lib9c.Plugin.Shared
{
public interface IPluginKeyValueStore
{
public byte[] Get(in ImmutableArray<byte> key);

public void Set(in ImmutableArray<byte> key, byte[] value);

public void Set(IDictionary<ImmutableArray<byte>, byte[]> values);

public void Delete(in ImmutableArray<byte> key);

public void Delete(IEnumerable<ImmutableArray<byte>> keys);

public bool Exists(in ImmutableArray<byte> key);

public IEnumerable<ImmutableArray<byte>> ListKeys();

public void Dispose();
}
}
9 changes: 9 additions & 0 deletions .Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
19 changes: 19 additions & 0 deletions .Lib9c.Plugin/Lib9c.Plugin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\.Libplanet.Extensions.ActionEvaluatorCommonComponents\Libplanet.Extensions.ActionEvaluatorCommonComponents.csproj" />
<ProjectReference Include="..\Lib9c\Lib9c.csproj" />
<ProjectReference Include="..\.Lib9c.Plugin.Shared\Lib9c.Plugin.Shared.csproj">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions .Lib9c.Plugin/PluginActionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Security.Cryptography;
using Lib9c.Plugin.Shared;
using Libplanet.Action;
using Libplanet.Common;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Libplanet.Store;
using Nekoyume.Action;
using Nekoyume.Action.Loader;


namespace Lib9c.Plugin
{
public class PluginActionEvaluator : IPluginActionEvaluator
{
private readonly IActionEvaluator _actionEvaluator;

public PluginActionEvaluator(IPluginKeyValueStore keyValueStore)
{
var stateStore = new TrieStateStore(new WrappedKeyValueStore(keyValueStore));
_actionEvaluator = new ActionEvaluator(
_ => new RewardGold(),
stateStore,
new NCActionLoader());
}

public byte[][] Evaluate(byte[] blockBytes, byte[]? baseStateRootHashBytes)
{
return _actionEvaluator.Evaluate(
PreEvaluationBlockMarshaller.Deserialize(blockBytes),
baseStateRootHashBytes is { } bytes ? new HashDigest<SHA256>(bytes) : null)
.Select(eval => ActionEvaluationMarshaller.Serialize(eval)).ToArray();
}
}
}
44 changes: 44 additions & 0 deletions .Lib9c.Plugin/WrappedKeyValueStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Lib9c.Plugin.Shared;
using Libplanet.Store.Trie;

namespace Lib9c.Plugin
{
public class WrappedKeyValueStore : IKeyValueStore
{
private readonly IPluginKeyValueStore _pluginKeyValueStore;

public WrappedKeyValueStore(IPluginKeyValueStore pluginKeyValueStore)
{
_pluginKeyValueStore = pluginKeyValueStore;
}

public void Dispose()
{
_pluginKeyValueStore.Dispose();
}

public byte[] Get(in KeyBytes key) =>
_pluginKeyValueStore.Get(key.ByteArray);

public void Set(in KeyBytes key, byte[] value) =>
_pluginKeyValueStore.Set(key.ByteArray, value);

public void Set(IDictionary<KeyBytes, byte[]> values) =>
_pluginKeyValueStore.Set(
values.ToDictionary(kv =>
kv.Key.ByteArray, kv => kv.Value));

public void Delete(in KeyBytes key) =>
_pluginKeyValueStore.Delete(key.ByteArray);

public void Delete(IEnumerable<KeyBytes> keys) =>
_pluginKeyValueStore.Delete(
keys.Select(key => key.ByteArray));

public bool Exists(in KeyBytes key) =>
_pluginKeyValueStore.Exists(key.ByteArray);

public IEnumerable<KeyBytes> ListKeys() =>
_pluginKeyValueStore.ListKeys().Select(key => new KeyBytes(key));
}
}
30 changes: 30 additions & 0 deletions .github/workflows/lib9c_plugin_build_and_push_s3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: lib9c plugin build and push s3

on:
workflow_dispatch:

jobs:
s3-lib9c-plugin:
strategy:
matrix:
runtime: [ "osx-arm64", "linux-arm64", "linux-x64", "win-x64" ]
name: Publish Lib9c.Plugin (${{ matrix.runtime }})
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.400
- name: Publish Lib9c.Plugin
run: dotnet publish ./.Lib9c.Plugin/Lib9c.Plugin.csproj -o out -r ${{ matrix.runtime }}
- name: Compress the build result
run: zip -r ../${{ matrix.runtime }}.zip .
working-directory: ./out
- name: Upload S3
run: aws s3 cp ${{ matrix.runtime }}.zip s3://9c-dx/Lib9c.Plugin/${{ github.sha }}/
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: "us-east-2"
26 changes: 19 additions & 7 deletions Lib9c.sln
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Types", ".Libplan
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Store", ".Libplanet\Libplanet.Store\Libplanet.Store.csproj", "{82BCD815-0AB6-4EEF-A12B-CDB9CD98EEA1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Extensions.ActionEvaluatorCommonComponents", ".Libplanet.Extensions.ActionEvaluatorCommonComponents\Libplanet.Extensions.ActionEvaluatorCommonComponents.csproj", "{64C44AFB-1E14-44D3-B236-A4A37DF2C27A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Extensions.ActionEvaluatorCommonComponents", ".Libplanet.Extensions.ActionEvaluatorCommonComponents\Libplanet.Extensions.ActionEvaluatorCommonComponents.csproj", "{64C44AFB-1E14-44D3-B236-A4A37DF2C27A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests", ".Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests\Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests.csproj", "{EACB2E8D-9A13-491C-BACD-5D79C6C13783}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests", ".Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests\Libplanet.Extensions.ActionEvaluatorCommonComponents.Tests.csproj", "{EACB2E8D-9A13-491C-BACD-5D79C6C13783}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Extensions.RemoteActionEvaluator", ".Libplanet.Extensions.RemoteActionEvaluator\Libplanet.Extensions.RemoteActionEvaluator.csproj", "{0ED5DBA2-C334-40F2-8EB6-2B4D15C1AB4B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Extensions.RemoteActionEvaluator", ".Libplanet.Extensions.RemoteActionEvaluator\Libplanet.Extensions.RemoteActionEvaluator.csproj", "{0ED5DBA2-C334-40F2-8EB6-2B4D15C1AB4B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Extensions.RemoteActionEvaluator.Tests", ".Libplanet.Extensions.RemoteActionEvaluator.Tests\Libplanet.Extensions.RemoteActionEvaluator.Tests.csproj", "{3608A63A-A52D-4EB5-A96D-36C8F11CE603}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Extensions.RemoteActionEvaluator.Tests", ".Libplanet.Extensions.RemoteActionEvaluator.Tests\Libplanet.Extensions.RemoteActionEvaluator.Tests.csproj", "{3608A63A-A52D-4EB5-A96D-36C8F11CE603}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Libplanet.Extensions.RemoteBlockChainStates", ".Libplanet.Extensions.RemoteBlockChainStates\Libplanet.Extensions.RemoteBlockChainStates.csproj", "{63544447-4FCD-48D1-898C-974FBA6834AD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Libplanet.Extensions.RemoteBlockChainStates", ".Libplanet.Extensions.RemoteBlockChainStates\Libplanet.Extensions.RemoteBlockChainStates.csproj", "{63544447-4FCD-48D1-898C-974FBA6834AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib9c.StateService", ".Lib9c.StateService\Lib9c.StateService.csproj", "{EB97AB26-1C8F-48F5-97FF-8A6DF8FAB879}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lib9c.StateService", ".Lib9c.StateService\Lib9c.StateService.csproj", "{EB97AB26-1C8F-48F5-97FF-8A6DF8FAB879}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib9c.StateService.Shared", ".Lib9c.StateService.Shared\Lib9c.StateService.Shared.csproj", "{5D3489AE-A403-4ADD-94E9-48463A42643E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lib9c.StateService.Shared", ".Lib9c.StateService.Shared\Lib9c.StateService.Shared.csproj", "{5D3489AE-A403-4ADD-94E9-48463A42643E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lib9c.Plugin", ".Lib9c.Plugin\Lib9c.Plugin.csproj", "{484A5A5B-D610-42D4-9CAC-B19EA1A71281}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lib9c.Plugin.Shared", ".Lib9c.Plugin.Shared\Lib9c.Plugin.Shared.csproj", "{76F6C25E-94D2-4EA9-B88D-0249F44D1D16}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -208,6 +212,14 @@ Global
{5D3489AE-A403-4ADD-94E9-48463A42643E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D3489AE-A403-4ADD-94E9-48463A42643E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D3489AE-A403-4ADD-94E9-48463A42643E}.Release|Any CPU.Build.0 = Release|Any CPU
{484A5A5B-D610-42D4-9CAC-B19EA1A71281}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{484A5A5B-D610-42D4-9CAC-B19EA1A71281}.Debug|Any CPU.Build.0 = Debug|Any CPU
{484A5A5B-D610-42D4-9CAC-B19EA1A71281}.Release|Any CPU.ActiveCfg = Release|Any CPU
{484A5A5B-D610-42D4-9CAC-B19EA1A71281}.Release|Any CPU.Build.0 = Release|Any CPU
{76F6C25E-94D2-4EA9-B88D-0249F44D1D16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76F6C25E-94D2-4EA9-B88D-0249F44D1D16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76F6C25E-94D2-4EA9-B88D-0249F44D1D16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76F6C25E-94D2-4EA9-B88D-0249F44D1D16}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit bce0756

Please sign in to comment.