Skip to content

Commit

Permalink
Merge pull request #2346 from planetarium/hotfix/add_action_loader
Browse files Browse the repository at this point in the history
🔥  hotfix: add action loader
  • Loading branch information
riemannulus authored Dec 7, 2023
2 parents 6351a7e + 1b8738f commit e403672
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Action.State;
using Libplanet.Types.Blocks;
using Libplanet.Common;
using Libplanet.Crypto;
Expand All @@ -19,7 +20,7 @@ public void ForkEvaluation()
{
((0L, 100L), new PreActionEvaluator()),
((101L, long.MaxValue), new PostActionEvaluator()),
});
}, new SingleActionLoader(typeof(MockAction)));

Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(0), null)).Action);
Assert.Equal((Text)"PRE", Assert.Single(evaluator.Evaluate(new MockBlock(99), null)).Action);
Expand All @@ -36,25 +37,25 @@ public void CheckPairs()
{
((0L, 100L), new PreActionEvaluator()),
((99L, long.MaxValue), new PostActionEvaluator()),
}));
}, new SingleActionLoader(typeof(MockAction))));
Assert.Throws<ArgumentOutOfRangeException>(() => new ForkableActionEvaluator(
new ((long, long), IActionEvaluator)[]
{
((0L, 100L), new PreActionEvaluator()),
((100L, long.MaxValue), new PostActionEvaluator()),
}));
}, new SingleActionLoader(typeof(MockAction))));
Assert.Throws<ArgumentOutOfRangeException>(() => new ForkableActionEvaluator(
new ((long, long), IActionEvaluator)[]
{
((50L, 100L), new PreActionEvaluator()),
((101L, long.MaxValue), new PostActionEvaluator()),
}));
}, new SingleActionLoader(typeof(MockAction))));
Assert.Throws<ArgumentOutOfRangeException>(() => new ForkableActionEvaluator(
new ((long, long), IActionEvaluator)[]
{
((0L, 100L), new PreActionEvaluator()),
((101L, long.MaxValue - 1), new PostActionEvaluator()),
}));
}, new SingleActionLoader(typeof(MockAction))));
}
}

Expand Down Expand Up @@ -104,6 +105,17 @@ public IReadOnlyList<ICommittedActionEvaluation> Evaluate(IPreEvaluationBlock bl
}
}

class MockAction : IAction
{
public IValue PlainValue => default(Null);

public void LoadPlainValue(IValue plainValue)
{
}

public IAccount Execute(IActionContext context) => context.PreviousState;
}

class MockBlock : IPreEvaluationBlock
{
public MockBlock(long blockIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ public class ForkableActionEvaluator : IActionEvaluator
{
private readonly HardForkRouter _router;

public ForkableActionEvaluator(IEnumerable<((long StartIndex, long EndIndex) Range, IActionEvaluator ActionEvaluator)> pairs)
public ForkableActionEvaluator(IEnumerable<((long StartIndex, long EndIndex) Range, IActionEvaluator ActionEvaluator)> pairs, IActionLoader actionLoader)
{
_router = new HardForkRouter(pairs);
ActionLoader = actionLoader;
}

public IActionLoader ActionLoader => throw new NotSupportedException();
public IActionLoader ActionLoader
{
get;
}

public IReadOnlyList<ICommittedActionEvaluation> Evaluate(
IPreEvaluationBlock block, HashDigest<SHA256>? baseStateRootHash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ public class PluggedActionEvaluator : IActionEvaluator
{
private readonly IPluginActionEvaluator _pluginActionEvaluator;

public IActionLoader ActionLoader => throw new NotImplementedException();
public IActionLoader ActionLoader
{
get;
}

public PluggedActionEvaluator(string pluginPath, string typeName, IKeyValueStore keyValueStore)
public PluggedActionEvaluator(string pluginPath, string typeName, IKeyValueStore keyValueStore, IActionLoader actionLoader)
{
_pluginActionEvaluator = CreateActionEvaluator(pluginPath, typeName, keyValueStore);
ActionLoader = actionLoader;
}

public static Assembly LoadPlugin(string absolutePath)
Expand Down
8 changes: 6 additions & 2 deletions Libplanet.Headless/Hosting/LibplanetNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ IActionEvaluator BuildActionEvaluator(IActionEvaluatorConfiguration actionEvalua
new PluggedActionEvaluator(
ResolvePluginPath(pluginActionEvaluatorConfiguration.PluginPath),
pluginActionEvaluatorConfiguration.TypeName,
keyValueStore),
keyValueStore,
actionLoader),
DefaultActionEvaluatorConfiguration _ =>
new ActionEvaluator(
_ => blockPolicy.BlockAction,
Expand All @@ -134,7 +135,10 @@ IActionEvaluator BuildActionEvaluator(IActionEvaluatorConfiguration actionEvalua
ForkableActionEvaluatorConfiguration forkableActionEvaluatorConfiguration =>
new ForkableActionEvaluator(
forkableActionEvaluatorConfiguration.Pairs.Select(
pair => ((pair.Item1.Start, pair.Item1.End), BuildActionEvaluator(pair.Item2)))),
pair => (
(pair.Item1.Start, pair.Item1.End),
BuildActionEvaluator(pair.Item2))),
actionLoader),
_ => throw new InvalidOperationException("Unexpected type."),
};
}
Expand Down

0 comments on commit e403672

Please sign in to comment.