Skip to content

Commit

Permalink
refine sp parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Aug 10, 2023
1 parent 27340e8 commit 61dd9d0
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 161 deletions.
2 changes: 1 addition & 1 deletion deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function generate_genesis() {
sed -i -e "s/\"discontinue_deletion_max\": \"100\"/\"discontinue_deletion_max\": \"2\"/g" ${workspace}/.local/validator${i}/config/genesis.json
sed -i -e "s/\"voting_period\": \"30s\"/\"voting_period\": \"5s\"/g" ${workspace}/.local/validator${i}/config/genesis.json
sed -i -e "s/\"update_global_price_interval\": \"0\"/\"update_global_price_interval\": \"1\"/g" ${workspace}/.local/validator${i}/config/genesis.json
sed -i -e "s/\"max_update_price_times\": 3/\"max_update_price_times\": 1000000/g" ${workspace}/.local/validator${i}/config/genesis.json
sed -i -e "s/\"update_price_disallowed_days\": 2/\"update_price_disallowed_days\": 0/g" ${workspace}/.local/validator${i}/config/genesis.json
#sed -i -e "s/\"community_tax\": \"0.020000000000000000\"/\"community_tax\": \"0\"/g" ${workspace}/.local/validator${i}/config/genesis.json
sed -i -e "s/log_level = \"info\"/\log_level= \"debug\"/g" ${workspace}/.local/validator${i}/config/config.toml
done
Expand Down
57 changes: 27 additions & 30 deletions e2e/tests/sp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,17 @@ func (s *StorageProviderTestSuite) TestUpdateSpStoragePrice() {
ctx := context.Background()
defer s.revertParams()

// update params
params := s.queryParams()
params.UpdateGlobalPriceInterval = 10
params.MaxUpdatePriceTimes = 2
s.updateParams(params)

// query sp storage price by time before it exists, expect error
_, err := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: 1,
})
s.Require().Error(err)

// check update price
// update params
params := s.queryParams()
params.UpdateGlobalPriceInterval = 5
s.updateParams(params)

