Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed May 22, 2023
1 parent fcbc172 commit 52ec318
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 37 deletions.
6 changes: 6 additions & 0 deletions x/storage/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const (
FlagApproveSignature = "approve-signature"
FlagApproveTimeoutHeight = "approve-timeout-height"
FlagChargedReadQuota = "charged-read-quota"
FlagBucketId = "bucket-id"
FlagBucketName = "bucket-name"
FlagObjectId = "object-id"
FlagObjectName = "object-name"
FlagGroupId = "group-id"
FlagGroupName = "group-name"
)

func GetVisibilityType(str string) (storagetypes.VisibilityType, error) {
Expand Down
104 changes: 67 additions & 37 deletions x/storage/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,20 +756,27 @@ $ %s tx delete-policy 3

func CmdMirrorBucket() *cobra.Command {
cmd := &cobra.Command{
Use: "mirror-bucket [bucket-id] [bucket-name]",
Use: "mirror-bucket",
Short: "Mirror an existing bucket to the destination chain",
Long: "If [bucket-id] is greater than 0, [bucket-name] will be ignored; otherwise [bucket-id] is ignored",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(0),
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 {
return fmt.Errorf("invalid bucket id: %s", argBucketId)
}
if bucketId.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("bucket id should not be negative")
argBucketId, _ := cmd.Flags().GetString(FlagBucketId)
argBucketName, _ := cmd.Flags().GetString(FlagBucketName)

bucketId := big.NewInt(0)
if argBucketId == "" && argBucketName == "" {
return fmt.Errorf("bucket id or bucket name should be provided")
} else if argBucketId != "" && argBucketName != "" {
return fmt.Errorf("bucket id and bucket name should not be provided together")
} else if argBucketId != "" {
ok := false
bucketId, ok = big.NewInt(0).SetString(argBucketId, 10)
if !ok {
return fmt.Errorf("invalid bucket id: %s", argBucketId)
}
if bucketId.Cmp(big.NewInt(0)) <= 0 {
return fmt.Errorf("bucket id should be positive")
}
}

clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -789,6 +796,8 @@ func CmdMirrorBucket() *cobra.Command {
},
}

cmd.Flags().String(FlagBucketId, "", "Id of the bucket to mirror")
cmd.Flags().String(FlagBucketName, "", "Name of the bucket to mirror")
flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down Expand Up @@ -827,21 +836,30 @@ func CmdDiscontinueBucket() *cobra.Command {

func CmdMirrorObject() *cobra.Command {
cmd := &cobra.Command{
Use: "mirror-object [object-id] [bucket-name] [object-name]",
Use: "mirror-object",
Short: "Mirror the object to the destination chain",
Long: "If [object-id] is greater than 0, [bucket-name] [object-name] will be ignored; otherwise [object-id] is ignored",
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(0),
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 {
return fmt.Errorf("invalid object id: %s", argObjectId)
}
if objectId.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("object id should not be negative")
argObjectId, _ := cmd.Flags().GetString(FlagObjectId)
argBucketName, _ := cmd.Flags().GetString(FlagBucketName)
argObjectName, _ := cmd.Flags().GetString(FlagObjectName)

objectId := big.NewInt(0)
if argObjectId == "" && argObjectName == "" {
return fmt.Errorf("object id or object name should be provided")
} else if argObjectId != "" && argObjectName != "" {
return fmt.Errorf("object id and object name should not be provided together")
} else if argObjectId != "" {
ok := false
objectId, ok = big.NewInt(0).SetString(argObjectId, 10)
if !ok {
return fmt.Errorf("invalid object id: %s", argObjectId)
}
if objectId.Cmp(big.NewInt(0)) <= 0 {
return fmt.Errorf("object id should be positive")
}
} else if argObjectName != "" && argBucketName == "" {
return fmt.Errorf("object name and bucket name should not be provided together")
}

clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -862,27 +880,37 @@ func CmdMirrorObject() *cobra.Command {
},
}

cmd.Flags().String(FlagObjectId, "", "Id of the object to mirror")
cmd.Flags().String(FlagObjectName, "", "Name of the object to mirror")
cmd.Flags().String(FlagBucketName, "", "Name of the bucket that the object belongs to")
flags.AddTxFlagsToCmd(cmd)

return cmd
}

func CmdMirrorGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "mirror-group [group-id] [group-name]",
Use: "mirror-group",
Short: "Mirror an existing group to the destination chain",
Long: "If [group-id] is greater than 0, [group-name] will be ignored; otherwise [group-id] is ignored",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(0),
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 {
return fmt.Errorf("invalid groupd id: %s", argGroupId)
}
if groupId.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("groupd id should not be negative")
argGroupId, _ := cmd.Flags().GetString(FlagGroupId)
argGroupName, _ := cmd.Flags().GetString(FlagGroupName)

groupId := big.NewInt(0)
if argGroupId == "" && argGroupName == "" {
return fmt.Errorf("group id or group name should be provided")
} else if argGroupId != "" && argGroupName != "" {
return fmt.Errorf("group id and group name should not be provided together")
} else if argGroupId != "" {
ok := false
groupId, ok = big.NewInt(0).SetString(argGroupId, 10)
if !ok {
return fmt.Errorf("invalid groupd id: %s", argGroupId)
}
if groupId.Cmp(big.NewInt(0)) <= 0 {
return fmt.Errorf("groupd id should be positive")
}
}

clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -902,6 +930,8 @@ func CmdMirrorGroup() *cobra.Command {
},
}

cmd.Flags().String(FlagGroupId, "", "Id of the group to mirror")
cmd.Flags().String(FlagGroupName, "", "Name of the group to mirror")
flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
12 changes: 12 additions & 0 deletions x/storage/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,9 @@ func (msg *MsgMirrorBucket) ValidateBasic() error {
}

if msg.Id.GT(sdk.NewUint(0)) {
if msg.BucketName != "" {
return errors.Wrap(gnfderrors.ErrInvalidBucketName, "Bucket name should be empty")
}
return nil
}

Expand Down Expand Up @@ -1278,6 +1281,12 @@ func (msg *MsgMirrorObject) ValidateBasic() error {
}

if msg.Id.GT(sdk.NewUint(0)) {
if msg.BucketName != "" {
return errors.Wrap(gnfderrors.ErrInvalidBucketName, "Bucket name should be empty")
}
if msg.ObjectName != "" {
return errors.Wrap(gnfderrors.ErrInvalidObjectName, "Object name should be empty")
}
return nil
}

Expand Down Expand Up @@ -1336,6 +1345,9 @@ func (msg *MsgMirrorGroup) ValidateBasic() error {
}

if msg.Id.GT(sdk.NewUint(0)) {
if msg.GroupName != "" {
return errors.Wrap(gnfderrors.ErrInvalidGroupName, "Group name should be empty")
}
return nil
}

Expand Down

0 comments on commit 52ec318

Please sign in to comment.