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

Throw exception when action type is not annotated #144

Merged
Merged
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ To be released.
from `IImmutableList<Uri>` to `IPEndPoint`.
[[#120], [#123] by Yang Chun Ung, [#126], [#127]]
- Added `IStore.ListNamespaces()` method.
- `Transaction<T>` now throws an `InvalidActionTypeException` if an action type
is not annotated with `ActionTypeAttribute`. [#144]

[#98]: https://github.com/planetarium/libplanet/issues/98
[#99]: https://github.com/planetarium/libplanet/issues/99
Expand All @@ -113,6 +115,7 @@ To be released.
[#132]: https://github.com/planetarium/libplanet/issues/132
[#135]: https://github.com/planetarium/libplanet/pull/135
[#136]: https://github.com/planetarium/libplanet/pull/136
[#144]: https://github.com/planetarium/libplanet/pull/144
[RFC 5389]: https://tools.ietf.org/html/rfc5389
[RFC 5766]: https://tools.ietf.org/html/rfc5766

Expand Down
27 changes: 27 additions & 0 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ public void EvaluateActions()
Assert.Equal(states[TestEvaluateAction.BlockIndexKey], blockIndex);
}

[Fact]
public void DetectInvalidActionType()
{
var privateKey = new PrivateKey();
var action = new ActionNotAttributeAnnotated();

Assert.Throws<InvalidActionTypeException>(
() => Transaction<BaseAction>.Create(
privateKey, new[] { action }));
}

[ActionType("test")]
private class TestEvaluateAction : BaseAction
{
Expand All @@ -303,5 +314,21 @@ public override IAccountStateDelta Execute(IActionContext context)
.SetState(BlockIndexKey, context.BlockIndex);
}
}

private class ActionNotAttributeAnnotated : BaseAction
{
public override IImmutableDictionary<string, object> PlainValue =>
new Dictionary<string, object>().ToImmutableDictionary();

public override void LoadPlainValue(
IImmutableDictionary<string, object> plainValue)
{
}

public override IAccountStateDelta Execute(IActionContext context)
{
return context.PreviousStates;
}
}
}
}
12 changes: 11 additions & 1 deletion Libplanet/Action/ActionTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Reflection;
using Libplanet.Tx;

namespace Libplanet.Action
{
Expand Down Expand Up @@ -40,11 +41,20 @@ public ActionTypeAttribute(string typeIdentifier)
/// <c>null</c>.</returns>
public static string ValueOf(Type actionType)
{
return actionType
string typeIdentifier = actionType
.GetCustomAttributes()
.OfType<ActionTypeAttribute>()
.Select(attr => attr.TypeIdentifier)
.FirstOrDefault();

if (typeIdentifier is null)
{
throw new InvalidActionTypeException(
"Action type should be annotated with ActionTypeAttribute."
);
}

return typeIdentifier;
}
}
}
24 changes: 24 additions & 0 deletions Libplanet/Tx/InvalidActionTypeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Runtime.Serialization;

namespace Libplanet.Tx
{
[Serializable]
public sealed class InvalidActionTypeException : Exception
{
public InvalidActionTypeException()
{
}

public InvalidActionTypeException(string message)
: base(message)
{
}

public InvalidActionTypeException(
string message, Exception innerException)
: base(message, innerException)
{
}
}
}