Skip to content

Commit

Permalink
Apply reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Oct 10, 2019
1 parent 67db3ac commit a79d7da
Show file tree
Hide file tree
Showing 25 changed files with 244 additions and 235 deletions.
9 changes: 7 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +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]]
- types of `IAction.PlainValue` and states were restricted to
- 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` to write more readable code.

### Added interfaces

Expand Down
10 changes: 5 additions & 5 deletions Libplanet.Tests/Action/AccountStateDeltaImplTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public AccountStateDeltaImplTest()

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

Expand All @@ -44,7 +44,7 @@ public void CreateNullDelta()
public void GetSetState()
{
IAccountStateDelta init = new AccountStateDeltaImpl(GetState);
IAccountStateDelta a = init.SetState(_addr[0], "A".ToBencodex());
IAccountStateDelta a = init.SetState(_addr[0], "A".AsText());
Assert.Equal("A", (Text)a.GetState(_addr[0]));
Assert.Equal("a", (Text)init.GetState(_addr[0]));
Assert.Equal("b", (Text)a.GetState(_addr[1]));
Expand All @@ -57,7 +57,7 @@ public void GetSetState()
);
Assert.Empty(init.UpdatedAddresses);

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

IAccountStateDelta c = b.SetState(_addr[0], "a".ToBencodex());
IAccountStateDelta c = b.SetState(_addr[0], "a".AsText());
Assert.Equal("a", (Text)c.GetState(_addr[0]));
Assert.Equal("z", (Text)b.GetState(_addr[0]));
Assert.Empty(init.UpdatedAddresses);
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Constructor()
false
),
new AccountStateDeltaImpl(
a => a.Equals(address) ? "item".ToBencodex() : null
a => a.Equals(address) ? "item".AsText() : null
)
);
var action = (DumbAction)evaluation.Action;
Expand All @@ -36,7 +36,7 @@ public void Constructor()
evaluation.InputContext.PreviousStates.GetState(address)
);
Assert.Equal(
"item".ToBencodex(),
"item".AsText(),
evaluation.OutputStates.GetState(address)
);
}
Expand Down
28 changes: 14 additions & 14 deletions Libplanet.Tests/Action/PolymorphicActionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public void PlainValue()
Assert.Equal(
new Dictionary<IKey, IValue>
{
["type_id".ToBencodex()] = "attack".ToBencodex(),
["values".ToBencodex()] = new Dictionary<IKey, IValue>
["type_id".AsText()] = "attack".AsText(),
["values".AsText()] = new Dictionary<IKey, IValue>
{
["weapon".ToBencodex()] = "frying pan".ToBencodex(),
["target".ToBencodex()] = "mosquito".ToBencodex(),
["target_address".ToBencodex()] = addr.ToByteArray().ToBencodex(),
}.ToBencodex(),
}.ToBencodex(),
["weapon".AsText()] = "frying pan".AsText(),
["target".AsText()] = "mosquito".AsText(),
["target_address".AsText()] = addr.ToByteArray().AsBinary(),
}.AsDictionary(),
}.AsDictionary(),
pa.PlainValue
);
}
Expand All @@ -48,14 +48,14 @@ public void LoadPlainValue()
pa.LoadPlainValue(
new Dictionary<IKey, IValue>
{
["type_id".ToBencodex()] = "attack".ToBencodex(),
["values".ToBencodex()] = new Dictionary<IKey, IValue>
["type_id".AsText()] = "attack".AsText(),
["values".AsText()] = new Dictionary<IKey, IValue>
{
["weapon".ToBencodex()] = "frying pan".ToBencodex(),
["target".ToBencodex()] = "mosquito".ToBencodex(),
["target_address".ToBencodex()] = addr.ToByteArray().ToBencodex(),
}.ToBencodex(),
}.ToBencodex()
["weapon".AsText()] = "frying pan".AsText(),
["target".AsText()] = "mosquito".AsText(),
["target_address".AsText()] = addr.ToByteArray().AsBinary(),
}.AsDictionary(),
}.AsDictionary()
);

Assert.IsType<Attack>(pa.InnerAction);
Expand Down
34 changes: 19 additions & 15 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ public void Append()
var minerAddress = addresses[4];
var blockRenders = MinerReward.RenderRecords.Value;

Assert.Equal(2, _blockChain.GetState(minerAddress)[minerAddress].As<Integer>().Value);
Assert.Equal(
2,
_blockChain.GetState(minerAddress)[minerAddress].As<Integer>().AsInt());
Assert.Equal(2, blockRenders.Count);
Assert.True(blockRenders.All(r => r.Render));
Assert.Equal(0, blockRenders[0].Context.BlockIndex);
Expand Down Expand Up @@ -446,12 +448,12 @@ public void ExecuteActions()

var expectedStates = new Dictionary<Address, IValue>
{
{ addresses[0], "foo".ToBencodex() },
{ addresses[1], "bar".ToBencodex() },
{ addresses[2], "baz".ToBencodex() },
{ addresses[3], "qux".ToBencodex() },
{ addresses[4], 2.ToBencodex() },
{ MinerReward.RewardRecordAddress, $"{addresses[4]},{addresses[4]}".ToBencodex() },
{ addresses[0], "foo".AsText() },
{ addresses[1], "bar".AsText() },
{ addresses[2], "baz".AsText() },
{ addresses[3], "qux".AsText() },
{ addresses[4], 2.AsInteger() },
{ MinerReward.RewardRecordAddress, $"{addresses[4]},{addresses[4]}".AsText() },
};

_blockChain.ExecuteActions(blocks[1], true);
Expand Down Expand Up @@ -889,7 +891,7 @@ public void Swap(bool render)

Assert.Equal(
totalBlockCount,
_blockChain.GetState(minerAddress)[minerAddress].As<Integer>().Value);
_blockChain.GetState(minerAddress)[minerAddress].As<Integer>().AsInt());
Assert.Equal(totalBlockCount, blockRenders.Count);
Assert.True(blockRenders.Take(unRenderBlockCount).All(r => r.Unrender));
Assert.True(blockRenders.Skip(unRenderBlockCount).All(r => r.Render));
Expand Down Expand Up @@ -1174,14 +1176,16 @@ public async void EvaluateActions()
await chain.MineBlock(_fx.Address1);

Assert.Equal(
chain.GetState(TestEvaluateAction.SignerKey)[TestEvaluateAction.SignerKey].ToString(),
chain.GetState(TestEvaluateAction.SignerKey)[TestEvaluateAction.SignerKey]
.ToString(),
fromAddress.ToHex()
);
Assert.Equal(
chain.GetState(TestEvaluateAction.MinerKey)[TestEvaluateAction.MinerKey].ToString(),
_fx.Address1.ToHex());
Assert.Equal(
chain.GetState(TestEvaluateAction.BlockIndexKey)[TestEvaluateAction.BlockIndexKey].As<Integer>().Value,
chain.GetState(TestEvaluateAction.BlockIndexKey)[TestEvaluateAction.BlockIndexKey]
.As<Integer>().AsLong(),
blockIndex);
}

Expand Down Expand Up @@ -1403,8 +1407,8 @@ public async void BlockActionWithMultipleAddress()
AddressStateMap miner2states = _blockChain.GetState(miner2);
AddressStateMap rewardStates = _blockChain.GetState(rewardRecordAddress);

int reward1 = (int)miner1states[miner1].As<Integer>().Value;
int reward2 = (int)miner2states[miner2].As<Integer>().Value;
int reward1 = miner1states[miner1].As<Integer>().AsInt();
int reward2 = miner2states[miner2].As<Integer>().AsInt();
string rewardRecord = rewardStates[rewardRecordAddress].ToString();

Assert.Equal(2, reward1);
Expand Down Expand Up @@ -1688,9 +1692,9 @@ public void LoadPlainValue(
public IAccountStateDelta Execute(IActionContext context)
{
return context.PreviousStates
.SetState(SignerKey, context.Signer.ToHex().ToBencodex())
.SetState(MinerKey, context.Miner.ToHex().ToBencodex())
.SetState(BlockIndexKey, context.BlockIndex.ToBencodex());
.SetState(SignerKey, context.Signer.ToHex().AsText())
.SetState(MinerKey, context.Miner.ToHex().AsText())
.SetState(BlockIndexKey, context.BlockIndex.AsInteger());
}

public void Render(
Expand Down
18 changes: 9 additions & 9 deletions Libplanet.Tests/Blocks/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ DumbAction MakeAction(Address address, char identifier) =>
Assert.Equal(
new Dictionary<Address, IValue>
{
[addresses[0]] = "A".ToBencodex(),
[addresses[1]] = "B".ToBencodex(),
[addresses[2]] = "C".ToBencodex(),
[DumbAction.RandomRecordsAddress] = randomValue.ToBencodex(),
[addresses[0]] = "A".AsText(),
[addresses[1]] = "B".AsText(),
[addresses[2]] = "C".AsText(),
[DumbAction.RandomRecordsAddress] = randomValue.AsInteger(),
}.ToImmutableDictionary(),
dirty1
);
Expand Down Expand Up @@ -362,7 +362,7 @@ DumbAction MakeAction(Address address, char identifier) =>
eval.OutputStates.GetState(
DumbAction.RandomRecordsAddress
),
randomValue.ToBencodex()
randomValue.AsInteger()
);
Assert.Equal(
expect.Item3,
Expand All @@ -378,10 +378,10 @@ DumbAction MakeAction(Address address, char identifier) =>
Assert.Equal(
new Dictionary<Address, IValue>
{
[addresses[0]] = "A,D".ToBencodex(),
[addresses[3]] = "E".ToBencodex(),
[addresses[4]] = "RecordRehearsal:False".ToBencodex(),
[DumbAction.RandomRecordsAddress] = randomValue.ToBencodex(),
[addresses[0]] = "A,D".AsText(),
[addresses[3]] = "E".AsText(),
[addresses[4]] = "RecordRehearsal:False".AsText(),
[DumbAction.RandomRecordsAddress] = randomValue.AsInteger(),
}.ToImmutableDictionary(),
dirty2
);
Expand Down
14 changes: 7 additions & 7 deletions Libplanet.Tests/Common/Action/Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class Attack : BaseAction
public override IValue PlainValue =>
new Dictionary<IKey, IValue>
{
["weapon".ToBencodex()] = Weapon.ToBencodex(),
["target".ToBencodex()] = Target.ToBencodex(),
["target_address".ToBencodex()] = TargetAddress.ToByteArray().ToBencodex(),
}.ToBencodex();
["weapon".AsText()] = Weapon.AsText(),
["target".AsText()] = Target.AsText(),
["target_address".AsText()] = TargetAddress.ToByteArray().AsBinary(),
}.AsDictionary();

public string Weapon { get; set; }

Expand All @@ -30,9 +30,9 @@ public override void LoadPlainValue(

public void LoadPlainValue(Dictionary plainValue)
{
Weapon = plainValue["weapon".ToBencodex()].ToString();
Target = plainValue["target".ToBencodex()].ToString();
TargetAddress = new Address(((Binary)plainValue["target_address".ToBencodex()]).Value);
Weapon = plainValue["weapon".AsText()].ToString();
Target = plainValue["target".AsText()].ToString();
TargetAddress = new Address(((Binary)plainValue["target_address".AsText()]).Value);
}

public override IAccountStateDelta Execute(IActionContext context)
Expand Down
9 changes: 5 additions & 4 deletions Libplanet.Tests/Common/Action/BattleResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Runtime.Serialization;
using Bencodex.Types;
using Libplanet.Serialization;
Expand Down Expand Up @@ -44,10 +45,10 @@ public static BattleResult FromBencodex(Dictionary dictionary)

public Dictionary ToBencodex() => new Dictionary<IKey, IValue>
{
["used_weapons".ToBencodex()] = new List(
UsedWeapons.Select(x => x.ToBencodex().As<IValue>())),
["targets".ToBencodex()] = new List(Targets.Select(x => x.ToBencodex().As<IValue>())),
}.ToBencodex();
["used_weapons".AsText()] = new List(
UsedWeapons.Select(x => x.AsText().As<IValue>())),
["targets".AsText()] = new List(Targets.Select(x => x.AsText().As<IValue>())),
}.AsDictionary();

public override bool Equals(object other)
{
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Tests/Common/Action/DetectRehearsal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class DetectRehearsal : BaseAction
public override IValue PlainValue =>
new Dictionary<IKey, IValue>
{
{ "target_address".ToBencodex(), TargetAddress.ToByteArray().ToBencodex() },
}.ToBencodex();
{ "target_address".AsText(), TargetAddress.ToByteArray().AsBinary() },
}.AsDictionary();

public bool ResultState { get; set; }

Expand All @@ -37,7 +37,7 @@ public override IAccountStateDelta Execute(IActionContext context)
{
IAccountStateDelta previousStates = context.PreviousStates;
ResultState = context.Rehearsal;
return previousStates.SetState(TargetAddress, context.Rehearsal.ToBencodex());
return previousStates.SetState(TargetAddress, context.Rehearsal.AsBoolean());
}
}
}
26 changes: 13 additions & 13 deletions Libplanet.Tests/Common/Action/DumbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ public IValue PlainValue
{
var plainValue = new Dictionary<IKey, IValue>
{
["item".ToBencodex()] = Item.ToBencodex(),
["target_address".ToBencodex()] = TargetAddress.ToByteArray().ToBencodex(),
["record_rehearsal".ToBencodex()] = new Boolean(RecordRehearsal),
}.ToBencodex();
["item".AsText()] = Item.AsText(),
["target_address".AsText()] = TargetAddress.ToByteArray().AsBinary(),
["record_rehearsal".AsText()] = new Boolean(RecordRehearsal),
}.AsDictionary();
if (RecordRandom)
{
// In order to avoid changing tx signatures in many test
// fixtures, adds field only if RecordRandom = true.
plainValue =
(Dictionary)plainValue.Add(
"record_random".ToBencodex(),
true.ToBencodex());
"record_random".AsText(),
true.AsBoolean());
}

if (Idempotent)
{
plainValue =
(Dictionary)plainValue.Add(
"idempotent".ToBencodex(),
Idempotent.ToBencodex());
"idempotent".AsText(),
Idempotent.AsBoolean());
}

return plainValue;
Expand Down Expand Up @@ -121,11 +121,11 @@ public IAccountStateDelta Execute(IActionContext context)
{
states = states.SetState(
RandomRecordsAddress,
context.Random.Next().ToBencodex()
context.Random.Next().AsInteger()
);
}

return states.SetState(TargetAddress, items.ToBencodex());
return states.SetState(TargetAddress, items.AsText());
}

public void Render(
Expand Down Expand Up @@ -177,11 +177,11 @@ Dictionary plainValue
TargetAddress = new Address(plainValue.GetValue<Binary>("target_address").Value);
RecordRehearsal = plainValue.GetValue<Boolean>("record_rehearsal").Value;
RecordRandom =
plainValue.ContainsKey("record_random".ToBencodex()) &&
plainValue["record_random".ToBencodex()] is Boolean r &&
plainValue.ContainsKey("record_random".AsText()) &&
plainValue["record_random".AsText()] is Boolean r &&
r.Value;

if (plainValue.ContainsKey("idempotent".ToBencodex()))
if (plainValue.ContainsKey("idempotent".AsText()))
{
Idempotent = plainValue.GetValue<Boolean>("idempotent").Value;
}
Expand Down
10 changes: 5 additions & 5 deletions Libplanet.Tests/Common/Action/MinerReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static AsyncLocal<ImmutableList<RenderRecord>>
public IValue PlainValue =>
new Dictionary<IKey, IValue>
{
["reward".ToBencodex()] = Reward.ToBencodex(),
}.ToBencodex();
["reward".AsText()] = Reward.AsInteger(),
}.AsDictionary();

public void LoadPlainValue(IValue plainValue)
{
Expand All @@ -51,12 +51,12 @@ public IAccountStateDelta Execute(IActionContext ctx)
? ctx.Miner.ToString()
: $"{rewardRecord},{ctx.Miner}";

states = states.SetState(RewardRecordAddress, rewardRecord.ToBencodex());
states = states.SetState(RewardRecordAddress, rewardRecord.AsText());

int previousReward = (int?)states?.GetState(ctx.Miner)?.As<Integer>().Value ?? 0;
int previousReward = states?.GetState(ctx.Miner)?.As<Integer>().AsInt() ?? 0;
int reward = previousReward + Reward;

return states.SetState(ctx.Miner, reward.ToBencodex());
return states.SetState(ctx.Miner, reward.AsInteger());
}

public void Render(IActionContext context, IAccountStateDelta nextStates)
Expand Down
Loading

0 comments on commit a79d7da

Please sign in to comment.