Skip to content

Commit

Permalink
add more check and params
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnss committed Jun 19, 2023
1 parent f415738 commit 2c2bca4
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 98 deletions.
13 changes: 6 additions & 7 deletions proto/greenfield/virtualgroup/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ message Params {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// min_deposit defines the minimum deposit amount for storage providers.
string min_deposit = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// the max number of lvg which allowed in a bucket
uint32 max_local_virtual_group_num_per_bucket = 3;
// the max number of gvg which can exist in a family
uint32 max_global_virtual_group_num_per_family = 4;
// if the store size reach the exceed, the family is not allowed to sever more buckets
uint64 max_store_size_per_family = 5;
}
10 changes: 8 additions & 2 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,9 @@ func (k Keeper) CreateObject(
if !found {
return sdkmath.ZeroUint(), types.ErrNoSuchBucket
}
if bucketInfo.BucketStatus == types.BUCKET_STATUS_DISCONTINUED {
return sdkmath.ZeroUint(), types.ErrBucketDiscontinued
err := bucketInfo.CheckBucketStatus()
if err != nil {
return sdkmath.ZeroUint(), err
}

// verify permission
Expand Down Expand Up @@ -856,6 +857,11 @@ func (k Keeper) CopyObject(
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrNoSuchBucket, "dst bucket name (%s)", dstBucketName)
}

err := dstBucketInfo.CheckBucketStatus()
if err != nil {
return sdkmath.ZeroUint(), err
}

srcObjectInfo, found := k.GetObjectInfo(ctx, srcBucketName, srcObjectName)
if !found {
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrNoSuchObject, "src object name (%s)", srcObjectName)
Expand Down
14 changes: 7 additions & 7 deletions x/storage/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ var (
ErrInvalidOperationType = errors.Register(ModuleName, 3002, "invalid operation type")
ErrInvalidId = errors.Register(ModuleName, 3003, "id is invalid")

ErrInvalidObjectIds = errors.Register(ModuleName, 3101, "object ids are invalid")
ErrInvalidReason = errors.Register(ModuleName, 3102, "reason is invalid")
ErrNoMoreDiscontinue = errors.Register(ModuleName, 3103, "no more discontinue requests")
ErrBucketDiscontinued = errors.Register(ModuleName, 3104, "the bucket is discontinued")
ErrInvalidObjectStatus = errors.Register(ModuleName, 3105, "invalid object status")
ErrInvalidBucketStatus = errors.Register(ModuleName, 3106, "invalid bucket status")

ErrInvalidObjectIds = errors.Register(ModuleName, 3101, "object ids are invalid")
ErrInvalidReason = errors.Register(ModuleName, 3102, "reason is invalid")
ErrNoMoreDiscontinue = errors.Register(ModuleName, 3103, "no more discontinue requests")
ErrBucketDiscontinued = errors.Register(ModuleName, 3104, "the bucket is discontinued")
ErrInvalidObjectStatus = errors.Register(ModuleName, 3105, "invalid object status")
ErrInvalidBucketStatus = errors.Register(ModuleName, 3106, "invalid bucket status")
ErrBucketMigrating = errors.Register(ModuleName, 3107, "the bucket is migrating")
ErrInvalidResource = errors.Register(ModuleName, 3201, "invalid resource type")
ErrMigtationBucketFailed = errors.Register(ModuleName, 3202, "migrate bucket failed.")
)
9 changes: 9 additions & 0 deletions x/storage/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func (m *BucketInfo) ToNFTMetadata() *BucketMetaData {
}
}

func (m *BucketInfo) CheckBucketStatus() error {
if m.BucketStatus == BUCKET_STATUS_DISCONTINUED {
return ErrBucketDiscontinued
} else if m.BucketStatus == BUCKET_STATUS_MIGRATING {
return ErrBucketMigrating
}
return nil
}

func (m *ObjectInfo) ToNFTMetadata() *ObjectMetaData {
return &ObjectMetaData{
ObjectName: m.ObjectName,
Expand Down
21 changes: 21 additions & 0 deletions x/virtualgroup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/binary"
"fmt"

"github.com/prysmaticlabs/prysm/crypto/bls"

"cosmossdk.io/errors"
Expand Down Expand Up @@ -231,6 +232,11 @@ func (k Keeper) GetOrCreateEmptyGVGFamily(ctx sdk.Context, familyID uint32, spID
return nil, types.ErrGVGFamilyNotExist
}
k.cdc.MustUnmarshal(bz, &gvgFamily)

storeSize := k.GetStoreSizeOfFamily(ctx, gvgFamily)
if storeSize > k.MaxStoreSizePerFamily(ctx) {
return nil, types.ErrStoreSizeExceed.Wrapf("A family only allow to store %ld, now: %ld", k.MaxStoreSizePerFamily(ctx), storeSize)
}
return &gvgFamily, nil
}
}
Expand Down Expand Up @@ -623,3 +629,18 @@ func (k Keeper) VerifyGVGSecondarySPsBlsSignature(ctx sdk.Context, gvgId uint32,
}
return types2.VerifyBlsAggSignature(secondarySpBlsPubKeys, signBz, signature)
}

