|
| 1 | +namespace Lib9c.Tests.Action.DPoS.Sys |
| 2 | +{ |
| 3 | + using System.Linq; |
| 4 | + using Libplanet.Action.State; |
| 5 | + using Libplanet.Crypto; |
| 6 | + using Libplanet.Types.Assets; |
| 7 | + using Nekoyume.Action.DPoS; |
| 8 | + using Nekoyume.Action.DPoS.Control; |
| 9 | + using Nekoyume.Action.DPoS.Misc; |
| 10 | + using Nekoyume.Action.DPoS.Model; |
| 11 | + using Nekoyume.Action.DPoS.Sys; |
| 12 | + using Nekoyume.Module; |
| 13 | + using Xunit; |
| 14 | + |
| 15 | + public class UpdateValidatorsTest : PoSTest |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void Execute() |
| 19 | + { |
| 20 | + // Prepare initial state. |
| 21 | + IWorld initialState = new World(new MockWorldState()); |
| 22 | + const int count = 4; |
| 23 | + var validatorKeys = Enumerable.Range(0, count).Select(_ => new PrivateKey().PublicKey).ToArray(); |
| 24 | + initialState = validatorKeys.Aggregate( |
| 25 | + initialState, |
| 26 | + (current, key) => current.MintAsset( |
| 27 | + new ActionContext(), |
| 28 | + key.Address, |
| 29 | + new FungibleAssetValue(Asset.GovernanceToken, 1, 0))); |
| 30 | + foreach (var key in validatorKeys) |
| 31 | + { |
| 32 | + Assert.Equal(1, initialState.GetBalance(key.Address, Asset.GovernanceToken).MajorUnit); |
| 33 | + Assert.Equal(0, initialState.GetBalance(key.Address, Asset.GovernanceToken).MinorUnit); |
| 34 | + } |
| 35 | + |
| 36 | + // Stake 1 for each validator. |
| 37 | + foreach (var key in validatorKeys) |
| 38 | + { |
| 39 | + initialState = new PromoteValidator( |
| 40 | + key, |
| 41 | + new FungibleAssetValue(Asset.GovernanceToken, 1, 0)).Execute( |
| 42 | + new ActionContext |
| 43 | + { |
| 44 | + PreviousState = initialState, |
| 45 | + Signer = key.Address, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + Assert.Equal(0, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); |
| 50 | + Assert.Equal(0, initialState.GetValidatorSet().TotalCount); |
| 51 | + |
| 52 | + // Execute the action. |
| 53 | + initialState = new UpdateValidators().Execute( |
| 54 | + new ActionContext |
| 55 | + { |
| 56 | + PreviousState = initialState, |
| 57 | + LastCommit = null, |
| 58 | + }); |
| 59 | + |
| 60 | + Assert.Equal(count, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); |
| 61 | + Assert.Equal(count, initialState.GetValidatorSet().TotalCount); |
| 62 | + Assert.Equal( |
| 63 | + validatorKeys.ToHashSet(), |
| 64 | + initialState.GetValidatorSet() |
| 65 | + .Validators.Select(validator => validator.PublicKey) |
| 66 | + .ToHashSet()); |
| 67 | + |
| 68 | + initialState = new Undelegate( |
| 69 | + Validator.DeriveAddress(validatorKeys[0].Address), |
| 70 | + new FungibleAssetValue(Asset.Share, 1, 0)).Execute( |
| 71 | + new ActionContext |
| 72 | + { |
| 73 | + PreviousState = initialState, |
| 74 | + Signer = validatorKeys[0].Address, |
| 75 | + }); |
| 76 | + |
| 77 | + Assert.Equal(count, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); |
| 78 | + Assert.Equal(count, initialState.GetValidatorSet().TotalCount); |
| 79 | + |
| 80 | + // Execute the action. |
| 81 | + initialState = new UpdateValidators().Execute( |
| 82 | + new ActionContext |
| 83 | + { |
| 84 | + PreviousState = initialState, |
| 85 | + LastCommit = null, |
| 86 | + }); |
| 87 | + |
| 88 | + Assert.Equal(count - 1, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); |
| 89 | + Assert.Equal(count - 1, initialState.GetValidatorSet().TotalCount); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments