-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/actionbase #3917
Draft
greymistcube
wants to merge
15
commits into
planetarium:main
Choose a base branch
from
greymistcube:refactor/actionbase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Refactor/actionbase #3917
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
98e9105
exp: actionbase
riemannulus bfb25a3
exp: dynamic action loader
riemannulus 670b197
Rebase fix; reorganized files; simplified ActionBase
greymistcube e4171f9
Overhauled ActionBase; sample tests
greymistcube 30d224a
Added GeneratePlainValue() method
greymistcube 8214808
Added a new set of test actions
greymistcube 479e278
Changed naming
greymistcube c78e97e
Removed const strings
greymistcube 225cd81
Changed plain value scheme
greymistcube 0552416
Fixed test
greymistcube 3ec8e05
Added plain value validation
greymistcube 427471b
Added an initial implementation for schema generation
greymistcube 8a1cd64
Cleanup
greymistcube 083f3b8
Added optional description feature to executable attribute
greymistcube 815b5ea
Added optional parameter description to schema
greymistcube File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
Libplanet.SDK.Action.Tests/SimpleRPG/Actions/AvatarAction.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,46 @@ | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Crypto; | ||
using Libplanet.SDK.Action.Attributes; | ||
using Libplanet.SDK.Action.Tests.SimpleRPG.Models; | ||
|
||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Actions | ||
{ | ||
[ActionType("Avatar")] | ||
public class AvatarAction : ActionBase | ||
{ | ||
// This has no IAccount associated with its domain. | ||
public override Address StorageAddress => default; | ||
|
||
[Executable] | ||
public void Create(IValue args) | ||
{ | ||
string name = (Text)args; | ||
Call<InfoAction, Info>("Create", new object?[] { name }); | ||
Call<InventoryAction, Inventory>("Create"); | ||
} | ||
|
||
[Callable] | ||
public Avatar GetAvatar(Address address) | ||
{ | ||
Info info = Call<InfoAction, Info>( | ||
"GetInfo", | ||
new object?[] { address }); | ||
Inventory inventory = Call<InventoryAction, Inventory>( | ||
"GetInventory", | ||
new object?[] { address }); | ||
return new Avatar(info, inventory); | ||
} | ||
|
||
[Callable] | ||
public void SetAvatar(Address address, Avatar avatar) | ||
{ | ||
Call<InfoAction>( | ||
"SetInfo", | ||
new object?[] { address, avatar.Info }); | ||
Call<InventoryAction>( | ||
"SetInventory", | ||
new object?[] { address, avatar.Inventory }); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Libplanet.SDK.Action.Tests/SimpleRPG/Actions/FarmAction.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,30 @@ | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Crypto; | ||
using Libplanet.SDK.Action.Attributes; | ||
using Libplanet.SDK.Action.Tests.SimpleRPG.Models; | ||
|
||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Actions | ||
{ | ||
[ActionType("Farm")] | ||
public class FarmAction : ActionBase | ||
{ | ||
public const int ExpPerFarm = 10; | ||
public const int GoldPerFarm = 20; | ||
|
||
// This has no IAccount associated with its domain. | ||
public override Address StorageAddress => default; | ||
|
||
[Executable] | ||
public void Farm(IValue args) | ||
{ | ||
// Simple type checking. | ||
_ = (Null)args; | ||
|
||
Avatar avatar = Call<AvatarAction, Avatar>("GetAvatar", new object?[] { Signer }); | ||
avatar.Info.AddExp(ExpPerFarm); | ||
avatar.Inventory.AddGold(GoldPerFarm); | ||
Call<AvatarAction>("SetAvatar", new object?[] { Signer, avatar }); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Libplanet.SDK.Action.Tests/SimpleRPG/Actions/InfoAction.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,33 @@ | ||
using Libplanet.Crypto; | ||
using Libplanet.SDK.Action.Attributes; | ||
using Libplanet.SDK.Action.Tests.SimpleRPG.Models; | ||
|
||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Actions | ||
{ | ||
public class InfoAction : ActionBase | ||
{ | ||
public override Address StorageAddress => | ||
new Address("0x1000000000000000000000000000000000000001"); | ||
|
||
[Callable] | ||
public Info Create(string name) | ||
{ | ||
if (GetState(Signer) is { } value) | ||
{ | ||
throw new InvalidOperationException("Info already exists."); | ||
} | ||
|
||
Info info = new Info(name, 0); | ||
SetInfo(Signer, info); | ||
return info; | ||
} | ||
|
||
[Callable] | ||
public Info GetInfo(Address address) => | ||
new Info(GetState(address) ?? throw new NullReferenceException()); | ||
|
||
[Callable] | ||
public void SetInfo(Address address, Info info) => | ||
SetState(address, info.Serialized); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Libplanet.SDK.Action.Tests/SimpleRPG/Actions/InventoryAction.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,33 @@ | ||
using Libplanet.Crypto; | ||
using Libplanet.SDK.Action.Attributes; | ||
using Libplanet.SDK.Action.Tests.SimpleRPG.Models; | ||
|
||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Actions | ||
{ | ||
public class InventoryAction : ActionBase | ||
{ | ||
public override Address StorageAddress => | ||
new Address("0x1000000000000000000000000000000000000002"); | ||
|
||
[Callable] | ||
public Inventory Create() | ||
{ | ||
if (GetState(Signer) is { }) | ||
{ | ||
throw new InvalidOperationException("Inventory already exists."); | ||
} | ||
|
||
Inventory inventory = new Inventory(); | ||
SetInventory(Signer, inventory); | ||
return inventory; | ||
} | ||
|
||
[Callable] | ||
public Inventory GetInventory(Address address) => | ||
new Inventory(GetState(address) ?? throw new NullReferenceException()); | ||
|
||
[Callable] | ||
public void SetInventory(Address address, Inventory inventory) => | ||
SetState(address, inventory.Serialized); | ||
} | ||
} |
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,14 @@ | ||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Models | ||
{ | ||
public class Avatar | ||
{ | ||
public Info Info { get; } | ||
public Inventory Inventory { get; } | ||
|
||
public Avatar(Info info, Inventory inventory) | ||
{ | ||
Info = info; | ||
Inventory = inventory; | ||
} | ||
} | ||
} |
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,38 @@ | ||
using Bencodex.Types; | ||
|
||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Models | ||
{ | ||
public class Info | ||
{ | ||
public string Name { get; } | ||
|
||
public int Exp { get; private set; } | ||
|
||
public int Level => (Exp / 100); | ||
|
||
public Info(string name) | ||
: this(name, 0) | ||
{ | ||
} | ||
|
||
public Info(IValue value) | ||
: this((Text)((List)value)[0], (Integer)((List)value)[1]) | ||
{ | ||
} | ||
|
||
public Info(string name, int exp) | ||
{ | ||
Name = name; | ||
Exp = exp; | ||
} | ||
|
||
public IValue Serialized => List.Empty | ||
.Add(Name) | ||
.Add(Exp); | ||
|
||
public void AddExp(int exp) | ||
{ | ||
Exp = Exp + exp; | ||
} | ||
} | ||
} |
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,31 @@ | ||
using Bencodex.Types; | ||
|
||
namespace Libplanet.SDK.Action.Tests.SimpleRPG.Models | ||
{ | ||
public class Inventory | ||
{ | ||
public int Gold { get; private set; } | ||
|
||
public Inventory() | ||
: this(0) | ||
{ | ||
} | ||
|
||
public Inventory(IValue value) | ||
: this((int)(Integer)value) | ||
{ | ||
} | ||
|
||
public Inventory(int gold) | ||
{ | ||
Gold = gold; | ||
} | ||
|
||
public IValue Serialized => new Integer(Gold); | ||
|
||
public void AddGold(int gold) | ||
{ | ||
Gold = Gold + gold; | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
Libplanet.SDK.Action.Tests/SimpleRPG/SimpleRPGActionsTest.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,118 @@ | ||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Action.Loader; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Libplanet.SDK.Action.Tests.SimpleRPG.Actions; | ||
using Libplanet.SDK.Action.Tests.SimpleRPG.Models; | ||
using Libplanet.Store; | ||
using Libplanet.Store.Trie; | ||
using Libplanet.Types.Blocks; | ||
using Xunit; | ||
|
||
namespace Libplanet.SDK.Action.Tests.Sample | ||
{ | ||
public class SimpleRPGActionsTest | ||
{ | ||
private TypedActionLoader _loader; | ||
private IStateStore _stateStore; | ||
private IWorld _world; | ||
|
||
public SimpleRPGActionsTest() | ||
{ | ||
_loader = new TypedActionLoader( | ||
ImmutableDictionary<IValue, Type>.Empty | ||
.Add(new Text("Avatar"), typeof(AvatarAction)) | ||
.Add(new Text("Farm"), typeof(FarmAction))); | ||
|
||
_stateStore = new TrieStateStore(new MemoryKeyValueStore()); | ||
|
||
ITrie trie = _stateStore.GetStateRoot(null); | ||
trie = trie.SetMetadata(new TrieMetadata(Block.CurrentProtocolVersion)); | ||
trie = _stateStore.Commit(trie); | ||
_world = new World(new WorldBaseState(trie, _stateStore)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void Scenario(bool commit) | ||
{ | ||
IValue plainValue = Dictionary.Empty | ||
.Add("type_id", "Avatar") | ||
.Add("call", "Create") | ||
.Add("args", "Hero"); | ||
IAction action = Assert.IsType<AvatarAction>(_loader.LoadAction(0, plainValue)); | ||
Address signer = new PrivateKey().Address; | ||
IWorld world = _world; | ||
|
||
world = action.Execute(new MockActionContext(signer, signer, world)); | ||
world = commit ? _stateStore.CommitWorld(world) : world; | ||
Assert.Equal( | ||
new Info("Hero").Serialized, | ||
world | ||
.GetAccountState(new Address("0x1000000000000000000000000000000000000001")) | ||
.GetState(signer)); | ||
Assert.Equal( | ||
new Inventory().Serialized, | ||
world | ||
.GetAccountState(new Address("0x1000000000000000000000000000000000000002")) | ||
.GetState(signer)); | ||
|
||
const int repeat = 3; | ||
foreach (var _ in Enumerable.Range(0, repeat)) | ||
{ | ||
plainValue = Dictionary.Empty | ||
.Add("type_id", "Farm") | ||
.Add("call", "Farm") | ||
.Add("args", Null.Value); | ||
action = Assert.IsType<FarmAction>(_loader.LoadAction(0, plainValue)); | ||
world = action.Execute(new MockActionContext(signer, signer, world)); | ||
world = commit ? _stateStore.CommitWorld(world) : world; | ||
} | ||
|
||
Assert.Equal( | ||
new Info("Hero", FarmAction.ExpPerFarm * repeat).Serialized, | ||
world | ||
.GetAccountState(new Address("0x1000000000000000000000000000000000000001")) | ||
.GetState(signer)); | ||
Assert.Equal( | ||
new Inventory(FarmAction.GoldPerFarm * repeat).Serialized, | ||
world | ||
.GetAccountState(new Address("0x1000000000000000000000000000000000000002")) | ||
.GetState(signer)); | ||
} | ||
|
||
[Fact] | ||
public void CannotCreateTwice() | ||
{ | ||
IValue plainValue = Dictionary.Empty | ||
.Add("type_id", "Avatar") | ||
.Add("call", "Create") | ||
.Add("args", "Hero"); | ||
IAction action = Assert.IsType<AvatarAction>(_loader.LoadAction(0, plainValue)); | ||
Address signer = new PrivateKey().Address; | ||
IWorld world = _world; | ||
|
||
world = action.Execute(new MockActionContext(signer, signer, world)); | ||
world = _stateStore.CommitWorld(world); | ||
|
||
plainValue = Dictionary.Empty | ||
.Add("type_id", "Avatar") | ||
.Add("call", "Create") | ||
.Add("args", "Princess"); | ||
action = Assert.IsType<AvatarAction>(_loader.LoadAction(0, plainValue)); | ||
Assert.Contains( | ||
"Info already exists", | ||
Assert.IsType<InvalidOperationException>( | ||
Assert.IsType<TargetInvocationException>( | ||
Assert.Throws<TargetInvocationException>(() => | ||
action.Execute(new MockActionContext(signer, signer, world))) | ||
.InnerException) | ||
.InnerException) | ||
.Message); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using
nameof(InfoAction.GetInfo)
instead of literal strings.