Skip to content

Commit

Permalink
test: Add test code for ValidatorDelegationSetCtrl.
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Apr 2, 2024
1 parent 69f72d1 commit 127e412
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions .Lib9c.Tests/Action/DPoS/Control/ValidatorDelegationSetCtrlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
namespace Lib9c.Tests.Action.DPoS.Control
{
using System.Collections.Immutable;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action.DPoS.Control;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Module;
using Xunit;

public class ValidatorDelegationSetCtrlTest : PoSTest
{
private readonly PublicKey _operatorPublicKey;
private readonly Address _operatorAddress;
private readonly Address _delegatorAddress;
private readonly Address _validatorAddress;
private ImmutableHashSet<Currency> _nativeTokens;
private IWorld _states;

public ValidatorDelegationSetCtrlTest()
: base()
{
_operatorPublicKey = new PrivateKey().PublicKey;
_operatorAddress = _operatorPublicKey.Address;
_validatorAddress = Validator.DeriveAddress(_operatorAddress);
_delegatorAddress = CreateAddress();
_nativeTokens = ImmutableHashSet.Create(
Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share);
_states = InitializeStates();
_states = _states.MintAsset(
context: new ActionContext
{
PreviousState = _states,
},
recipient: _operatorAddress,
value: Asset.GovernanceToken * 100000);
_states = _states.MintAsset(
context: new ActionContext
{
PreviousState = _states,
},
recipient: _delegatorAddress,
value: Asset.GovernanceToken * 100000);

_states = ValidatorCtrl.Create(
states: _states,
ctx: new ActionContext
{
PreviousState = _states,
},
operatorAddress: _operatorAddress,
operatorPublicKey: _operatorPublicKey,
Asset.GovernanceToken * 10,
nativeTokens: _nativeTokens
);
}

[Fact]
public void PromoteTest()
{
var validatorDelegationSet = ValidatorDelegationSetCtrl.GetValidatorDelegationSet(
_states,
_validatorAddress
);
Assert.NotNull(validatorDelegationSet);
Assert.Single(validatorDelegationSet.Set);
var delegation = DelegateCtrl.GetDelegation(
states: _states,
delegationAddress: validatorDelegationSet.Set[0]
);
Assert.NotNull(delegation);
Assert.Equal(_validatorAddress, delegation.ValidatorAddress);
Assert.Equal(_operatorAddress, delegation.DelegatorAddress);
}

[Fact]
public void DelegateTest()
{
_states = DelegateCtrl.Execute(
states: _states,
ctx: new ActionContext
{
PreviousState = _states,
},
delegatorAddress: _delegatorAddress,
validatorAddress: _validatorAddress,
Asset.GovernanceToken * 10,
nativeTokens: _nativeTokens
);
var validatorDelegationSet = ValidatorDelegationSetCtrl.GetValidatorDelegationSet(
_states,
_validatorAddress
);
Assert.NotNull(validatorDelegationSet);
Assert.Equal(2, validatorDelegationSet.Set.Count);
var delegation = DelegateCtrl.GetDelegation(
states: _states,
delegationAddress: validatorDelegationSet.Set[1]
);
Assert.NotNull(delegation);
Assert.Equal(_validatorAddress, delegation.ValidatorAddress);
Assert.Equal(_delegatorAddress, delegation.DelegatorAddress);
}

[Fact]
public void UndelegateTest()
{
_states = DelegateCtrl.Execute(
states: _states,
ctx: new ActionContext
{
PreviousState = _states,
},
delegatorAddress: _delegatorAddress,
validatorAddress: _validatorAddress,
Asset.GovernanceToken * 10,
nativeTokens: _nativeTokens
);
var validatorDelegationSet1 = ValidatorDelegationSetCtrl.GetValidatorDelegationSet(
_states,
_validatorAddress
);
Assert.NotNull(validatorDelegationSet1);
Assert.Equal(2, validatorDelegationSet1.Set.Count);
_states = UndelegateCtrl.Execute(
states: _states,
ctx: new ActionContext
{
PreviousState = _states,
},
delegatorAddress: _delegatorAddress,
validatorAddress: _validatorAddress,
Asset.Share * 10,
nativeTokens: _nativeTokens
);
var validatorDelegationSet2 = ValidatorDelegationSetCtrl.GetValidatorDelegationSet(
_states,
_validatorAddress
);
Assert.NotNull(validatorDelegationSet2);
Assert.Single(validatorDelegationSet2.Set);
}
}
}

0 comments on commit 127e412

Please sign in to comment.