-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test code for ValidatorDelegationSetCtrl.
- Loading branch information
Showing
1 changed file
with
146 additions
and
0 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
.Lib9c.Tests/Action/DPoS/Control/ValidatorDelegationSetCtrlTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |