Skip to content

Commit

Permalink
Throw exception when action type is not annotated
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Mar 22, 2019
1 parent 0cf9e8c commit 9ecf915
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
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
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
@@ -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
var 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
@@ -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)
{
}
}
}

0 comments on commit 9ecf915

Please sign in to comment.