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

Restrict actions and states types #552

Merged
merged 1 commit into from
Oct 15, 2019
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
11 changes: 10 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ To be released.
disposed to clean up its internal resources. [[#485]]
- `IStore.IterateStateReferences()` method became to receive
`highestIndex`, `lowestIndex`, and `limit` parameters. [[#447], [#545]]
- Reworked `BlockChain<T>.GetStates()` into `GetState()` which takes only one `Address` instead of `IEnumerable<Address>`. [[#510], [#563]]
- Reworked `BlockChain<T>.GetStates()` into `GetState()` which takes only
one `Address` instead of `IEnumerable<Address>`. [[#510], [#563]]
- Types of `IAction.PlainValue` and states became restricted to
`Bencodex.Types.IValue`. [[#541], [#552]]
moreal marked this conversation as resolved.
Show resolved Hide resolved
- `IAction.LoadPlainValue(IImmutableDictionary<string, object>)` method
became replaced by `LoadPlainValue(IValue)`.
- `AccountStateGetter` became to return `IValue`, not `object`.
- Added `BencodexExtension` static class.

### Added interfaces

Expand Down Expand Up @@ -48,8 +55,10 @@ To be released.
[#485]: https://github.com/planetarium/libplanet/pull/485
[#510]: https://github.com/planetarium/libplanet/issues/510
[#528]: https://github.com/planetarium/libplanet/issues/528
[#541]: https://github.com/planetarium/libplanet/issues/541
[#545]: https://github.com/planetarium/libplanet/pull/545
[#550]: https://github.com/planetarium/libplanet/issues/550
[#552]: https://github.com/planetarium/libplanet/pull/552
[#555]: https://github.com/planetarium/libplanet/issues/555
[#558]: https://github.com/planetarium/libplanet/pull/558
[#560]: https://github.com/planetarium/libplanet/pull/560
Expand Down
43 changes: 22 additions & 21 deletions Libplanet.Tests/Action/AccountStateDeltaImplTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Xunit;
Expand All @@ -9,7 +10,7 @@ namespace Libplanet.Tests.Action
public class AccountStateDeltaImplTest
{
private readonly Address[] _addr;
private readonly IImmutableDictionary<Address, object> _states;
private readonly IImmutableDictionary<Address, IValue> _states;

public AccountStateDeltaImplTest()
{
Expand All @@ -22,10 +23,10 @@ public AccountStateDeltaImplTest()
Addr(),
};

_states = new Dictionary<Address, object>()
_states = new Dictionary<Address, IValue>
{
[_addr[0]] = "a",
[_addr[1]] = "b",
[_addr[0]] = (Text)"a",
[_addr[1]] = (Text)"b",
}.ToImmutableDictionary();
}

Expand All @@ -34,20 +35,20 @@ public void CreateNullDelta()
{
IAccountStateDelta delta = new AccountStateDeltaImpl(GetState);
Assert.Empty(delta.UpdatedAddresses);
Assert.Equal("a", delta.GetState(_addr[0]));
Assert.Equal("b", delta.GetState(_addr[1]));
Assert.Equal("a", (Text)delta.GetState(_addr[0]));
Assert.Equal("b", (Text)delta.GetState(_addr[1]));
Assert.Null(delta.GetState(_addr[2]));
}

[Fact]
public void GetSetState()
{
IAccountStateDelta init = new AccountStateDeltaImpl(GetState);
IAccountStateDelta a = init.SetState(_addr[0], "A");
Assert.Equal("A", a.GetState(_addr[0]));
Assert.Equal("a", init.GetState(_addr[0]));
Assert.Equal("b", a.GetState(_addr[1]));
Assert.Equal("b", init.GetState(_addr[1]));
IAccountStateDelta a = init.SetState(_addr[0], (Text)"A");
Assert.Equal("A", (Text)a.GetState(_addr[0]));
Assert.Equal("a", (Text)init.GetState(_addr[0]));
Assert.Equal("b", (Text)a.GetState(_addr[1]));
Assert.Equal("b", (Text)init.GetState(_addr[1]));
Assert.Null(a.GetState(_addr[2]));
Assert.Null(init.GetState(_addr[2]));
Assert.Equal(
Expand All @@ -56,12 +57,12 @@ public void GetSetState()
);
Assert.Empty(init.UpdatedAddresses);

IAccountStateDelta b = a.SetState(_addr[0], "z");
Assert.Equal("z", b.GetState(_addr[0]));
Assert.Equal("A", a.GetState(_addr[0]));
Assert.Equal("a", init.GetState(_addr[0]));
Assert.Equal("b", b.GetState(_addr[1]));
Assert.Equal("b", a.GetState(_addr[1]));
IAccountStateDelta b = a.SetState(_addr[0], (Text)"z");
Assert.Equal("z", (Text)b.GetState(_addr[0]));
Assert.Equal("A", (Text)a.GetState(_addr[0]));
Assert.Equal("a", (Text)init.GetState(_addr[0]));
Assert.Equal("b", (Text)b.GetState(_addr[1]));
Assert.Equal("b", (Text)a.GetState(_addr[1]));
Assert.Null(b.GetState(_addr[2]));
Assert.Null(a.GetState(_addr[2]));
Assert.Equal(
Expand All @@ -70,13 +71,13 @@ public void GetSetState()
);
Assert.Empty(init.UpdatedAddresses);

IAccountStateDelta c = b.SetState(_addr[0], "a");
Assert.Equal("a", c.GetState(_addr[0]));
Assert.Equal("z", b.GetState(_addr[0]));
IAccountStateDelta c = b.SetState(_addr[0], (Text)"a");
Assert.Equal("a", (Text)c.GetState(_addr[0]));
Assert.Equal("z", (Text)b.GetState(_addr[0]));
Assert.Empty(init.UpdatedAddresses);
}

private object GetState(Address address)
private IValue GetState(Address address)
{
try
{
Expand Down
5 changes: 3 additions & 2 deletions Libplanet.Tests/Action/ActionContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Xunit;

Expand Down Expand Up @@ -104,12 +105,12 @@ private class DumbAccountStateDelta : IAccountStateDelta
public IImmutableSet<Address> UpdatedAddresses =>
ImmutableHashSet<Address>.Empty;

public object GetState(Address address)
public IValue GetState(Address address)
{
return null;
}

public IAccountStateDelta SetState(Address address, object state)
public IAccountStateDelta SetState(Address address, IValue state)
{
return this;
}
Expand Down
5 changes: 3 additions & 2 deletions Libplanet.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Tests.Common.Action;
Expand All @@ -22,7 +23,7 @@ public void Constructor()
false
),
new AccountStateDeltaImpl(
a => a.Equals(address) ? "item" : null
a => a.Equals(address) ? (Text)"item" : null
)
);
var action = (DumbAction)evaluation.Action;
Expand All @@ -36,7 +37,7 @@ public void Constructor()
evaluation.InputContext.PreviousStates.GetState(address)
);
Assert.Equal(
"item",
(Text)"item",
evaluation.OutputStates.GetState(address)
);
}
Expand Down
52 changes: 25 additions & 27 deletions Libplanet.Tests/Action/PolymorphicActionTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Tests.Common.Action;
using Libplanet.Tx;
using Xunit;

namespace Libplanet.Tests.Action
Expand All @@ -23,19 +22,16 @@ public void PlainValue()
}
);
Assert.Equal(
new Dictionary<string, object>
new Dictionary(new Dictionary<IKey, IValue>
{
{ "type_id", "attack" },
[(Text)"type_id"] = (Text)"attack",
[(Text)"values"] = new Dictionary(new Dictionary<IKey, IValue>
{
"values",
new Dictionary<string, object>
{
{ "weapon", "frying pan" },
{ "target", "mosquito" },
{ "target_address", addr.ToByteArray() },
}.ToImmutableDictionary()
},
}.ToImmutableDictionary(),
[(Text)"weapon"] = (Text)"frying pan",
[(Text)"target"] = (Text)"mosquito",
[(Text)"target_address"] = new Binary(addr.ToByteArray()),
}),
}),
pa.PlainValue
);
}
Expand All @@ -48,19 +44,16 @@ public void LoadPlainValue()
var pa = new PolymorphicAction<BaseAction>();
#pragma warning restore 612
pa.LoadPlainValue(
new Dictionary<string, object>
new Dictionary(new Dictionary<IKey, IValue>
{
{ "type_id", "attack" },
[(Text)"type_id"] = (Text)"attack",
[(Text)"values"] = new Dictionary(new Dictionary<IKey, IValue>
{
"values",
new Dictionary<string, object>
{
{ "weapon", "frying pan" },
{ "target", "mosquito" },
{ "target_address", addr.ToByteArray() },
}.ToImmutableDictionary()
},
}.ToImmutableDictionary()
[(Text)"weapon"] = (Text)"frying pan",
[(Text)"target"] = (Text)"mosquito",
[(Text)"target_address"] = new Binary(addr.ToByteArray()),
}),
})
);

Assert.IsType<Attack>(pa.InnerAction);
Expand Down Expand Up @@ -100,14 +93,19 @@ public ActionNotAttributeAnnotated()
{
}

public IImmutableDictionary<string, object> PlainValue =>
new Dictionary<string, object>().ToImmutableDictionary();
public IValue PlainValue =>
default(Dictionary);

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

public void LoadPlainValue(IValue plainValue)
{
LoadPlainValue((Dictionary)plainValue);
}

public IAccountStateDelta Execute(IActionContext context)
{
return context.PreviousStates;
Expand Down
Loading