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

Active release unbonding #3102

Draft
wants to merge 22 commits into
base: refactor/guild-and-unbonding
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion .Lib9c.Plugin/PluginActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public PluginActionEvaluator(IPluginKeyValueStore keyValueStore)
new UpdateValidators(),
new RecordProposer(),
new RewardGold(),
new ReleaseValidatorUnbondings(),
}.ToImmutableArray(),
beginTxActions: new IAction[] {
new Mortgage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ public void StressTest()

private IWorld Stake(IWorld world, Address agentAddress)
{
var action = new Stake(new BigInteger(500_000));
var action = new Stake(new BigInteger(500_000), TesterAvatarAddress);
var state = action.Execute(
new ActionContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ out var items

private IWorld Stake(IWorld world, Address agentAddress)
{
var action = new Stake(new BigInteger(500_000));
var action = new Stake(new BigInteger(500_000), TesterAvatarAddress);
var state = action.Execute(
new ActionContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void Execute(

private IWorld Stake(IWorld world, Address agentAddress)
{
var action = new Stake(new BigInteger(500_000));
var action = new Stake(new BigInteger(500_000), TesterAvatarAddress);
var state = action.Execute(
new ActionContext
{
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/AdventureBoss/UnlockFloorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Type exc

private IWorld Stake(IWorld world, Address agentAddress)
{
var action = new Stake(new BigInteger(500_000));
var action = new Stake(new BigInteger(500_000), TesterAvatarAddress);
var state = action.Execute(
new ActionContext
{
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/AdventureBoss/WantedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ private IWorld Stake(IWorld world, long amount = 0)
world = DelegationUtil.EnsureValidatorPromotionReady(world, validatorKey, 0L);
world = DelegationUtil.MakeGuild(world, AgentAddress, validatorKey.Address, 0L);

var action = new Stake(new BigInteger(amount));
var action = new Stake(new BigInteger(amount), AvatarAddress);
return action.Execute(
new ActionContext
{
Expand Down
24 changes: 15 additions & 9 deletions .Lib9c.Tests/Action/Scenario/StakeAndClaimScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,26 @@ public void Test()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
LegacyStakeState.RewardInterval,
LegacyStakeState.LockupInterval);
0);

var validatorKey = new PrivateKey().PublicKey;
state = DelegationUtil.EnsureValidatorPromotionReady(state, validatorKey, 0L);
state = DelegationUtil.MakeGuild(state, _agentAddr, validatorKey.Address, 0L);

var withdrawHeight = stake2BlockIndex + LegacyStakeState.LockupInterval + 1;
// Withdraw stake via stake3.
state = Stake3(state, _agentAddr, 0, stake2BlockIndex + LegacyStakeState.LockupInterval + 1);
state = Stake3(state, _agentAddr, _avatarAddr, 0, withdrawHeight);

state = DelegationUtil.EnsureStakeReleased(state, stake2BlockIndex + ValidatorDelegatee.ValidatorUnbondingPeriod);
var unbondedHeight = withdrawHeight + ValidatorDelegatee.ValidatorUnbondingPeriod;
state = DelegationUtil.EnsureUnbondedClaimed(
state, _agentAddr, unbondedHeight);

// Stake 50 NCG via stake3 before patching.
const long firstStake3BlockIndex = stake2BlockIndex + LegacyStakeState.LockupInterval + 1;
var firstStake3BlockIndex = unbondedHeight;
state = Stake3(
state,
_agentAddr,
_avatarAddr,
stakedAmount,
firstStake3BlockIndex);

Expand All @@ -135,7 +139,7 @@ public void Test()
"StakeRegularFixedRewardSheet_V2",
"StakeRegularRewardSheet_V2",
50_400,
201_600);
75_600);

// Patch StakePolicySheet and so on.
state = state.SetLegacyState(
Expand All @@ -146,6 +150,7 @@ public void Test()
state = Stake3(
state,
_agentAddr,
_avatarAddr,
stakedAmount,
firstStake3BlockIndex + 1);

Expand All @@ -158,7 +163,7 @@ public void Test()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
40,
150);
75_600);
}

private static IWorld MintAsset(
Expand Down Expand Up @@ -197,10 +202,11 @@ private static IWorld Stake2(
private static IWorld Stake3(
IWorld state,
Address agentAddr,
Address avatarAddr,
long stakingAmount,
long blockIndex)
{
var stake3 = new Stake(stakingAmount);
var stake3 = new Stake(stakingAmount, avatarAddr);
return stake3.Execute(
new ActionContext
{
Expand Down Expand Up @@ -247,7 +253,7 @@ private static void ValidateStakedStateV2(
string expectStakeRegularFixedRewardSheetName,
string expectStakeRegularRewardSheetName,
long expectRewardInterval,
long expectLockupInterval)
long expectUnstakingInterval)
{
var stakeAddr = LegacyStakeState.DeriveAddress(agentAddr);
var actualStakedAmount = state.GetBalance(stakeAddr, expectStakedAmount.Currency);
Expand All @@ -261,7 +267,7 @@ private static void ValidateStakedStateV2(
expectStakeRegularRewardSheetName,
stakeState.Contract.StakeRegularRewardSheetTableName);
Assert.Equal(expectRewardInterval, stakeState.Contract.RewardInterval);
Assert.Equal(expectLockupInterval, stakeState.Contract.LockupInterval);
Assert.Equal(expectUnstakingInterval, stakeState.Contract.UnstakingInterval);
}
}
}
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/Stake2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void RestrictForStakeStateV2()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
50400,
201600),
75600),
0).Serialize()),
Signer = _signerAddress,
BlockIndex = 0,
Expand Down
84 changes: 38 additions & 46 deletions .Lib9c.Tests/Action/StakeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class StakeTest
private readonly Currency _ncg;
private readonly PublicKey _agentPublicKey = new PrivateKey().PublicKey;
private readonly Address _agentAddr;
private readonly Address _avatarAddr;
private readonly StakePolicySheet _stakePolicySheet;

public StakeTest(ITestOutputHelper outputHelper)
Expand Down Expand Up @@ -65,7 +66,7 @@ public StakeTest(ITestOutputHelper outputHelper)
(
_,
_agentAddr,
_,
_avatarAddr,
_initialState
) = InitializeUtil.InitializeStates(
sheetsOverride: sheetsOverride,
Expand All @@ -80,13 +81,15 @@ public StakeTest(ITestOutputHelper outputHelper)
[InlineData(long.MaxValue, true)]
public void Constructor(long amount, bool success)
{
var avatarAddress = new PrivateKey().Address;

if (success)
{
var stake = new Stake(amount);
var stake = new Stake(amount, _avatarAddr);
}
else
{
Assert.Throws<ArgumentOutOfRangeException>(() => new Stake(amount));
Assert.Throws<ArgumentOutOfRangeException>(() => new Stake(amount, _avatarAddr));
}
}

Expand All @@ -95,7 +98,7 @@ public void Constructor(long amount, bool success)
[InlineData(long.MaxValue)]
public void Serialization(long amount)
{
var action = new Stake(amount);
var action = new Stake(amount, _avatarAddr);
var ser = action.PlainValue;
var de = new Stake();
de.LoadPlainValue(ser);
Expand Down Expand Up @@ -236,11 +239,7 @@ public void Execute_Throw_StateNullException_Via_0_Amount()
[Theory]
// NOTE: minimum required_gold of StakeRegularRewardSheetFixtures.V2 is 50.
[InlineData(0, 50, LegacyStakeState.RewardInterval)]
[InlineData(
long.MaxValue - LegacyStakeState.RewardInterval,
long.MaxValue,
long.MaxValue)]
public void Execute_Throw_StakeExistingClaimableException_With_StakeState(
public void Execute_Success_When_Claimable_With_StakeState(
long previousStartedBlockIndex,
long previousAmount,
long blockIndex)
Expand All @@ -256,24 +255,22 @@ public void Execute_Throw_StakeExistingClaimableException_With_StakeState(
stakeStateAddr,
_ncg * previousAmount)
.SetLegacyState(stakeStateAddr, stakeState.Serialize());
Assert.Throws<StakeExistingClaimableException>(
() =>
Execute(
blockIndex,
previousState,
new TestRandom(),
_agentAddr,
previousAmount));

var nextState = Execute(
blockIndex,
previousState,
new TestRandom(),
_agentAddr,
previousAmount);

Assert.True(nextState.TryGetStakeState(_agentAddr, out var newStakeState));
Assert.Equal(blockIndex, newStakeState.ClaimedBlockIndex);
}

[Theory]
// NOTE: minimum required_gold of StakeRegularRewardSheetFixtures.V2 is 50.
// NOTE: RewardInterval of StakePolicySheetFixtures.V2 is 50,400.
[InlineData(0, 50, 50400)]
[InlineData(
long.MaxValue - 50400,
long.MaxValue,
long.MaxValue)]
public void Execute_Throw_StakeExistingClaimableException_With_StakeStateV2(
long previousStartedBlockIndex,
long previousAmount,
Expand All @@ -289,14 +286,15 @@ public void Execute_Throw_StakeExistingClaimableException_With_StakeStateV2(
stakeStateAddr,
_ncg * previousAmount)
.SetLegacyState(stakeStateAddr, stakeStateV2.Serialize());
Assert.Throws<StakeExistingClaimableException>(
() =>
Execute(
blockIndex,
previousState,
new TestRandom(),
_agentAddr,
previousAmount));
var nextState = Execute(
blockIndex,
previousState,
new TestRandom(),
_agentAddr,
previousAmount);

Assert.True(nextState.TryGetStakeState(_agentAddr, out var newStakeState));
Assert.Equal(blockIndex, newStakeState.ClaimedBlockIndex);
}

[Theory]
Expand All @@ -322,8 +320,8 @@ public void Execute_Success_When_Staking_State_Null(long amount)
_agentAddr,
amount);

world = DelegationUtil.EnsureStakeReleased(
world, height + ValidatorDelegatee.ValidatorUnbondingPeriod);
world = DelegationUtil.EnsureUnbondedClaimed(
world, _agentAddr, height + ValidatorDelegatee.ValidatorUnbondingPeriod);
}

[Theory]
Expand Down Expand Up @@ -394,8 +392,8 @@ public void Execute_Success_When_Exist_StakeStateV3(
Assert.Equal(3, nextStakeState.StateVersion);
}

world = DelegationUtil.EnsureStakeReleased(
nextState, height + LegacyStakeState.LockupInterval);
world = DelegationUtil.EnsureUnbondedClaimed(
nextState, _agentAddr, height + interval + ValidatorDelegatee.ValidatorUnbondingPeriod);

var expectedBalance = _ncg * Math.Max(0, previousAmount - amount);
var actualBalance = world.GetBalance(_agentAddr, _ncg);
Expand Down Expand Up @@ -461,16 +459,10 @@ public void Execute_Success_When_Exist_StakeStateV3_Without_Guild(
Assert.Equal(3, nextStakeState.StateVersion);
}

world = DelegationUtil.EnsureStakeReleased(
nextState, height + LegacyStakeState.LockupInterval);

var expectedBalance = _ncg * Math.Max(0, previousAmount - amount);
var actualBalance = world.GetBalance(_agentAddr, _ncg);
var nonValidatorDelegateeBalance = world.GetBalance(
Addresses.NonValidatorDelegatee, Currencies.GuildGold);
var stakeBalance = world.GetBalance(stakeStateAddr, Currencies.GuildGold);
var actualBalance = nextState.GetBalance(_agentAddr, _ncg);
var stakeBalance = nextState.GetBalance(stakeStateAddr, Currencies.GuildGold);
Assert.Equal(expectedBalance, actualBalance);
Assert.Equal(Currencies.GuildGold * 0, nonValidatorDelegateeBalance);
Assert.Equal(Currencies.GuildGold * amount, stakeBalance);
}

Expand Down Expand Up @@ -535,8 +527,8 @@ public void Execute_Success_When_Validator_Tries_To_Increase_Amount_Without_Clai
Assert.Equal(3, nextStakeState.StateVersion);
}

world = DelegationUtil.EnsureStakeReleased(
nextState, height + LegacyStakeState.LockupInterval);
world = DelegationUtil.EnsureUnbondedClaimed(
nextState, _agentAddr, height + interval + ValidatorDelegatee.ValidatorUnbondingPeriod);

var expectedBalance = _ncg * Math.Max(0, previousAmount - amount);
var actualBalance = world.GetBalance(_agentAddr, _ncg);
Expand All @@ -555,7 +547,7 @@ private IWorld Execute(
Address signer,
long amount)
{
var action = new Stake(amount);
var action = new Stake(amount, _avatarAddr);
var nextState = action.Execute(
new ActionContext
{
Expand Down Expand Up @@ -594,8 +586,8 @@ private IWorld Execute(
_stakePolicySheet.RewardIntervalValue,
stakeStateV2.Contract.RewardInterval);
Assert.Equal(
_stakePolicySheet.LockupIntervalValue,
stakeStateV2.Contract.LockupInterval);
_stakePolicySheet.UnstakingIntervalValue,
stakeStateV2.Contract.UnstakingInterval);
Assert.Equal(blockIndex, stakeStateV2.StartedBlockIndex);
Assert.Equal(0, stakeStateV2.ReceivedBlockIndex);
Assert.Equal(blockIndex, stakeStateV2.ClaimedBlockIndex);
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/TransferAssetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void Execute_Throw_ArgumentException()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
50400,
201600),
75600),
0).Serialize()),
Signer = _sender,
BlockIndex = 1,
Expand Down
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Action/TransferAssetsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public void Execute_Throw_ArgumentException()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
50400,
201600),
75600),
0).Serialize()),
Signer = _sender,
BlockIndex = 1,
Expand Down
Loading
Loading