-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
253 additions
and
22 deletions.
There are no files selected for viewing
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,92 @@ | ||
namespace Lib9c.Tests.Action.DPoS | ||
{ | ||
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.Sys; | ||
using Nekoyume.Module; | ||
using Xunit; | ||
|
||
public class DPoSActionTest | ||
{ | ||
[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 PoSAction().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()); | ||
|
||
// TODO: Add gold distribution tests | ||
initialState = new Undelegate( | ||
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 PoSAction().Execute( | ||
new ActionContext | ||
{ | ||
PreviousState = initialState, | ||
LastCommit = null, | ||
}); | ||
|
||
Assert.Equal(count - 1, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count); | ||
Assert.Equal(count - 1, initialState.GetValidatorSet().TotalCount); | ||
} | ||
} | ||
} |
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
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
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
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
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
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,94 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Numerics; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
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.Util; | ||
using Nekoyume.Module; | ||
using Serilog; | ||
|
||
namespace Nekoyume.Action.DPoS | ||
{ | ||
[ActionType(ActionTypeValue)] | ||
public sealed class InitializeValidators : ActionBase | ||
{ | ||
private const string ActionTypeValue = "initialize_validators"; | ||
|
||
public InitializeValidators(Dictionary<PublicKey, BigInteger> validators) | ||
{ | ||
Validators = validators.ToImmutableDictionary(); | ||
} | ||
|
||
public InitializeValidators() | ||
{ | ||
} | ||
|
||
public ImmutableDictionary<PublicKey, BigInteger> Validators { get; set; } | ||
|
||
public override IValue PlainValue { | ||
get | ||
{ | ||
var validators = Dictionary.Empty; | ||
#pragma warning disable LAA1002 | ||
foreach (var (validator, power) in Validators) | ||
{ | ||
validators = validators.Add((Binary)validator.Serialize(), power); | ||
} | ||
#pragma warning restore LAA1002 | ||
|
||
return Dictionary.Empty | ||
.Add("type_id", new Text(ActionTypeValue)) | ||
.Add("validators", validators); | ||
} | ||
} | ||
|
||
public override void LoadPlainValue(IValue plainValue) | ||
{ | ||
var dict = (Bencodex.Types.Dictionary)plainValue; | ||
var validatorDict = (Dictionary)dict["validators"]; | ||
Validators = validatorDict.Select( | ||
pair => | ||
{ | ||
var key = pair.Key.ToPublicKey(); | ||
var power = (Integer)pair.Value; | ||
return new KeyValuePair<PublicKey, BigInteger>(key, power); | ||
}).ToImmutableDictionary(); | ||
} | ||
|
||
public override IWorld Execute(IActionContext context) | ||
{ | ||
Log.Debug("InitializeValidators"); | ||
context.UseGas(1); | ||
var states = context.PreviousState; | ||
|
||
var nativeTokens = ImmutableHashSet.Create( | ||
Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share); | ||
#pragma warning disable LAA1002 | ||
foreach(var (validator, power) in Validators) | ||
{ | ||
var amount = new FungibleAssetValue(Asset.GovernanceToken, power, 0); | ||
states = states.MintAsset( | ||
context, | ||
validator.Address, | ||
amount); | ||
states = ValidatorCtrl.Create( | ||
states, | ||
context, | ||
validator.Address, | ||
validator, | ||
amount, | ||
nativeTokens); | ||
} | ||
#pragma warning restore LAA1002 | ||
|
||
Log.Debug("InitializeValidators complete"); | ||
return states; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.