Skip to content

Commit

Permalink
Merge branch 'development' into PDX-203-config-json-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal authored Dec 6, 2023
2 parents 230ac07 + 68761cc commit 2ec5092
Show file tree
Hide file tree
Showing 29 changed files with 453 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Lib9c
Submodule Lib9c updated 73 files
+7 −0 .Lib9c.Plugin.Shared/IPluginActionEvaluator.cs
+23 −0 .Lib9c.Plugin.Shared/IPluginKeyValueStore.cs
+0 −0 .Lib9c.Plugin.Shared/Lib9c.Plugin.Shared.csproj
+8 −4 .Lib9c.Plugin/Lib9c.Plugin.csproj
+34 −0 .Lib9c.Plugin/PluginActionEvaluator.cs
+44 −0 .Lib9c.Plugin/WrappedKeyValueStore.cs
+0 −8 .Lib9c.StateService.Shared/RemoteEvaluationRequest.cs
+0 −6 .Lib9c.StateService.Shared/RemoteEvaluationResponse.cs
+0 −62 .Lib9c.StateService/Controllers/RemoteEvaluationController.cs
+0 −21 .Lib9c.StateService/Lib9c.StateService.csproj
+0 −39 .Lib9c.StateService/Program.cs
+0 −41 .Lib9c.StateService/Properties/launchSettings.json
+0 −11 .Lib9c.StateService/appsettings-schema.json
+0 −8 .Lib9c.StateService/appsettings.Development.json
+0 −10 .Lib9c.StateService/appsettings.json
+62 −0 .Lib9c.Tests/AccountExtensions.cs
+1 −1 .Lib9c.Tests/Action/AccountStateViewExtensionsTest.cs
+1 −1 .Lib9c.Tests/Action/BattleArena4Test.cs
+2 −2 .Lib9c.Tests/Action/Buy11Test.cs
+1 −1 .Lib9c.Tests/Action/CombinationEquipment11Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash10Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash11Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash12Test.cs
+4 −4 .Lib9c.Tests/Action/HackAndSlash13Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash15Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash16Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash17Test.cs
+4 −4 .Lib9c.Tests/Action/HackAndSlash18Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash19Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash20Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash21Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash6Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash7Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash8Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlash9Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep1Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep2Test.cs
+4 −4 .Lib9c.Tests/Action/HackAndSlashSweep3Test.cs
+4 −4 .Lib9c.Tests/Action/HackAndSlashSweep4Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep5Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep6Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep7Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep8Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashSweep9Test.cs
+3 −3 .Lib9c.Tests/Action/HackAndSlashTest14.cs
+1 −1 .Lib9c.Tests/Action/ItemEnhancement10Test.cs
+1 −1 .Lib9c.Tests/Action/ItemEnhancement11Test.cs
+1 −1 .Lib9c.Tests/Action/MimisbrunnrBattle10Test.cs
+152 −77 .Lib9c.Tests/Action/MockState.cs
+1 −1 .Lib9c.Tests/Action/RankingBattle11Test.cs
+4 −14 .Lib9c.Tests/Action/Snapshot/TransferAsset0SnapshotTest.cs
+148 −0 .Lib9c.Tests/Model/PlayerTest.cs
+1 −1 .Libplanet
+0 −29 .Libplanet.Extensions.RemoteActionEvaluator.Tests/Libplanet.Extensions.RemoteActionEvaluator.Tests.csproj
+0 −1 .Libplanet.Extensions.RemoteActionEvaluator.Tests/Usings.cs
+0 −3 .Libplanet.Extensions.RemoteActionEvaluator/AssemblyInfo.cs
+0 −42 .Libplanet.Extensions.RemoteActionEvaluator/RemoteActionEvaluator.cs
+4 −0 .github/workflows/lib9c_plugin_build_and_push_s3.yaml
+0 −24 .github/workflows/publish.yml
+0 −31 Lib9c.MessagePack/AccountStateDelta.cs
+12 −3 Lib9c.Policy/Policy/BlockPolicySource.cs
+0 −5 Lib9c.Policy/Policy/MaxTransactionsBytesPolicy.cs
+13 −25 Lib9c.sln
+27 −0 Lib9c/Model/BattleStatus/Arena/ArenaTick.cs
+28 −0 Lib9c/Model/BattleStatus/Tick.cs
+4 −0 Lib9c/Model/Buff/BuffFactory.cs
+40 −0 Lib9c/Model/Buff/Stun.cs
+13 −4 Lib9c/Model/Character/ArenaCharacter.cs
+18 −4 Lib9c/Model/Character/CharacterBase.cs
+1 −0 Lib9c/Model/Skill/ActionBuffType.cs
+2 −1 Lib9c/TableCSV/Skill/ActionBuffSheet.csv
+2 −1 Lib9c/TableCSV/Skill/SkillActionBuffSheet.csv
+2 −1 Lib9c/TableCSV/Skill/SkillSheet.csv
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>
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 Libplanet.Extensions.PluggedActionEvaluator/PluginKeyValueStore.cs
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 Libplanet.Extensions.PluggedActionEvaluator/PluginLoadContext.cs
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;
}
}
}

