Skip to content
Permalink
Browse files

Restrict actions and states types

  • Loading branch information
moreal committed Oct 15, 2019
1 parent d37c6c5 commit 958d80398cb4cea9447a63659e69b726444e3c3a
Showing with 575 additions and 443 deletions.
  1. +10 −1 CHANGES.md
  2. +22 −21 Libplanet.Tests/Action/AccountStateDeltaImplTest.cs
  3. +3 −2 Libplanet.Tests/Action/ActionContextTest.cs
  4. +3 −2 Libplanet.Tests/Action/ActionEvaluationTest.cs
  5. +25 −27 Libplanet.Tests/Action/PolymorphicActionTest.cs
  6. +52 −45 Libplanet.Tests/Blockchain/BlockChainTest.cs
  7. +29 −21 Libplanet.Tests/Blocks/BlockTest.cs
  8. +18 −12 Libplanet.Tests/Common/Action/Attack.cs
  9. +3 −2 Libplanet.Tests/Common/Action/BaseAction.cs
  10. +17 −0 Libplanet.Tests/Common/Action/BattleResult.cs
  11. +16 −8 Libplanet.Tests/Common/Action/DetectRehearsal.cs
  12. +35 −19 Libplanet.Tests/Common/Action/DumbAction.cs
  13. +17 −10 Libplanet.Tests/Common/Action/MinerReward.cs
  14. +12 −10 Libplanet.Tests/Common/Action/Sleep.cs
  15. +13 −6 Libplanet.Tests/Common/Action/ThrowException.cs
  16. +16 −9 Libplanet.Tests/Net/Messages/RecentStatesTest.cs
  17. +8 −8 Libplanet.Tests/Net/SwarmTest.cs
  18. +21 −15 Libplanet.Tests/Store/StoreTest.cs
  19. +18 −12 Libplanet.Tests/Tx/TransactionTest.cs
  20. +1 −0 Libplanet.sln.DotSettings
  21. +10 −7 Libplanet/Action/AccountStateDeltaImpl.cs
  22. +3 −1 Libplanet/Action/AccountStateGetter.cs
  23. +23 −22 Libplanet/Action/AddressStateMap.cs
  24. +6 −5 Libplanet/Action/IAccountStateDelta.cs
  25. +49 −57 Libplanet/Action/IAction.cs
  26. +58 −50 Libplanet/Action/PolymorphicAction.cs
  27. +13 −0 Libplanet/BencodexExtension.cs
  28. +3 −2 Libplanet/Blockchain/BlockChain.cs
  29. +12 −16 Libplanet/Net/Messages/RecentStates.cs
  30. +3 −2 Libplanet/Net/Protocols/KademliaProtocol.cs
  31. +3 −2 Libplanet/Net/Swarm.cs
  32. +33 −8 Libplanet/Serialization/BencodexFormatter.cs
  33. +5 −6 Libplanet/Store/LiteDBStore.cs
  34. +11 −27 Libplanet/Tx/RawTransaction.cs
  35. +4 −8 Libplanet/Tx/Transaction.cs
@@ -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]]
- `IAction.LoadPlainValue(IImmutableDictionary<string, object>)` method
became replaced by `LoadPlainValue(IValue)`.
- `AccountStateGetter` became to return `IValue`, not `object`.
- Added `BencodexExtension` static class.

### Added interfaces

@@ -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
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Xunit;
@@ -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()
{
@@ -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();
}

@@ -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(
@@ -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(
@@ -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
{
@@ -1,5 +1,6 @@
using System;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Xunit;

@@ -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;
}
@@ -1,3 +1,4 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Tests.Common.Action;
@@ -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;
@@ -36,7 +37,7 @@ public void Constructor()
evaluation.InputContext.PreviousStates.GetState(address)
);
Assert.Equal(
"item",
(Text)"item",
evaluation.OutputStates.GetState(address)
);
}
@@ -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
@@ -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
);
}
@@ -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);
@@ -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;

0 comments on commit 958d803

Please sign in to comment.
You can’t perform that action at this time.