Skip to content

Commit

Permalink
feat: Introduce UnstakingInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Jan 8, 2025
1 parent 77b998c commit a2179b1
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 58 deletions.
10 changes: 5 additions & 5 deletions .Lib9c.Tests/Action/Scenario/StakeAndClaimScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void Test()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
LegacyStakeState.RewardInterval,
LegacyStakeState.LockupInterval);
0);

var validatorKey = new PrivateKey().PublicKey;
state = DelegationUtil.EnsureValidatorPromotionReady(state, validatorKey, 0L);
Expand Down Expand Up @@ -139,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 @@ -163,7 +163,7 @@ public void Test()
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
40,
150);
75_600);
}

private static IWorld MintAsset(
Expand Down Expand Up @@ -253,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 @@ -267,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
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/StakeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,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
12 changes: 8 additions & 4 deletions .Lib9c.Tests/Fixtures/TableCSV/Stake/StakePolicySheetFixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ public static class StakePolicySheetFixtures
StakeRegularFixedRewardSheet,StakeRegularFixedRewardSheet_V1
StakeRegularRewardSheet,StakeRegularRewardSheet_V1
RewardInterval,50400
LockupInterval,201600";
LockupInterval,201600
UnstakingInterval,75600";

public const string V2 = @"attr_name,value
StakeRegularFixedRewardSheet,StakeRegularFixedRewardSheet_V2
StakeRegularRewardSheet,StakeRegularRewardSheet_V2
RewardInterval,50400
LockupInterval,201600";
LockupInterval,201600
UnstakingInterval,75600";

public const string V3 = @"attr_name,value
StakeRegularFixedRewardSheet,StakeRegularFixedRewardSheet_V1
StakeRegularRewardSheet,StakeRegularRewardSheet_V1
RewardInterval,40
LockupInterval,150";
LockupInterval,150
UnstakingInterval,75600";

public const string V6 = @"attr_name,value
StakeRegularFixedRewardSheet,StakeRegularFixedRewardSheet_V3
StakeRegularRewardSheet,StakeRegularRewardSheet_V6
RewardInterval,75600
LockupInterval,302400";
LockupInterval,302400
UnstakingInterval,75600";
}
}
28 changes: 9 additions & 19 deletions .Lib9c.Tests/Model/Stake/ContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ public void Constructor(
string stakeRegularFixedRewardSheetTableName,
string stakeRegularRewardSheetTableName,
long rewardInterval,
long lockupInterval)
long unstakingInterval)
{
var contract = new Contract(
stakeRegularFixedRewardSheetTableName,
stakeRegularRewardSheetTableName,
rewardInterval,
lockupInterval);
unstakingInterval);
Assert.Equal(
stakeRegularFixedRewardSheetTableName,
contract.StakeRegularFixedRewardSheetTableName);
Assert.Equal(
stakeRegularRewardSheetTableName,
contract.StakeRegularRewardSheetTableName);
Assert.Equal(rewardInterval, contract.RewardInterval);
Assert.Equal(lockupInterval, contract.LockupInterval);
Assert.Equal(unstakingInterval, contract.UnstakingInterval);
}

[Theory]
Expand Down Expand Up @@ -75,14 +75,14 @@ public void Constructor_Throws_ArgumentException(
string stakeRegularFixedRewardSheetTableName,
string stakeRegularRewardSheetTableName,
long rewardInterval,
long lockupInterval)
long unstakingInterval)
{
Assert.Throws<ArgumentException>(
() => new Contract(
stakeRegularFixedRewardSheetTableName,
stakeRegularRewardSheetTableName,
rewardInterval,
lockupInterval));
unstakingInterval));
}

[Theory]
Expand All @@ -96,28 +96,18 @@ public void Constructor_Throws_ArgumentException(
Contract.StakeRegularRewardSheetPrefix,
0,
1)]
[InlineData(
Contract.StakeRegularFixedRewardSheetPrefix,
Contract.StakeRegularRewardSheetPrefix,
1,
-1)]
[InlineData(
Contract.StakeRegularFixedRewardSheetPrefix,
Contract.StakeRegularRewardSheetPrefix,
1,
0)]
public void Constructor_Throws_ArgumentOutOfRangeException(
string stakeRegularFixedRewardSheetTableName,
string stakeRegularRewardSheetTableName,
long rewardInterval,
long lockupInterval)
long unstakingInterval)
{
Assert.Throws<ArgumentOutOfRangeException>(
() => new Contract(
stakeRegularFixedRewardSheetTableName,
stakeRegularRewardSheetTableName,
rewardInterval,
lockupInterval));
unstakingInterval));
}

[Fact]
Expand All @@ -141,7 +131,7 @@ public void Constructor_With_StakePolicySheet(string stakePolicySheetCsv)
sheet.StakeRegularRewardSheetValue,
contract.StakeRegularRewardSheetTableName);
Assert.Equal(sheet.RewardIntervalValue, contract.RewardInterval);
Assert.Equal(sheet.LockupIntervalValue, contract.LockupInterval);
Assert.Equal(sheet.UnstakingIntervalValue, contract.UnstakingInterval);
}

