Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor state ncg #2631

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions .Lib9c.Tests/Action/DPoS/CancelUndelegationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Action.DPoS.Sys;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Xunit;

public class CancelUndelegationTest : PoSTest
Expand All @@ -21,22 +23,24 @@ public void Execute()
var validatorAddress =
Nekoyume.Action.DPoS.Model.Validator.DeriveAddress(validatorOperatorAddress);
var delegatorAddress = delegatorPrivateKey.Address;
var states = InitializeStates();
var states = InitialState;

// Prepare initial governance token for the delegator and validator
states = states.MintAsset(
new ActionContext { PreviousState = states },
states = states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
validatorOperatorAddress,
Asset.GovernanceToken * 100);
states = states.MintAsset(
new ActionContext { PreviousState = states },
GovernanceToken * 100);
states = states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
delegatorAddress,
Asset.GovernanceToken * 50);
GovernanceToken * 50);

// Promote the validator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: Asset.GovernanceToken * 100)
amount: GovernanceToken * 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -45,7 +49,7 @@ public void Execute()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the delegator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, Asset.GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down Expand Up @@ -82,22 +86,24 @@ public void CancelAll()
var validatorAddress =
Nekoyume.Action.DPoS.Model.Validator.DeriveAddress(validatorOperatorAddress);
var delegatorAddress = delegatorPrivateKey.Address;
var states = InitializeStates();
var states = InitialState;

// Prepare initial governance token for the delegator and validator
states = states.MintAsset(
new ActionContext { PreviousState = states },
states = states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
validatorOperatorAddress,
Asset.GovernanceToken * 100);
states = states.MintAsset(
new ActionContext { PreviousState = states },
GovernanceToken * 100);
states = states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
delegatorAddress,
Asset.GovernanceToken * 50);
GovernanceToken * 50);

// Promote the validator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: Asset.GovernanceToken * 100)
amount: GovernanceToken * 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -106,7 +112,7 @@ public void CancelAll()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the delegator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, Asset.GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down
65 changes: 29 additions & 36 deletions .Lib9c.Tests/Action/DPoS/Control/DelegateCtrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Lib9c.Tests.Action.DPoS.Control
using Nekoyume.Action.DPoS.Exception;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Xunit;

Expand All @@ -26,9 +27,8 @@ public DelegateCtrlTest()
_operatorAddress = _operatorPublicKey.Address;
_delegatorAddress = CreateAddress();
_validatorAddress = Validator.DeriveAddress(_operatorAddress);
_nativeTokens = ImmutableHashSet.Create(
Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share);
_states = InitializeStates();
_states = InitialState;
_nativeTokens = NativeTokens;
}

[Fact]
Expand All @@ -42,7 +42,7 @@ public void InvalidCurrencyTest()
BlockIndex = 1,
},
_delegatorAddress,
Asset.ConsensusFromGovernance(50));
Asset.ConvertTokens(GovernanceToken * 50, Asset.ConsensusToken));
Assert.Throws<InvalidCurrencyException>(
() => _states = DelegateCtrl.Execute(
_states,
Expand All @@ -53,7 +53,7 @@ public void InvalidCurrencyTest()
},
_delegatorAddress,
_validatorAddress,
Asset.ConsensusFromGovernance(30),
Asset.ConvertTokens(GovernanceToken * 30, Asset.ConsensusToken),
_nativeTokens));
}

Expand All @@ -71,7 +71,7 @@ public void InvalidValidatorTest()
},
_delegatorAddress,
CreateAddress(),
Asset.GovernanceToken * 10,
GovernanceToken * 10,
_nativeTokens));
}

Expand All @@ -86,7 +86,7 @@ public void InvalidShareTest()
BlockIndex = 1,
},
_validatorAddress,
Asset.ConsensusFromGovernance(100));
Asset.ConvertTokens(PoSTest.GovernanceToken * 100, Asset.ConsensusToken));
Assert.Throws<InvalidExchangeRateException>(
() => _states = DelegateCtrl.Execute(
_states,
Expand All @@ -97,7 +97,7 @@ public void InvalidShareTest()
},
_delegatorAddress,
_validatorAddress,
Asset.GovernanceToken * 10,
GovernanceToken * 10,
_nativeTokens));
}

