From d5b20386c9effdfdd49c9f5c1999ca25f0812bb7 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Wed, 14 Jun 2023 11:16:43 +0800 Subject: [PATCH 1/2] feat: use median store price for secondary sp price --- x/sp/keeper/sp_storage_price.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/x/sp/keeper/sp_storage_price.go b/x/sp/keeper/sp_storage_price.go index ee1d03165..79297015b 100644 --- a/x/sp/keeper/sp_storage_price.go +++ b/x/sp/keeper/sp_storage_price.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + "sort" "github.com/cosmos/cosmos-sdk/store/prefix" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -119,9 +120,8 @@ func (k Keeper) SetSecondarySpStorePrice(ctx sdk.Context, secondarySpStorePrice // UpdateSecondarySpStorePrice calculate the price of secondary store by the average price of all sp store price func (k Keeper) UpdateSecondarySpStorePrice(ctx sdk.Context) error { sps := k.GetAllStorageProviders(ctx) - total := sdk.ZeroDec() current := ctx.BlockTime().Unix() - var spNumInService int64 + prices := make([]sdk.Dec, 0) for _, sp := range sps { if sp.Status != types.STATUS_IN_SERVICE { continue @@ -130,13 +130,21 @@ func (k Keeper) UpdateSecondarySpStorePrice(ctx sdk.Context) error { if err != nil { return err } - spNumInService++ - total = total.Add(price.StorePrice) + prices = append(prices, price.StorePrice) } - if spNumInService == 0 { + l := len(prices) + if l == 0 { return nil } - price := k.SecondarySpStorePriceRatio(ctx).Mul(total).QuoInt64(spNumInService) + + sort.Slice(prices, func(i, j int) bool { return prices[i].LT(prices[j]) }) + var median sdk.Dec + if l%2 == 0 { + median = prices[l/2-1].Add(prices[l/2]).QuoInt64(2) + } else { + median = prices[l/2] + } + price := k.SecondarySpStorePriceRatio(ctx).Mul(median) secondarySpStorePrice := types.SecondarySpStorePrice{ StorePrice: price, UpdateTimeSec: current, From 362a82d3e8dce0913d66df1720b76cfd89f40142 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Wed, 14 Jun 2023 11:34:26 +0800 Subject: [PATCH 2/2] fix e2e test --- e2e/tests/sp_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/e2e/tests/sp_test.go b/e2e/tests/sp_test.go index e1513a663..07e687f55 100644 --- a/e2e/tests/sp_test.go +++ b/e2e/tests/sp_test.go @@ -2,6 +2,7 @@ package tests import ( "context" + "sort" "strconv" "testing" "time" @@ -276,7 +277,7 @@ func (s *StorageProviderTestSuite) CheckSecondarySpPrice() { s.Require().NoError(err) s.T().Logf("sps: %s", sps) spNum := int64(sps.Pagination.Total) - total := sdk.ZeroDec() + prices := make([]sdk.Dec, 0) for _, sp := range sps.Sps { spStoragePrice, err := s.Client.QueryGetSpStoragePriceByTime(ctx, &sptypes.QueryGetSpStoragePriceByTimeRequest{ SpAddr: sp.OperatorAddress, @@ -284,11 +285,19 @@ func (s *StorageProviderTestSuite) CheckSecondarySpPrice() { }) s.Require().NoError(err) s.T().Logf("sp: %s, storage price: %s", sp.OperatorAddress, core.YamlString(spStoragePrice.SpStoragePrice)) - total = total.Add(spStoragePrice.SpStoragePrice.StorePrice) + prices = append(prices, spStoragePrice.SpStoragePrice.StorePrice) } + sort.Slice(prices, func(i, j int) bool { return prices[i].LT(prices[j]) }) + var median sdk.Dec + if spNum%2 == 0 { + median = prices[spNum/2-1].Add(prices[spNum/2]).QuoInt64(2) + } else { + median = prices[spNum/2] + } + params, err := s.Client.SpQueryClient.Params(ctx, &sptypes.QueryParamsRequest{}) s.Require().NoError(err) - expectedSecondarySpStorePrice := params.Params.SecondarySpStorePriceRatio.Mul(total).QuoInt64(spNum) + expectedSecondarySpStorePrice := params.Params.SecondarySpStorePriceRatio.Mul(median) s.Require().Equal(expectedSecondarySpStorePrice, queryGetSecondarySpStorePriceByTimeResp.SecondarySpStorePrice.StorePrice) }