From 63a3d2059d442b43b3fdb082851e5fe1ced6adc9 Mon Sep 17 00:00:00 2001 From: Chanhyuck Ko Date: Fri, 5 Apr 2024 15:59:47 +0900 Subject: [PATCH] test: add regression test --- .../Action/DPoS/Sys/UpdateValidatorsTest.cs | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 .Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs diff --git a/.Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs b/.Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs new file mode 100644 index 0000000000..8b5abaf6c4 --- /dev/null +++ b/.Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs @@ -0,0 +1,92 @@ +namespace Lib9c.Tests.Action.DPoS.Sys +{ + using System.Linq; + using Libplanet.Action.State; + using Libplanet.Crypto; + using Libplanet.Types.Assets; + using Nekoyume.Action.DPoS; + using Nekoyume.Action.DPoS.Control; + using Nekoyume.Action.DPoS.Misc; + using Nekoyume.Action.DPoS.Model; + using Nekoyume.Action.DPoS.Sys; + using Nekoyume.Module; + using Xunit; + + public class UpdateValidatorsTest : PoSTest + { + [Fact] + public void Execute() + { + // Prepare initial state. + IWorld initialState = new World(new MockWorldState()); + const int count = 4; + var validatorKeys = Enumerable.Range(0, count).Select(_ => new PrivateKey().PublicKey).ToArray(); + initialState = validatorKeys.Aggregate( + initialState, + (current, key) => current.MintAsset( + new ActionContext(), + key.Address, + new FungibleAssetValue(Asset.GovernanceToken, 1, 0))); + foreach (var key in validatorKeys) + { + Assert.Equal(1, initialState.GetBalance(key.Address, Asset.GovernanceToken).MajorUnit); + Assert.Equal(0, initialState.GetBalance(key.Address, Asset.GovernanceToken).MinorUnit); + } + + // Stake 1 for each validator. + foreach (var key in validatorKeys) + { + initialState = new PromoteValidator( + key, + new FungibleAssetValue(Asset.GovernanceToken, 1, 0)).Execute( + new ActionContext + { + PreviousState = initialState, + Signer = key.Address, + }); + } + + Assert.Equal(0, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); + Assert.Equal(0, initialState.GetValidatorSet().TotalCount); + + // Execute the action. + initialState = new UpdateValidators().Execute( + new ActionContext + { + PreviousState = initialState, + LastCommit = null, + }); + + Assert.Equal(count, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); + Assert.Equal(count, initialState.GetValidatorSet().TotalCount); + Assert.Equal( + validatorKeys.ToHashSet(), + initialState.GetValidatorSet() + .Validators.Select(validator => validator.PublicKey) + .ToHashSet()); + + initialState = new Undelegate( + Validator.DeriveAddress(validatorKeys[0].Address), + new FungibleAssetValue(Asset.Share, 1, 0)).Execute( + new ActionContext + { + PreviousState = initialState, + Signer = validatorKeys[0].Address, + }); + + Assert.Equal(count, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); + Assert.Equal(count, initialState.GetValidatorSet().TotalCount); + + // Execute the action. + initialState = new UpdateValidators().Execute( + new ActionContext + { + PreviousState = initialState, + LastCommit = null, + }); + + Assert.Equal(count - 1, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); + Assert.Equal(count - 1, initialState.GetValidatorSet().TotalCount); + } + } +}