Skip to content

Commit

Permalink
test: add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 5, 2024
1 parent 9953037 commit eeaebaa
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions .Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit eeaebaa

Please sign in to comment.