sp := s.BaseSuite.PickStorageProvider()
spAddr := sp.OperatorKey.GetAddr().String()
spStoragePrice, err := s.Client.QuerySpStoragePrice(ctx, &sptypes.QuerySpStoragePriceRequest{
Expand All @@ -182,7 +180,7 @@ func (s *StorageProviderTestSuite) TestUpdateSpStoragePrice() {
s.Require().NoError(err)
s.T().Log(spStoragePrice)

// update storage price - first update is ok
// update storage price - update is ok
msgUpdateSpStoragePrice := &sptypes.MsgUpdateSpStoragePrice{
SpAddress: spAddr,
ReadPrice: spStoragePrice.SpStoragePrice.ReadPrice,
Expand All @@ -191,25 +189,7 @@ func (s *StorageProviderTestSuite) TestUpdateSpStoragePrice() {
}
_ = s.SendTxBlock(sp.OperatorKey, msgUpdateSpStoragePrice)

// update storage price - secondary update is ok
msgUpdateSpStoragePrice = &sptypes.MsgUpdateSpStoragePrice{
SpAddress: spAddr,
ReadPrice: spStoragePrice.SpStoragePrice.ReadPrice,
StorePrice: spStoragePrice.SpStoragePrice.StorePrice,
FreeReadQuota: spStoragePrice.SpStoragePrice.FreeReadQuota,
}
_ = s.SendTxBlock(sp.OperatorKey, msgUpdateSpStoragePrice)

// update storage price - third update is not ok
msgUpdateSpStoragePrice = &sptypes.MsgUpdateSpStoragePrice{
SpAddress: spAddr,
ReadPrice: spStoragePrice.SpStoragePrice.ReadPrice,
StorePrice: spStoragePrice.SpStoragePrice.StorePrice,
FreeReadQuota: spStoragePrice.SpStoragePrice.FreeReadQuota,
}
s.SendTxBlockWithExpectErrorString(msgUpdateSpStoragePrice, sp.OperatorKey, "cannot update price due to frequency limited")

time.Sleep(12 * time.Second)
time.Sleep(6 * time.Second)

// verify price is updated after interval
globalPriceResBefore, _ := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{Timestamp: 0})
Expand All @@ -235,20 +215,37 @@ func (s *StorageProviderTestSuite) TestUpdateSpStoragePrice() {
}
}

time.Sleep(12 * time.Second)
time.Sleep(6 * time.Second)
globalPriceResAfter2, _ := s.Client.QueryGlobalSpStorePriceByTime(ctx, &sptypes.QueryGlobalSpStorePriceByTimeRequest{Timestamp: 0})
s.T().Log("globalPriceResAfter2", core.YamlString(globalPriceResAfter2))

s.CheckGlobalSpStorePrice()
if !priceChanged { //if price not changed, then after 10 seconds, it should change
if !priceChanged { //if price not changed, then after 6 seconds, it should change
s.Require().NotEqual(globalPriceResAfter2.GlobalSpStorePrice.PrimaryStorePrice, globalPriceResBefore.GlobalSpStorePrice.PrimaryStorePrice)
s.Require().NotEqual(globalPriceResAfter2.GlobalSpStorePrice.SecondaryStorePrice, globalPriceResBefore.GlobalSpStorePrice.SecondaryStorePrice)
s.Require().NotEqual(globalPriceResAfter2.GlobalSpStorePrice.ReadPrice, globalPriceResBefore.GlobalSpStorePrice.ReadPrice)
} else { //if price not changed already, then after 10 seconds, it should not change
} else { //if price not changed already, then after 6 seconds, it should not change
s.Require().Equal(globalPriceResAfter2.GlobalSpStorePrice.PrimaryStorePrice, globalPriceResAfter1.GlobalSpStorePrice.PrimaryStorePrice)
s.Require().Equal(globalPriceResAfter2.GlobalSpStorePrice.SecondaryStorePrice, globalPriceResAfter1.GlobalSpStorePrice.SecondaryStorePrice)
s.Require().Equal(globalPriceResAfter2.GlobalSpStorePrice.ReadPrice, globalPriceResAfter1.GlobalSpStorePrice.ReadPrice)
}

// update params
now := time.Now().UTC()
_, _, day := now.Date()
params = s.queryParams()
params.UpdateGlobalPriceInterval = 0 // update by month
params.UpdatePriceDisallowedDays = uint32(31 - day + 1)
s.updateParams(params)

// update storage price - third update is not ok
msgUpdateSpStoragePrice = &sptypes.MsgUpdateSpStoragePrice{
SpAddress: spAddr,
ReadPrice: spStoragePrice.SpStoragePrice.ReadPrice,
StorePrice: spStoragePrice.SpStoragePrice.StorePrice,
FreeReadQuota: spStoragePrice.SpStoragePrice.FreeReadQuota,
}
s.SendTxBlockWithExpectErrorString(msgUpdateSpStoragePrice, sp.OperatorKey, "update price is disallowed")
}

func (s *StorageProviderTestSuite) CheckGlobalSpStorePrice() {
Expand Down
4 changes: 2 additions & 2 deletions proto/greenfield/sp/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ message Params {
int64 num_of_lockup_blocks_for_maintenance = 6 [(gogoproto.moretags) = "yaml:\"num_of_lockup_blocks_for_maintenance\""];
// the time interval to update global storage price, if it is not set then the price will be updated at the first block of each natural month
uint64 update_global_price_interval = 7 [(gogoproto.moretags) = "yaml:\"update_global_price_interval\""];
// the max times allowed to update price during an interval
uint32 max_update_price_times = 8 [(gogoproto.moretags) = "yaml:\"max_update_price_times\""];
// the days counting backwards from end of a month in which a sp cannot update its price
uint32 update_price_disallowed_days = 8 [(gogoproto.moretags) = "yaml:\"update_price_disallowed_days\""];
}
18 changes: 12 additions & 6 deletions swagger/static/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1663,10 +1663,12 @@ paths:
the time interval to update global storage price, if it is
not set then the price will be updated at the first block
of each natural month
max_update_price_times:
update_price_disallowed_days:
type: integer
format: int64
title: the max times allowed to update price during an interval
title: >-
the days counting backwards from end of a month in which a
sp cannot update its price
description: >-
QueryParamsResponse is response type for the Query/Params RPC
method.
Expand Down Expand Up @@ -32786,10 +32788,12 @@ definitions:
the time interval to update global storage price, if it is not set
then the price will be updated at the first block of each natural
month
max_update_price_times:
update_price_disallowed_days:
type: integer
format: int64
title: the max times allowed to update price during an interval
title: >-
the days counting backwards from end of a month in which a sp cannot
update its price
description: Params defines the parameters for the module.
greenfield.sp.QueryGlobalSpStorePriceByTimeResponse:
type: object
Expand Down Expand Up @@ -32854,10 +32858,12 @@ definitions:
the time interval to update global storage price, if it is not set
then the price will be updated at the first block of each natural
month
max_update_price_times:
update_price_disallowed_days:
type: integer
format: int64
title: the max times allowed to update price during an interval
title: >-
the days counting backwards from end of a month in which a sp
cannot update its price
description: QueryParamsResponse is response type for the Query/Params RPC method.
greenfield.sp.QuerySpStoragePriceResponse:
type: object
Expand Down
1 change: 0 additions & 1 deletion x/sp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
if err != nil {
ctx.Logger().Error("fail to update global sp store price", "err", err)
}
k.ClearSpUpdatePriceTimes(ctx)
}
}
17 changes: 3 additions & 14 deletions x/sp/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func (k msgServer) CreateStorageProvider(goCtx context.Context, msg *types.MsgCr
FreeReadQuota: msg.FreeReadQuota,
}
k.SetSpStoragePrice(ctx, spStoragePrice)
k.SetSpUpdatePriceTimes(ctx, sp.Id, 1)

if err = ctx.EventManager().EmitTypedEvents(&types.EventCreateStorageProvider{
SpId: sp.Id,
Expand Down Expand Up @@ -317,17 +316,10 @@ func (k msgServer) UpdateSpStoragePrice(goCtx context.Context, msg *types.MsgUpd
}

params := k.GetParams(ctx)
updateTimes := uint32(0)
if params.UpdateGlobalPriceInterval > 0 { // update price by interval
updateTimes = k.GetSpUpdatePriceTimes(ctx, sp.Id)
if updateTimes+1 > params.MaxUpdatePriceTimes {
return nil, errors.Wrapf(types.ErrStorageProviderPriceUpdateNotAllow, "exceeds the limit %d", params.MaxUpdatePriceTimes)
}
updateTimes++
} else { // update price by month
if params.UpdateGlobalPriceInterval == 0 { // update price by month
blockTime := ctx.BlockTime().UTC()
days := 2
if IsLastDaysOfTheMonth(blockTime, days) {
days := params.UpdatePriceDisallowedDays
if IsLastDaysOfTheMonth(blockTime, int(days)) {
return nil, errors.Wrapf(types.ErrStorageProviderPriceUpdateNotAllow, "price cannot be updated in the last %d days of the month", days)
}
}
Expand All @@ -341,9 +333,6 @@ func (k msgServer) UpdateSpStoragePrice(goCtx context.Context, msg *types.MsgUpd
FreeReadQuota: msg.FreeReadQuota,
}
k.SetSpStoragePrice(ctx, spStorePrice)
if updateTimes > 0 {
k.SetSpUpdatePriceTimes(ctx, sp.Id, updateTimes)
}

return &types.MsgUpdateSpStoragePriceResponse{}, nil
}
Expand Down
33 changes: 0 additions & 33 deletions x/sp/keeper/sp_storage_price.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"encoding/binary"
"fmt"
"sort"

Expand Down Expand Up @@ -145,35 +144,3 @@ func (k Keeper) GetGlobalSpStorePriceByTime(ctx sdk.Context, time int64) (val ty
val.UpdateTimeSec = updateTimeSec
return val, nil
}

func (k Keeper) SetSpUpdatePriceTimes(ctx sdk.Context, spId uint32, times uint32) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SpStoragePriceUpdateTimesKeyPrefix)
idBz := make([]byte, 4)
binary.BigEndian.PutUint32(idBz, spId)
timesBz := make([]byte, 4)
binary.BigEndian.PutUint32(timesBz, times)
store.Set(idBz, timesBz)
}

func (k Keeper) GetSpUpdatePriceTimes(ctx sdk.Context, spId uint32) uint32 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SpStoragePriceUpdateTimesKeyPrefix)
idBz := make([]byte, 4)
binary.BigEndian.PutUint32(idBz, spId)
timesBz := store.Get(idBz)
if timesBz == nil {
return 0
}
times := binary.BigEndian.Uint32(timesBz)
return times
}

