From 9053d297a7e41f3338711e8e6116289a1fc17c33 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Fri, 6 Sep 2019 13:36:25 +0900 Subject: [PATCH 1/2] Fix GetState of block action to return proper state --- CHANGES.md | 5 ++++ Libplanet.Tests/Blockchain/BlockChainTest.cs | 26 +++++++++++++++++++- Libplanet.Tests/Common/Action/MinerReward.cs | 11 +++++++++ Libplanet/Blockchain/BlockChain.cs | 10 ++------ 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7fc7711a2ad..87dd102f26d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,11 @@ To be released. ### Bug fixes + - Fix bug where `GetState` hadn't return proper state when the block action + is evaluated. [[#500]] + +[#500]: https://github.com/planetarium/libplanet/pull/500 + Version 0.5.2 ------------- diff --git a/Libplanet.Tests/Blockchain/BlockChainTest.cs b/Libplanet.Tests/Blockchain/BlockChainTest.cs index fceb5c2bc05..a173a2f6497 100644 --- a/Libplanet.Tests/Blockchain/BlockChainTest.cs +++ b/Libplanet.Tests/Blockchain/BlockChainTest.cs @@ -368,7 +368,6 @@ public void Append() Assert.Equal(0, blockRenders[0].Context.BlockIndex); Assert.Equal(1, blockRenders[1].Context.BlockIndex); - Assert.Null(blockRenders[0].Context.PreviousStates.GetState(minerAddress)); Assert.Equal(1, blockRenders[0].NextStates.GetState(minerAddress)); Assert.Equal( 1, @@ -450,6 +449,7 @@ public void ExecuteActions() { addresses[2], "baz" }, { addresses[3], "qux" }, { addresses[4], 2 }, + { MinerReward.RewardRecordAddress, $"{addresses[4]},{addresses[4]}" }, }; _blockChain.ExecuteActions(blocks[1], true); @@ -1319,6 +1319,30 @@ public void EvaluateBlockAction() Assert.Equal(2, blockActionEvaluation.OutputStates.GetState(miner)); } + [Fact] + public void BlockActionWithMultipleAddress() + { + var miner1 = _fx.Address1; + var miner2 = _fx.Address2; + var rewardRecordAddress = MinerReward.RewardRecordAddress; + + _blockChain.MineBlock(miner1); + _blockChain.MineBlock(miner1); + _blockChain.MineBlock(miner2); + + AddressStateMap states = _blockChain.GetStates( + new[] { miner1, miner2, MinerReward.RewardRecordAddress }); + + int reward1 = (int)states[miner1]; + int reward2 = (int)states[miner2]; + string rewardRecord = (string)states[rewardRecordAddress]; + + Assert.Equal(2, reward1); + Assert.Equal(1, reward2); + + Assert.Equal($"{miner1},{miner1},{miner2}", rewardRecord); + } + /// /// Builds a fixture that has incomplete states for blocks other /// than the tip, to test GetStates() method's diff --git a/Libplanet.Tests/Common/Action/MinerReward.cs b/Libplanet.Tests/Common/Action/MinerReward.cs index 8e5e1523026..aba1a88965b 100644 --- a/Libplanet.Tests/Common/Action/MinerReward.cs +++ b/Libplanet.Tests/Common/Action/MinerReward.cs @@ -16,6 +16,9 @@ public MinerReward(int reward) Reward = reward; } + public static Address RewardRecordAddress => + new Address("0000000000000000000000000000000000000000"); + public static AsyncLocal> RenderRecords { get; } = new AsyncLocal>(); @@ -36,6 +39,14 @@ public IAccountStateDelta Execute(IActionContext ctx) { IAccountStateDelta states = ctx.PreviousStates; + string rewardRecord = (string)states.GetState(RewardRecordAddress); + + rewardRecord = rewardRecord is null + ? ctx.Miner.ToString() + : $"{rewardRecord},{ctx.Miner}"; + + states = states.SetState(RewardRecordAddress, rewardRecord); + int previousReward = (int?)states?.GetState(ctx.Miner) ?? 0; int reward = previousReward + Reward; diff --git a/Libplanet/Blockchain/BlockChain.cs b/Libplanet/Blockchain/BlockChain.cs index a92e720d113..ccc6b6f9fb1 100644 --- a/Libplanet/Blockchain/BlockChain.cs +++ b/Libplanet/Blockchain/BlockChain.cs @@ -708,16 +708,10 @@ internal ActionEvaluation EvaluateBlockAction( Address miner = block.Miner.GetValueOrDefault(); - var minerState = GetStates(new[] { miner }, block.PreviousHash) - .GetValueOrDefault(miner); - if (lastStates is null) { - lastStates = new AccountStateDeltaImpl(a => minerState); - } - else if (lastStates.GetState(miner) is null) - { - lastStates = lastStates.SetState(miner, minerState); + lastStates = new AccountStateDeltaImpl( + a => GetStates(new[] { a }, block.PreviousHash).GetValueOrDefault(a)); } return ActionEvaluation.EvaluateActionsGradually( From 487ade30203725499e50225893c3d2e2f6e735ad Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Fri, 6 Sep 2019 19:06:22 +0900 Subject: [PATCH 2/2] Reword changelog Co-Authored-By: Hong Minhee --- CHANGES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 87dd102f26d..2a690552ea9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,8 +8,8 @@ To be released. ### Bug fixes - - Fix bug where `GetState` hadn't return proper state when the block action - is evaluated. [[#500]] + - Fix bug where `IAccountStateDelta.GetState()` hadn't returned proper state + when the block action is evaluated. [[#500]] [#500]: https://github.com/planetarium/libplanet/pull/500