Skip to content

Commit

Permalink
update msg and test
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Nov 21, 2023
1 parent 5a65cc8 commit 3e0c871
Show file tree
Hide file tree
Showing 18 changed files with 1,571 additions and 594 deletions.
1 change: 1 addition & 0 deletions deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function generate_genesis() {
sed -i -e "s/log_level = \"info\"/\log_level= \"debug\"/g" ${workspace}/.local/validator${i}/config/config.toml
echo -e '[[upgrade]]\nname = "Nagqu"\nheight = 20\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
echo -e '[[upgrade]]\nname = "Pampas"\nheight = 20\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
echo -e '[[upgrade]]\nname = "Eddystone"\nheight = 20\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
done

# enable swagger API for validator0
Expand Down
35 changes: 35 additions & 0 deletions e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/bnb-chain/greenfield/sdk/keys"
"github.com/bnb-chain/greenfield/sdk/types"
storageutils "github.com/bnb-chain/greenfield/testutil/storage"
types2 "github.com/bnb-chain/greenfield/types"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)
Expand Down Expand Up @@ -2073,3 +2074,37 @@ func (s *StorageTestSuite) TestMaintenanceSPCreateBucketAndObject() {
// s.Require().Error(err)
// s.Require().Equal(queryHeadBucketResponse.BucketInfo.BucketStatus, storagetypes.BUCKET_STATUS_CREATED)
//}

func (s *StorageTestSuite) TestSetTag() {
var err error
user := s.GenAndChargeAccounts(1, 1000000)

// CreateBucket
sp := s.BaseSuite.PickStorageProvider()
gvg, found := sp.GetFirstGlobalVirtualGroup()
s.Require().True(found)

bucketName := storageutils.GenRandomBucketName()
msgCreateBucket := storagetypes.NewMsgCreateBucket(
user[0].GetAddr(), bucketName, storagetypes.VISIBILITY_TYPE_PUBLIC_READ, sp.OperatorKey.GetAddr(),
nil, math.MaxUint, nil, 0)
msgCreateBucket.PrimarySpApproval.GlobalVirtualGroupFamilyId = gvg.FamilyId
msgCreateBucket.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateBucket.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(user[0], msgCreateBucket)

// Set tag
grn := types2.NewBucketGRN(bucketName)
var tags storagetypes.ResourceTags
tags.Tags = append(tags.Tags, &storagetypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
msgSetTag := storagetypes.NewMsgSetTag(user[0].GetAddr(), grn.String(), &tags)
s.SendTxBlock(user[0], msgSetTag)

// Query
req := storagetypes.QueryResourceTagRequest{
Resource: grn.String(),
}
resp, err := s.Client.QueryResourceTag(context.Background(), &req)
s.Require().NoError(err)
s.Require().Equal(tags, &resp.Tags)
}
15 changes: 15 additions & 0 deletions proto/greenfield/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ service Query {
option (google.api.http).get = "/greenfield/storage/groups_exist_by_id/{group_ids}";
}

// Queries resource tags.
rpc QueryResourceTag(QueryResourceTagRequest) returns (QueryResourceTagResponse) {
option (google.api.http).get = "/greenfield/storage/resource_tag/{resource}";
}

// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -407,4 +412,14 @@ message QueryGroupsExistResponse {
map<string, bool> exists = 1;
}

message QueryResourceTagRequest {
// resource defines a greenfield standard resource name that can be generated by GRN structure
string resource = 1;
}

message QueryResourceTagResponse {
// tags defines a list of tags the resource has
ResourceTags tags = 1;
}

// this line is used by starport scaffolding # 3
3 changes: 2 additions & 1 deletion proto/greenfield/storage/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "greenfield/common/wrapper.proto";
import "greenfield/permission/common.proto";
import "greenfield/storage/common.proto";
import "greenfield/storage/params.proto";
import "greenfield/storage/types.proto";

option go_package = "github.com/bnb-chain/greenfield/x/storage/types";

Expand Down Expand Up @@ -641,7 +642,7 @@ message MsgSetTag {
string resource = 2;

// tags defines a list of tags which will be set to the resource
map<string, string> tags = 3;
ResourceTags tags = 3;
}

message MsgSetTagResponse {}
9 changes: 9 additions & 0 deletions proto/greenfield/storage/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,12 @@ message MigrationBucketInfo {
(gogoproto.nullable) = false
];
}

