Skip to content

Commit

Permalink
feat: allow mirror bucket/object/group using name
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed May 22, 2023
1 parent 9fa39d9 commit fcbc172
Show file tree
Hide file tree
Showing 6 changed files with 482 additions and 147 deletions.
65 changes: 59 additions & 6 deletions e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,21 @@ func (s *StorageTestSuite) TestMirrorBucket() {
s.Require().Equal(queryHeadBucketResponse.BucketInfo.Visibility, storagetypes.VISIBILITY_TYPE_PRIVATE)
s.Require().Equal(queryHeadBucketResponse.BucketInfo.SourceType, storagetypes.SOURCE_TYPE_ORIGIN)

// Mirror bucket
msgMirrorBucket := storagetypes.NewMsgMirrorBucket(user.GetAddr(), queryHeadBucketResponse.BucketInfo.Id)
// MirrorBucket using id
msgMirrorBucket := storagetypes.NewMsgMirrorBucket(user.GetAddr(), queryHeadBucketResponse.BucketInfo.Id, "")
s.SendTxBlock(user, msgMirrorBucket)

// CreateBucket
bucketName = storageutils.GenRandomBucketName()
msgCreateBucket = storagetypes.NewMsgCreateBucket(
user.GetAddr(), bucketName, storagetypes.VISIBILITY_TYPE_PRIVATE, sp.OperatorKey.GetAddr(),
nil, math.MaxUint, nil, 0)
msgCreateBucket.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateBucket.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(user, msgCreateBucket)

// MirrorBucket using name
msgMirrorBucket = storagetypes.NewMsgMirrorBucket(user.GetAddr(), sdk.NewUint(0), bucketName)
s.SendTxBlock(user, msgMirrorBucket)
}

Expand Down Expand Up @@ -953,8 +966,39 @@ func (s *StorageTestSuite) TestMirrorObject() {
s.Require().Equal(len(queryListObjectsResponse.ObjectInfos), 1)
s.Require().Equal(queryListObjectsResponse.ObjectInfos[0].ObjectName, objectName)

// Mirror object
msgMirrorObject := storagetypes.NewMsgMirrorObject(user.GetAddr(), queryHeadObjectResponse.ObjectInfo.Id)
// MirrorObject using id
msgMirrorObject := storagetypes.NewMsgMirrorObject(user.GetAddr(), queryHeadObjectResponse.ObjectInfo.Id, "", "")
s.SendTxBlock(user, msgMirrorObject)

// CreateObject
objectName = storageutils.GenRandomObjectName()
msgCreateObject = storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), storagetypes.VISIBILITY_TYPE_PRIVATE, expectChecksum, contextType, storagetypes.REDUNDANCY_EC_TYPE, math.MaxUint, nil, nil)
msgCreateObject.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateObject.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(user, msgCreateObject)

queryHeadObjectRequest = storagetypes.QueryHeadObjectRequest{
BucketName: bucketName,
ObjectName: objectName,
}
queryHeadObjectResponse, err = s.Client.HeadObject(ctx, &queryHeadObjectRequest)
s.Require().NoError(err)

// SealObject
msgSealObject = storagetypes.NewMsgSealObject(sp.SealKey.GetAddr(), bucketName, objectName, secondarySPs, nil)
sr = storagetypes.NewSecondarySpSignDoc(sp.OperatorKey.GetAddr(), queryHeadObjectResponse.ObjectInfo.Id, checksum)
secondarySig, err = sp.ApprovalKey.Sign(sr.GetSignBytes())
s.Require().NoError(err)
err = storagetypes.VerifySignature(s.StorageProviders[0].ApprovalKey.GetAddr(), sdk.Keccak256(sr.GetSignBytes()),
secondarySig)
s.Require().NoError(err)

secondarySigs = [][]byte{secondarySig, secondarySig, secondarySig, secondarySig, secondarySig, secondarySig}
msgSealObject.SecondarySpSignatures = secondarySigs
s.SendTxBlock(sp.SealKey, msgSealObject)

// MirrorObject using names
msgMirrorObject = storagetypes.NewMsgMirrorObject(user.GetAddr(), sdk.NewUint(0), bucketName, objectName)
s.SendTxBlock(user, msgMirrorObject)
}

Expand All @@ -977,8 +1021,17 @@ func (s *StorageTestSuite) TestMirrorGroup() {
s.Require().Equal(queryHeadGroupResp.GroupInfo.GroupName, groupName)
s.Require().Equal(queryHeadGroupResp.GroupInfo.Owner, owner.GetAddr().String())

// Mirror group
msgMirrorGroup := storagetypes.NewMsgMirrorGroup(owner.GetAddr(), queryHeadGroupResp.GroupInfo.Id)
// MirrorGroup using id
msgMirrorGroup := storagetypes.NewMsgMirrorGroup(owner.GetAddr(), queryHeadGroupResp.GroupInfo.Id, "")
s.SendTxBlock(owner, msgMirrorGroup)

// CreateGroup
groupName = storageutils.GenRandomGroupName()
msgCreateGroup = storagetypes.NewMsgCreateGroup(owner.GetAddr(), groupName, []sdk.AccAddress{member.GetAddr()})
s.SendTxBlock(owner, msgCreateGroup)

// MirrorGroup using name
msgMirrorGroup = storagetypes.NewMsgMirrorGroup(owner.GetAddr(), sdk.NewUint(0), groupName)
s.SendTxBlock(owner, msgMirrorGroup)
}

