From 91f403d4d579a62d2fab59a2182d810ed8713915 Mon Sep 17 00:00:00 2001 From: ilgyu Date: Thu, 30 Nov 2023 10:53:15 +0900 Subject: [PATCH 1/6] feat: Introduce PluginAEV --- .../IPluginActionEvaluator.cs | 7 ++++ .../Lib9c.Plugin.Shared.csproj | 9 +++++ .Lib9c.Plugin/Lib9c.Plugin.csproj | 20 +++++++++++ .Lib9c.Plugin/PluginActionEvaluator.cs | 36 +++++++++++++++++++ Lib9c.sln | 26 ++++++++++---- 5 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 .Lib9c.Plugin.Shared/IPluginActionEvaluator.cs create mode 100644 .Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj create mode 100644 .Lib9c.Plugin/Lib9c.Plugin.csproj create mode 100644 .Lib9c.Plugin/PluginActionEvaluator.cs diff --git a/.Lib9c.Plugin.Shared/IPluginActionEvaluator.cs b/.Lib9c.Plugin.Shared/IPluginActionEvaluator.cs new file mode 100644 index 0000000000..7f2d17b3ef --- /dev/null +++ b/.Lib9c.Plugin.Shared/IPluginActionEvaluator.cs @@ -0,0 +1,7 @@ +namespace Lib9c.Plugin.Shared +{ + public interface IPluginActionEvaluator + { + byte[][] Evaluate(byte[] blockBytes, byte[]? baseStateRootHashBytes); + } +} diff --git a/.Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj b/.Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj new file mode 100644 index 0000000000..132c02c59c --- /dev/null +++ b/.Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/.Lib9c.Plugin/Lib9c.Plugin.csproj b/.Lib9c.Plugin/Lib9c.Plugin.csproj new file mode 100644 index 0000000000..5bee100597 --- /dev/null +++ b/.Lib9c.Plugin/Lib9c.Plugin.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + true + + + + + + + + false + runtime + + + + diff --git a/.Lib9c.Plugin/PluginActionEvaluator.cs b/.Lib9c.Plugin/PluginActionEvaluator.cs new file mode 100644 index 0000000000..b145dd0a65 --- /dev/null +++ b/.Lib9c.Plugin/PluginActionEvaluator.cs @@ -0,0 +1,36 @@ +using System.Security.Cryptography; +using Lib9c.Plugin.Shared; +using Libplanet.Action; +using Libplanet.Common; +using Libplanet.Extensions.ActionEvaluatorCommonComponents; +using Libplanet.RocksDBStore; +using Libplanet.Store; +using Nekoyume.Action; +using Nekoyume.Action.Loader; + + +namespace Lib9c.Plugin +{ + public class PluginActionEvaluator : IPluginActionEvaluator + { + private readonly IActionEvaluator _actionEvaluator; + private readonly IStateStore _stateStore; + + public PluginActionEvaluator(string stateStorePath) + { + _stateStore = new TrieStateStore(new RocksDBKeyValueStore(stateStorePath)); + _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(bytes) : null) + .Select(eval => ActionEvaluationMarshaller.Serialize(eval)).ToArray(); + } + } +} diff --git a/Lib9c.sln b/Lib9c.sln index 37a3e8594e..b2c1c6bf76 100644 --- a/Lib9c.sln +++ b/Lib9c.sln @@ -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 @@ -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 From ad4edfa01e4b5caf57d30fa1b2168be6ec5bd274 Mon Sep 17 00:00:00 2001 From: Suho Lee Date: Thu, 30 Nov 2023 16:36:12 +0900 Subject: [PATCH 2/6] introduce: `IPluginKeyValueStore` --- .Lib9c.Plugin.Shared/IPluginKeyValueStore.cs | 23 ++++++++++ .../PluginRocksDBKeyValueStore.cs | 43 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 .Lib9c.Plugin.Shared/IPluginKeyValueStore.cs create mode 100644 .Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs diff --git a/.Lib9c.Plugin.Shared/IPluginKeyValueStore.cs b/.Lib9c.Plugin.Shared/IPluginKeyValueStore.cs new file mode 100644 index 0000000000..55004c42dc --- /dev/null +++ b/.Lib9c.Plugin.Shared/IPluginKeyValueStore.cs @@ -0,0 +1,23 @@ +using System.Collections.Immutable; + +namespace Lib9c.Plugin.Shared +{ + public interface IPluginKeyValueStore + { + public byte[] Get(in ImmutableArray key); + + public void Set(in ImmutableArray key, byte[] value); + + public void Set(IDictionary, byte[]> values); + + public void Delete(in ImmutableArray key); + + public void Delete(IEnumerable> keys); + + public bool Exists(in ImmutableArray key); + + public IEnumerable> ListKeys(); + + public void Dispose(); + } +} diff --git a/.Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs b/.Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs new file mode 100644 index 0000000000..4badef55ea --- /dev/null +++ b/.Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs @@ -0,0 +1,43 @@ +using System.Collections.Immutable; +using Lib9c.PluginBase; +using Libplanet.RocksDBStore; +using Libplanet.Store.Trie; + +namespace Libplanet.Extensions.PluginActionEvaluator +{ + public class PluginRocksDBKeyValueStore : IPluginKeyValueStore + { + private readonly RocksDBKeyValueStore _rocksDbKeyValueStore; + + public PluginRocksDBKeyValueStore(RocksDBKeyValueStore rocksDbKeyValueStore) + { + _rocksDbKeyValueStore = rocksDbKeyValueStore; + } + public byte[] Get(in ImmutableArray key) => + _rocksDbKeyValueStore.Get(new KeyBytes(key)); + + public void Set(in ImmutableArray key, byte[] value) => + _rocksDbKeyValueStore.Set(new KeyBytes(key), value); + + public void Set(IDictionary, byte[]> values) => + _rocksDbKeyValueStore.Set( + values.ToDictionary(kv => + new KeyBytes(kv.Key), kv => kv.Value)); + + public void Delete(in ImmutableArray key) => + _rocksDbKeyValueStore.Delete(new KeyBytes(key)); + + public void Delete(IEnumerable> keys) => + _rocksDbKeyValueStore.Delete( + keys.Select(key => new KeyBytes(key))); + + public bool Exists(in ImmutableArray key) => + _rocksDbKeyValueStore.Exists(new KeyBytes(key)); + + public IEnumerable> ListKeys() => + _rocksDbKeyValueStore.ListKeys().Select(key => key.ByteArray); + + public void Dispose() => + _rocksDbKeyValueStore.Dispose(); + } +} From 584092d2551e824b05eac3ed4e934f780a9b6ded Mon Sep 17 00:00:00 2001 From: Suho Lee Date: Thu, 30 Nov 2023 17:55:30 +0900 Subject: [PATCH 3/6] ci: for plugin upload `Lib9c.Plugin` is not yet exist but will prepared. --- .../lib9c_plugin_build_and_push_s3.yaml | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/lib9c_plugin_build_and_push_s3.yaml diff --git a/.github/workflows/lib9c_plugin_build_and_push_s3.yaml b/.github/workflows/lib9c_plugin_build_and_push_s3.yaml new file mode 100644 index 0000000000..4634826a63 --- /dev/null +++ b/.github/workflows/lib9c_plugin_build_and_push_s3.yaml @@ -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" From 087c9e05eccba3b4d893e83ef169c8f83c622ac8 Mon Sep 17 00:00:00 2001 From: Suho Lee Date: Fri, 1 Dec 2023 13:58:47 +0900 Subject: [PATCH 4/6] fix: use `IPluginKeyValueStore` instead of `path` --- .Lib9c.Plugin/PluginActionEvaluator.cs | 7 ++-- .Lib9c.Plugin/WrappedKeyValueStore.cs | 44 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .Lib9c.Plugin/WrappedKeyValueStore.cs diff --git a/.Lib9c.Plugin/PluginActionEvaluator.cs b/.Lib9c.Plugin/PluginActionEvaluator.cs index b145dd0a65..ee8c43ce29 100644 --- a/.Lib9c.Plugin/PluginActionEvaluator.cs +++ b/.Lib9c.Plugin/PluginActionEvaluator.cs @@ -14,14 +14,13 @@ namespace Lib9c.Plugin public class PluginActionEvaluator : IPluginActionEvaluator { private readonly IActionEvaluator _actionEvaluator; - private readonly IStateStore _stateStore; - public PluginActionEvaluator(string stateStorePath) + public PluginActionEvaluator(IPluginKeyValueStore keyValueStore) { - _stateStore = new TrieStateStore(new RocksDBKeyValueStore(stateStorePath)); + var stateStore = new TrieStateStore(new WrappedKeyValueStore(keyValueStore)); _actionEvaluator = new ActionEvaluator( _ => new RewardGold(), - _stateStore, + stateStore, new NCActionLoader()); } diff --git a/.Lib9c.Plugin/WrappedKeyValueStore.cs b/.Lib9c.Plugin/WrappedKeyValueStore.cs new file mode 100644 index 0000000000..dde51d60dc --- /dev/null +++ b/.Lib9c.Plugin/WrappedKeyValueStore.cs @@ -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 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 keys) => + _pluginKeyValueStore.Delete( + keys.Select(key => key.ByteArray)); + + public bool Exists(in KeyBytes key) => + _pluginKeyValueStore.Exists(key.ByteArray); + + public IEnumerable ListKeys() => + _pluginKeyValueStore.ListKeys().Select(key => new KeyBytes(key)); + } +} From dc140bc6c3b06a9c23714a9152e4f2020fe3bb0c Mon Sep 17 00:00:00 2001 From: Suho Lee Date: Mon, 4 Dec 2023 16:59:22 +0900 Subject: [PATCH 5/6] fix: remove `RocksDBStore` --- .Lib9c.Plugin/Lib9c.Plugin.csproj | 1 - .Lib9c.Plugin/PluginActionEvaluator.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/.Lib9c.Plugin/Lib9c.Plugin.csproj b/.Lib9c.Plugin/Lib9c.Plugin.csproj index 5bee100597..13a041c34a 100644 --- a/.Lib9c.Plugin/Lib9c.Plugin.csproj +++ b/.Lib9c.Plugin/Lib9c.Plugin.csproj @@ -8,7 +8,6 @@ - diff --git a/.Lib9c.Plugin/PluginActionEvaluator.cs b/.Lib9c.Plugin/PluginActionEvaluator.cs index ee8c43ce29..55bd9ea460 100644 --- a/.Lib9c.Plugin/PluginActionEvaluator.cs +++ b/.Lib9c.Plugin/PluginActionEvaluator.cs @@ -3,7 +3,6 @@ using Libplanet.Action; using Libplanet.Common; using Libplanet.Extensions.ActionEvaluatorCommonComponents; -using Libplanet.RocksDBStore; using Libplanet.Store; using Nekoyume.Action; using Nekoyume.Action.Loader; From 31d5f0c41d5b72f374016b536f7e90f4754ac0e9 Mon Sep 17 00:00:00 2001 From: Suho Lee Date: Mon, 4 Dec 2023 17:24:37 +0900 Subject: [PATCH 6/6] chore: remove useless directory --- .../PluginRocksDBKeyValueStore.cs | 43 ------------------- 1 file changed, 43 deletions(-) delete mode 100644 .Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs diff --git a/.Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs b/.Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs deleted file mode 100644 index 4badef55ea..0000000000 --- a/.Libplanet.Extensions.PluginActionEvaluator/PluginRocksDBKeyValueStore.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections.Immutable; -using Lib9c.PluginBase; -using Libplanet.RocksDBStore; -using Libplanet.Store.Trie; - -namespace Libplanet.Extensions.PluginActionEvaluator -{ - public class PluginRocksDBKeyValueStore : IPluginKeyValueStore - { - private readonly RocksDBKeyValueStore _rocksDbKeyValueStore; - - public PluginRocksDBKeyValueStore(RocksDBKeyValueStore rocksDbKeyValueStore) - { - _rocksDbKeyValueStore = rocksDbKeyValueStore; - } - public byte[] Get(in ImmutableArray key) => - _rocksDbKeyValueStore.Get(new KeyBytes(key)); - - public void Set(in ImmutableArray key, byte[] value) => - _rocksDbKeyValueStore.Set(new KeyBytes(key), value); - - public void Set(IDictionary, byte[]> values) => - _rocksDbKeyValueStore.Set( - values.ToDictionary(kv => - new KeyBytes(kv.Key), kv => kv.Value)); - - public void Delete(in ImmutableArray key) => - _rocksDbKeyValueStore.Delete(new KeyBytes(key)); - - public void Delete(IEnumerable> keys) => - _rocksDbKeyValueStore.Delete( - keys.Select(key => new KeyBytes(key))); - - public bool Exists(in ImmutableArray key) => - _rocksDbKeyValueStore.Exists(new KeyBytes(key)); - - public IEnumerable> ListKeys() => - _rocksDbKeyValueStore.ListKeys().Select(key => key.ByteArray); - - public void Dispose() => - _rocksDbKeyValueStore.Dispose(); - } -}