-
Notifications
You must be signed in to change notification settings - Fork 18
/
api_bucket.go
1051 lines (922 loc) · 35.8 KB
/
api_bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package client
import (
"context"
"encoding/hex"
"encoding/xml"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
gnfdsdk "github.com/bnb-chain/greenfield/sdk/types"
gnfdTypes "github.com/bnb-chain/greenfield/types"
"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"
govTypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/rs/zerolog/log"
"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
)
// IBucketClient interface defines functions related to bucket.
// The concept of "bucket" is the same as the concept of a bucket in AWS S3 storage.
type IBucketClient interface {
GetCreateBucketApproval(ctx context.Context, createBucketMsg *storageTypes.MsgCreateBucket) (*storageTypes.MsgCreateBucket, error)
CreateBucket(ctx context.Context, bucketName string, primaryAddr string, opts types.CreateBucketOptions) (string, error)
DeleteBucket(ctx context.Context, bucketName string, opt types.DeleteBucketOption) (string, error)
UpdateBucketVisibility(ctx context.Context, bucketName string, visibility storageTypes.VisibilityType, opt types.UpdateVisibilityOption) (string, error)
UpdateBucketInfo(ctx context.Context, bucketName string, opts types.UpdateBucketOptions) (string, error)
UpdateBucketPaymentAddr(ctx context.Context, bucketName string, paymentAddr sdk.AccAddress, opt types.UpdatePaymentOption) (string, error)
HeadBucket(ctx context.Context, bucketName string) (*storageTypes.BucketInfo, error)
HeadBucketByID(ctx context.Context, bucketID string) (*storageTypes.BucketInfo, error)
PutBucketPolicy(ctx context.Context, bucketName string, principal types.Principal, statements []*permTypes.Statement, opt types.PutPolicyOption) (string, error)
DeleteBucketPolicy(ctx context.Context, bucketName string, principal types.Principal, opt types.DeletePolicyOption) (string, error)
GetBucketPolicy(ctx context.Context, bucketName string, principalAddr string) (*permTypes.Policy, error)
IsBucketPermissionAllowed(ctx context.Context, userAddr string, bucketName string, action permTypes.ActionType) (permTypes.Effect, error)
ListBuckets(ctx context.Context, opts types.ListBucketsOptions) (types.ListBucketsResult, error)
ListBucketReadRecord(ctx context.Context, bucketName string, opts types.ListReadRecordOptions) (types.QuotaRecordInfo, error)
GetQuotaUpdateTime(ctx context.Context, bucketName string) (int64, error)
BuyQuotaForBucket(ctx context.Context, bucketName string, targetQuota uint64, opt types.BuyQuotaOption) (string, error)
GetBucketReadQuota(ctx context.Context, bucketName string) (types.QuotaInfo, error)
ListBucketsByBucketID(ctx context.Context, bucketIds []uint64, opts types.EndPointOptions) (types.ListBucketsByBucketIDResponse, error)
GetMigrateBucketApproval(ctx context.Context, migrateBucketMsg *storageTypes.MsgMigrateBucket) (*storageTypes.MsgMigrateBucket, error)
MigrateBucket(ctx context.Context, bucketName string, dstPrimarySPID uint32, opts types.MigrateBucketOptions) (string, error)
CancelMigrateBucket(ctx context.Context, bucketName string, opts types.CancelMigrateBucketOptions) (uint64, string, error)
ListBucketsByPaymentAccount(ctx context.Context, paymentAccount string, opts types.ListBucketsByPaymentAccountOptions) (types.ListBucketsByPaymentAccountResult, error)
}
// GetCreateBucketApproval - Send create bucket approval request to SP and returns the signature info for the approval of preCreating resources.
//
// - ctx: Context variables for the current API call.
//
// - createBucketMsg: The msg of create bucket which defined by the greenfield chain.
//
// - ret1: The msg of create bucket which contain the approval signature from the storage provider
//
// - ret2: Return error when get approval failed, otherwise return nil.
func (c *Client) GetCreateBucketApproval(ctx context.Context, createBucketMsg *storageTypes.MsgCreateBucket) (*storageTypes.MsgCreateBucket, error) {
unsignedBytes := createBucketMsg.GetSignBytes()
// set the action type
urlVal := make(url.Values)
urlVal["action"] = []string{types.CreateBucketAction}
reqMeta := requestMeta{
urlValues: urlVal,
urlRelPath: "get-approval",
contentSHA256: types.EmptyStringSHA256,
txnMsg: hex.EncodeToString(unsignedBytes),
}
sendOpt := sendOptions{
method: http.MethodGet,
isAdminApi: true,
}
primarySPAddr := createBucketMsg.GetPrimarySpAddress()
endpoint, err := c.getSPUrlByAddr(primarySPAddr)
if err != nil {
log.Error().Msg(fmt.Sprintf("route endpoint by addr: %s failed, err: %s", primarySPAddr, err.Error()))
return nil, err
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return nil, err
}
// fetch primary signed msg from sp response
signedRawMsg := resp.Header.Get(types.HTTPHeaderSignedMsg)
if signedRawMsg == "" {
return nil, errors.New("fail to fetch pre createBucket signature")
}
signedMsgBytes, err := hex.DecodeString(signedRawMsg)
if err != nil {
return nil, err
}
var signedMsg storageTypes.MsgCreateBucket
storageTypes.ModuleCdc.MustUnmarshalJSON(signedMsgBytes, &signedMsg)
return &signedMsg, nil
}
// CreateBucket - Create a new bucket in greenfield.
//
// This API sends a request to the storage provider to get approval for creating bucket and sends the createBucket transaction to the Greenfield.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to be created.
//
// - primaryAddr: The primary SP address to which the bucket will be created on.
//
// - opts: The Options indicates the meta to construct createBucket msg and the way to send transaction
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error if create bucket failed, otherwise return nil.
func (c *Client) CreateBucket(ctx context.Context, bucketName string, primaryAddr string, opts types.CreateBucketOptions) (string, error) {
address, err := sdk.AccAddressFromHexUnsafe(primaryAddr)
if err != nil {
return "", err
}
var visibility storageTypes.VisibilityType
if opts.Visibility == storageTypes.VISIBILITY_TYPE_UNSPECIFIED {
visibility = storageTypes.VISIBILITY_TYPE_PRIVATE // set default visibility type
} else {
visibility = opts.Visibility
}
var paymentAddr sdk.AccAddress
if opts.PaymentAddress != "" {
paymentAddr, err = sdk.AccAddressFromHexUnsafe(opts.PaymentAddress)
if err != nil {
return "", err
}
}
createBucketMsg := storageTypes.NewMsgCreateBucket(c.MustGetDefaultAccount().GetAddress(), bucketName,
visibility, address, paymentAddr, 0, nil, opts.ChargedQuota)
err = createBucketMsg.ValidateBasic()
if err != nil {
return "", err
}
signedMsg, err := c.GetCreateBucketApproval(ctx, createBucketMsg)
if err != nil {
return "", err
}
// set the default txn broadcast mode as block mode
if opts.TxOpts == nil {
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
resp, err := c.BroadcastTx(ctx, []sdk.Msg{signedMsg}, opts.TxOpts)
if err != nil {
return "", err
}
txnHash := resp.TxResponse.TxHash
if !opts.IsAsyncMode {
ctxTimeout, cancel := context.WithTimeout(ctx, types.ContextTimeout)
defer cancel()
txnResponse, err := c.WaitForTx(ctxTimeout, txnHash)
if err != nil {
return txnHash, fmt.Errorf("the transaction has been submitted, please check it later:%v", err)
}
if txnResponse.TxResult.Code != 0 {
return txnHash, fmt.Errorf("the createBucket txn has failed with response code: %d, codespace:%s", txnResponse.TxResult.Code, txnResponse.TxResult.Codespace)
}
}
return txnHash, nil
}
// DeleteBucket - Send DeleteBucket msg to greenfield chain and return txn hash.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to be deleted
//
// - opt: The Options for customizing the transaction.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error if delete bucket failed, otherwise return nil.
func (c *Client) DeleteBucket(ctx context.Context, bucketName string, opt types.DeleteBucketOption) (string, error) {
if err := s3util.CheckValidBucketName(bucketName); err != nil {
return "", err
}
delBucketMsg := storageTypes.NewMsgDeleteBucket(c.MustGetDefaultAccount().GetAddress(), bucketName)
return c.sendTxn(ctx, delBucketMsg, opt.TxOpts)
}
// UpdateBucketVisibility - Update the visibilityType of bucket.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to be updated.
//
// - visibility: The VisibilityType defines on greenfield which can be PUBLIC_READ, PRIVATE or INHERIT
//
// - opt: The Options for customizing the transaction.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error if update visibility failed, otherwise return nil.
func (c *Client) UpdateBucketVisibility(ctx context.Context, bucketName string,
visibility storageTypes.VisibilityType, opt types.UpdateVisibilityOption,
) (string, error) {
bucketInfo, err := c.HeadBucket(ctx, bucketName)
if err != nil {
return "", err
}
paymentAddr, err := sdk.AccAddressFromHexUnsafe(bucketInfo.PaymentAddress)
if err != nil {
return "", err
}
updateBucketMsg := storageTypes.NewMsgUpdateBucketInfo(c.MustGetDefaultAccount().GetAddress(), bucketName, &bucketInfo.ChargedReadQuota, paymentAddr, visibility)
return c.sendTxn(ctx, updateBucketMsg, opt.TxOpts)
}
// UpdateBucketPaymentAddr - Update the payment address of bucket. It will send the MsgUpdateBucketInfo msg to greenfield to update the meta.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to be updated.
//
// - paymentAddr: The payment address from which deduct the cost of bucket storage or quota.
//
// - opt: The Options for customizing the transaction.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error if update payment address failed, otherwise return nil.
func (c *Client) UpdateBucketPaymentAddr(ctx context.Context, bucketName string,
paymentAddr sdk.AccAddress, opt types.UpdatePaymentOption,
) (string, error) {
bucketInfo, err := c.HeadBucket(ctx, bucketName)
if err != nil {
return "", err
}
updateBucketMsg := storageTypes.NewMsgUpdateBucketInfo(c.MustGetDefaultAccount().GetAddress(), bucketName, &bucketInfo.ChargedReadQuota, paymentAddr, bucketInfo.Visibility)
return c.sendTxn(ctx, updateBucketMsg, opt.TxOpts)
}
// UpdateBucketInfo - Update the bucket meta on chain, including read quota, payment address or visibility. It will send the MsgUpdateBucketInfo msg to greenfield to update the meta.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to be updated.
//
// - paymentAddr: The payment address from which deduct the cost of bucket storage or quota.
//
// - opts: The Options used to specify which metas need to be updated and the option to send transaction.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error if update bucket meta failed, otherwise return nil.
func (c *Client) UpdateBucketInfo(ctx context.Context, bucketName string, opts types.UpdateBucketOptions) (string, error) {
bucketInfo, err := c.HeadBucket(ctx, bucketName)
if err != nil {
return "", err
}
if opts.Visibility == bucketInfo.Visibility && opts.PaymentAddress == "" && opts.ChargedQuota == nil {
return "", errors.New("no meta need to update")
}
var visibility storageTypes.VisibilityType
var chargedReadQuota uint64
var paymentAddr sdk.AccAddress
if opts.Visibility != bucketInfo.Visibility {
visibility = opts.Visibility
} else {
visibility = bucketInfo.Visibility
}
if opts.PaymentAddress != "" {
paymentAddr, err = sdk.AccAddressFromHexUnsafe(opts.PaymentAddress)
if err != nil {
return "", err
}
} else {
paymentAddr, err = sdk.AccAddressFromHexUnsafe(bucketInfo.PaymentAddress)
if err != nil {
return "", err
}
}
if opts.ChargedQuota != nil {
chargedReadQuota = *opts.ChargedQuota
} else {
chargedReadQuota = bucketInfo.ChargedReadQuota
}
updateBucketMsg := storageTypes.NewMsgUpdateBucketInfo(c.MustGetDefaultAccount().GetAddress(), bucketName,
&chargedReadQuota, paymentAddr, visibility)
// set the default txn broadcast mode as block mode
if opts.TxOpts == nil {
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
return c.sendTxn(ctx, updateBucketMsg, opts.TxOpts)
}
// HeadBucket - query the bucketInfo on chain by bucket name, return the bucket info if exists.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to query.
//
// - ret1: The bucket specific metadata information, including Visibility, payment address, charged quota and so on.
//
// - ret2: Return error if bucket not exist, otherwise return nil.
func (c *Client) HeadBucket(ctx context.Context, bucketName string) (*storageTypes.BucketInfo, error) {
queryHeadBucketRequest := storageTypes.QueryHeadBucketRequest{
BucketName: bucketName,
}
queryHeadBucketResponse, err := c.chainClient.HeadBucket(ctx, &queryHeadBucketRequest)
if err != nil {
return nil, err
}
return queryHeadBucketResponse.BucketInfo, nil
}
// HeadBucketByID - query the bucketInfo on chain by the bucket id, return the bucket info if exists.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to query.
//
// - ret1: The bucket specific metadata information, including Visibility, payment address, charged quota and so on.
//
// - ret2: Return error if bucket not exist, otherwise return nil.
func (c *Client) HeadBucketByID(ctx context.Context, bucketID string) (*storageTypes.BucketInfo, error) {
headBucketRequest := &storageTypes.QueryHeadBucketByIdRequest{
BucketId: bucketID,
}
headBucketResponse, err := c.chainClient.HeadBucketById(ctx, headBucketRequest)
if err != nil {
return nil, err
}
return headBucketResponse.BucketInfo, nil
}
// PutBucketPolicy - Apply bucket policy to the principal, return the txn hash.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - principalStr: Indicates the marshaled principal content of greenfield permission types, users can generate it by NewPrincipalWithAccount or NewPrincipalWithGroupId method.
//
// - statements: Policies outline the specific details of permissions, including the Effect, ActionList, and Resources.
//
// - opt: The options for customizing the policy expiration time and transaction.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) PutBucketPolicy(ctx context.Context, bucketName string, principalStr types.Principal,
statements []*permTypes.Statement, opt types.PutPolicyOption,
) (string, error) {
resource := gnfdTypes.NewBucketGRN(bucketName)
principal := &permTypes.Principal{}
if err := principal.Unmarshal([]byte(principalStr)); err != nil {
return "", err
}
putPolicyMsg := storageTypes.NewMsgPutPolicy(c.MustGetDefaultAccount().GetAddress(), resource.String(),
principal, statements, opt.PolicyExpireTime)
return c.sendPutPolicyTxn(ctx, putPolicyMsg, opt.TxOpts)
}
// DeleteBucketPolicy - Delete the bucket policy of the principal.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - principalStr: Indicates the marshaled principal content of greenfield permission types, users can generate it by NewPrincipalWithAccount or NewPrincipalWithGroupId method.
//
// - opt: The option for send delete policy transaction.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) DeleteBucketPolicy(ctx context.Context, bucketName string, principalStr types.Principal, opt types.DeletePolicyOption) (string, error) {
resource := gnfdTypes.NewBucketGRN(bucketName).String()
principal := &permTypes.Principal{}
if err := principal.Unmarshal([]byte(principalStr)); err != nil {
return "", err
}
return c.sendDelPolicyTxn(ctx, c.MustGetDefaultAccount().GetAddress(), resource, principal, opt.TxOpts)
}
// IsBucketPermissionAllowed - Check if the permission of bucket is allowed to the user.
//
// - ctx: Context variables for the current API call.
//
// - userAddr: The HEX-encoded string of the user address
//
// - bucketName: The bucket name identifies the bucket.
//
// - action: Indicates the permission corresponding to which type of action needs to be verified
//
// - ret1: Return EFFECT_ALLOW if the permission is allowed and EFFECT_DENY if the permission is denied
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) IsBucketPermissionAllowed(ctx context.Context, userAddr string,
bucketName string, action permTypes.ActionType,
) (permTypes.Effect, error) {
_, err := sdk.AccAddressFromHexUnsafe(userAddr)
if err != nil {
return permTypes.EFFECT_DENY, err
}
verifyReq := storageTypes.QueryVerifyPermissionRequest{
Operator: userAddr,
BucketName: bucketName,
ActionType: action,
}
verifyResp, err := c.chainClient.VerifyPermission(ctx, &verifyReq)
if err != nil {
return permTypes.EFFECT_DENY, err
}
return verifyResp.Effect, nil
}
// GetBucketPolicy - Get the bucket policy info of the user specified by principalAddr.
//
// - bucketName: The bucket name identifies the bucket.
//
// - principalAddr: The HEX-encoded string of the principal address.
//
// - ret1: The bucket policy info defined on greenfield.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) GetBucketPolicy(ctx context.Context, bucketName string, principalAddr string) (*permTypes.Policy, error) {
_, err := sdk.AccAddressFromHexUnsafe(principalAddr)
if err != nil {
return nil, err
}
resource := gnfdTypes.NewBucketGRN(bucketName).String()
queryPolicy := storageTypes.QueryPolicyForAccountRequest{
Resource: resource,
PrincipalAddress: principalAddr,
}
queryPolicyResp, err := c.chainClient.QueryPolicyForAccount(ctx, &queryPolicy)
if err != nil {
return nil, err
}
return queryPolicyResp.Policy, nil
}
type listBucketsByIDsResponse map[uint64]*types.BucketMeta
type bucketEntry struct {
Id uint64
Value *types.BucketMeta
}
func (m *listBucketsByIDsResponse) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
*m = listBucketsByIDsResponse{}
for {
var e bucketEntry
err := d.Decode(&e)
if err == io.EOF {
break
} else if err != nil {
return err
} else {
(*m)[e.Id] = e.Value
}
}
return nil
}
// ListBuckets - Lists the bucket info of the user.
//
// If the opts.Account is not set, the user is default set as the sender.
//
// - ctx: Context variables for the current API call.
//
// - opts: The options to set the meta to list the bucket
//
// - ret1: The result of list bucket under specific user address
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) ListBuckets(ctx context.Context, opts types.ListBucketsOptions) (types.ListBucketsResult, error) {
params := url.Values{}
params.Set("include-removed", strconv.FormatBool(opts.ShowRemovedBucket))
account := opts.Account
if account == "" {
acc, err := c.GetDefaultAccount()
if err != nil {
log.Error().Msg(fmt.Sprintf("failed to get default account: %s", err.Error()))
return types.ListBucketsResult{}, err
}
account = acc.GetAddress().String()
} else {
_, err := sdk.AccAddressFromHexUnsafe(account)
if err != nil {
return types.ListBucketsResult{}, err
}
}
reqMeta := requestMeta{
urlValues: params,
contentSHA256: types.EmptyStringSHA256,
userAddress: account,
}
sendOpt := sendOptions{
method: http.MethodGet,
disableCloseBody: true,
}
endpoint, err := c.getEndpointByOpt(&types.EndPointOptions{
Endpoint: opts.Endpoint,
SPAddress: opts.SPAddress,
})
if err != nil {
log.Error().Msg(fmt.Sprintf("get endpoint by option failed %s", err.Error()))
return types.ListBucketsResult{}, err
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
log.Error().Msg("the list of user's buckets failed: " + err.Error())
return types.ListBucketsResult{}, err
}
defer utils.CloseResponse(resp)
listBucketsResult := types.ListBucketsResult{}
// unmarshal the json content from response body
buf := new(strings.Builder)
_, err = io.Copy(buf, resp.Body)
if err != nil {
log.Error().Msg("the list of user's buckets failed: " + err.Error())
return types.ListBucketsResult{}, err
}
bufStr := buf.String()
err = xml.Unmarshal([]byte(bufStr), &listBucketsResult)
// TODO(annie) remove tolerance for unmarshal err after structs got stabilized
if err != nil {
return types.ListBucketsResult{}, err
}
return listBucketsResult, nil
}
// ListBucketReadRecord - List the download record info of the specific bucket of the current month.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - opts: Indicates the start timestamp of return read records and the max number of return items
//
// - ret1: The read record info of the bucket returned by SP.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) ListBucketReadRecord(ctx context.Context, bucketName string, opts types.ListReadRecordOptions) (types.QuotaRecordInfo, error) {
if err := s3util.CheckValidBucketName(bucketName); err != nil {
return types.QuotaRecordInfo{}, err
}
timeNow := time.Now()
timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location())
if opts.StartTimeStamp < 0 {
return types.QuotaRecordInfo{}, errors.New("start timestamp less than 0")
}
var startTimeStamp int64
if opts.StartTimeStamp == 0 {
// the timestamp of the first day of this month
startTimeStamp = timeToday.AddDate(0, 0, -timeToday.Day()+1).UnixMicro()
} else {
startTimeStamp = opts.StartTimeStamp
}
// the timestamp of the last day of this month
timeMonthEnd := timeToday.AddDate(0, 1, -timeToday.Day()+1).UnixMicro()
if timeMonthEnd < startTimeStamp {
return types.QuotaRecordInfo{}, errors.New("start timestamp larger than the end timestamp of this month")
}
params := url.Values{}
params.Set("list-read-record", "")
if opts.MaxRecords > 0 {
params.Set("max-records", strconv.Itoa(opts.MaxRecords))
} else {
params.Set("max-records", strconv.Itoa(math.MaxUint32))
}
params.Set("start-timestamp", strconv.FormatInt(startTimeStamp, 10))
params.Set("end-timestamp", strconv.FormatInt(timeMonthEnd, 10))
reqMeta := requestMeta{
urlValues: params,
bucketName: bucketName,
contentSHA256: types.EmptyStringSHA256,
}
sendOpt := sendOptions{
method: http.MethodGet,
disableCloseBody: true,
}
endpoint, err := c.getSPUrlByBucket(bucketName)
if err != nil {
log.Error().Msg(fmt.Sprintf("route endpoint by bucket: %s failed, err: %s", bucketName, err.Error()))
return types.QuotaRecordInfo{}, err
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return types.QuotaRecordInfo{}, err
}
defer utils.CloseResponse(resp)
QuotaRecords := types.QuotaRecordInfo{}
// decode the xml content from response body
err = xml.NewDecoder(resp.Body).Decode(&QuotaRecords)
if err != nil {
return types.QuotaRecordInfo{}, err
}
return QuotaRecords, nil
}
// GetBucketReadQuota - Query the quota info of the specific bucket of current month.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - ret1: The info of quota which contains the consumed quota, the charged quota and free quota info of the bucket
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) GetBucketReadQuota(ctx context.Context, bucketName string) (types.QuotaInfo, error) {
if err := s3util.CheckValidBucketName(bucketName); err != nil {
return types.QuotaInfo{}, err
}
year, month, _ := time.Now().Date()
var date string
if int(month) < 10 {
date = strconv.Itoa(year) + "-" + "0" + strconv.Itoa(int(month))
} else {
date = strconv.Itoa(year) + "-" + strconv.Itoa(int(month))
}
params := url.Values{}
params.Add("read-quota", "")
params.Add("year-month", date)
reqMeta := requestMeta{
urlValues: params,
bucketName: bucketName,
contentSHA256: types.EmptyStringSHA256,
}
sendOpt := sendOptions{
method: http.MethodGet,
disableCloseBody: true,
}
endpoint, err := c.getSPUrlByBucket(bucketName)
if err != nil {
log.Error().Msg(fmt.Sprintf("route endpoint by bucket: %s failed, err: %s", bucketName, err.Error()))
return types.QuotaInfo{}, err
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return types.QuotaInfo{}, err
}
defer utils.CloseResponse(resp)
QuotaResult := types.QuotaInfo{}
// decode the xml content from response body
err = xml.NewDecoder(resp.Body).Decode(&QuotaResult)
if err != nil {
return types.QuotaInfo{}, err
}
return QuotaResult, nil
}
// GetQuotaUpdateTime - Query the update time stamp of the bucket quota info.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - ret1: The update time stamp.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) GetQuotaUpdateTime(ctx context.Context, bucketName string) (int64, error) {
resp, err := c.chainClient.QueryQuotaUpdateTime(ctx, &storageTypes.QueryQuoteUpdateTimeRequest{
BucketName: bucketName,
})
if err != nil {
return 0, err
}
return resp.UpdateAt, nil
}
// BuyQuotaForBucket - Buy the target quota for the specific bucket.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - targetQuota: Indicates the target quota value after setting, and can only be set to a higher value than the current value.
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) BuyQuotaForBucket(ctx context.Context, bucketName string, targetQuota uint64, opt types.BuyQuotaOption) (string, error) {
bucketInfo, err := c.HeadBucket(ctx, bucketName)
if err != nil {
return "", err
}
paymentAddr, err := sdk.AccAddressFromHexUnsafe(bucketInfo.PaymentAddress)
if err != nil {
return "", err
}
updateBucketMsg := storageTypes.NewMsgUpdateBucketInfo(c.MustGetDefaultAccount().GetAddress(), bucketName, &targetQuota, paymentAddr, bucketInfo.Visibility)
resp, err := c.BroadcastTx(ctx, []sdk.Msg{updateBucketMsg}, opt.TxOpts)
if err != nil {
return "", err
}
return resp.TxResponse.TxHash, err
}
// ListBucketsByBucketID - List buckets by bucket ids.
//
// By inputting a collection of bucket IDs, we can retrieve the corresponding bucket data. If the bucket is nonexistent or has been deleted, a null value will be returned
//
// - ctx: Context variables for the current API call.
//
// - bucketIds: The list of bucket ids.
//
// - opts: The options to set the meta to list buckets by bucket id.
//
// - ret1: The result of bucket info map by given bucket ids.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) ListBucketsByBucketID(ctx context.Context, bucketIds []uint64, opts types.EndPointOptions) (types.ListBucketsByBucketIDResponse, error) {
const MaximumListBucketsSize = 1000
if len(bucketIds) == 0 || len(bucketIds) > MaximumListBucketsSize {
return types.ListBucketsByBucketIDResponse{}, nil
}
bucketIDMap := make(map[uint64]bool)
for _, id := range bucketIds {
if _, ok := bucketIDMap[id]; ok {
// repeat id keys in request
return types.ListBucketsByBucketIDResponse{}, nil
}
bucketIDMap[id] = true
}
idStr := make([]string, len(bucketIds))
for i, id := range bucketIds {
idStr[i] = strconv.FormatUint(id, 10)
}
IDs := strings.Join(idStr, ",")
params := url.Values{}
params.Set("buckets-query", "")
params.Set("ids", IDs)
reqMeta := requestMeta{
urlValues: params,
contentSHA256: types.EmptyStringSHA256,
}
sendOpt := sendOptions{
method: http.MethodGet,
disableCloseBody: true,
}
endpoint, err := c.getEndpointByOpt(&opts)
if err != nil {
log.Error().Msg(fmt.Sprintf("get endpoint by option failed %s", err.Error()))
return types.ListBucketsByBucketIDResponse{}, err
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return types.ListBucketsByBucketIDResponse{}, err
}
defer utils.CloseResponse(resp)
// unmarshal the json content from response body
buf := new(strings.Builder)
_, err = io.Copy(buf, resp.Body)
if err != nil {
log.Error().Msgf("the list of buckets in bucket ids:%v failed: %s", bucketIds, err.Error())
return types.ListBucketsByBucketIDResponse{}, err
}
buckets := types.ListBucketsByBucketIDResponse{}
bufStr := buf.String()
err = xml.Unmarshal([]byte(bufStr), (*listBucketsByIDsResponse)(&buckets.Buckets))
if err != nil && buckets.Buckets == nil {
log.Error().Msgf("the list of buckets in bucket ids:%v failed: %s", bucketIds, err.Error())
return types.ListBucketsByBucketIDResponse{}, err
}
return buckets, nil
}
// GetMigrateBucketApproval - Send migrate get approval request to the storage provider and return the signed MsgMigrateBucket by SP.
//
// - ctx: Context variables for the current API call.
//
// - migrateBucketMsg: Indicates msg of migrating bucket which defined by greenfield
//
// - ret1: The msg of migrating bucket which contain the approval signature from the storage provider.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) GetMigrateBucketApproval(ctx context.Context, migrateBucketMsg *storageTypes.MsgMigrateBucket) (*storageTypes.MsgMigrateBucket, error) {
unsignedBytes := migrateBucketMsg.GetSignBytes()
// set the action type
urlVal := make(url.Values)
urlVal["action"] = []string{types.MigrateBucketAction}
reqMeta := requestMeta{
urlValues: urlVal,
urlRelPath: "get-approval",
contentSHA256: types.EmptyStringSHA256,
txnMsg: hex.EncodeToString(unsignedBytes),
}
sendOpt := sendOptions{
method: http.MethodGet,
isAdminApi: true,
}
primarySPID := migrateBucketMsg.DstPrimarySpId
endpoint, err := c.getSPUrlByID(primarySPID)
if err != nil {
log.Error().Msg(fmt.Sprintf("route endpoint by addr: %d failed, err: %s", primarySPID, err.Error()))
return nil, err
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return nil, err
}
// fetch primary signed msg from sp response
signedRawMsg := resp.Header.Get(types.HTTPHeaderSignedMsg)
if signedRawMsg == "" {
return nil, errors.New("fail to fetch pre createBucket signature")
}
signedMsgBytes, err := hex.DecodeString(signedRawMsg)
if err != nil {
return nil, err
}
var signedMsg storageTypes.MsgMigrateBucket
storageTypes.ModuleCdc.MustUnmarshalJSON(signedMsgBytes, &signedMsg)
return &signedMsg, nil
}
// MigrateBucket - Get approval of migrating from SP, send the signed migrate bucket msg to greenfield chain and return the txn hash.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the bucket to be migrated.
//
// - dstPrimarySPID: The sp id of the migration target SP.
//
// - opt: The options of send transaction of migration bucket
//
// - ret1: Transaction hash return from blockchain.
//
// - ret2: Return error when the request of getting approval or sending transaction failed, otherwise return nil.
func (c *Client) MigrateBucket(ctx context.Context, bucketName string, dstPrimarySPID uint32, opts types.MigrateBucketOptions) (string, error) {
migrateBucketMsg := storageTypes.NewMsgMigrateBucket(c.MustGetDefaultAccount().GetAddress(), bucketName, dstPrimarySPID)
err := migrateBucketMsg.ValidateBasic()
if err != nil {
return "", err
}
signedMsg, err := c.GetMigrateBucketApproval(ctx, migrateBucketMsg)
if err != nil {
return "", err
}
// set the default txn broadcast mode as block mode
if opts.TxOpts == nil {
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
resp, err := c.BroadcastTx(ctx, []sdk.Msg{signedMsg}, opts.TxOpts)
if err != nil {
return "", err
}
txnHash := resp.TxResponse.TxHash
if !opts.IsAsyncMode {
ctxTimeout, cancel := context.WithTimeout(ctx, types.ContextTimeout)
defer cancel()
txnResponse, err := c.WaitForTx(ctxTimeout, txnHash)
if err != nil {
return txnHash, fmt.Errorf("the transaction has been submitted, please check it later:%v", err)
}
if txnResponse.TxResult.Code != 0 {
return txnHash, fmt.Errorf("the migrateBucket txn has failed with response code: %d, codespace:%s", txnResponse.TxResult.Code, txnResponse.TxResult.Codespace)
}
}
return txnHash, nil
}
// CancelMigrateBucket - Cancel migrate migration by sending the MsgCancelMigrateBucket msg.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The name of the migrating bucket to be canceled.
//
// - opt: The options of the proposal meta and transaction.
//
// - ret1: The proposal ID of canceling migration.
//
// - ret2: Transaction hash return from blockchain.
//
// - ret3: Return error when the request of cancel migration failed, otherwise return nil.
func (c *Client) CancelMigrateBucket(ctx context.Context, bucketName string, opts types.CancelMigrateBucketOptions) (uint64, string, error) {
govModuleAddress, err := c.GetModuleAccountByName(ctx, govTypes.ModuleName)
if err != nil {
return 0, "", err
}
cancelBucketMsg := storageTypes.NewMsgCancelMigrateBucket(
govModuleAddress.GetAddress(), bucketName,
)
err = cancelBucketMsg.ValidateBasic()
if err != nil {
return 0, "", err
}
return c.SubmitProposal(ctx, []sdk.Msg{cancelBucketMsg}, opts.ProposalDepositAmount, opts.ProposalTitle, opts.ProposalSummary, types.SubmitProposalOptions{Metadata: opts.ProposalMetadata, TxOpts: opts.TxOpts})
}
// ListBucketsByPaymentAccount - List bucket info by payment account.
//
// By inputting a collection of bucket IDs, we can retrieve the corresponding bucket data. If the bucket is nonexistent or has been deleted, a null value will be returned
//
// - ctx: Context variables for the current API call.
//
// - paymentAccount: Payment account defines the address of payment account.
//
// - opts: The options to set the meta to list buckets by payment account.
//
// - ret1: The result of bucket info under specific payment account.
//
// - ret2: Return error when the request failed, otherwise return nil.
func (c *Client) ListBucketsByPaymentAccount(ctx context.Context, paymentAccount string, opts types.ListBucketsByPaymentAccountOptions) (types.ListBucketsByPaymentAccountResult, error) {