// GetStoreSizeOfFamily Rather than calculating the stored size of a Global Virtual Group Family (GVGF) in real-time,
// it is preferable to calculate it once during the creation of a Global Virtual Group (GVG). This approach is favored
// because GVG creation is infrequent and occurs with low frequency.
func (k Keeper) GetStoreSizeOfFamily(ctx sdk.Context, gvgFamily types.GlobalVirtualGroupFamily) uint64 {
var totalStoreSize uint64
for _, gvgID := range gvgFamily.GlobalVirtualGroupIds {
gvg, found := k.GetGVG(ctx, gvgID)
if !found {
panic("gvg not found when get store size of family")
}
totalStoreSize += gvg.StoredSize
}
return totalStoreSize
}
1 change: 0 additions & 1 deletion x/virtualgroup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (k msgServer) CreateGlobalVirtualGroup(goCtx context.Context, req *types.Ms

// TODO(fynn): add some limit for gvgs in a family
gvgFamily, err := k.GetOrCreateEmptyGVGFamily(ctx, req.FamilyId, sp.Id)

if err != nil {
return nil, err
}
Expand Down
19 changes: 14 additions & 5 deletions x/virtualgroup/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"cosmossdk.io/math"
"github.com/bnb-chain/greenfield/x/virtualgroup/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -11,14 +10,24 @@ func (k Keeper) DepositDenomForGVG(ctx sdk.Context) (res string) {
return params.DepositDenom
}

