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
19 changes: 0 additions & 19 deletions Libplanet.SDK.Action.Tests/BarAction.cs

This file was deleted.

31 changes: 0 additions & 31 deletions Libplanet.SDK.Action.Tests/FooAction.cs

This file was deleted.

92 changes: 38 additions & 54 deletions Libplanet.SDK.Action.Tests/MockActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,60 @@
using System.Collections.Generic;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Evidence;
using Libplanet.Types.Tx;

namespace Libplanet.SDK.Tests;

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

public TxId? TxId
{
get;
}
public Address Signer { get; }

public Address Miner
{
get;
}
public TxId? TxId =>
throw new NotSupportedException();

public long BlockIndex
{
get;
}
public Address Miner { get; }

public int BlockProtocolVersion
{
get;
}
public long BlockIndex =>
throw new NotSupportedException();

public IWorld PreviousState
{
get;
set;
}
public int BlockProtocolVersion =>
throw new NotSupportedException();

public int RandomSeed
{
get;
}
public IWorld PreviousState { get; }

public bool IsPolicyAction
{
get;
}
public int RandomSeed =>
throw new NotSupportedException();

public IReadOnlyList<ITransaction> Txs
{
get;
}
public bool IsPolicyAction =>
throw new NotSupportedException();

public IReadOnlyList<EvidenceBase> Evidence
{
get;
}
public IReadOnlyList<ITransaction> Txs =>
throw new NotSupportedException();

public void UseGas(long gas)
{
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 IRandom GetRandom() =>
throw new NotSupportedException();

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

public long GasLimit() =>
throw new NotSupportedException();
public long GasLimit() =>
throw new NotSupportedException();
}
}
47 changes: 47 additions & 0 deletions Libplanet.SDK.Action.Tests/Sample/Actions/NumberAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.SDK.Action.Attributes;

namespace Libplanet.SDK.Action.Tests.Sample.Actions
{
[ActionType("Number")]
public class NumberAction : ActionBase
{
public override Address StorageAddress =>
new Address("0x1000000000000000000000000000000000000001");

[Executable]
public void Add(IValue args)
{
Integer operand = (Integer)args;
Integer stored = GetState(Signer) is IValue value
? (Integer)value
: new Integer(0);
Call<NumberLogAction>(nameof(NumberLogAction.Add), new object?[] { operand });
SetState(Signer, new Integer(stored.Value + operand.Value));
}

[Executable]
public void Subtract(IValue args)
{
Integer operand = (Integer)args;
Integer stored = GetState(Signer) is IValue value
? (Integer)value
: new Integer(0);
Call<NumberLogAction>(nameof(NumberLogAction.Subtract), new object?[] { operand });
SetState(Signer, new Integer(stored.Value - operand.Value));
}

[Executable]
public void Multiply(IValue args)
{
Integer operand = (Integer)args;
Integer stored = GetState(Signer) is IValue value
? (Integer)value
: new Integer(1);
Call<NumberLogAction>(nameof(NumberLogAction.Multiply), new object?[] { operand });
SetState(Signer, new Integer(stored.Value * operand.Value));
}
}
}
57 changes: 57 additions & 0 deletions Libplanet.SDK.Action.Tests/Sample/Actions/NumberLogAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Bencodex.Types;
using Libplanet.Crypto;
using Libplanet.SDK.Action.Attributes;

namespace Libplanet.SDK.Action.Tests.Sample.Actions
{
public class NumberLogAction : ActionBase
{
public override Address StorageAddress =>
new Address("0x1000000000000000000000000000000000000002");

[Callable]
public void Add(Integer operand)
{
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
Text formatted = operand.Value >= 0
? new Text($"{operand.Value}")
: new Text($"({operand.Value})");
formatted = stored.Value.Length == 0
? new Text(stored.Value + $"{formatted.Value}")
: new Text(stored.Value + $" + {formatted.Value}");
SetState(Signer, formatted);
}

[Callable]
public void Subtract(Integer operand)
{
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
Text formatted = operand.Value >= 0
? new Text($"{operand.Value}")
: new Text($"({operand.Value})");
formatted = stored.Value.Length == 0
? new Text(stored.Value + $"{formatted.Value}")
: new Text(stored.Value + $" - {formatted.Value}");
SetState(Signer, formatted);
}

// This is without Callable attribute on purpose for testing.
public void Multiply(Integer operand)
{
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
Text formatted = operand.Value >= 0
? new Text($"{operand.Value}")
: new Text($"({operand.Value})");
formatted = stored.Value.Length == 0
? new Text($"{formatted.Value}")
: new Text(stored.Value + $" * {formatted.Value}");
SetState(Signer, formatted);
}
}
}
24 changes: 24 additions & 0 deletions Libplanet.SDK.Action.Tests/Sample/Actions/TextAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.SDK.Action.Attributes;

namespace Libplanet.SDK.Action.Tests
{
[ActionType("Text")]
public class TextAction : ActionBase
{
public override Address StorageAddress =>
new Address("0x1000000000000000000000000000000000000003");

[Executable]
public void Append(IValue args)
{
Text operand = (Text)args;
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
SetState(Signer, new Text(stored.Value + operand.Value));
}
}
}
Loading