Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Dec 14, 2023
1 parent 72fe38f commit 22e3b8d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 70 deletions.
45 changes: 30 additions & 15 deletions e2e/tests/virtualgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,8 @@ func (s *VirtualGroupTestSuite) TestSPExit() {
// 7. SP-x complete exit, it would fail due to there are family and GVG binded to it.
s.SendTxBlockWithExpectErrorString(
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{
Operator: spx.OperatorKey.GetAddr().String(),
StorageProvider: spx.OperatorKey.GetAddr().String()},
StorageProvider: spx.OperatorKey.GetAddr().String(),
ExitStorageProvider: spx.OperatorKey.GetAddr().String()},
spx.OperatorKey,
"not swap out from all the family")

Expand Down Expand Up @@ -857,8 +857,8 @@ func (s *VirtualGroupTestSuite) TestSPExit() {
// 15. SP-x tries to complete exit, but would fail, since SP-2 has a GVG that includes SP-x [2|x,3,4,5,6,7]
s.SendTxBlockWithExpectErrorString(
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{
Operator: spx.OperatorKey.GetAddr().String(),
StorageProvider: spx.OperatorKey.GetAddr().String()},
StorageProvider: spx.OperatorKey.GetAddr().String(),
ExitStorageProvider: spx.OperatorKey.GetAddr().String()},
spx.OperatorKey,
"not swap out from all the gvgs")

Expand Down Expand Up @@ -908,7 +908,7 @@ func (s *VirtualGroupTestSuite) TestSPExit() {
// 23. SP-x complete exit success
s.SendTxBlock(
spx.OperatorKey,
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{StorageProvider: spx.OperatorKey.GetAddr().String()},
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{StorageProvider: spx.OperatorKey.GetAddr().String(), ExitStorageProvider: spx.OperatorKey.GetAddr().String()},
)

// 24 SP-x no longer found on chain
Expand Down Expand Up @@ -957,8 +957,8 @@ func (s *VirtualGroupTestSuite) TestSPExit2() {
s.SendTxBlock(
spx.OperatorKey,
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{
Operator: spx.OperatorKey.GetAddr().String(),
StorageProvider: spx.OperatorKey.GetAddr().String()},
StorageProvider: spx.OperatorKey.GetAddr().String(),
ExitStorageProvider: spx.OperatorKey.GetAddr().String()},
)
_, err = s.Client.StorageProvider(context.Background(), &sptypes.QueryStorageProviderRequest{Id: spx.Info.Id})
s.Require().Error(err)
Expand Down Expand Up @@ -1001,24 +1001,32 @@ func (s *VirtualGroupTestSuite) TestSPExit2() {
s.Require().Equal(familyID, gvgAfterSwapIn.GlobalVirtualGroup.FamilyId)
s.Require().Equal([]uint32{spz.Info.Id, 3, 4, 5, 6, 7}, gvgAfterSwapIn.GlobalVirtualGroup.SecondarySpIds)

// 11 SP-y complete the exit by sp-z
// 11 complete SPy's exit by sp-z
s.SendTxBlock(
spy.OperatorKey,
spz.OperatorKey,
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{
Operator: spz.OperatorKey.GetAddr().String(),
StorageProvider: spy.OperatorKey.GetAddr().String()},
StorageProvider: spz.OperatorKey.GetAddr().String(),
ExitStorageProvider: spy.OperatorKey.GetAddr().String()},
)
_, err = s.Client.StorageProvider(context.Background(), &sptypes.QueryStorageProviderRequest{Id: spy.Info.Id})
s.Require().Error(err)
}