func (k Keeper) MinDeposit(ctx sdk.Context) (res math.Int) {
func (k Keeper) GVGStakingPrice(ctx sdk.Context) (res sdk.Dec) {
params := k.GetParams(ctx)
return params.MinDeposit
return params.GvgStakingPrice
}

func (k Keeper) GVGStakingPrice(ctx sdk.Context) (res sdk.Dec) {
func (k Keeper) MaxLocalVirtualGroupNumPerBucket(ctx sdk.Context) (res uint32) {
params := k.GetParams(ctx)
return params.GvgStakingPrice
return params.MaxLocalVirtualGroupNumPerBucket
}

func (k Keeper) MaxGlobalVirtualGroupNumPerFamily(ctx sdk.Context) (res uint32) {
params := k.GetParams(ctx)
return params.MaxGlobalVirtualGroupNumPerFamily
}

func (k Keeper) MaxStoreSizePerFamily(ctx sdk.Context) (res uint64) {
params := k.GetParams(ctx)
return params.MaxStoreSizePerFamily
}

// GetParams returns the current sp module parameters.
Expand Down
1 change: 1 addition & 0 deletions x/virtualgroup/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
ErrInvalidGVGCount = errors.Register(ModuleName, 1120, "the count of global virtual group ids is invalid.")
ErrMigrationBucketFailed = errors.Register(ModuleName, 1121, "fail to migration bucket.")
ErrInvalidBlsPubKey = errors.Register(ModuleName, 1122, "invalid bls public key")
ErrStoreSizeExceed = errors.Register(ModuleName, 1123, "store size exceed.")

ErrInvalidDenom = errors.Register(ModuleName, 2000, "Invalid denom.")
)
79 changes: 53 additions & 26 deletions x/virtualgroup/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package types
import (
"errors"
"fmt"
"math/big"
"strings"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

Expand All @@ -19,16 +17,18 @@ const (
)

var (
// DefaultMinDeposit defines the minimum deposit amount for all storage provider
DefaultMinDeposit = math.NewIntFromBigInt(new(big.Int).Mul(big.NewInt(10000), big.NewInt(1e18)))

// DefaultGVGStakingPrice defines the default gvg staking price
// TODO: Set a reasonable value.
DefaultGVGStakingPrice = sdk.NewDecFromIntWithPrec(sdk.NewInt(2), 18)

KeyDepositDenom = []byte("DepositDenom")
KeyMinDeposit = []byte("MinDeposit")
KeyGVGStakingPrice = []byte("GVGStakingPrice")
DefaultGVGStakingPrice = sdk.NewDecFromIntWithPrec(sdk.NewInt(2), 18)
DefaultMaxLocalVirtualGroupNumPerBucket = uint32(10)
DefaultMaxGlocakVirtualGroupNumPerFamily = uint32(10)
DefaultMaxStoreSizePerFamily = uint64(64) * 1024 * 1024 * 1024 //64T

KeyDepositDenom = []byte("DepositDenom")
KeyGVGStakingPrice = []byte("GVGStakingPrice")
KeyMaxLocalVirtualGroupNumPerBucket = []byte("MaxLocalVirtualGroupNumPerBucket")
KeyMaxGlobalVirtualGroupNumPerFamily = []byte("MaxGlobalVirtualGroupNumPerFamily")
KeyMaxStoreSizePerFamily = []byte("MaxStoreSizePerFamily")
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -39,25 +39,30 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new Params instance
func NewParams(depositDenom string, minDeposit math.Int, baseGVGStorageStakingPrice sdk.Dec) Params {
func NewParams(depositDenom string, gvgStakingPrice sdk.Dec, maxLocalVirtualGroupNumPerBucket, maxGlobalVirtualGroupPerFamily uint32,
maxStoreSizePerFamily uint64) Params {
return Params{
DepositDenom: depositDenom,
GvgStakingPrice: baseGVGStorageStakingPrice,
MinDeposit: minDeposit,
DepositDenom: depositDenom,
GvgStakingPrice: gvgStakingPrice,
MaxLocalVirtualGroupNumPerBucket: maxLocalVirtualGroupNumPerBucket,
MaxGlobalVirtualGroupNumPerFamily: maxGlobalVirtualGroupPerFamily,
MaxStoreSizePerFamily: maxStoreSizePerFamily,
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultDepositDenom, DefaultMinDeposit, DefaultGVGStakingPrice)
return NewParams(DefaultDepositDenom, DefaultGVGStakingPrice, DefaultMaxLocalVirtualGroupNumPerBucket, DefaultMaxGlocakVirtualGroupNumPerFamily, DefaultMaxStoreSizePerFamily)
}

// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMinDeposit, &p.MinDeposit, validateMinDeposit),
paramtypes.NewParamSetPair(KeyDepositDenom, &p.DepositDenom, validateDepositDenom),
paramtypes.NewParamSetPair(KeyGVGStakingPrice, &p.GvgStakingPrice, validateGVGStakingPrice),
paramtypes.NewParamSetPair(KeyMaxLocalVirtualGroupNumPerBucket, &p.MaxLocalVirtualGroupNumPerBucket, validateMaxLocalVirtualGroupNumPerBucket),
paramtypes.NewParamSetPair(KeyMaxGlobalVirtualGroupNumPerFamily, &p.MaxGlobalVirtualGroupNumPerFamily, validateMaxGlobalVirtualGroupNumPerFamily),
paramtypes.NewParamSetPair(KeyMaxStoreSizePerFamily, &p.MaxStoreSizePerFamily, validateMaxStoreSizePerFamily),
}
}

Expand Down Expand Up @@ -89,30 +94,52 @@ func validateDepositDenom(i interface{}) error {
return nil
}

func validateMinDeposit(i interface{}) error {
v, ok := i.(math.Int)
func validateGVGStakingPrice(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() || !v.IsPositive() || v.GT(sdk.OneDec()) {
return fmt.Errorf("invalid secondary sp store price ratio")
}
return nil
}

if v.IsNil() {
return fmt.Errorf("minimum deposit amount cannot be nil")
func validateMaxLocalVirtualGroupNumPerBucket(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNegative() {
return fmt.Errorf("minimum deposit amount cannot be lower than 0")
if v == 0 {
return fmt.Errorf("max buckets per account must be positive: %d", v)
}

return nil
}

func validateGVGStakingPrice(i interface{}) error {
v, ok := i.(sdk.Dec)
func validateMaxGlobalVirtualGroupNumPerFamily(i interface{}) error {
v, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() || !v.IsPositive() || v.GT(sdk.OneDec()) {
return fmt.Errorf("invalid secondary sp store price ratio")

if v == 0 {
return fmt.Errorf("max buckets per account must be positive: %d", v)
}

return nil
}

func validateMaxStoreSizePerFamily(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v == 0 {
return fmt.Errorf("max buckets per account must be positive: %d", v)
}

return nil
}
Loading

0 comments on commit 2c2bca4

Please sign in to comment.