Skip to content

Commit

Permalink
Merge pull request #611 from bnb-chain/vgf-strategy
Browse files Browse the repository at this point in the history
feat: add pick vgf strategy
  • Loading branch information
alexgao001 authored Apr 18, 2024
2 parents 0ef6152 + 86f5423 commit 964001c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
3 changes: 3 additions & 0 deletions proto/greenfield/virtualgroup/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ enum PickVGFStrategy {
option (gogoproto.goproto_enum_prefix) = false;

Strategy_Maximize_Free_Store_Size = 0;
Strategy_Minimal_Free_Store_Size = 1;
Strategy_Oldest_Create_Time = 2;
Strategy_Recentest_Create_Time = 3;
}
6 changes: 6 additions & 0 deletions swagger/static/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6585,6 +6585,9 @@ paths:
type: string
enum:
- Strategy_Maximize_Free_Store_Size
- Strategy_Minimal_Free_Store_Size
- Strategy_Oldest_Create_Time
- Strategy_Recentest_Create_Time
default: Strategy_Maximize_Free_Store_Size
tags:
- Query
Expand Down Expand Up @@ -37383,6 +37386,9 @@ definitions:
type: string
enum:
- Strategy_Maximize_Free_Store_Size
- Strategy_Minimal_Free_Store_Size
- Strategy_Oldest_Create_Time
- Strategy_Recentest_Create_Time
default: Strategy_Maximize_Free_Store_Size
title: >-
PickVGFStrategy represents the method for selecting the best global
Expand Down
29 changes: 27 additions & 2 deletions x/virtualgroup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"math"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/bnb-chain/greenfield/x/virtualgroup/types"
)
Expand Down Expand Up @@ -230,6 +231,30 @@ func (k Keeper) QuerySpOptimalGlobalVirtualGroupFamily(goCtx context.Context, re
}
}
}
case types.Strategy_Minimal_Free_Store_Size:
for _, gvgfID := range stats.GlobalVirtualGroupFamilyIds {
gvgFamily, found := k.GetGVGFamily(ctx, gvgfID)
if !found {
return nil, types.ErrGVGFamilyNotExist
}
totalStakingSize, stored, err := k.GetGlobalVirtualFamilyTotalStakingAndStoredSize(ctx, gvgFamily)
if err != nil {
return nil, err
}
currentFreeStoreSize = math.Min(float64(totalStakingSize), float64(k.MaxStoreSizePerFamily(ctx))) - float64(stored)
if currentFreeStoreSize < freeStoreSize {
familyID = gvgFamily.Id
freeStoreSize = currentFreeStoreSize
}
}
case types.Strategy_Oldest_Create_Time:
if len(stats.GlobalVirtualGroupFamilyIds) != 0 {
familyID = stats.GlobalVirtualGroupFamilyIds[0]
}
case types.Strategy_Recentest_Create_Time:
if len(stats.GlobalVirtualGroupFamilyIds) != 0 {
familyID = stats.GlobalVirtualGroupFamilyIds[len(stats.GlobalVirtualGroupFamilyIds)-1]
}
default:
return nil, status.Error(codes.InvalidArgument, "invalid pick vgf strategy")
}
Expand Down
41 changes: 27 additions & 14 deletions x/virtualgroup/types/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 964001c

Please sign in to comment.