func (k Keeper) ClearSpUpdatePriceTimes(ctx sdk.Context) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SpStoragePriceUpdateTimesKeyPrefix)

iterator := storetypes.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}
}
2 changes: 1 addition & 1 deletion x/sp/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
ErrStorageProviderBlsKeyExists = errors.Register(ModuleName, 15, "StorageProvider already exist for this bls pubkey; must use new bls pubkey")
ErrStorageProviderStatusUpdateNotAllow = errors.Register(ModuleName, 16, "StorageProvider status is not allow to change")
ErrStorageProviderMaintenanceAddrExists = errors.Register(ModuleName, 17, "StorageProvider already exist for this maintenance address; must use new StorageProvider maintenance address.")
ErrStorageProviderPriceUpdateNotAllow = errors.Register(ModuleName, 18, "StorageProvider cannot update price due to frequency limited.")
ErrStorageProviderPriceUpdateNotAllow = errors.Register(ModuleName, 18, "StorageProvider update price is disallowed")

ErrSignerNotGovModule = errors.Register(ModuleName, 40, "signer is not gov module account")
ErrSignerEmpty = errors.Register(ModuleName, 41, "signer is empty")
Expand Down
21 changes: 10 additions & 11 deletions x/sp/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ const (
var (
ParamsKey = []byte{0x01}

StorageProviderKey = []byte{0x21} // prefix for each key to a storage provider
StorageProviderByOperatorAddrKey = []byte{0x23} // prefix for each key to a storage provider index, by operator address
StorageProviderByFundingAddrKey = []byte{0x24} // prefix for each key to a storage provider index, by funding address
StorageProviderBySealAddrKey = []byte{0x25} // prefix for each key to a storage provider index, by seal address
StorageProviderByApprovalAddrKey = []byte{0x26} // prefix for each key to a storage provider index, by approval address
StorageProviderByGcAddrKey = []byte{0x27} // prefix for each key to a storage provider index, by gc address
SpStoragePriceKeyPrefix = []byte{0x28}
GlobalSpStorePriceKeyPrefix = []byte{0x29}
StorageProviderByBlsPubKeyKey = []byte{0x30} // prefix for each key to a storage provider index, by bls pub key
StorageProviderSequenceKey = []byte{0x31}
SpStoragePriceUpdateTimesKeyPrefix = []byte{0x32}
StorageProviderKey = []byte{0x21} // prefix for each key to a storage provider
StorageProviderByOperatorAddrKey = []byte{0x23} // prefix for each key to a storage provider index, by operator address
StorageProviderByFundingAddrKey = []byte{0x24} // prefix for each key to a storage provider index, by funding address
StorageProviderBySealAddrKey = []byte{0x25} // prefix for each key to a storage provider index, by seal address
StorageProviderByApprovalAddrKey = []byte{0x26} // prefix for each key to a storage provider index, by approval address
StorageProviderByGcAddrKey = []byte{0x27} // prefix for each key to a storage provider index, by gc address
SpStoragePriceKeyPrefix = []byte{0x28}
GlobalSpStorePriceKeyPrefix = []byte{0x29}
StorageProviderByBlsPubKeyKey = []byte{0x30} // prefix for each key to a storage provider index, by bls pub key
StorageProviderSequenceKey = []byte{0x31}

StorageProviderMaintenanceRecordPrefix = []byte{0x41}
)
Expand Down
23 changes: 10 additions & 13 deletions x/sp/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const (
DefaultNumOfLockUpBlocksForMaintenance = 21600
// DefaultUpdateGlobalPriceInterval defines the default time duration for updating global storage price
DefaultUpdateGlobalPriceInterval uint64 = 0 // 0 means the global price will be updated at the first day of each month
// DefaultMaxUpdatePriceTimes defines the max allowed times to update price for each sp in one interval
DefaultMaxUpdatePriceTimes uint32 = 3
// UpdatePriceDisallowedDays defines the days, counting backward from the end of a month, in which sp is not allowed to update its price
DefaultUpdatePriceDisallowedDays uint32 = 2
)

var (
Expand All @@ -43,7 +43,7 @@ var (
KeyMaintenanceDurationQuota = []byte("MaintenanceDurationQuota")
KeyNumOfLockUpBlocksForMaintenance = []byte("NumOfLockUpBlocksForMaintenance")
KeyUpdateGlobalPriceInterval = []byte("UpdateGlobalPriceInterval")
KeyMaxUpdatePriceTimes = []byte("MaxUpdatePriceTimes")
KeyUpdatePriceDisallowedDays = []byte("UpdatePriceDisallowedDays")
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -56,7 +56,7 @@ func ParamKeyTable() paramtypes.KeyTable {
// NewParams creates a new Params instance
func NewParams(depositDenom string, minDeposit math.Int, secondarySpStorePriceRatio sdk.Dec,
historicalBlocksForMaintenanceRecords, maintenanceDurationQuota, lockUpBlocksForMaintenance int64,
updateGlobalPriceInterval uint64, maxUpdatePriceTimes uint32) Params {
updateGlobalPriceInterval uint64, updatePriceDisallowedDays uint32) Params {
return Params{
DepositDenom: depositDenom,
MinDeposit: minDeposit,
Expand All @@ -65,15 +65,15 @@ func NewParams(depositDenom string, minDeposit math.Int, secondarySpStorePriceRa
MaintenanceDurationQuota: maintenanceDurationQuota,
NumOfLockupBlocksForMaintenance: lockUpBlocksForMaintenance,
UpdateGlobalPriceInterval: updateGlobalPriceInterval,
MaxUpdatePriceTimes: maxUpdatePriceTimes,
UpdatePriceDisallowedDays: updatePriceDisallowedDays,
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultDepositDenom, DefaultMinDeposit, DefaultSecondarySpStorePriceRatio,
DefaultNumOfHistoricalBlocksForMaintenanceRecords, DefaultMaintenanceDurationQuota, DefaultNumOfLockUpBlocksForMaintenance,
DefaultUpdateGlobalPriceInterval, DefaultMaxUpdatePriceTimes)
DefaultUpdateGlobalPriceInterval, DefaultUpdatePriceDisallowedDays)
}

// ParamSetPairs get the params.ParamSet
Expand All @@ -86,7 +86,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyMaintenanceDurationQuota, &p.MaintenanceDurationQuota, validateMaintenanceDurationQuota),
paramtypes.NewParamSetPair(KeyNumOfLockUpBlocksForMaintenance, &p.NumOfLockupBlocksForMaintenance, validateLockUpBlocksForMaintenance),
paramtypes.NewParamSetPair(KeyUpdateGlobalPriceInterval, &p.UpdateGlobalPriceInterval, validateUpdateGlobalPriceInterval),
paramtypes.NewParamSetPair(KeyMaxUpdatePriceTimes, &p.MaxUpdatePriceTimes, validateMaxUpdatePriceTimes),
paramtypes.NewParamSetPair(KeyUpdatePriceDisallowedDays, &p.UpdatePriceDisallowedDays, validateUpdatePriceDisallowedDays),
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (p Params) Validate() error {
if err := validateUpdateGlobalPriceInterval(p.UpdateGlobalPriceInterval); err != nil {
return err
}
if err := validateMaxUpdatePriceTimes(p.MaxUpdatePriceTimes); err != nil {
if err := validateUpdatePriceDisallowedDays(p.UpdatePriceDisallowedDays); err != nil {
return err
}

Expand Down Expand Up @@ -214,13 +214,10 @@ func validateUpdateGlobalPriceInterval(i interface{}) error {
return nil
}

func validateMaxUpdatePriceTimes(i interface{}) error {
v, ok := i.(uint32)
func validateUpdatePriceDisallowedDays(i interface{}) error {
_, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return errors.New("MaxUpdatePriceTimes cannot be zero")
}
return nil
}
Loading

0 comments on commit 61dd9d0

Please sign in to comment.