Skip to content

Commit

Permalink
Merge pull request #217 from bnb-chain/feat-adapt-tags
Browse files Browse the repository at this point in the history
fix: remove tags field when creating object/group/bucket
  • Loading branch information
ruojunm authored Dec 21, 2023
2 parents 695488d + 7467073 commit 5be80fe
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ on:
- develop

env:
GreenfieldTag: develop
GreenfieldStorageProviderTag: develop
GreenfieldTag: master
GreenfieldStorageProviderTag: fix-adapt-new-tag
GOPRIVATE: github.com/bnb-chain
GH_ACCESS_TOKEN: ${{ secrets.GH_TOKEN }}
MYSQL_USER: root
Expand Down
13 changes: 9 additions & 4 deletions client/api_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ func (c *Client) CreateBucket(ctx context.Context, bucketName string, primaryAdd
}

createBucketMsg := storageTypes.NewMsgCreateBucket(c.MustGetDefaultAccount().GetAddress(), bucketName, visibility, address, paymentAddr, 0, nil, opts.ChargedQuota)
if opts.Tags != nil {
createBucketMsg.Tags = *opts.Tags
}

err = createBucketMsg.ValidateBasic()
if err != nil {
Expand All @@ -171,7 +168,15 @@ func (c *Client) CreateBucket(ctx context.Context, bucketName string, primaryAdd
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
resp, err := c.BroadcastTx(ctx, []sdk.Msg{signedMsg}, opts.TxOpts)
msgs := []sdk.Msg{signedMsg}

if opts.Tags != nil {
// Set tag
grn := gnfdTypes.NewBucketGRN(bucketName)
msgSetTag := storageTypes.NewMsgSetTag(c.MustGetDefaultAccount().GetAddress(), grn.String(), opts.Tags)
msgs = append(msgs, msgSetTag)
}
resp, err := c.BroadcastTx(ctx, msgs, opts.TxOpts)
if err != nil {
return "", err
}
Expand Down
23 changes: 21 additions & 2 deletions client/api_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/rs/zerolog/log"

"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
gnfdsdk "github.com/bnb-chain/greenfield/sdk/types"
gnfdTypes "github.com/bnb-chain/greenfield/types"
permTypes "github.com/bnb-chain/greenfield/x/permission/types"
storageTypes "github.com/bnb-chain/greenfield/x/storage/types"
Expand Down Expand Up @@ -70,10 +72,27 @@ type IGroupClient interface {
// - ret3: Return error when the request failed, otherwise return nil.
func (c *Client) CreateGroup(ctx context.Context, groupName string, opt types.CreateGroupOptions) (string, error) {
createGroupMsg := storageTypes.NewMsgCreateGroup(c.MustGetDefaultAccount().GetAddress(), groupName, opt.Extra)
// set the default txn broadcast mode as block mode
if opt.TxOpts == nil {
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opt.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
msgs := []sdk.Msg{createGroupMsg}

if opt.Tags != nil {
createGroupMsg.Tags = *opt.Tags
// Set tag
grn := gnfdTypes.NewGroupGRN(c.MustGetDefaultAccount().GetAddress(), groupName)
msgSetTag := storageTypes.NewMsgSetTag(c.MustGetDefaultAccount().GetAddress(), grn.String(), opt.Tags)
msgs = append(msgs, msgSetTag)
}
return c.sendTxn(ctx, createGroupMsg, opt.TxOpts)

resp, err := c.BroadcastTx(ctx, msgs, opt.TxOpts)
if err != nil {
return "", err
}
txnHash := resp.TxResponse.TxHash

return txnHash, nil
}

// DeleteGroup - Delete a group on Greenfield blockchain. The sender MUST only be the group owner, group members or others would fail to send this transaction.
Expand Down
20 changes: 13 additions & 7 deletions client/api_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/rs/zerolog/log"

hashlib "github.com/bnb-chain/greenfield-common/go/hash"
"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
Expand All @@ -25,9 +29,6 @@ import (
"github.com/bnb-chain/greenfield/types/s3util"
permTypes "github.com/bnb-chain/greenfield/x/permission/types"
storageTypes "github.com/bnb-chain/greenfield/x/storage/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/rs/zerolog/log"
)

// IObjectClient interface defines functions related to object operations.
Expand Down Expand Up @@ -137,9 +138,6 @@ func (c *Client) CreateObject(ctx context.Context, bucketName, objectName string

createObjectMsg := storageTypes.NewMsgCreateObject(c.MustGetDefaultAccount().GetAddress(), bucketName, objectName,
uint64(size), visibility, expectCheckSums, contentType, redundancyType, math.MaxUint, nil)
if opts.Tags != nil {
createObjectMsg.Tags = *opts.Tags
}

err = createObjectMsg.ValidateBasic()
if err != nil {
Expand All @@ -156,8 +154,16 @@ func (c *Client) CreateObject(ctx context.Context, bucketName, objectName string
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
msgs := []sdk.Msg{signedCreateObjectMsg}

if opts.Tags != nil {
// Set tag
grn := gnfdTypes.NewObjectGRN(bucketName, objectName)
msgSetTag := storageTypes.NewMsgSetTag(c.MustGetDefaultAccount().GetAddress(), grn.String(), opts.Tags)
msgs = append(msgs, msgSetTag)
}

resp, err := c.BroadcastTx(ctx, []sdk.Msg{signedCreateObjectMsg}, opts.TxOpts)
resp, err := c.BroadcastTx(ctx, msgs, opts.TxOpts)
if err != nil {
return "", err
}
Expand Down
220 changes: 219 additions & 1 deletion e2e/e2e_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import (
"time"

"cosmossdk.io/math"
"github.com/stretchr/testify/suite"

"github.com/bnb-chain/greenfield-go-sdk/client"
"github.com/bnb-chain/greenfield-go-sdk/e2e/basesuite"
"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
types2 "github.com/bnb-chain/greenfield/sdk/types"
storageTestUtil "github.com/bnb-chain/greenfield/testutil/storage"
greenfield_types "github.com/bnb-chain/greenfield/types"
"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"
storageTypes "github.com/bnb-chain/greenfield/x/storage/types"
"github.com/stretchr/testify/suite"
)

type StorageTestSuite struct {
Expand Down Expand Up @@ -626,3 +628,219 @@ func (s *StorageTestSuite) Test_Upload_Object_With_Tampering_Content() {
s.Require().Equal(objectDetail.ObjectInfo.ObjectName, objectName)
s.Require().Equal(objectDetail.ObjectInfo.GetObjectStatus().String(), "OBJECT_STATUS_CREATED")
}

func (s *StorageTestSuite) Test_Group_with_Tag() {
//create group with tag
groupName := storageTestUtil.GenRandomGroupName()

groupOwner := s.DefaultAccount.GetAddress()
s.T().Log("---> CreateGroup and HeadGroup <---")

var tags storageTypes.ResourceTags
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key2", Value: "value2"})

_, err := s.Client.CreateGroup(s.ClientContext, groupName, types.CreateGroupOptions{Tags: &tags})
s.Require().NoError(err)
s.T().Logf("create GroupName: %s", groupName)

time.Sleep(5 * time.Second)
headResult, err := s.Client.HeadGroup(s.ClientContext, groupName, groupOwner.String())
s.Require().NoError(err)
s.Require().Equal(groupName, headResult.GroupName)
s.Require().Equal(tags, *headResult.Tags)
}

func (s *StorageTestSuite) Test_CreateGroup_And_Set_Tag() {
//create group with tag
groupName := storageTestUtil.GenRandomGroupName()

groupOwner := s.DefaultAccount.GetAddress()
s.T().Log("---> CreateGroup and HeadGroup <---")

_, err := s.Client.CreateGroup(s.ClientContext, groupName, types.CreateGroupOptions{})
s.Require().NoError(err)
s.T().Logf("create GroupName: %s", groupName)

time.Sleep(5 * time.Second)
headResult, err := s.Client.HeadGroup(s.ClientContext, groupName, groupOwner.String())
s.Require().NoError(err)
s.Require().Equal(groupName, headResult.GroupName)

grn := greenfield_types.NewGroupGRN(groupOwner, groupName)
var tags storageTypes.ResourceTags
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key2", Value: "value2"})

_, err = s.Client.SetTag(s.ClientContext, grn.String(), tags, types.SetTagsOptions{})
s.Require().NoError(err)
s.T().Logf("set tag: %v for group %s", tags, groupName)

time.Sleep(5 * time.Second)
headResult, err = s.Client.HeadGroup(s.ClientContext, groupName, groupOwner.String())
s.Require().NoError(err)
s.Require().Equal(groupName, headResult.GroupName)
s.Require().Equal(tags, *headResult.Tags)
}

func (s *StorageTestSuite) Test_Bucket_with_Tag() {
bucketName := storageTestUtil.GenRandomBucketName()
var tags storageTypes.ResourceTags
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key2", Value: "value2"})

chargedQuota := uint64(100)
s.T().Log("---> CreateBucket and HeadBucket <---")
opts := types.CreateBucketOptions{ChargedQuota: chargedQuota, Tags: &tags}

bucketTx, err := s.Client.CreateBucket(s.ClientContext, bucketName, s.PrimarySP.OperatorAddress, opts)
s.Require().NoError(err)

_, err = s.Client.WaitForTx(s.ClientContext, bucketTx)
s.Require().NoError(err)

bucketInfo, err := s.Client.HeadBucket(s.ClientContext, bucketName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(bucketInfo.Visibility, storageTypes.VISIBILITY_TYPE_PRIVATE)
s.Require().Equal(bucketInfo.ChargedReadQuota, chargedQuota)
s.Require().Equal(tags, *bucketInfo.Tags)
}
}

func (s *StorageTestSuite) Test_CreateBucket_And_Set_Tag() {
bucketName := storageTestUtil.GenRandomBucketName()

chargedQuota := uint64(100)
s.T().Log("---> CreateBucket and HeadBucket <---")
opts := types.CreateBucketOptions{ChargedQuota: chargedQuota}

bucketTx, err := s.Client.CreateBucket(s.ClientContext, bucketName, s.PrimarySP.OperatorAddress, opts)
s.Require().NoError(err)

_, err = s.Client.WaitForTx(s.ClientContext, bucketTx)
s.Require().NoError(err)

bucketInfo, err := s.Client.HeadBucket(s.ClientContext, bucketName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(bucketInfo.Visibility, storageTypes.VISIBILITY_TYPE_PRIVATE)
s.Require().Equal(bucketInfo.ChargedReadQuota, chargedQuota)
}

// set tag
grn := greenfield_types.NewBucketGRN(bucketName)
var tags storageTypes.ResourceTags
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key2", Value: "value2"})

_, err = s.Client.SetTag(s.ClientContext, grn.String(), tags, types.SetTagsOptions{})
s.Require().NoError(err)
s.T().Logf("set tag: %v for bucket %s", tags, bucketName)

time.Sleep(5 * time.Second)
bucketInfo, err = s.Client.HeadBucket(s.ClientContext, bucketName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(tags, *bucketInfo.Tags)
}
}

func (s *StorageTestSuite) Test_Object_with_Tag() {
bucketName := storageTestUtil.GenRandomBucketName()
objectName := storageTestUtil.GenRandomObjectName()

s.T().Logf("BucketName:%s, objectName: %s", bucketName, objectName)

bucketTx, err := s.Client.CreateBucket(s.ClientContext, bucketName, s.PrimarySP.OperatorAddress, types.CreateBucketOptions{})
s.Require().NoError(err)

_, err = s.Client.WaitForTx(s.ClientContext, bucketTx)
s.Require().NoError(err)

bucketInfo, err := s.Client.HeadBucket(s.ClientContext, bucketName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(bucketInfo.Visibility, storageTypes.VISIBILITY_TYPE_PRIVATE)
}

var buffer bytes.Buffer
line := `1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,123456789012`
// Create 1MiB content where each line contains 1024 characters.
for i := 0; i < 1024*300; i++ {
buffer.WriteString(fmt.Sprintf("[%05d] %s\n", i, line))
}

var tags storageTypes.ResourceTags
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key2", Value: "value2"})
s.T().Log("---> CreateObject and HeadObject <---")
objectTx, err := s.Client.CreateObject(s.ClientContext, bucketName, objectName, bytes.NewReader(buffer.Bytes()),
types.CreateObjectOptions{Tags: &tags})
s.Require().NoError(err)
_, err = s.Client.WaitForTx(s.ClientContext, objectTx)
s.Require().NoError(err)

time.Sleep(5 * time.Second)
objectDetail, err := s.Client.HeadObject(s.ClientContext, bucketName, objectName)
s.Require().NoError(err)
s.Require().Equal(objectDetail.ObjectInfo.ObjectName, objectName)
s.Require().Equal(objectDetail.ObjectInfo.GetObjectStatus().String(), "OBJECT_STATUS_CREATED")
s.Require().Equal(tags, *objectDetail.ObjectInfo.Tags)
}

func (s *StorageTestSuite) Test_Object_And_Set_Tag() {
bucketName := storageTestUtil.GenRandomBucketName()
objectName := storageTestUtil.GenRandomObjectName()

s.T().Logf("BucketName:%s, objectName: %s", bucketName, objectName)

bucketTx, err := s.Client.CreateBucket(s.ClientContext, bucketName, s.PrimarySP.OperatorAddress, types.CreateBucketOptions{})
s.Require().NoError(err)

_, err = s.Client.WaitForTx(s.ClientContext, bucketTx)
s.Require().NoError(err)

bucketInfo, err := s.Client.HeadBucket(s.ClientContext, bucketName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(bucketInfo.Visibility, storageTypes.VISIBILITY_TYPE_PRIVATE)
}

var buffer bytes.Buffer
line := `1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,123456789012`
// Create 1MiB content where each line contains 1024 characters.
for i := 0; i < 1024*300; i++ {
buffer.WriteString(fmt.Sprintf("[%05d] %s\n", i, line))
}

s.T().Log("---> CreateObject and HeadObject <---")
objectTx, err := s.Client.CreateObject(s.ClientContext, bucketName, objectName, bytes.NewReader(buffer.Bytes()),
types.CreateObjectOptions{})
s.Require().NoError(err)
_, err = s.Client.WaitForTx(s.ClientContext, objectTx)
s.Require().NoError(err)

time.Sleep(5 * time.Second)
objectDetail, err := s.Client.HeadObject(s.ClientContext, bucketName, objectName)
s.Require().NoError(err)
s.Require().Equal(objectDetail.ObjectInfo.ObjectName, objectName)
s.Require().Equal(objectDetail.ObjectInfo.GetObjectStatus().String(), "OBJECT_STATUS_CREATED")

// set tag
grn := greenfield_types.NewObjectGRN(bucketName, objectName)
var tags storageTypes.ResourceTags
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key1", Value: "value1"})
tags.Tags = append(tags.Tags, storageTypes.ResourceTags_Tag{Key: "key2", Value: "value2"})

_, err = s.Client.SetTag(s.ClientContext, grn.String(), tags, types.SetTagsOptions{})
s.Require().NoError(err)
s.T().Logf("set tag: %v for object %s", tags, objectName)

time.Sleep(5 * time.Second)
objectDetail, err = s.Client.HeadObject(s.ClientContext, bucketName, objectName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(tags, *objectDetail.ObjectInfo.Tags)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.20
require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/bnb-chain/greenfield v1.0.2-0.20231205022545-1760620afde0
github.com/bnb-chain/greenfield v1.2.1-0.20231221015040-11071a6ee95b
github.com/bnb-chain/greenfield-common/go v0.0.0-20230906132736-eb2f0efea228
github.com/cometbft/cometbft v0.37.2
github.com/consensys/gnark-crypto v0.7.0
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/ethereum/go-ethereum v1.10.26
github.com/prysmaticlabs/prysm v0.0.0-20220124113610-e26cde5e091b
github.com/rs/zerolog v1.29.1
Expand Down Expand Up @@ -40,7 +41,6 @@ require (
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.3 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogoproto v1.4.10 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
Expand Down
Loading

0 comments on commit 5be80fe

Please sign in to comment.