Skip to content

Commit

Permalink
fix: delete and set GVGFamilyStatistics after hard fork
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryTong65 committed Mar 5, 2024
1 parent 7109e4b commit b8e69c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
18 changes: 7 additions & 11 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@ 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) {
// check primary sp approval
if opts.PrimarySpApproval.ExpiredHeight < uint64(ctx.BlockHeight()) {
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrInvalidApproval, "The approval of sp is expired.")
}
err = k.VerifySPAndSignature(ctx, sp, opts.ApprovalMsgBytes, opts.PrimarySpApproval.Sig, ownerAcc)
if err != nil {
return sdkmath.ZeroUint(), err
Expand Down Expand Up @@ -595,13 +593,11 @@ func (k Keeper) CreateObject(
creator = operator
}

// 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) {
if opts.PrimarySpApproval.ExpiredHeight < uint64(ctx.BlockHeight()) {
return sdkmath.ZeroUint(), errors.Wrapf(types.ErrInvalidApproval, "The approval of sp is expired.")
}
err = k.VerifySPAndSignature(ctx, sp, opts.ApprovalMsgBytes, opts.PrimarySpApproval.Sig, operator)
if err != nil {
return sdkmath.ZeroUint(), err
Expand Down
42 changes: 25 additions & 17 deletions x/virtualgroup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import (
math2 "math"

"cosmossdk.io/math"
"github.com/bnb-chain/greenfield/internal/sequence"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
"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"
"github.com/cosmos/cosmos-sdk/types/address"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"golang.org/x/exp/slices"

"github.com/bnb-chain/greenfield/internal/sequence"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
"github.com/bnb-chain/greenfield/x/virtualgroup/types"
)

type (
Expand Down Expand Up @@ -253,9 +251,11 @@ func (k Keeper) GetOrCreateEmptyGVGFamily(ctx sdk.Context, familyID uint32, prim
PrimarySpId: primarySPID,
VirtualPaymentAddress: k.DeriveVirtualPaymentAccount(types.GVGFamilyName, id).String(),
}
gvgFamilyStatistics := k.GetOrCreateGVGFamilyStatisticsWithinSP(ctx, primarySPID)
gvgFamilyStatistics.GlobalVirtualGroupFamilyIds = append(gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, id)
k.SetGVGFamilyStatisticsWithinSP(ctx, gvgFamilyStatistics)
if ctx.IsUpgraded(upgradetypes.Pawnee) {
gvgFamilyStatistics := k.GetOrCreateGVGFamilyStatisticsWithinSP(ctx, primarySPID)
gvgFamilyStatistics.GlobalVirtualGroupFamilyIds = append(gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, id)
k.SetGVGFamilyStatisticsWithinSP(ctx, gvgFamilyStatistics)
}
return &gvgFamily, nil
} else {
bz := store.Get(types.GetGVGFamilyKey(familyID))
Expand Down Expand Up @@ -294,8 +294,13 @@ func (k Keeper) SwapAsPrimarySP(ctx sdk.Context, primarySP, successorSP *sptypes

srcStat := k.MustGetGVGStatisticsWithinSP(ctx, primarySP.Id)
dstStat := k.GetOrCreateGVGStatisticsWithinSP(ctx, successorSP.Id)
srcVGFStat := k.MustGetGVGFamilyStatisticsWithinSP(ctx, primarySP.Id)
dstVGFStat := k.GetOrCreateGVGFamilyStatisticsWithinSP(ctx, successorSP.Id)
// TODO: Select the correct hard fork version to update vgf
var srcVGFStat *types.GVGFamilyStatisticsWithinSP
var dstVGFStat *types.GVGFamilyStatisticsWithinSP
if ctx.IsUpgraded(upgradetypes.Pawnee) {
srcVGFStat = k.MustGetGVGFamilyStatisticsWithinSP(ctx, primarySP.Id)
dstVGFStat = k.GetOrCreateGVGFamilyStatisticsWithinSP(ctx, successorSP.Id)
}

var gvgs []*types.GlobalVirtualGroup
for _, gvgID := range family.GlobalVirtualGroupIds {
Expand Down Expand Up @@ -365,9 +370,11 @@ func (k Keeper) SwapAsPrimarySP(ctx sdk.Context, primarySP, successorSP *sptypes
}
k.SetGVGStatisticsWithSP(ctx, srcStat)
k.SetGVGStatisticsWithSP(ctx, dstStat)
k.DeleteGVGFamilyWithinSP(ctx, srcVGFStat.SpId, family.Id)
dstVGFStat.GlobalVirtualGroupFamilyIds = append(dstVGFStat.GlobalVirtualGroupFamilyIds, family.Id)
k.SetGVGFamilyStatisticsWithinSP(ctx, dstVGFStat)
if ctx.IsUpgraded(upgradetypes.Pawnee) {
k.DeleteGVGFamilyWithinSP(ctx, srcVGFStat.SpId, family.Id)
dstVGFStat.GlobalVirtualGroupFamilyIds = append(dstVGFStat.GlobalVirtualGroupFamilyIds, family.Id)
k.SetGVGFamilyStatisticsWithinSP(ctx, dstVGFStat)
}

return nil
}
Expand Down Expand Up @@ -566,11 +573,12 @@ func (k Keeper) MigrateGlobalVirtualGroupFamiliesForSP(ctx sdk.Context) {
for ; iterator.Valid(); iterator.Next() {
var gvgFamily types.GlobalVirtualGroupFamily
k.cdc.MustUnmarshal(iterator.Value(), &gvgFamily)
k.Logger(ctx).Info(fmt.Sprintf("gvgFamily id %d, sp id: %d", gvgFamily.Id, gvgFamily.PrimarySpId))
gvgFamilyStatistics := k.GetOrCreateGVGFamilyStatisticsWithinSP(ctx, gvgFamily.PrimarySpId)
if !slices.Contains(gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, gvgFamily.Id) {
gvgFamilyStatistics.GlobalVirtualGroupFamilyIds = append(gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, gvgFamily.Id)
k.SetGVGFamilyStatisticsWithinSP(ctx, gvgFamilyStatistics)
}
k.Logger(ctx).Info(fmt.Sprintf("gvgFamilyStatistics1 gvgFamily id %v, sp id: %d", gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, gvgFamilyStatistics.SpId))
gvgFamilyStatistics.GlobalVirtualGroupFamilyIds = append(gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, gvgFamily.Id)
k.Logger(ctx).Info(fmt.Sprintf("gvgFamilyStatistics2 gvgFamily id %v, sp id: %d", gvgFamilyStatistics.GlobalVirtualGroupFamilyIds, gvgFamilyStatistics.SpId))
k.SetGVGFamilyStatisticsWithinSP(ctx, gvgFamilyStatistics)
}
}

Expand Down

0 comments on commit b8e69c7

Please sign in to comment.