Skip to content

Commit

Permalink
fix: make handleCreateBucketSynPackage forward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Oct 20, 2023
1 parent ccc5951 commit 33a071a
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 5 deletions.
76 changes: 75 additions & 1 deletion x/storage/keeper/cross_app_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

Check failure on line 8 in x/storage/keeper/cross_app_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-latest)

File is not `gofmt`-ed with `-s` `-r 'a[b:len(a)] -> a[b:]'` (gofmt)
"github.com/bnb-chain/greenfield/types/common"
"github.com/bnb-chain/greenfield/x/storage/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"fmt"
)

var _ sdk.CrossChainApplication = &BucketApp{}
Expand Down Expand Up @@ -106,6 +108,9 @@ func (app *BucketApp) ExecuteSynPackage(ctx sdk.Context, appCtx *sdk.CrossChainA
case *types.CreateBucketSynPackage:
operationType = types.OperationCreateBucket
result = app.handleCreateBucketSynPackage(ctx, appCtx, p)
case *types.CreateBucketSynPackageV2:
operationType = types.OperationCreateBucket
result = app.handleCreateBucketSynPackageV2(ctx, appCtx, p)
case *types.DeleteBucketSynPackage:
operationType = types.OperationDeleteBucket
result = app.handleDeleteBucketSynPackage(ctx, appCtx, p)
Expand Down Expand Up @@ -205,6 +210,17 @@ func (app *BucketApp) handleCreateBucketFailAckPackage(ctx sdk.Context, appCtx *
}

func (app *BucketApp) handleCreateBucketSynPackage(ctx sdk.Context, appCtx *sdk.CrossChainAppContext, createBucketPackage *types.CreateBucketSynPackage) sdk.ExecuteResult {
if ctx.IsUpgraded(upgradetypes.Pampas) {
return sdk.ExecuteResult{
Payload: types.CreateBucketAckPackage{
Status: types.StatusFail,
Creator: createBucketPackage.Creator,
ExtraData: createBucketPackage.ExtraData,
}.MustSerialize(),
Err: fmt.Errorf("old CreateBucketSynPackage is not supported after pampas upgrade"),
}
}

err := createBucketPackage.ValidateBasic()
if err != nil {
return sdk.ExecuteResult{
Expand Down Expand Up @@ -236,7 +252,6 @@ func (app *BucketApp) handleCreateBucketSynPackage(ctx sdk.Context, appCtx *sdk.
PaymentAddress: createBucketPackage.PaymentAddress.String(),
PrimarySpApproval: &common.Approval{
ExpiredHeight: createBucketPackage.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: createBucketPackage.GlobalVirtualGroupFamilyId,
Sig: createBucketPackage.PrimarySpApprovalSignature,
},
ApprovalMsgBytes: createBucketPackage.GetApprovalBytes(),
Expand All @@ -263,6 +278,65 @@ func (app *BucketApp) handleCreateBucketSynPackage(ctx sdk.Context, appCtx *sdk.
}
}

func (app *BucketApp) handleCreateBucketSynPackageV2(ctx sdk.Context, appCtx *sdk.CrossChainAppContext, createBucketPackageV2 *types.CreateBucketSynPackageV2) sdk.ExecuteResult {
err := createBucketPackageV2.ValidateBasic()
if err != nil {
return sdk.ExecuteResult{
Payload: types.CreateBucketAckPackage{
Status: types.StatusFail,
Creator: createBucketPackageV2.Creator,
ExtraData: createBucketPackageV2.ExtraData,
}.MustSerialize(),
Err: err,
}
}
app.storageKeeper.Logger(ctx).Info("process create bucket syn package v2", "bucket name", createBucketPackageV2.BucketName)

sourceType, err := app.storageKeeper.GetSourceTypeByChainId(ctx, appCtx.SrcChainId)
if err != nil {
return sdk.ExecuteResult{
Err: err,
}
}

bucketId, err := app.storageKeeper.CreateBucket(ctx,
createBucketPackageV2.Creator,
createBucketPackageV2.BucketName,
createBucketPackageV2.PrimarySpAddress,
&types.CreateBucketOptions{
Visibility: types.VisibilityType(createBucketPackageV2.Visibility),
SourceType: sourceType,
ChargedReadQuota: createBucketPackageV2.ChargedReadQuota,
PaymentAddress: createBucketPackageV2.PaymentAddress.String(),
PrimarySpApproval: &common.Approval{
ExpiredHeight: createBucketPackageV2.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: createBucketPackageV2.GlobalVirtualGroupFamilyId,
Sig: createBucketPackageV2.PrimarySpApprovalSignature,
},
ApprovalMsgBytes: createBucketPackageV2.GetApprovalBytes(),
},
)
if err != nil {
return sdk.ExecuteResult{
Payload: types.CreateBucketAckPackage{
Status: types.StatusFail,
Creator: createBucketPackageV2.Creator,
ExtraData: createBucketPackageV2.ExtraData,
}.MustSerialize(),
Err: err,
}
}

return sdk.ExecuteResult{
Payload: types.CreateBucketAckPackage{
Status: types.StatusSuccess,
Id: bucketId.BigInt(),
Creator: createBucketPackageV2.Creator,
ExtraData: createBucketPackageV2.ExtraData,
}.MustSerialize(),
}
}

func (app *BucketApp) handleDeleteBucketAckPackage(ctx sdk.Context, appCtx *sdk.CrossChainAppContext, ackPackage *types.DeleteBucketAckPackage) sdk.ExecuteResult {
app.storageKeeper.Logger(ctx).Error("received delete bucket ack package ")

Expand Down
125 changes: 121 additions & 4 deletions x/storage/types/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ func DeserializeMirrorGroupAckPackage(serializedPackage []byte) (interface{}, er
}

type CreateBucketSynPackage struct {
Creator sdk.AccAddress
BucketName string
Visibility uint32
PaymentAddress sdk.AccAddress
PrimarySpAddress sdk.AccAddress
PrimarySpApprovalExpiredHeight uint64
PrimarySpApprovalSignature []byte
ChargedReadQuota uint64
ExtraData []byte
}

type CreateBucketSynPackageV2 struct {
Creator sdk.AccAddress
BucketName string
Visibility uint32
Expand All @@ -354,6 +366,18 @@ type CreateBucketSynPackage struct {
}

type CreateBucketSynPackageStruct struct {
Creator common.Address
BucketName string
Visibility uint32
PaymentAddress common.Address
PrimarySpAddress common.Address
PrimarySpApprovalExpiredHeight uint64
PrimarySpApprovalSignature []byte
ChargedReadQuota uint64
ExtraData []byte
}

type CreateBucketSynPackageV2Struct struct {
Creator common.Address
BucketName string
Visibility uint32
Expand All @@ -374,7 +398,6 @@ var (
{Name: "PaymentAddress", Type: "address"},
{Name: "PrimarySpAddress", Type: "address"},
{Name: "PrimarySpApprovalExpiredHeight", Type: "uint64"},
{Name: "GlobalVirtualGroupFamilyId", Type: "uint32"},
{Name: "PrimarySpApprovalSignature", Type: "bytes"},
{Name: "ChargedReadQuota", Type: "uint64"},
{Name: "ExtraData", Type: "bytes"},
Expand All @@ -383,6 +406,23 @@ var (
createBucketSynPackageStructArgs = abi.Arguments{
{Type: createBucketSynPackageStructType},
}

createBucketSynPackageV2StructType, _ = abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{Name: "Creator", Type: "address"},
{Name: "BucketName", Type: "string"},
{Name: "Visibility", Type: "uint32"},
{Name: "PaymentAddress", Type: "address"},
{Name: "PrimarySpAddress", Type: "address"},
{Name: "PrimarySpApprovalExpiredHeight", Type: "uint64"},
{Name: "GlobalVirtualGroupFamilyId", Type: "uint32"},
{Name: "PrimarySpApprovalSignature", Type: "bytes"},
{Name: "ChargedReadQuota", Type: "uint64"},
{Name: "ExtraData", Type: "bytes"},
})

createBucketSynPackageV2StructArgs = abi.Arguments{
{Type: createBucketSynPackageV2StructType},
}
)

func (p CreateBucketSynPackage) MustSerialize() []byte {
Expand All @@ -393,7 +433,6 @@ func (p CreateBucketSynPackage) MustSerialize() []byte {
PaymentAddress: common.BytesToAddress(p.PaymentAddress),
PrimarySpAddress: common.BytesToAddress(p.PrimarySpAddress),
PrimarySpApprovalExpiredHeight: p.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: p.GlobalVirtualGroupFamilyId,
PrimarySpApprovalSignature: p.PrimarySpApprovalSignature,
ChargedReadQuota: p.ChargedReadQuota,
ExtraData: p.ExtraData,
Expand All @@ -413,7 +452,6 @@ func (p CreateBucketSynPackage) ValidateBasic() error {
PrimarySpAddress: p.PrimarySpAddress.String(),
PrimarySpApproval: &gnfdcommon.Approval{
ExpiredHeight: p.PrimarySpApprovalExpiredHeight,

Check failure on line 454 in x/storage/types/crosschain.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-latest)

File is not `gofmt`-ed with `-s` `-r 'a[b:len(a)] -> a[b:]'` (gofmt)
GlobalVirtualGroupFamilyId: p.GlobalVirtualGroupFamilyId,
Sig: p.PrimarySpApprovalSignature,
},
ChargedReadQuota: p.ChargedReadQuota,
Expand All @@ -431,7 +469,6 @@ func (p CreateBucketSynPackage) GetApprovalBytes() []byte {
PrimarySpAddress: p.PrimarySpAddress.String(),
PrimarySpApproval: &gnfdcommon.Approval{
ExpiredHeight: p.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: p.GlobalVirtualGroupFamilyId,
Sig: p.PrimarySpApprovalSignature,
},
ChargedReadQuota: p.ChargedReadQuota,
Expand All @@ -452,6 +489,86 @@ func DeserializeCreateBucketSynPackage(serializedPackage []byte) (interface{}, e
}

tp := CreateBucketSynPackage{
pkgStruct.Creator.Bytes(),
pkgStruct.BucketName,
pkgStruct.Visibility,
pkgStruct.PaymentAddress.Bytes(),
pkgStruct.PrimarySpAddress.Bytes(),
pkgStruct.PrimarySpApprovalExpiredHeight,
pkgStruct.PrimarySpApprovalSignature,
pkgStruct.ChargedReadQuota,
pkgStruct.ExtraData,
}
return &tp, nil
}

func (p CreateBucketSynPackageV2) MustSerialize() []byte {
encodedBytes, err := createBucketSynPackageStructArgs.Pack(&CreateBucketSynPackageV2Struct{
Creator: common.BytesToAddress(p.Creator),
BucketName: p.BucketName,
Visibility: p.Visibility,
PaymentAddress: common.BytesToAddress(p.PaymentAddress),
PrimarySpAddress: common.BytesToAddress(p.PrimarySpAddress),
PrimarySpApprovalExpiredHeight: p.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: p.GlobalVirtualGroupFamilyId,
PrimarySpApprovalSignature: p.PrimarySpApprovalSignature,
ChargedReadQuota: p.ChargedReadQuota,
ExtraData: p.ExtraData,
})
if err != nil {
panic("encode create bucket syn package v2 error")
}
return encodedBytes
}

func (p CreateBucketSynPackageV2) ValidateBasic() error {
msg := MsgCreateBucket{
Creator: p.Creator.String(),
BucketName: p.BucketName,
Visibility: VisibilityType(p.Visibility),
PaymentAddress: p.PaymentAddress.String(),
PrimarySpAddress: p.PrimarySpAddress.String(),
PrimarySpApproval: &gnfdcommon.Approval{
ExpiredHeight: p.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: p.GlobalVirtualGroupFamilyId,
Sig: p.PrimarySpApprovalSignature,
},
ChargedReadQuota: p.ChargedReadQuota,
}

return msg.ValidateBasic()
}

func (p CreateBucketSynPackageV2) GetApprovalBytes() []byte {
msg := MsgCreateBucket{
Creator: p.Creator.String(),
BucketName: p.BucketName,
Visibility: VisibilityType(p.Visibility),
PaymentAddress: p.PaymentAddress.String(),
PrimarySpAddress: p.PrimarySpAddress.String(),
PrimarySpApproval: &gnfdcommon.Approval{
ExpiredHeight: p.PrimarySpApprovalExpiredHeight,
GlobalVirtualGroupFamilyId: p.GlobalVirtualGroupFamilyId,
Sig: p.PrimarySpApprovalSignature,
},
ChargedReadQuota: p.ChargedReadQuota,
}
return msg.GetApprovalBytes()
}

func DeserializeCreateBucketSynPackageV2(serializedPackage []byte) (interface{}, error) {
unpacked, err := createBucketSynPackageV2StructArgs.Unpack(serializedPackage)
if err != nil {
return nil, errors.Wrapf(ErrInvalidCrossChainPackage, "deserialize create bucket syn package v2 failed")
}

unpackedStruct := abi.ConvertType(unpacked[0], CreateBucketSynPackageV2Struct{})
pkgStruct, ok := unpackedStruct.(CreateBucketSynPackageV2Struct)
if !ok {
return nil, errors.Wrapf(ErrInvalidCrossChainPackage, "reflect create bucket syn package v2 failed")
}

tp := CreateBucketSynPackageV2{
pkgStruct.Creator.Bytes(),
pkgStruct.BucketName,
pkgStruct.Visibility,
Expand Down

0 comments on commit 33a071a

Please sign in to comment.