Expand Down
10 changes: 9 additions & 1 deletion proto/greenfield/storage/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ message MsgMirrorObject {
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
// bucket_name defines the name of the bucket where the object is stored
string bucket_name = 3;
// object_name defines the name of object
string object_name = 4;
}

message MsgMirrorObjectResponse {}
Expand All @@ -390,6 +394,8 @@ message MsgMirrorBucket {
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
// bucket_name defines a globally unique name of bucket
string bucket_name = 3;
}

message MsgUpdateObjectInfoResponse {}
Expand All @@ -412,14 +418,16 @@ message MsgMirrorBucketResponse {}

message MsgMirrorGroup {
option (cosmos.msg.v1.signer) = "operator";
// operator defines the account address of the operator who has the DeleteGroup permission of the group to be deleted.
// operator defines the account address of the operator who is the owner of the group
string operator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// id defines the unique u256 for group.
string id = 2 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
// group_name defines the name of the group
string group_name = 3;
}

message MsgMirrorGroupResponse {}
Expand Down
26 changes: 19 additions & 7 deletions x/storage/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func CmdCancelCreateObject() *cobra.Command {
func CmdCreateObject() *cobra.Command {
cmd := &cobra.Command{
Use: "create-object [bucket-name] [object-name] [payload-size] [content-type]",
Short: "create a new object in the bucket, checksums split by ','",
Short: "Create a new object in the bucket, checksums split by ','",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argBucketName := args[0]
Expand Down Expand Up @@ -756,11 +756,13 @@ $ %s tx delete-policy 3

func CmdMirrorBucket() *cobra.Command {
cmd := &cobra.Command{
Use: "mirror-bucket [bucket-id]",
Use: "mirror-bucket [bucket-id] [bucket-name]",
Short: "Mirror an existing bucket to the destination chain",
Args: cobra.ExactArgs(1),
Long: "If [bucket-id] is greater than 0, [bucket-name] will be ignored; otherwise [bucket-id] is ignored",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argBucketId := args[0]
argBucketName := args[1]

bucketId, ok := big.NewInt(0).SetString(argBucketId, 10)
if !ok {
Expand All @@ -769,6 +771,7 @@ func CmdMirrorBucket() *cobra.Command {
if bucketId.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("bucket id should not be negative")
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand All @@ -777,6 +780,7 @@ func CmdMirrorBucket() *cobra.Command {
msg := types.NewMsgMirrorBucket(
clientCtx.GetFromAddress(),
cmath.NewUintFromBigInt(bucketId),
argBucketName,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down Expand Up @@ -823,11 +827,14 @@ func CmdDiscontinueBucket() *cobra.Command {

func CmdMirrorObject() *cobra.Command {
cmd := &cobra.Command{
Use: "mirror-object [object-id]",
Use: "mirror-object [object-id] [bucket-name] [object-name]",
Short: "Mirror the object to the destination chain",
Args: cobra.ExactArgs(1),
Long: "If [object-id] is greater than 0, [bucket-name] [object-name] will be ignored; otherwise [object-id] is ignored",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argObjectId := args[0]
argBucketName := args[1]
argObjectName := args[2]

objectId, ok := big.NewInt(0).SetString(argObjectId, 10)
if !ok {
Expand All @@ -845,6 +852,8 @@ func CmdMirrorObject() *cobra.Command {
msg := types.NewMsgMirrorObject(
clientCtx.GetFromAddress(),
cmath.NewUintFromBigInt(objectId),
argBucketName,
argObjectName,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -860,11 +869,13 @@ func CmdMirrorObject() *cobra.Command {

func CmdMirrorGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "mirror-group [group-id]",
Use: "mirror-group [group-id] [group-name]",
Short: "Mirror an existing group to the destination chain",
Args: cobra.ExactArgs(1),
Long: "If [group-id] is greater than 0, [group-name] will be ignored; otherwise [group-id] is ignored",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argGroupId := args[0]
argGroupName := args[1]

groupId, ok := big.NewInt(0).SetString(argGroupId, 10)
if !ok {
Expand All @@ -882,6 +893,7 @@ func CmdMirrorGroup() *cobra.Command {
msg := types.NewMsgMirrorGroup(
clientCtx.GetFromAddress(),
cmath.NewUintFromBigInt(groupId),
argGroupName,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
24 changes: 21 additions & 3 deletions x/storage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ func (k msgServer) MirrorObject(goCtx context.Context, msg *types.MsgMirrorObjec

operator := sdk.MustAccAddressFromHex(msg.Operator)

objectInfo, found := k.Keeper.GetObjectInfoById(ctx, msg.Id)
var objectInfo *types.ObjectInfo
found := false
if msg.Id.GT(sdk.NewUint(0)) {
objectInfo, found = k.Keeper.GetObjectInfoById(ctx, msg.Id)
} else {
objectInfo, found = k.Keeper.GetObjectInfo(ctx, msg.BucketName, msg.ObjectName)
}
if !found {
return nil, types.ErrNoSuchObject
}
Expand Down Expand Up @@ -429,7 +435,13 @@ func (k msgServer) MirrorBucket(goCtx context.Context, msg *types.MsgMirrorBucke

operator := sdk.MustAccAddressFromHex(msg.Operator)

bucketInfo, found := k.Keeper.GetBucketInfoById(ctx, msg.Id)
var bucketInfo *types.BucketInfo
found := false
if msg.Id.GT(sdk.NewUint(0)) {
bucketInfo, found = k.Keeper.GetBucketInfoById(ctx, msg.Id)
} else {
bucketInfo, found = k.Keeper.GetBucketInfo(ctx, msg.BucketName)
}
if !found {
return nil, types.ErrNoSuchBucket
}
Expand Down Expand Up @@ -495,7 +507,13 @@ func (k msgServer) MirrorGroup(goCtx context.Context, msg *types.MsgMirrorGroup)

operator := sdk.MustAccAddressFromHex(msg.Operator)

groupInfo, found := k.Keeper.GetGroupInfoById(ctx, msg.Id)
var groupInfo *types.GroupInfo
found := false
if msg.Id.GT(sdk.NewUint(0)) {
groupInfo, found = k.Keeper.GetGroupInfoById(ctx, msg.Id)
} else {
groupInfo, found = k.Keeper.GetGroupInfo(ctx, operator, msg.GroupName)
}
if !found {
return nil, types.ErrNoSuchGroup
}
Expand Down
54 changes: 45 additions & 9 deletions x/storage/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,10 +1183,11 @@ func (msg *MsgDeletePolicy) ValidateBasic() error {
}

// NewMsgMirrorBucket creates a new MsgMirrorBucket instance
func NewMsgMirrorBucket(operator sdk.AccAddress, id Uint) *MsgMirrorBucket {
func NewMsgMirrorBucket(operator sdk.AccAddress, id Uint, bucketName string) *MsgMirrorBucket {
return &MsgMirrorBucket{
Operator: operator.String(),
Id: id,
Operator: operator.String(),
Id: id,
BucketName: bucketName,
}
}

Expand Down Expand Up @@ -1222,14 +1223,25 @@ func (msg *MsgMirrorBucket) ValidateBasic() error {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if msg.Id.GT(sdk.NewUint(0)) {
return nil
}

err = s3util.CheckValidBucketName(msg.BucketName)
if err != nil {
return err
}

return nil
}

// NewMsgMirrorObject creates a new MsgMirrorObject instance
func NewMsgMirrorObject(operator sdk.AccAddress, id Uint) *MsgMirrorObject {
func NewMsgMirrorObject(operator sdk.AccAddress, id Uint, bucketName, objectName string) *MsgMirrorObject {
return &MsgMirrorObject{
Operator: operator.String(),
Id: id,
Operator: operator.String(),
Id: id,
BucketName: bucketName,
ObjectName: objectName,
}
}

Expand Down Expand Up @@ -1265,14 +1277,29 @@ func (msg *MsgMirrorObject) ValidateBasic() error {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if msg.Id.GT(sdk.NewUint(0)) {
return nil
}

err = s3util.CheckValidBucketName(msg.BucketName)
if err != nil {
return err
}

err = s3util.CheckValidObjectName(msg.ObjectName)
if err != nil {
return err
}

return nil
}

// NewMsgMirrorGroup creates a new MsgMirrorGroup instance
func NewMsgMirrorGroup(operator sdk.AccAddress, id Uint) *MsgMirrorGroup {
func NewMsgMirrorGroup(operator sdk.AccAddress, id Uint, groupName string) *MsgMirrorGroup {
return &MsgMirrorGroup{
Operator: operator.String(),
Id: id,
Operator: operator.String(),
Id: id,
GroupName: groupName,
}
}

Expand Down Expand Up @@ -1308,6 +1335,15 @@ func (msg *MsgMirrorGroup) ValidateBasic() error {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if msg.Id.GT(sdk.NewUint(0)) {
return nil
}

err = s3util.CheckValidGroupName(msg.GroupName)
if err != nil {
return gnfderrors.ErrInvalidGroupName.Wrapf("invalid groupName (%s)", err)
}

return nil
}

Expand Down
Loading

0 comments on commit fcbc172

Please sign in to comment.