2 changes: 1 addition & 1 deletion Libplanet.Headless/Hosting/ActionEvaluatorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public enum ActionEvaluatorType
{
Default, // ActionEvaluator
ForkableActionEvaluator,
RemoteActionEvaluator,
PluggedActionEvaluator,
}
68 changes: 48 additions & 20 deletions Libplanet.Headless/Hosting/LibplanetNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Threading;
Expand All @@ -15,7 +16,7 @@
using Libplanet.Types.Blocks;
using Libplanet.Crypto;
using Libplanet.Extensions.ForkableActionEvaluator;
using Libplanet.Extensions.RemoteActionEvaluator;
using Libplanet.Extensions.PluggedActionEvaluator;
using Libplanet.Net;
using Libplanet.Net.Consensus;
using Libplanet.Net.Options;
Expand Down Expand Up @@ -80,8 +81,7 @@ public LibplanetNodeService(
Action<bool> preloadStatusHandlerAction,
IActionLoader actionLoader,
bool ignoreBootstrapFailure = false,
bool ignorePreloadFailure = false,
bool useRemoteActionEvaluator = false
bool ignorePreloadFailure = false
)
{
if (blockPolicy is null)
Expand All @@ -95,7 +95,7 @@ public LibplanetNodeService(

var iceServers = Properties.IceServers;

(Store, StateStore) = LoadStore(
(Store, StateStore, IKeyValueStore keyValueStore) = LoadStore(
Properties.StorePath,
Properties.StoreType,
Properties.StoreStatesCacheSize);
Expand All @@ -116,23 +116,25 @@ public LibplanetNodeService(
}

var blockChainStates = new BlockChainStates(Store, StateStore);

IActionEvaluator BuildActionEvaluator(IActionEvaluatorConfiguration actionEvaluatorConfiguration)
{
return actionEvaluatorConfiguration switch
{
RemoteActionEvaluatorConfiguration remoteActionEvaluatorConfiguration => new RemoteActionEvaluator(
new Uri(remoteActionEvaluatorConfiguration.StateServiceEndpoint)),
DefaultActionEvaluatorConfiguration _ => new ActionEvaluator(
_ => blockPolicy.BlockAction,
stateStore: StateStore,
actionTypeLoader: actionLoader
),
ForkableActionEvaluatorConfiguration forkableActionEvaluatorConfiguration => new
ForkableActionEvaluator(
forkableActionEvaluatorConfiguration.Pairs.Select(pair => (
(pair.Item1.Start, pair.Item1.End), BuildActionEvaluator(pair.Item2)
))
),
PluggedActionEvaluatorConfiguration pluginActionEvaluatorConfiguration =>
new PluggedActionEvaluator(
ResolvePluginPath(pluginActionEvaluatorConfiguration.PluginPath),
pluginActionEvaluatorConfiguration.TypeName,
keyValueStore),
DefaultActionEvaluatorConfiguration _ =>
new ActionEvaluator(
_ => blockPolicy.BlockAction,
stateStore: StateStore,
actionTypeLoader: actionLoader),
ForkableActionEvaluatorConfiguration forkableActionEvaluatorConfiguration =>
new ForkableActionEvaluator(
forkableActionEvaluatorConfiguration.Pairs.Select(
pair => ((pair.Item1.Start, pair.Item1.End), BuildActionEvaluator(pair.Item2)))),
_ => throw new InvalidOperationException("Unexpected type."),
};
}
Expand Down Expand Up @@ -302,7 +304,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
}
}

protected (IStore, IStateStore) LoadStore(string path, string type, int statesCacheSize)
protected (IStore, IStateStore, IKeyValueStore) LoadStore(string path, string type, int statesCacheSize)
{
IStore store = null;
if (type == "rocksdb")
Expand Down Expand Up @@ -346,7 +348,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)

IKeyValueStore stateKeyValueStore = new RocksDBKeyValueStore(Path.Combine(path, "states"));
IStateStore stateStore = new TrieStateStore(stateKeyValueStore);
return (store, stateStore);
return (store, stateStore, stateKeyValueStore);
}

