Skip to content
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
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Libplanet.SDK.Action.Tests/Libplanet.SDK.Action.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>

<RootNamespace>Libplanet.SDK.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
<PackageReference Include="Moq.AutoMock" Version="3.5.0" />
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Libplanet.SDK.Action\Libplanet.SDK.Action.csproj" />
<ProjectReference Include="..\test\Libplanet.Mocks\Libplanet.Mocks.csproj" />
</ItemGroup>

</Project>
60 changes: 60 additions & 0 deletions Libplanet.SDK.Action.Tests/MockActionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Evidence;
using Libplanet.Types.Tx;

namespace Libplanet.SDK.Action.Tests
{
public class MockActionContext : IActionContext
{
public MockActionContext(
Address signer,
Address miner,
IWorld previousState)
{
Signer = signer;
Miner = miner;
PreviousState = previousState;
}

public Address Signer { get; }

public TxId? TxId =>
throw new NotSupportedException();

public Address Miner { get; }

public long BlockIndex =>
throw new NotSupportedException();

public int BlockProtocolVersion =>
throw new NotSupportedException();

public IWorld PreviousState { get; }

public int RandomSeed =>
throw new NotSupportedException();

public bool IsPolicyAction =>
throw new NotSupportedException();

public IReadOnlyList<ITransaction> Txs =>
throw new NotSupportedException();

public IReadOnlyList<EvidenceBase> Evidence =>
throw new NotSupportedException();

public void UseGas(long gas) =>
throw new NotSupportedException();

public IRandom GetRandom() =>
throw new NotSupportedException();

public long GasUsed() =>
throw new NotSupportedException();

public long GasLimit() =>
throw new NotSupportedException();
}
}
46 changes: 46 additions & 0 deletions Libplanet.SDK.Action.Tests/SimpleRPG/Actions/AvatarAction.cs
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(Text args)
{
string name = args;
Call<InfoAction, Info>(nameof(InfoAction.Create), new object?[] { name });
Call<InventoryAction, Inventory>(nameof(InventoryAction.Create));
}

[Callable]
public Avatar GetAvatar(Address address)
{
Info info = Call<InfoAction, Info>(
nameof(InfoAction.GetInfo),
new object?[] { address });
Inventory inventory = Call<InventoryAction, Inventory>(
nameof(InventoryAction.GetInventory),
new object?[] { address });
return new Avatar(info, inventory);
}

[Callable]
public void SetAvatar(Address address, Avatar avatar)
{
Call<InfoAction>(
nameof(InfoAction.SetInfo),
new object?[] { address, avatar.Info });
Call<InventoryAction>(
nameof(InventoryAction.SetInventory),
new object?[] { address, avatar.Inventory });
}
}
}
31 changes: 31 additions & 0 deletions Libplanet.SDK.Action.Tests/SimpleRPG/Actions/FarmAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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()
{
Avatar avatar = Call<AvatarAction, Avatar>(
nameof(AvatarAction.GetAvatar),
new object?[] { Signer });
avatar.Info.AddExp(ExpPerFarm);
avatar.Inventory.AddGold(GoldPerFarm);
Call<AvatarAction>(
nameof(AvatarAction.SetAvatar),
new object?[] { Signer, avatar });
}
}
}
33 changes: 33 additions & 0 deletions Libplanet.SDK.Action.Tests/SimpleRPG/Actions/InfoAction.cs
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 Libplanet.SDK.Action.Tests/SimpleRPG/Actions/InventoryAction.cs
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);
}
}
14 changes: 14 additions & 0 deletions Libplanet.SDK.Action.Tests/SimpleRPG/Models/Avatar.cs
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; }

Check warning on line 6 in Libplanet.SDK.Action.Tests/SimpleRPG/Models/Avatar.cs

View workflow job for this annotation

GitHub Actions / docs

Elements should be separated by blank line

public Avatar(Info info, Inventory inventory)

Check warning on line 8 in Libplanet.SDK.Action.Tests/SimpleRPG/Models/Avatar.cs

View workflow job for this annotation

GitHub Actions / docs

A constructor should not follow a property
{
Info = info;
Inventory = inventory;
}
}
}
38 changes: 38 additions & 0 deletions Libplanet.SDK.Action.Tests/SimpleRPG/Models/Info.cs
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);

Check warning on line 11 in Libplanet.SDK.Action.Tests/SimpleRPG/Models/Info.cs

View workflow job for this annotation

GitHub Actions / docs

Statement should not use unnecessary parenthesis

public Info(string name)

Check warning on line 13 in Libplanet.SDK.Action.Tests/SimpleRPG/Models/Info.cs

View workflow job for this annotation

GitHub Actions / docs

A constructor should not follow a property
: 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;
}
}
}
31 changes: 31 additions & 0 deletions Libplanet.SDK.Action.Tests/SimpleRPG/Models/Inventory.cs
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()

Check warning on line 9 in Libplanet.SDK.Action.Tests/SimpleRPG/Models/Inventory.cs

View workflow job for this annotation

GitHub Actions / docs

A constructor should not follow a property
: 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;
}
}
}
Loading
Loading