func (s *VirtualGroupTestSuite) TestSPForceExit() {

ctx := context.Background()
user := s.GenAndChargeAccounts(1, 1000000)[0]

// 1. create SPs
spx := s.BaseSuite.CreateNewStorageProvider()
spy := s.BaseSuite.CreateNewStorageProvider()

// gov module balance
govModuleBalance, err := s.Client.Balance(context.Background(), &types2.QueryBalanceRequest{
Denom: s.Config.Denom,
Address: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
})
s.Require().NoError(err)
// 2. SP-x creates a new family with a gvg: {[x|2,3,4,5,6,7]}
gvgID, familyID := s.BaseSuite.CreateGlobalVirtualGroup(spx, 0, []uint32{2, 3, 4, 5, 6, 7}, 1)

Expand Down Expand Up @@ -1093,7 +1101,7 @@ func (s *VirtualGroupTestSuite) TestSPForceExit() {
s.Require().Equal(swapInInfo.SwapInInfo.SuccessorSpId, spy.Info.Id)
s.Require().Equal(swapInInfo.SwapInInfo.TargetSpId, spx.Info.Id)

// object not found
// swapin info not found
_, err = s.Client.SwapInInfo(context.Background(), &virtualgroupmoduletypes.QuerySwapInInfoRequest{
GlobalVirtualGroupId: gvgID,
})
Expand Down Expand Up @@ -1131,10 +1139,17 @@ func (s *VirtualGroupTestSuite) TestSPForceExit() {
// 11 SP-y help complete the exit
s.SendTxBlock(
spy.OperatorKey,
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{Operator: spy.OperatorKey.GetAddr().String(), StorageProvider: spx.OperatorKey.GetAddr().String()},
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{StorageProvider: spy.OperatorKey.GetAddr().String(), ExitStorageProvider: spx.OperatorKey.GetAddr().String()},
)
_, err = s.Client.StorageProvider(context.Background(), &sptypes.QueryStorageProviderRequest{Id: spy.Info.Id})
_, err = s.Client.StorageProvider(context.Background(), &sptypes.QueryStorageProviderRequest{Id: spx.Info.Id})
s.Require().Error(err)

govModuleBalanceAfterSPForcedExit, err := s.Client.Balance(context.Background(), &types2.QueryBalanceRequest{
Denom: s.Config.Denom,
Address: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
})
s.Require().NoError(err)
s.Require().Equal(govModuleBalance.Balance.Amount.Add(types.NewIntFromInt64WithDecimal(10000, types.DecimalBNB)), govModuleBalanceAfterSPForcedExit.Balance.Amount)
}

func (s *VirtualGroupTestSuite) updateParams(params virtualgroupmoduletypes.Params) {
Expand Down Expand Up @@ -1273,7 +1288,7 @@ func (s *VirtualGroupTestSuite) TestSPExit_SwapInfo_Expired() {
// sp-x completes the exit
s.SendTxBlock(
spx.OperatorKey,
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{StorageProvider: spx.OperatorKey.GetAddr().String()},
&virtualgroupmoduletypes.MsgCompleteStorageProviderExit{StorageProvider: spx.OperatorKey.GetAddr().String(), ExitStorageProvider: spx.OperatorKey.GetAddr().String()},
)
_, err = s.Client.StorageProvider(context.Background(), &sptypes.QueryStorageProviderRequest{Id: spx.Info.Id})
s.Require().Error(err)
Expand Down
1 change: 1 addition & 0 deletions x/challenge/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package challenge

import (
"fmt"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
1 change: 1 addition & 0 deletions x/challenge/keeper/msg_server_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"cosmossdk.io/errors"
Expand Down
2 changes: 1 addition & 1 deletion x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ func (k Keeper) DiscontinueObject(ctx sdk.Context, operator sdk.AccAddress, buck
}
swapInInfo, found := k.virtualGroupKeeper.GetSwapInInfo(ctx, bucketInfo.GlobalVirtualGroupFamilyId, virtualgroupmoduletypes.NoSpecifiedGVGId)
if !found || swapInInfo.TargetSpId != spInState.Id || swapInInfo.SuccessorSpId != sp.Id || uint64(ctx.BlockTime().Unix()) >= swapInInfo.ExpirationTime {
return errors.Wrapf(types.ErrAccessDenied, "the sp is allowed to do discontinue objects")
return errors.Wrapf(types.ErrAccessDenied, "the sp is not allowed to do discontinue objects")
}
}

Expand Down
58 changes: 26 additions & 32 deletions x/virtualgroup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,40 +708,34 @@ func (k Keeper) SwapIn(ctx sdk.Context, gvgFamilyID uint32, gvgID uint32, succes

func (k Keeper) setSwapInInfo(ctx sdk.Context, key []byte, successorSPID, targetSPID uint32, curTime uint64) error {
store := ctx.KVStore(k.storeKey)
swapInInfo := &types.SwapInInfo{
SuccessorSpId: successorSPID,
TargetSpId: targetSPID,
ExpirationTime: curTime + k.SwapInValidityPeriod(ctx),
}
bz := store.Get(key)
if bz == nil {
swapInInfo := &types.SwapInInfo{
SuccessorSpId: successorSPID,
TargetSpId: targetSPID,
ExpirationTime: curTime + k.SwapInValidityPeriod(ctx),
}
store.Set(key, k.cdc.MustMarshal(swapInInfo))
} else {
curSwapInInfo := &types.SwapInInfo{}
k.cdc.MustUnmarshal(bz, curSwapInInfo)
if curTime < curSwapInInfo.ExpirationTime {
return types.ErrSwapInFailed.Wrapf("already exist SP(ID=%d) try to swap in", curSwapInInfo.SuccessorSpId)
}
// override the stale swapIn info of prev successor sp
if curSwapInInfo.SuccessorSpId == successorSPID {
return types.ErrSwapInFailed.Wrapf("already tried to swap in but expired")
}
swapInInfo := &types.SwapInInfo{
SuccessorSpId: successorSPID,
TargetSpId: targetSPID,
ExpirationTime: curTime + k.SwapInValidityPeriod(ctx),
}
store.Set(key, k.cdc.MustMarshal(swapInInfo))
return nil
}
curSwapInInfo := &types.SwapInInfo{}
k.cdc.MustUnmarshal(bz, curSwapInInfo)
if curTime < curSwapInInfo.ExpirationTime {
return types.ErrSwapInFailed.Wrapf("already exist SP(ID=%d) try to swap in", curSwapInInfo.SuccessorSpId)
}
// override the stale swapIn info of prev successor sp
if curSwapInInfo.SuccessorSpId == successorSPID {
return types.ErrSwapInFailed.Wrapf("already tried to swap in but expired")
}
store.Set(key, k.cdc.MustMarshal(swapInInfo))
return nil
}

func (k Keeper) DeleteSwapInInfo(ctx sdk.Context, gvgFamilyID, gvgID uint32, successorSPID uint32) error {
store := ctx.KVStore(k.storeKey)

swapInInfo := types.SwapInInfo{}
if gvgFamilyID != types.NoSpecifiedFamilyId {
key := types.GetSwapInFamilyKey(gvgFamilyID)
deleteSwapInfo := func(key []byte) error {
bz := store.Get(key)
if bz == nil {
return types.ErrSwapInFailed.Wrapf("The swap info not found in blockchain.")
Expand All @@ -751,17 +745,17 @@ func (k Keeper) DeleteSwapInInfo(ctx sdk.Context, gvgFamilyID, gvgID uint32, suc
return sptypes.ErrStorageProviderNotFound.Wrapf("spID(%d) is different from the spID(%d) in swapInInfo", successorSPID, swapInInfo.SuccessorSpId)
}
store.Delete(key)
} else {
key := types.GetSwapInGVGKey(gvgID)
bz := store.Get(key)
if bz == nil {
return types.ErrSwapInFailed.Wrapf("The swap info not found in blockchain.")
return nil
}

if gvgFamilyID != types.NoSpecifiedFamilyId {
if err := deleteSwapInfo(types.GetSwapInFamilyKey(gvgFamilyID)); err != nil {
return err
}
k.cdc.MustUnmarshal(bz, &swapInInfo)
if swapInInfo.SuccessorSpId != successorSPID {
return sptypes.ErrStorageProviderNotFound.Wrapf("spID(%d) is different from the spID(%d) in swapInInfo", successorSPID, swapInInfo.SuccessorSpId)
} else {
if err := deleteSwapInfo(types.GetSwapInGVGKey(gvgID)); err != nil {
return err
}
store.Delete(key)
}

if err := ctx.EventManager().EmitTypedEvents(&types.EventCancelSwapIn{
Expand Down
19 changes: 9 additions & 10 deletions x/virtualgroup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"

sdkerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -516,7 +515,7 @@ func (k msgServer) CompleteStorageProviderExit(goCtx context.Context, msg *types
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("Invalid address of SP")
}

exitSP, found := k.spKeeper.GetStorageProviderByOperatorAddr(ctx, exitSPAddr)
exitSP, found = k.spKeeper.GetStorageProviderByOperatorAddr(ctx, exitSPAddr)
if !found {
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("The address of SP is not found")
}
Expand All @@ -543,7 +542,7 @@ func (k msgServer) CompleteStorageProviderExit(goCtx context.Context, msg *types
} else {
forcedExit = true
coins := sdk.NewCoins(sdk.NewCoin(k.spKeeper.DepositDenomForSP(ctx), exitSP.TotalDeposit))
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, sptypes.ModuleName, paymenttypes.ModuleName, coins)
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, sptypes.ModuleName, govtypes.ModuleName, coins)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -631,21 +630,21 @@ func (k msgServer) StorageProviderForceExit(goCtx context.Context, msg *types.Ms

sp, found := k.spKeeper.GetStorageProviderByOperatorAddr(ctx, spAddr)
if !found {
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("The SP with operator address %s must not found", msg.StorageProvider)
return nil, sptypes.ErrStorageProviderNotFound.Wrapf("The SP with operator address %s not found", msg.StorageProvider)
}

exittingSPNum := uint32(0)
exitingSPNum := uint32(0)
maxSPExitingNum := k.SpConcurrentExitNum(ctx)
sps := k.spKeeper.GetAllStorageProviders(ctx)
for _, curSP := range sps {
if curSP.Status == sptypes.STATUS_GRACEFUL_EXITING ||
curSP.Status == sptypes.STATUS_FORCED_EXITING {
exittingSPNum++
exitingSPNum++
if exitingSPNum >= maxSPExitingNum {
return nil, sptypes.ErrStorageProviderExitFailed.Wrapf("%d SP are exiting, allow %d sp exit concurrently", exitingSPNum, maxSPExitingNum)
}
}
}
maxSPExitingNum := k.SpConcurrentExitNum(ctx)
if exittingSPNum >= maxSPExitingNum {
return nil, sptypes.ErrStorageProviderExitFailed.Wrapf("%d SP are exiting, allow %d sp exit concurrently", exittingSPNum, maxSPExitingNum)
}

// Governance can put an SP into force exiting status no matter what status it is in.
sp.Status = sptypes.STATUS_FORCED_EXITING
Expand Down
19 changes: 8 additions & 11 deletions x/virtualgroup/keeper/v1/params.pb.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: greenfield/virtualgroup/params.proto

package types
package v1

import (
fmt "fmt"

Check failure on line 4 in x/virtualgroup/keeper/v1/params.pb.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-latest)

File is not `goimports`-ed (goimports)
Expand Down Expand Up @@ -105,13 +102,13 @@ func (m *Params) GetMaxStoreSizePerFamily() uint64 {
return 0
}

func init() {
proto.RegisterType((*Params)(nil), "greenfield.virtualgroup.Params")
}

func init() {
proto.RegisterFile("greenfield/virtualgroup/params.proto", fileDescriptor_d8ecf89dd5128885)
}
//func init() {
// proto.RegisterType((*Params)(nil), "greenfield.virtualgroup.Params")
//}
//
//func init() {
// proto.RegisterFile("greenfield/virtualgroup/params.proto", fileDescriptor_d8ecf89dd5128885)
//}

var fileDescriptor_d8ecf89dd5128885 = []byte{
// 414 bytes of a gzipped FileDescriptorProto
Expand Down
2 changes: 1 addition & 1 deletion x/virtualgroup/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
5 changes: 5 additions & 0 deletions x/virtualgroup/types/expected_keepers_mocks.go

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

0 comments on commit 22e3b8d

Please sign in to comment.