Skip to content

Commit

Permalink
fix: add hardfork logic and remove delete vgf index
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryTong65 committed Mar 5, 2024
1 parent aaedf3e commit fbec6fa
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 87 deletions.
2 changes: 2 additions & 0 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func (app *App) registerPawneeUpgradeHandler() {
app.UpgradeKeeper.SetUpgradeHandler(upgradetypes.Pawnee,
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
app.Logger().Info("upgrade to ", plan.Name)
// TODO: Select the correct hard fork version to update vgf
app.VirtualgroupKeeper.MigrateGlobalVirtualGroupFamiliesForSP(ctx)
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas(sdk.MsgTypeURL(&storagemoduletypes.MsgUpdateObjectContent{}), 1.2e3))
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas(sdk.MsgTypeURL(&storagemoduletypes.MsgCancelUpdateObjectContent{}), 1.2e3))
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
Expand Down
2 changes: 1 addition & 1 deletion deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function generate_genesis() {
echo -e '[[upgrade]]\nname = "Hulunbeier"\nheight = 21\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
echo -e '[[upgrade]]\nname = "HulunbeierPatch"\nheight = 21\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
echo -e '[[upgrade]]\nname = "Ural"\nheight = 22\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
echo -e '[[upgrade]]\nname = "Pawnee"\nheight = 23\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
echo -e '[[upgrade]]\nname = "Pawnee"\nheight = 300\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
done

# enable swagger API for validator0
Expand Down
3 changes: 1 addition & 2 deletions proto/greenfield/virtualgroup/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import "gogoproto/gogo.proto";

option go_package = "github.com/bnb-chain/greenfield/x/virtualgroup/types";


// PickVGFStrategy represents the method for selecting the best global virtual group family based on the strategy
enum PickVGFStrategy {
option (gogoproto.goproto_enum_prefix) = false;

StrategyMaximizeFreeStoreSize = 0;
}
}
2 changes: 1 addition & 1 deletion proto/greenfield/virtualgroup/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package greenfield.virtualgroup;
import "cosmos/base/query/v1beta1/pagination.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "greenfield/virtualgroup/common.proto";
import "greenfield/virtualgroup/params.proto";
import "greenfield/virtualgroup/types.proto";
import "greenfield/virtualgroup/common.proto";

option go_package = "github.com/bnb-chain/greenfield/x/virtualgroup/types";

