Skip to content

Commit

Permalink
optimized staking weight value (#3119)
Browse files Browse the repository at this point in the history
* optimized staking weight value

* optimized code

* optimized code
  • Loading branch information
zjg555543 authored Apr 27, 2023
1 parent 528b9aa commit 95eb909
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 5 additions & 5 deletions x/staking/keeper/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 11 additions & 9 deletions x/staking/keeper/weight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}

0 comments on commit 95eb909

Please sign in to comment.