diff --git a/x/staking/keeper/weight.go b/x/staking/keeper/weight.go index 992fca48d6..6973461097 100644 --- a/x/staking/keeper/weight.go +++ b/x/staking/keeper/weight.go @@ -14,16 +14,16 @@ const ( blockTimestampEpoch = int64(946684800) secondsPerWeek = int64(60 * 60 * 24 * 7) weeksPerYear = float64(52) - fixedTimeStamp = int64(1685577600) // 2023-06-01 00:00:00 GMT+0 - fixedWeek = int64(1221) // (fixedTimeStamp - blockTimestampEpoch) / secondsPerWeek + fixedWeight = int64(11700000) // The weight of 1 okt, calculated by calculateWeight before venus6. (nowTime=2023-06-01 00:00:00 GMT+0) ) func calculateWeight(nowTime int64, tokens sdk.Dec, height int64) (shares types.Shares, sdkErr error) { - nowWeek := fixedWeek - if !types2.HigherThanVenus6(height) { - nowWeek = (nowTime - blockTimestampEpoch) / secondsPerWeek + if types2.HigherThanVenus6(height) { + shares = tokens.MulInt64(fixedWeight) + return } + nowWeek := (nowTime - blockTimestampEpoch) / secondsPerWeek rate := float64(nowWeek) / weeksPerYear weight := math.Pow(float64(2), rate) diff --git a/x/staking/keeper/weight_test.go b/x/staking/keeper/weight_test.go index 7a2f602023..7e68ce918a 100644 --- a/x/staking/keeper/weight_test.go +++ b/x/staking/keeper/weight_test.go @@ -35,27 +35,29 @@ func TestProposalSuite(t *testing.T) { func (suite *ProposalSuite) TestNewChangeDistributionTypeProposal() { testCases := []struct { title string + curTime string curHeight int64 upgradeHeight int64 quo int64 }{ - {"default", 100, 0, 2}, - {"set upgrade height, not reached height", 100, 100, 2}, - {"set upgrade height, reached height", 101, 100, 1}, + {"default", "2023-05-01 00:00:00", 100, 0, 2}, + {"set upgrade height, not reached height", "2023-05-01 00:00:00", 100, 100, 2}, + {"set upgrade height, reached height", "2023-05-01 00:00:00", 101, 100, 1}, } - formatTime, _ := gotime.Parse("2006-01-02 15:04:05", "2023-06-01 00:00:00") - require.Equal(suite.T(), formatTime.Unix(), fixedTimeStamp) for _, tc := range testCases { suite.Run(tc.title, func() { + tokens := sdk.NewDec(1) + curTime, _ := gotime.Parse("2006-01-02 15:04:05", tc.curTime) + curDecBefore, err := calculateWeight(curTime.Unix(), tokens, tc.curHeight) global.SetGlobalHeight(tc.curHeight) tmtypes.InitMilestoneVenus6Height(tc.upgradeHeight) - tokens := sdk.NewDec(1000) - nowDec, err := calculateWeight(time.Now().Unix(), tokens, tc.curHeight) + curDec, err := calculateWeight(curTime.Unix(), tokens, tc.curHeight) + require.Equal(suite.T(), true, curDec.GTE(curDecBefore)) require.NoError(suite.T(), err) - afterDec, err := calculateWeight(time.Now().AddDate(0, 0, 52*7).Unix(), tokens, tc.curHeight) + afterDec, err := calculateWeight(curTime.AddDate(0, 0, 52*7).Unix(), tokens, tc.curHeight) require.NoError(suite.T(), err) - require.Equal(suite.T(), sdk.NewDec(tc.quo), afterDec.Quo(nowDec)) + require.Equal(suite.T(), sdk.NewDec(tc.quo), afterDec.Quo(curDec)) }) } }