Expand All @@ -120,16 +120,16 @@ public void BalanceTest(
},
_delegatorAddress,
_validatorAddress,
Asset.GovernanceToken * delegateAmount,
GovernanceToken * delegateAmount,
_nativeTokens);
Assert.Equal(
Asset.GovernanceToken * 0,
_states.GetBalance(_validatorAddress, Asset.GovernanceToken));
GovernanceToken * 0,
_states.GetBalance(_validatorAddress, GovernanceToken));
Assert.Equal(
Asset.ConsensusFromGovernance(0),
Asset.ConvertTokens(GovernanceToken * 0, Asset.ConsensusToken),
_states.GetBalance(_operatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusFromGovernance(0),
Asset.ConvertTokens(GovernanceToken * 0, Asset.ConsensusToken),
_states.GetBalance(_delegatorAddress, Asset.ConsensusToken));
Assert.Equal(
ShareFromGovernance(0),
Expand All @@ -138,17 +138,17 @@ public void BalanceTest(
ShareFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.Share));
Assert.Equal(
Asset.ConsensusFromGovernance(selfDelegateAmount + delegateAmount),
Asset.ConvertTokens(GovernanceToken * (selfDelegateAmount + delegateAmount), Asset.ConsensusToken),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.GovernanceToken * (operatorMintAmount - selfDelegateAmount),
_states.GetBalance(_operatorAddress, Asset.GovernanceToken));
GovernanceToken * (operatorMintAmount - selfDelegateAmount),
_states.GetBalance(_operatorAddress, GovernanceToken));
Assert.Equal(
Asset.GovernanceToken * (delegatorMintAmount - delegateAmount),
_states.GetBalance(_delegatorAddress, Asset.GovernanceToken));
GovernanceToken * (delegatorMintAmount - delegateAmount),
_states.GetBalance(_delegatorAddress, GovernanceToken));
Assert.Equal(
Asset.GovernanceToken * (selfDelegateAmount + delegateAmount),
_states.GetBalance(ReservedAddress.UnbondedPool, Asset.GovernanceToken));
GovernanceToken * (selfDelegateAmount + delegateAmount),
_states.GetBalance(ReservedAddress.UnbondedPool, GovernanceToken));
var balanceA = _states.GetBalance(
Delegation.DeriveAddress(_operatorAddress, _validatorAddress),
Asset.Share);
Expand All @@ -163,23 +163,16 @@ public void BalanceTest(
private void Initialize(
int operatorMintAmount, int delegatorMintAmount, int selfDelegateAmount)
{
_states = InitializeStates();
_states = _states.MintAsset(
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_states = _states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
_operatorAddress,
Asset.GovernanceToken * operatorMintAmount);
_states = _states.MintAsset(
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
GovernanceToken * operatorMintAmount);
_states = _states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
_delegatorAddress,
Asset.GovernanceToken * delegatorMintAmount);
GovernanceToken * delegatorMintAmount);
_states = ValidatorCtrl.Create(
_states,
new ActionContext
Expand All @@ -189,7 +182,7 @@ private void Initialize(
},
_operatorAddress,
_operatorPublicKey,
Asset.GovernanceToken * selfDelegateAmount,
GovernanceToken * selfDelegateAmount,
_nativeTokens);
}
}
Expand Down
13 changes: 6 additions & 7 deletions .Lib9c.Tests/Action/DPoS/Control/EvidenceCtrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class EvidenceCtrlTest : PoSTest
private readonly Address _operatorAddress;
private readonly Address _delegatorAddress;
private readonly Address _validatorAddress;
private readonly FungibleAssetValue _governanceToken
= new FungibleAssetValue(Asset.GovernanceToken, 100, 0);
private readonly FungibleAssetValue _governanceTokens = GovernanceToken * 100;

private IWorld _states;

Expand All @@ -31,13 +30,13 @@ public EvidenceCtrlTest()
_operatorAddress = _operatorPublicKey.Address;
_delegatorAddress = CreateAddress();
_validatorAddress = Validator.DeriveAddress(_operatorAddress);
_states = InitializeStates();
_states = InitialState;
}

[Fact]
public void Execute_Test()
{
var governanceToken = _governanceToken;
var governanceTokens = _governanceTokens;
var states = _states;
var operatorPublicKey = _operatorPublicKey;
var validatorAddress = _validatorAddress;
Expand All @@ -46,7 +45,7 @@ public void Execute_Test()
states: states,
blockIndex: 1,
operatorPublicKey: operatorPublicKey,
ncg: governanceToken);
ncg: governanceTokens);
states = Update(
states: states,
blockIndex: 1);
Expand Down Expand Up @@ -80,7 +79,7 @@ public void Execute_Test()
[Fact]
public void Execute_MaxAge_Test()
{
var governanceToken = _governanceToken;
var governanceTokens = _governanceTokens;
var states = _states;
var operatorPublicKey = _operatorPublicKey;
var validatorAddress = _validatorAddress;
Expand All @@ -90,7 +89,7 @@ public void Execute_MaxAge_Test()
states: states,
blockIndex: blockIndex,
operatorPublicKey: operatorPublicKey,
ncg: governanceToken);
ncg: governanceTokens);
states = Update(
states: states,
blockIndex: blockIndex);
Expand Down
Loading
Loading