[Fact]
Expand All @@ -161,7 +151,7 @@ public void Serde()
contract.StakeRegularRewardSheetTableName,
des.StakeRegularRewardSheetTableName);
Assert.Equal(contract.RewardInterval, des.RewardInterval);
Assert.Equal(contract.LockupInterval, des.LockupInterval);
Assert.Equal(contract.UnstakingInterval, des.UnstakingInterval);
var ser2 = des.Serialize();
Assert.Equal(ser, ser2);
}
Expand Down
5 changes: 4 additions & 1 deletion .Lib9c.Tests/TableData/Stake/StakePolicySheetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Set_Success()
{
var sheet = new StakePolicySheet();
sheet.Set(StakePolicySheetFixtures.V1);
Assert.Equal(4, sheet.Count);
Assert.Equal(5, sheet.Count);
var row = sheet["StakeRegularFixedRewardSheet"];
Assert.Equal("StakeRegularFixedRewardSheet", row.AttrName);
Assert.Equal("StakeRegularFixedRewardSheet_V1", row.Value);
Expand All @@ -27,6 +27,9 @@ public void Set_Success()
row = sheet["LockupInterval"];
Assert.Equal("LockupInterval", row.AttrName);
Assert.Equal("201600", row.Value);
row = sheet["UnstakingInterval"];
Assert.Equal("UnstakingInterval", row.AttrName);
Assert.Equal("75600", row.Value);
}

[Theory]
Expand Down
4 changes: 2 additions & 2 deletions Lib9c/Action/Stake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public override IWorld Execute(IActionContext context)

// NOTE: When the staking state is locked up.
// TODO: Remove this condition after the migration is done.
if (stakeStateV2.CancellableBlockIndex > context.BlockIndex)
if (stakeStateV2.UnstakableBlockIndex > context.BlockIndex)
{
// NOTE: Cannot re-contract with less balance.
if (targetStakeBalance < stakedBalance)
Expand Down Expand Up @@ -246,8 +246,8 @@ private static IWorld ContractNewStake(
FungibleAssetValue targetStakeBalance,
Contract latestStakeContract)
{
var stakeStateValue = new StakeState(latestStakeContract, context.BlockIndex).Serialize();
var additionalBalance = targetStakeBalance - stakedBalance;
var stakeStateValue = new StakeState(latestStakeContract, context.BlockIndex, unstaked: additionalBalance.Sign < 0).Serialize();
var height = context.BlockIndex;
var agentAddress = new AgentAddress(context.Signer);

Expand Down
26 changes: 9 additions & 17 deletions Lib9c/Model/Stake/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Nekoyume.Model.Stake
public class Contract
{
public const string StateTypeName = "stake_contract";
public const long StateTypeVersion = 1;
public const long StateTypeVersion = 2;

public const string StakeRegularFixedRewardSheetPrefix
= "StakeRegularFixedRewardSheet_";
Expand All @@ -18,24 +18,23 @@ public const string StakeRegularRewardSheetPrefix
public string StakeRegularFixedRewardSheetTableName { get; }
public string StakeRegularRewardSheetTableName { get; }
public long RewardInterval { get; }
[Obsolete("Not used because of guild system")]
public long LockupInterval { get; }
public long UnstakingInterval { get; }

public Contract(StakePolicySheet stakePolicySheet) : this(
stakePolicySheet?.StakeRegularFixedRewardSheetValue ?? throw new ArgumentNullException(
nameof(stakePolicySheet),
$"{nameof(stakePolicySheet)} is null"),
stakePolicySheet.StakeRegularRewardSheetValue,
stakePolicySheet.RewardIntervalValue,
stakePolicySheet.LockupIntervalValue)
stakePolicySheet.UnstakingIntervalValue)
{
}

public Contract(
string stakeRegularFixedRewardSheetTableName,
string stakeRegularRewardSheetTableName,
long rewardInterval,
long lockupInterval)
long unstakingInterval)
{
if (string.IsNullOrEmpty(stakeRegularFixedRewardSheetTableName))
{
Expand Down Expand Up @@ -67,17 +66,10 @@ public Contract(
$"{nameof(rewardInterval)} must be greater than 0");
}

if (lockupInterval <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(lockupInterval),
$"{nameof(lockupInterval)} must be greater than 0");
}

StakeRegularFixedRewardSheetTableName = stakeRegularFixedRewardSheetTableName;
StakeRegularRewardSheetTableName = stakeRegularRewardSheetTableName;
RewardInterval = rewardInterval;
LockupInterval = lockupInterval;
UnstakingInterval = unstakingInterval;
}

public Contract(IValue serialized)
Expand Down Expand Up @@ -109,7 +101,7 @@ public Contract(IValue serialized)
StakeRegularFixedRewardSheetTableName = (Text)list[reservedCount];
StakeRegularRewardSheetTableName = (Text)list[reservedCount + 1];
RewardInterval = (Integer)list[reservedCount + 2];
LockupInterval = (Integer)list[reservedCount + 3];
UnstakingInterval = (Integer)list[reservedCount + 3];
}

public List Serialize()
Expand All @@ -120,7 +112,7 @@ public List Serialize()
(Text)StakeRegularFixedRewardSheetTableName,
(Text)StakeRegularRewardSheetTableName,
(Integer)RewardInterval,
(Integer)LockupInterval
(Integer)UnstakingInterval
);
}

Expand All @@ -130,7 +122,7 @@ protected bool Equals(Contract other)
other.StakeRegularFixedRewardSheetTableName &&
StakeRegularRewardSheetTableName == other.StakeRegularRewardSheetTableName &&
RewardInterval == other.RewardInterval &&
LockupInterval == other.LockupInterval;
UnstakingInterval == other.UnstakingInterval;
}

public override bool Equals(object obj)
Expand All @@ -152,7 +144,7 @@ public override int GetHashCode()
? StakeRegularRewardSheetTableName.GetHashCode()
: 0);
hashCode = (hashCode * 397) ^ RewardInterval.GetHashCode();
hashCode = (hashCode * 397) ^ LockupInterval.GetHashCode();
hashCode = (hashCode * 397) ^ UnstakingInterval.GetHashCode();
return hashCode;
}
}
Expand Down
Loading

0 comments on commit a2179b1

Please sign in to comment.