From 1ea890f827b8d6a348701aaeb66f374b33241824 Mon Sep 17 00:00:00 2001 From: yutianwu Date: Fri, 28 Jul 2023 17:27:53 +0800 Subject: [PATCH] add test cases for cross chain apps --- x/storage/keeper/cross_app_bucket_test.go | 156 +++++++++++ x/storage/keeper/cross_app_group_test.go | 307 ++++++++++++++++++++++ x/storage/keeper/cross_app_object_test.go | 162 ++++++++++++ x/storage/types/crosschain.go | 34 ++- 4 files changed, 658 insertions(+), 1 deletion(-) create mode 100644 x/storage/keeper/cross_app_group_test.go create mode 100644 x/storage/keeper/cross_app_object_test.go diff --git a/x/storage/keeper/cross_app_bucket_test.go b/x/storage/keeper/cross_app_bucket_test.go index 89059a317..43bf71c60 100644 --- a/x/storage/keeper/cross_app_bucket_test.go +++ b/x/storage/keeper/cross_app_bucket_test.go @@ -89,6 +89,162 @@ func (s *TestSuite) TestSynCreateBucket() { s.Require().NoError(res.Err) } +func (s *TestSuite) TestSynMirrorBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + synPackage := types.MirrorBucketSynPackage{ + Owner: sample.RandAccAddress(), + Id: big.NewInt(10), + } + + serializedSynPack, err := synPackage.Serialize() + s.Require().NoError(err) + serializedSynPack = append([]byte{types.OperationMirrorBucket}, serializedSynPack...) + + // case 1: normal case + res := app.ExecuteSynPackage(s.ctx, nil, serializedSynPack) + s.Require().NoError(res.Err) +} + func (s *TestSuite) TestAckMirrorBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + ackPackage := types.MirrorBucketAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + } + serializedAckPack, err := ackPackage.Serialize() + s.Require().NoError(err) + serializedAckPack = append([]byte{types.OperationMirrorBucket}, serializedAckPack...) + + // case 1: bucket not found + storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPack) + s.Require().ErrorIs(res.Err, types.ErrNoSuchBucket) + + // case 2: success case + storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&types.BucketInfo{}, true) + storageKeeper.EXPECT().SetBucketInfo(gomock.Any(), gomock.Any()).Return() + + res = app.ExecuteAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPack) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestAckCreateBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + ackPackage := types.CreateBucketAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + Creator: sample.RandAccAddress(), + ExtraData: []byte("extra data"), + } + + serializedAckPack := ackPackage.MustSerialize() + serializedAckPack = append([]byte{types.OperationCreateBucket}, serializedAckPack...) + + // case 1: normal case + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPack) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestAckDeleteBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + ackPackage := types.DeleteBucketAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedAckPack := ackPackage.MustSerialize() + serializedAckPack = append([]byte{types.OperationDeleteBucket}, serializedAckPack...) + + // case 1: normal case + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPack) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckMirrorBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + ackPackage := types.MirrorBucketSynPackage{ + Id: big.NewInt(10), + Owner: sample.RandAccAddress(), + } + + serializedAckPack, err := ackPackage.Serialize() + s.Require().NoError(err) + serializedAckPack = append([]byte{types.OperationMirrorBucket}, serializedAckPack...) + + // case 1: bucket not found + storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&types.BucketInfo{}, false) + + res := app.ExecuteFailAckPackage(s.ctx, nil, serializedAckPack) + s.Require().ErrorIs(res.Err, types.ErrNoSuchBucket) + + // case 2: normal case + storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&types.BucketInfo{}, true) + storageKeeper.EXPECT().SetBucketInfo(gomock.Any(), gomock.Any()).Return() + + res = app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPack) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckCreateBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + createSynPackage := types.CreateBucketSynPackage{ + Creator: sample.RandAccAddress(), + BucketName: "bucketname", + ExtraData: []byte("extra data"), + PaymentAddress: sample.RandAccAddress(), + PrimarySpAddress: sample.RandAccAddress(), + } + serializedSynPackage := createSynPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationCreateBucket}, serializedSynPackage...) + + // case 1: normal case + res := app.ExecuteFailAckPackage(s.ctx, nil, serializedSynPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckDeleteBucket() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewBucketApp(storageKeeper) + deleteSynPackage := types.DeleteBucketSynPackage{ + Operator: sample.RandAccAddress(), + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedSynPackage := deleteSynPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationDeleteBucket}, serializedSynPackage...) + + // case 1: normal case + res := app.ExecuteFailAckPackage(s.ctx, nil, serializedSynPackage) + s.Require().NoError(res.Err) } diff --git a/x/storage/keeper/cross_app_group_test.go b/x/storage/keeper/cross_app_group_test.go new file mode 100644 index 000000000..23a3a6aed --- /dev/null +++ b/x/storage/keeper/cross_app_group_test.go @@ -0,0 +1,307 @@ +package keeper_test + +import ( + "fmt" + "math/big" + + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/golang/mock/gomock" + + "github.com/bnb-chain/greenfield/testutil/sample" + gnfderrors "github.com/bnb-chain/greenfield/types/errors" + "github.com/bnb-chain/greenfield/x/storage/keeper" + "github.com/bnb-chain/greenfield/x/storage/types" +) + +func (s *TestSuite) TestAckMirrorGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.MirrorGroupAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + } + + serializedAckPackage, err := ackPackage.Serialize() + s.Require().NoError(err) + serializedAckPackage = append([]byte{types.OperationMirrorGroup}, serializedAckPackage...) + + // case 1: mirror group not found + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchGroup) + + // case 2: normal case + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(&types.GroupInfo{}, true) + storageKeeper.EXPECT().SetGroupInfo(gomock.Any(), gomock.Any()).Return() + res = app.ExecuteAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestAckCreateGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.CreateGroupAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + Creator: sample.RandAccAddress(), + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationCreateGroup}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestAckDeleteGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.DeleteGroupAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationDeleteGroup}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckMirrorGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.MirrorGroupSynPackage{ + Id: big.NewInt(10), + Owner: sample.RandAccAddress(), + } + + serializedAckPackage, err := ackPackage.Serialize() + s.Require().NoError(err) + serializedAckPackage = append([]byte{types.OperationMirrorGroup}, serializedAckPackage...) + + // case 1: group not found + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res := app.ExecuteFailAckPackage(s.ctx, nil, serializedAckPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchGroup) + + // case 2: normal case + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(&types.GroupInfo{}, true) + storageKeeper.EXPECT().SetGroupInfo(gomock.Any(), gomock.Any()).Return() + res = app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckCreateGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.CreateGroupSynPackage{ + Creator: sample.RandAccAddress(), + GroupName: "group", + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationCreateGroup}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckDeleteGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.DeleteBucketSynPackage{ + Operator: sample.RandAccAddress(), + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationDeleteGroup}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckUpdateGroupMember() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + ackPackage := types.UpdateGroupMemberSynPackage{ + Operator: sample.RandAccAddress(), + GroupId: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationUpdateGroupMember}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestSynMirrorGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + synPackage := types.MirrorGroupSynPackage{ + Owner: sample.RandAccAddress(), + Id: big.NewInt(10), + } + + serializedSynPackage, err := synPackage.Serialize() + s.Require().NoError(err) + serializedSynPackage = append([]byte{types.OperationMirrorGroup}, serializedSynPackage...) + + // case 1: normal case + res := app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestSynCreateGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + synPackage := types.CreateGroupSynPackage{ + Creator: sample.RandAccAddress(), + GroupName: "group", + ExtraData: []byte("extra data"), + } + + // case 1: invalid group name + synPackage.GroupName = "g" + serializedSynPackage := synPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationCreateGroup}, serializedSynPackage...) + + res := app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorIs(res.Err, gnfderrors.ErrInvalidGroupName) + s.Require().NotEmpty(res.Payload) + + // case 2: create group error + synPackage.GroupName = "group" + serializedSynPackage = synPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationCreateGroup}, serializedSynPackage...) + + storageKeeper.EXPECT().CreateGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(math.NewUint(0), fmt.Errorf("create group error")) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorContains(res.Err, "create group error") + + // case 3: normal case + storageKeeper.EXPECT().CreateGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(math.NewUint(10), nil) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestSynDeleteGroup() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + synPackage := types.DeleteBucketSynPackage{ + Operator: sample.RandAccAddress(), + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + serializedSynPackage := synPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationDeleteGroup}, serializedSynPackage...) + + // case 1: group not exist + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res := app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchGroup) + s.Require().NotEmpty(res.Payload) + + // case 2: delete group error + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(&types.GroupInfo{}, true) + storageKeeper.EXPECT().DeleteGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("delete group error")) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorContains(res.Err, "delete group error") + + // case 3: normal case + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(&types.GroupInfo{ + Id: sdk.NewUint(10), + }, true) + storageKeeper.EXPECT().DeleteGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestSynUpdateGroupMember() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewGroupApp(storageKeeper) + synPackage := types.UpdateGroupMemberSynPackage{ + Operator: sample.RandAccAddress(), + GroupId: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + // case 1: invalid package + synPackage.OperationType = 3 + serializedSynPackage := synPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationUpdateGroupMember}, serializedSynPackage...) + + res := app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorIs(res.Err, types.ErrInvalidOperationType) + s.Require().NotEmpty(res.Payload) + + // case 2: group not exist + synPackage.OperationType = types.OperationAddGroupMember + serializedSynPackage = synPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationUpdateGroupMember}, serializedSynPackage...) + + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchGroup) + s.Require().NotEmpty(res.Payload) + + // case 3: update group member error + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(&types.GroupInfo{}, true) + storageKeeper.EXPECT().UpdateGroupMember(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("update group member error")) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().ErrorContains(res.Err, "update group member error") + + // case 4: normal case + storageKeeper.EXPECT().GetGroupInfoById(gomock.Any(), gomock.Any()).Return(&types.GroupInfo{ + Id: sdk.NewUint(10), + }, true) + storageKeeper.EXPECT().UpdateGroupMember(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + res = app.ExecuteSynPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedSynPackage) + s.Require().NoError(res.Err) +} diff --git a/x/storage/keeper/cross_app_object_test.go b/x/storage/keeper/cross_app_object_test.go new file mode 100644 index 000000000..d535d3aeb --- /dev/null +++ b/x/storage/keeper/cross_app_object_test.go @@ -0,0 +1,162 @@ +package keeper_test + +import ( + "fmt" + "math/big" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/golang/mock/gomock" + + "github.com/bnb-chain/greenfield/testutil/sample" + "github.com/bnb-chain/greenfield/x/storage/keeper" + "github.com/bnb-chain/greenfield/x/storage/types" +) + +func (s *TestSuite) TestAckMirrorObject() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewObjectApp(storageKeeper) + ackPackage := types.MirrorObjectAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + } + + serializedAckPackage, err := ackPackage.Serialize() + s.Require().NoError(err) + serializedAckPackage = append([]byte{types.OperationMirrorObject}, serializedAckPackage...) + + // case 1: object not exist + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchObject) + + // case 2: normal case + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(&types.ObjectInfo{}, true) + storageKeeper.EXPECT().SetObjectInfo(gomock.Any(), gomock.Any()).Return() + res = app.ExecuteAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestAckDeleteObject() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewObjectApp(storageKeeper) + ackPackage := types.DeleteObjectAckPackage{ + Status: types.StatusSuccess, + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationDeleteObject}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckMirrorObject() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewObjectApp(storageKeeper) + ackPackage := types.MirrorObjectSynPackage{ + Owner: sample.RandAccAddress(), + Id: big.NewInt(10), + } + + serializedAckPackage, err := ackPackage.Serialize() + s.Require().NoError(err) + serializedAckPackage = append([]byte{types.OperationMirrorObject}, serializedAckPackage...) + + // case 1: object not exist + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res := app.ExecuteFailAckPackage(s.ctx, nil, serializedAckPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchObject) + + // case 2: normal case + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(&types.ObjectInfo{}, true) + storageKeeper.EXPECT().SetObjectInfo(gomock.Any(), gomock.Any()).Return() + res = app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestFailAckDeleteObject() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewObjectApp(storageKeeper) + ackPackage := types.DeleteBucketSynPackage{ + Operator: sample.RandAccAddress(), + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedAckPackage := ackPackage.MustSerialize() + serializedAckPackage = append([]byte{types.OperationDeleteObject}, serializedAckPackage...) + + // case 1: normal case + res := app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestSynMirrorObject() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewObjectApp(storageKeeper) + synPackage := types.MirrorObjectSynPackage{ + Owner: sample.RandAccAddress(), + Id: big.NewInt(10), + } + + serializedSynPackage, err := synPackage.Serialize() + s.Require().NoError(err) + serializedSynPackage = append([]byte{types.OperationMirrorObject}, serializedSynPackage...) + + // case 1: normal case + res := app.ExecuteSynPackage(s.ctx, nil, serializedSynPackage) + s.Require().NoError(res.Err) +} + +func (s *TestSuite) TestSynDeleteObject() { + ctrl := gomock.NewController(s.T()) + storageKeeper := types.NewMockStorageKeeper(ctrl) + storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes() + + app := keeper.NewObjectApp(storageKeeper) + synPackage := types.DeleteBucketSynPackage{ + Operator: sample.RandAccAddress(), + Id: big.NewInt(10), + ExtraData: []byte("extra data"), + } + + serializedSynPackage := synPackage.MustSerialize() + serializedSynPackage = append([]byte{types.OperationDeleteObject}, serializedSynPackage...) + + // case 1: object not exist + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(nil, false) + res := app.ExecuteSynPackage(s.ctx, nil, serializedSynPackage) + s.Require().ErrorIs(res.Err, types.ErrNoSuchObject) + + // case 2: delete object error + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(&types.ObjectInfo{}, true) + storageKeeper.EXPECT().DeleteObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("delete object error")) + res = app.ExecuteSynPackage(s.ctx, nil, serializedSynPackage) + s.Require().ErrorContains(res.Err, "delete object error") + + // case 3: normal case + storageKeeper.EXPECT().GetObjectInfoById(gomock.Any(), gomock.Any()).Return(&types.ObjectInfo{ + Id: sdk.NewUint(10), + }, true) + storageKeeper.EXPECT().DeleteObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + res = app.ExecuteSynPackage(s.ctx, nil, serializedSynPackage) + s.Require().NoError(res.Err) +} diff --git a/x/storage/types/crosschain.go b/x/storage/types/crosschain.go index 96cf90cc3..e43b2650b 100644 --- a/x/storage/types/crosschain.go +++ b/x/storage/types/crosschain.go @@ -661,6 +661,18 @@ func (p CreateGroupSynPackage) ValidateBasic() error { return msg.ValidateBasic() } +func (p CreateGroupSynPackage) MustSerialize() []byte { + encodedBytes, err := createGroupSynPackageArgs.Pack(&CreateGroupSynPackageStruct{ + Creator: common.BytesToAddress(p.Creator), + GroupName: p.GroupName, + ExtraData: p.ExtraData, + }) + if err != nil { + panic("encode create group syn package error") + } + return encodedBytes +} + func DeserializeCreateGroupSynPackage(serializedPackage []byte) (interface{}, error) { unpacked, err := createGroupSynPackageArgs.Unpack(serializedPackage) if err != nil { @@ -901,6 +913,26 @@ func (p UpdateGroupMemberSynPackage) GetMembers() []string { return members } +func (p UpdateGroupMemberSynPackage) MustSerialize() []byte { + totalMember := len(p.Members) + members := make([]common.Address, totalMember) + for i, member := range p.Members { + members[i] = common.BytesToAddress(member) + } + + encodedBytes, err := updateGroupMemberSynPackageArgs.Pack(&UpdateGroupMemberSynPackageStruct{ + common.BytesToAddress(p.Operator), + SafeBigInt(p.GroupId), + p.OperationType, + members, + p.ExtraData, + }) + if err != nil { + panic("encode update group member syn package error") + } + return encodedBytes +} + func (p UpdateGroupMemberSynPackage) ValidateBasic() error { if p.OperationType != OperationAddGroupMember && p.OperationType != OperationDeleteGroupMember { return ErrInvalidOperationType @@ -1025,7 +1057,7 @@ func (p UpdateGroupMemberAckPackage) MustSerialize() []byte { p.ExtraData, }) if err != nil { - panic("encode delete group ack package error") + panic("encode update group member ack package error") } return encodedBytes }