Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Dec 4, 2023
1 parent 312358c commit c14218a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
27 changes: 25 additions & 2 deletions e2e/tests/virtualgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,14 +1001,24 @@ func (s *VirtualGroupTestSuite) TestSPExit_SwapInfo_Expired() {

func (s *VirtualGroupTestSuite) TestSPForceExit() {
ctx := context.Background()
user := s.GenAndChargeAccounts(1, 1000000)[0]

// 1. create SPs
spx := s.BaseSuite.CreateNewStorageProvider()
spy := s.BaseSuite.CreateNewStorageProvider()
// 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)

// 3. create a proposal that put SP-x to FORCE_EXIT
// User creates an object and sealed by the SP-x's GVG
bucketName := storagetestutil.GenRandomBucketName()
objectName := storagetestutil.GenRandomBucketName()
s.BaseSuite.CreateObject(user, spx, gvgID, bucketName, objectName)
objectResp, err := s.Client.HeadObject(context.Background(), &storagetypes.QueryHeadObjectRequest{
BucketName: bucketName, ObjectName: objectName,
})
s.Require().NoError(err)

// 3. create a proposal that puts SP-x to FORCE_EXIT
govAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
msgForceExit := virtualgroupmoduletypes.NewMsgStorageProviderForceExit(govAddr, spx.OperatorKey.GetAddr())

Expand Down Expand Up @@ -1070,12 +1080,25 @@ func (s *VirtualGroupTestSuite) TestSPForceExit() {
s.Require().Equal(swapInInfo.SwapInInfo.SuccessorSpId, spy.Info.Id)
s.Require().Equal(swapInInfo.SwapInInfo.TargetSpId, spx.Info.Id)

// should be null
// object not found
swapInInfo, err = s.Client.SwapInInfo(context.Background(), &virtualgroupmoduletypes.QuerySwapInInfoRequest{
GlobalVirtualGroupId: gvgID,
})
s.Require().Error(err)

// SP-y is able to discontinue the object as a successor Primary SP
msgDiscontinueObject := &storagetypes.MsgDiscontinueObject{
Operator: spy.GcKey.GetAddr().String(),
BucketName: bucketName,
ObjectIds: []sdk.Uint{objectResp.ObjectInfo.Id},
}
s.SendTxBlock(spy.GcKey, msgDiscontinueObject)
time.Sleep(2 * time.Second)
_, err = s.Client.HeadObject(context.Background(), &storagetypes.QueryHeadObjectRequest{
BucketName: bucketName, ObjectName: objectName,
})
s.Require().Error(err)

// 8. SP-y complete SwapIn
msgCompleteSwapIn := virtualgroupmoduletypes.NewMsgCompleteSwapIn(spy.OperatorKey.GetAddr(), familyID, 0)
s.SendTxBlock(spy.OperatorKey, msgCompleteSwapIn)
Expand Down
13 changes: 10 additions & 3 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ func (k Keeper) UpdateBucketInfo(ctx sdk.Context, operator sdk.AccAddress, bucke
return types.ErrSourceTypeMismatch
}

//TODO change name
if ctx.IsUpgraded(upgradetypes.Pampas) {
//TODO rename the harfork
if ctx.IsUpgraded(upgradetypes.Eddystone) {
sp := k.MustGetPrimarySPForBucket(ctx, bucketInfo)
if sp.Status == sptypes.STATUS_GRACEFUL_EXITING || sp.Status == sptypes.STATUS_FORCE_EXITING {
return types.ErrUpdateQuotaFailed.Wrapf("The SP is in %s, bucket can not be updated", sp.Status)
Expand Down Expand Up @@ -905,6 +905,13 @@ func (k Keeper) DeleteObject(
}

spInState := k.MustGetPrimarySPForBucket(ctx, bucketInfo)

if ctx.IsUpgraded(upgradetypes.Eddystone) {
if spInState.Status == sptypes.STATUS_GRACEFUL_EXITING || spInState.Status == sptypes.STATUS_FORCE_EXITING {
return types.ErrUpdateQuotaFailed.Wrapf("The SP is in %s, object can not be deleted", spInState.Status)
}
}

internalBucketInfo := k.MustGetInternalBucketInfo(ctx, bucketInfo.Id)

err := k.UnChargeObjectStoreFee(ctx, spInState.Id, bucketInfo, internalBucketInfo, objectInfo)
Expand Down Expand Up @@ -1175,7 +1182,7 @@ func (k Keeper) DiscontinueObject(ctx sdk.Context, operator sdk.AccAddress, buck
if found {
if swapInInfo.TargetSpId != spInState.Id ||
swapInInfo.SuccessorSpId != sp.Id ||
uint64(ctx.BlockTime().Unix()) > swapInInfo.ExpirationTime {
uint64(ctx.BlockTime().Unix()) >= swapInInfo.ExpirationTime {
return errors.Wrapf(types.ErrAccessDenied, "the sp is allowed to do discontinue objects, reserved swapInfo=%s", swapInInfo.String())
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/virtualgroup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (k Keeper) SwapInInfo(goCtx context.Context, req *types.QuerySwapInInfoRequ
if !found {
return nil, types.ErrSwapInInfoNotExist
}
if uint64(ctx.BlockTime().Unix()) > swapInInfo.ExpirationTime {
if uint64(ctx.BlockTime().Unix()) >= swapInInfo.ExpirationTime {
return nil, types.ErrSwapInInfoExpired
}
return &types.QuerySwapInInfoResponse{
Expand Down
3 changes: 1 addition & 2 deletions x/virtualgroup/types/message_storage_provider_force_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func (msg *MsgStorageProviderForceExit) ValidateBasic() error {
if _, err := sdk.AccAddressFromHexUnsafe(msg.Authority); err != nil {
return errors.Wrap(err, "invalid authority address")
}
_, err := sdk.AccAddressFromHexUnsafe(msg.StorageProvider)
if err != nil {
if _, err := sdk.AccAddressFromHexUnsafe(msg.StorageProvider); err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sp address (%s)", err)
}
return nil
Expand Down

0 comments on commit c14218a

Please sign in to comment.