private async Task StartSwarm(bool preload, CancellationToken cancellationToken)
Expand Down Expand Up @@ -568,7 +570,7 @@ protected async Task CheckPeerTable(CancellationToken cancellationToken = defaul
if (grace == count)
{
var message = "No any peers are connected even seed peers were given. " +
$"(grace: {grace}";
$"(grace: {grace}";
Log.Error(message);
// _exceptionHandlerAction(RPCException.NetworkException, message);
Properties.NodeExceptionOccurred(NodeExceptionType.NoAnyPeer, message);
Expand Down Expand Up @@ -629,6 +631,32 @@ public override void Dispose()
Log.Debug("Store disposed.");
}

private string ResolvePluginPath(string path) =>
Uri.IsWellFormedUriString(path, UriKind.Absolute)
? DownloadPlugin(path).Result
: path;

private async Task<string> DownloadPlugin(string url)
{
var path = Path.Combine(Environment.CurrentDirectory, "plugins");
Directory.CreateDirectory(path);
var hashed = url.GetHashCode().ToString();
var logger = Log.ForContext("LibplanetNodeService", hashed);
using var httpClient = new HttpClient();
var downloadPath = Path.Join(path, hashed + ".zip");
var extractPath = Path.Join(path, hashed);
logger.Debug("Downloading...");
await File.WriteAllBytesAsync(
downloadPath,
await httpClient.GetByteArrayAsync(url, SwarmCancellationToken),
SwarmCancellationToken);
logger.Debug("Finished downloading.");
logger.Debug("Extracting...");
ZipFile.ExtractToDirectory(downloadPath, extractPath);
logger.Debug("Finished extracting.");
return Path.Combine(extractPath, "Lib9c.Plugin.dll");
}

// FIXME: Request libplanet provide default implementation.
private sealed class ActionTypeLoaderContext : IActionTypeLoaderContext
{
Expand Down
10 changes: 10 additions & 0 deletions Libplanet.Headless/Hosting/PluggedActionEvaluatorConfiguration.cs
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 was deleted.

2 changes: 1 addition & 1 deletion Libplanet.Headless/Libplanet.Headless.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ProjectReference Include="..\Lib9c\.Libplanet\Libplanet.RocksDBStore\Libplanet.RocksDBStore.csproj" />
<ProjectReference Include="..\Lib9c\.Libplanet\Libplanet.Net\Libplanet.Net.csproj" />
<ProjectReference Include="..\NineChronicles.RPC.Shared\NineChronicles.RPC.Shared\NineChronicles.RPC.Shared.csproj" />
<ProjectReference Include="..\Lib9c\.Libplanet.Extensions.RemoteActionEvaluator\Libplanet.Extensions.RemoteActionEvaluator.csproj" />
<ProjectReference Include="..\Libplanet.Extensions.ForkableActionEvaluator\Libplanet.Extensions.ForkableActionEvaluator.csproj" />
<ProjectReference Include="..\Libplanet.Extensions.PluggedActionEvaluator\Libplanet.Extensions.PluggedActionEvaluator.csproj" />
</ItemGroup>
</Project>
17 changes: 7 additions & 10 deletions NineChronicles.Headless.Executable.Tests/Commands/TxCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ private void Assert_Tx(long txNonce, string filePath, bool gas)
{
var timeStamp = DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.UtcNow.ToUnixTimeSeconds());
var hashHex = ByteUtil.Hex(_blockHash.ByteArray);
long? maxGasPrice = null;
if (gas)
{
maxGasPrice = 1L;
}
long? maxGasPrice = gas ? (long?)1L : null;
_command.Sign(ByteUtil.Hex(_privateKey.ByteArray), txNonce, hashHex, timeStamp.ToString(),
new[] { filePath }, maxGasPrice: maxGasPrice);
var output = _console.Out.ToString();
Expand All @@ -146,11 +142,12 @@ private void Assert_Tx(long txNonce, string filePath, bool gas)
Assert.Equal(_privateKey.Address, tx.Signer);
Assert.Equal(timeStamp, tx.Timestamp);
ActionBase action = (ActionBase)new NCActionLoader().LoadAction(1L, tx.Actions.Single());
long expectedGasLimit = 1L;
if (action is ITransferAsset || action is ITransferAssets)
{
expectedGasLimit = 4L;
}
long? expectedGasLimit = gas
? action is ITransferAsset || action is ITransferAssets
? (long?)4L
: (long?)1L
: null;

Assert.Equal(expectedGasLimit, tx.GasLimit);
if (gas)
{
Expand Down
Loading

0 comments on commit 2ec5092

Please sign in to comment.