message ResourceTags {
message Tag {
string key = 1;
string value = 2;
}
// tags defines a list of tags the resource has
repeated Tag tags = 1;
}
4 changes: 2 additions & 2 deletions x/permission/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ func (k Keeper) RemoveExpiredPolicies(ctx sdk.Context) {
ctx.EventManager().EmitTypedEvents(&types.EventDeletePolicy{PolicyId: policyId}) //nolint: errcheck
count++

//1. the policy is an account policy, delete policyKey -> policyId.
//2. the policy is group policy within a policy group, delete the index in the policy group
// 1. the policy is an account policy, delete policyKey -> policyId.
// 2. the policy is group policy within a policy group, delete the index in the policy group
if ctx.IsUpgraded(upgradetypes.Pampas) {
if policy.Principal.Type == types.PRINCIPAL_TYPE_GNFD_ACCOUNT {
policyKey := types.GetPolicyForAccountKey(policy.ResourceId, policy.ResourceType,
Expand Down
10 changes: 5 additions & 5 deletions x/storage/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func FlagSetApproval() *flag.FlagSet {
return fs
}

func GetTags(str string) map[string]string {
tags := make(map[string]string)
func GetTags(str string) *storagetypes.ResourceTags {
var tags storagetypes.ResourceTags
if str == "" {
return tags
return &tags
}

tagsStr := str
Expand All @@ -170,8 +170,8 @@ func GetTags(str string) map[string]string {
if len(kv) != 2 {
continue
}
tags[kv[0]] = kv[1]
tags.Tags = append(tags.Tags, &storagetypes.ResourceTags_Tag{Key: kv[0], Value: kv[1]})
}

return tags
return &tags
}
35 changes: 35 additions & 0 deletions x/storage/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func GetQueryCmd() *cobra.Command {
CmdHeadGroupMember(),
CmdQueryAccountPolicy(),
CmdQueryGroupPolicy(),
CmdQueryResourceTag(),
)

// this line is used by starport scaffolding # 1
Expand Down Expand Up @@ -417,3 +418,37 @@ Examples:

return cmd
}

func CmdQueryResourceTag() *cobra.Command {
cmd := &cobra.Command{
Use: "resource-tag [grn]",
Short: "Query resource tag",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
grnStr := args[0]
var grn gnfd.GRN
err = grn.ParseFromString(grnStr, false)
if err != nil {
return err
}

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryResourceTagRequest{
Resource: grn.String(),
}
res, err := queryClient.QueryResourceTag(cmd.Context(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
4 changes: 2 additions & 2 deletions x/storage/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,8 @@ func CmdMirrorGroup() *cobra.Command {

func CmdSetTag() *cobra.Command {
cmd := &cobra.Command{
Use: "set-tag [resource] [tags]",
Short: "set a bucket/object/group's tag. The tags should be like: key1=value1,key2=value2",
Use: "set-tag [grn] [tags]",
Short: "set a bucket/object/group's tag. The tags should be like: `key1=value1,key2=value2`",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argResource := args[0]
Expand Down
63 changes: 63 additions & 0 deletions x/storage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/bnb-chain/greenfield/internal/sequence"
gnfd "github.com/bnb-chain/greenfield/types"
"github.com/bnb-chain/greenfield/types/errors"
gnfderrors "github.com/bnb-chain/greenfield/types/errors"
gnfdresource "github.com/bnb-chain/greenfield/types/resource"
permtypes "github.com/bnb-chain/greenfield/x/permission/types"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
"github.com/bnb-chain/greenfield/x/storage/types"
Expand Down Expand Up @@ -659,3 +661,64 @@ func (k Keeper) QueryGroupsExistById(goCtx context.Context, req *types.QueryGrou
}
return &types.QueryGroupsExistResponse{Exists: exists}, nil
}

func (k Keeper) QueryResourceTag(goCtx context.Context, req *types.QueryResourceTagRequest) (*types.QueryResourceTagResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

var grn gnfd.GRN
err := grn.ParseFromString(req.Resource, false)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to parse GRN %s: %v", req.Resource, err)
}

var resID math.Uint
switch grn.ResourceType() {
case gnfdresource.RESOURCE_TYPE_BUCKET:
bucketName, grnErr := grn.GetBucketName()
if grnErr != nil {
return nil, grnErr
}
bucketInfo, found := k.GetBucketInfo(ctx, bucketName)
if !found {
return nil, types.ErrNoSuchBucket.Wrapf("bucketName: %s", bucketName)
}
resID = bucketInfo.Id
case gnfdresource.RESOURCE_TYPE_OBJECT:
bucketName, objectName, grnErr := grn.GetBucketAndObjectName()
if grnErr != nil {
return nil, grnErr
}
objectInfo, found := k.GetObjectInfo(ctx, bucketName, objectName)
if !found {
return nil, types.ErrNoSuchObject.Wrapf("BucketName: %s, objectName: %s", bucketName, objectName)
}
resID = objectInfo.Id
case gnfdresource.RESOURCE_TYPE_GROUP:
groupOwner, groupName, grnErr := grn.GetGroupOwnerAndAccount()
if grnErr != nil {
return nil, grnErr
}
groupInfo, found := k.GetGroupInfo(ctx, groupOwner, groupName)
if !found {
return nil, types.ErrNoSuchBucket.Wrapf("groupOwner: %s, groupName: %s", groupOwner.String(), groupName)
}
resID = groupInfo.Id
default:
return nil, gnfderrors.ErrInvalidGRN.Wrap("Unknown resource type in greenfield resource name")
}

store := ctx.KVStore(k.storeKey)
tagKey := types.GetResourceTagKey(string(grn.ResourceType()), resID)
bz := store.Get(tagKey)
if bz == nil {
return &types.QueryResourceTagResponse{Tags: nil}, nil
}
var tags types.ResourceTags
k.cdc.MustUnmarshal(bz, &tags)

return &types.QueryResourceTagResponse{Tags: &tags}, nil
}
72 changes: 72 additions & 0 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (

"github.com/bnb-chain/greenfield/internal/sequence"
gnfdtypes "github.com/bnb-chain/greenfield/types"
types2 "github.com/bnb-chain/greenfield/types"
"github.com/bnb-chain/greenfield/types/common"
gnfderrors "github.com/bnb-chain/greenfield/types/errors"
"github.com/bnb-chain/greenfield/types/resource"
gnfdresource "github.com/bnb-chain/greenfield/types/resource"
paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"
permtypes "github.com/bnb-chain/greenfield/x/permission/types"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
Expand Down Expand Up @@ -2199,3 +2202,72 @@ func (k Keeper) GetSourceTypeByChainId(ctx sdk.Context, chainId sdk.ChainID) (ty

return 0, types.ErrChainNotSupported
}

func (k Keeper) SetTag(ctx sdk.Context, operator sdk.AccAddress, grn types2.GRN, tags *types.ResourceTags) error {
var resOwner sdk.AccAddress
var resID sdkmath.Uint
switch grn.ResourceType() {
case gnfdresource.RESOURCE_TYPE_BUCKET:
bucketName, grnErr := grn.GetBucketName()
if grnErr != nil {
return grnErr
}
bucketInfo, found := k.GetBucketInfo(ctx, bucketName)
if !found {
return types.ErrNoSuchBucket.Wrapf("bucketName: %s", bucketName)
}
resOwner = sdk.MustAccAddressFromHex(bucketInfo.Owner)
resID = bucketInfo.Id
case gnfdresource.RESOURCE_TYPE_OBJECT:
bucketName, objectName, grnErr := grn.GetBucketAndObjectName()
if grnErr != nil {
return grnErr
}
objectInfo, found := k.GetObjectInfo(ctx, bucketName, objectName)
if !found {
return types.ErrNoSuchObject.Wrapf("BucketName: %s, objectName: %s", bucketName, objectName)
}
resOwner = sdk.MustAccAddressFromHex(objectInfo.Owner)
resID = objectInfo.Id
case gnfdresource.RESOURCE_TYPE_GROUP:
groupOwner, groupName, grnErr := grn.GetGroupOwnerAndAccount()
if grnErr != nil {
return grnErr
}
groupInfo, found := k.GetGroupInfo(ctx, groupOwner, groupName)
if !found {
return types.ErrNoSuchBucket.Wrapf("groupOwner: %s, groupName: %s", groupOwner.String(), groupName)
}
resOwner = sdk.MustAccAddressFromHex(groupInfo.Owner)
resID = groupInfo.Id
default:
return gnfderrors.ErrInvalidGRN.Wrap("Unknown resource type in greenfield resource name")
}

if !operator.Equals(resOwner) {
return types.ErrAccessDenied.Wrapf(
"Only resource owner can set tag, operator (%s), owner(%s)",
operator.String(), resOwner.String())
}

store := ctx.KVStore(k.storeKey)
tagKey := types.GetResourceTagKey(string(grn.ResourceType()), resID)
bz := k.cdc.MustMarshal(tags)
store.Set(tagKey, bz)

tagMap := make(map[string]string)
for _, tag := range tags.GetTags() {
tagMap[tag.Key] = tag.Value
}

// emit Event
if err := ctx.EventManager().EmitTypedEvents(&types.EventSetTag{
ResourceType: grn.ResourceType(),
ResourceId: resID.String(),
Tags: tagMap,
}); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 3e0c871

Please sign in to comment.