Expand Down
15 changes: 15 additions & 0 deletions swagger/static/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6486,6 +6486,13 @@ paths:
required: false
type: integer
format: int64
- name: pick_vgf_strategy
in: query
required: false
type: string
enum:
- StrategyMaximizeFreeStoreSize
default: StrategyMaximizeFreeStoreSize
tags:
- Query
/greenfield/virtualgroup/swap_in_info:
Expand Down Expand Up @@ -37230,6 +37237,14 @@ definitions:
type: string
description: the the number of sp allowed to exit concurrently.
description: Params defines the parameters for the module.
greenfield.virtualgroup.PickVGFStrategy:
type: string
enum:
- StrategyMaximizeFreeStoreSize
default: StrategyMaximizeFreeStoreSize
title: >-
PickVGFStrategy represents the method for selecting the best global
virtual group family based on the strategy
greenfield.virtualgroup.QueryGlobalVirtualGroupByFamilyIDResponse:
type: object
properties:
Expand Down
53 changes: 42 additions & 11 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ import (

"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/cosmos/gogoproto/proto"

"github.com/bnb-chain/greenfield/internal/sequence"
gnfdtypes "github.com/bnb-chain/greenfield/types"
types2 "github.com/bnb-chain/greenfield/types"
Expand All @@ -26,6 +18,13 @@ import (
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
"github.com/bnb-chain/greenfield/x/storage/types"
virtualgroupmoduletypes "github.com/bnb-chain/greenfield/x/virtualgroup/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/cosmos/gogoproto/proto"
)

type (
Expand Down Expand Up @@ -117,6 +116,25 @@ func (k Keeper) CreateBucket(
return sdkmath.ZeroUint(), errors.Wrap(types.ErrNoSuchStorageProvider, "the storage provider is not in service")
}

// check primary sp approval
// TODO: Select the correct hard fork version to update vgf
if ctx.IsUpgraded(upgradetypes.Pawnee) && opts.PrimarySpApproval.ExpiredHeight < uint64(ctx.BlockHeight()) {
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrInvalidApproval, "The approval of sp is expired.")
}

// TODO: Select the correct hard fork version to update vgf
if ctx.IsUpgraded(upgradetypes.Pawnee) {
err = k.VerifySPAndSignature(ctx, sp, opts.ApprovalMsgBytes, opts.PrimarySpApproval.Sig, ownerAcc)
if err != nil {
return sdkmath.ZeroUint(), err
}
} else {
err = k.VerifySP(ctx, sp, ownerAcc)
if err != nil {
return sdkmath.ZeroUint(), err
}
}

gvgFamily, err := k.virtualGroupKeeper.GetAndCheckGVGFamilyAvailableForNewBucket(ctx, opts.PrimarySpApproval.GlobalVirtualGroupFamilyId)
if err != nil {
return sdkmath.ZeroUint(), err
Expand Down Expand Up @@ -583,9 +601,22 @@ func (k Keeper) CreateObject(
creator = operator
}

err = k.VerifySP(ctx, sp, operator)
if err != nil {
return sdkmath.ZeroUint(), err
// TODO: Select the correct hard fork version to update vgf
if ctx.IsUpgraded(upgradetypes.Pawnee) && opts.PrimarySpApproval.ExpiredHeight < uint64(ctx.BlockHeight()) {
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrInvalidApproval, "The approval of sp is expired.")
}

// TODO: Select the correct hard fork version to update vgf
if ctx.IsUpgraded(upgradetypes.Pawnee) {
err = k.VerifySPAndSignature(ctx, sp, opts.ApprovalMsgBytes, opts.PrimarySpApproval.Sig, operator)
if err != nil {
return sdkmath.ZeroUint(), err
}
} else {
err = k.VerifySP(ctx, sp, operator)
if err != nil {
return sdkmath.ZeroUint(), err
}
}

objectKey := types.GetObjectKey(bucketName, objectName)
Expand Down
17 changes: 16 additions & 1 deletion x/virtualgroup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
Expand Down Expand Up @@ -167,7 +168,6 @@ func (k Keeper) DeleteGVG(ctx sdk.Context, primarySp *sptypes.StorageProvider, g
k.paymentKeeper.IsEmptyNetFlow(ctx, sdk.MustAccAddressFromHex(gvgFamily.VirtualPaymentAddress)) &&
!ctx.IsUpgraded(upgradetypes.Manchurian) {
store.Delete(types.GetGVGFamilyKey(gvg.FamilyId))
k.DeleteGVGFamilyWithinSP(ctx, primarySp.Id, gvg.FamilyId)
if err := ctx.EventManager().EmitTypedEvents(&types.EventDeleteGlobalVirtualGroupFamily{
Id: gvgFamily.Id,
PrimarySpId: gvgFamily.PrimarySpId,
Expand Down Expand Up @@ -556,6 +556,21 @@ func (k Keeper) BatchSetGVGFamilyStatisticsWithinSP(ctx sdk.Context, vgfStatisti
}
}

func (k Keeper) MigrateGlobalVirtualGroupFamiliesForSP(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
gvgFamilyStore := prefix.NewStore(store, types.GVGFamilyKey)

iterator := gvgFamilyStore.Iterator(nil, nil)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var gvgFamily types.GlobalVirtualGroupFamily
k.cdc.MustUnmarshal(iterator.Value(), &gvgFamily)
gvgFamilyStatistics := k.GetOrCreateGVGFamilyStatisticsWithinSP(ctx, gvgFamily.PrimarySpId)
gvgFamilyStatistics.GlobalVirtualGroupFamilyIds = append(gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, gvgFamily.PrimarySpId)
k.SetGVGFamilyStatisticsWithinSP(ctx, gvgFamilyStatistics)
}
}

// 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.
Expand Down
Loading

0 comments on commit fbec6fa

Please sign in to comment.