Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace rlp with abi.encode in cross-chain transfer #290

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ replace (
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.23.0
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-cometbft v0.0.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.1
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.1.0.20230705070958-bd8637d81e30
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edY
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/greenfield-cometbft v0.0.1 h1:pX8S9oZKjWJCrxH07l6rbU3zee9txZ11+UwO9stsNMQ=
github.com/bnb-chain/greenfield-cometbft v0.0.1/go.mod h1:9q11eHNRY9FDwFH+4pompzPNGv//Z3VcfvkELaHJPMs=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.1 h1:woVNr3N+h/dY7sTZaKscTHARJv4RHLh0+qhniqkF734=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.1/go.mod h1:ePxSdTXosDo5YE9TRxqzmXv8T0PH7AVqTfofRf81aRo=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.1.0.20230705070958-bd8637d81e30 h1:EXzLVm9zERcbCJOnhBaJ+S4UJbwUi4iAyfgbBQdO9K0=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.3-alpha.1.0.20230705070958-bd8637d81e30/go.mod h1:ePxSdTXosDo5YE9TRxqzmXv8T0PH7AVqTfofRf81aRo=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230425074444-eb5869b05fe9 h1:6fLpmmI0EZvDTfPvI0zy5dBaaTUboHnEkoC5/p/w8TQ=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230425074444-eb5869b05fe9/go.mod h1:rbc4o84RSEvhf09o2+4Qiazsv0snRJLiEZdk17HeIDw=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230425074444-eb5869b05fe9 h1:1ZdK+iR1Up02bOa2YTZCml7PBpP//kcdamOcK6aWO/s=
Expand Down
7 changes: 3 additions & 4 deletions x/bridge/keeper/cross_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/big"
"testing"

"github.com/cosmos/cosmos-sdk/bsc/rlp"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (s *TestSuite) TestTransferOutAck() {
RefundReason: 1,
}

packageBytes, err := rlp.EncodeToBytes(&refundPackage)
packageBytes, err := refundPackage.Serialize()
s.Require().Nil(err, "encode refund package error")

transferOutApp := keeper.NewTransferOutApp(*s.bridgeKeeper)
Expand All @@ -94,7 +93,7 @@ func (s *TestSuite) TestTransferOutFailAck() {
RefundAddress: addr1,
}

packageBytes, err := rlp.EncodeToBytes(&synPackage)
packageBytes, err := synPackage.Serialize()
s.Require().Nil(err, "encode refund package error")

transferOutApp := keeper.NewTransferOutApp(*s.bridgeKeeper)
Expand Down Expand Up @@ -172,7 +171,7 @@ func (s *TestSuite) TestTransferInSyn() {
RefundAddress: sdk.AccAddress{1},
}

packageBytes, err := rlp.EncodeToBytes(&transferInSynPackage)
packageBytes, err := transferInSynPackage.Serialize()
s.Require().Nil(err, "encode refund package error")

transferInApp := keeper.NewTransferInApp(*s.bridgeKeeper)
Expand Down
3 changes: 1 addition & 2 deletions x/bridge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"cosmossdk.io/errors"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/bsc/rlp"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (k Keeper) GetRefundTransferInPayload(transferInClaim *types.TransferInSynP
RefundReason: refundReason,
}

encodedBytes, err := rlp.EncodeToBytes(refundPackage)
encodedBytes, err := refundPackage.Serialize()
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidPackage, "encode refund package error")
}
Expand Down
3 changes: 1 addition & 2 deletions x/bridge/keeper/msg_server_transfer_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"
crosschaintypes "github.com/cosmos/cosmos-sdk/x/crosschain/types"

Expand Down Expand Up @@ -42,7 +41,7 @@ func (k msgServer) TransferOut(goCtx context.Context, msg *types.MsgTransferOut)
Amount: msg.Amount.Amount.BigInt(),
}

encodedPackage, err := rlp.EncodeToBytes(transferPackage)
encodedPackage, err := transferPackage.Serialize()
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidPackage, "encode transfer out package error")
}
Expand Down
162 changes: 153 additions & 9 deletions x/bridge/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"math/big"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/ethereum/go-ethereum/accounts/abi"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sort the imports here

"github.com/ethereum/go-ethereum/common"
)

const (
Expand All @@ -18,17 +19,61 @@ const (
SyncParamsChannelID = types.SyncParamsChannelID
)

func SafeBigInt(input *big.Int) *big.Int {
if input == nil {
return big.NewInt(0)
}
return input
}

type TransferOutSynPackage struct {
Amount *big.Int
Recipient sdk.AccAddress
RefundAddress sdk.AccAddress
}

type TransferOutSynPackageStruct struct {
Amount *big.Int
Recipient common.Address
RefundAddress common.Address
}

var (
transferOutSynPackageType, _ = abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{Name: "Amount", Type: "uint256"},
{Name: "Recipient", Type: "address"},
{Name: "RefundAddress", Type: "address"},
})

transferOutSynPackageArgs = abi.Arguments{
{Type: transferOutSynPackageType},
}
)

func (pkg *TransferOutSynPackage) Serialize() ([]byte, error) {
return transferOutSynPackageArgs.Pack(&TransferOutSynPackageStruct{
SafeBigInt(pkg.Amount),
common.BytesToAddress(pkg.Recipient),
common.BytesToAddress(pkg.RefundAddress),
})
}

func DeserializeTransferOutSynPackage(serializedPackage []byte) (*TransferOutSynPackage, error) {
var tp TransferOutSynPackage
err := rlp.DecodeBytes(serializedPackage, &tp)
unpacked, err := transferOutSynPackageArgs.Unpack(serializedPackage)
if err != nil {
return nil, errors.Wrapf(ErrInvalidPackage, "deserialize transfer out package failed")
return nil, errors.Wrapf(ErrInvalidPackage, "deserialize transfer out sync package failed")
}

unpackedStruct := abi.ConvertType(unpacked[0], TransferOutSynPackageStruct{})
pkgStruct, ok := unpackedStruct.(TransferOutSynPackageStruct)
if !ok {
return nil, errors.Wrapf(ErrInvalidPackage, "reflect transfer out sync package failed")
}

tp := TransferOutSynPackage{
pkgStruct.Amount,
pkgStruct.Recipient.Bytes(),
pkgStruct.RefundAddress.Bytes(),
}
return &tp, nil
}
Expand All @@ -39,12 +84,49 @@ type TransferOutRefundPackage struct {
RefundReason uint32
}

type TransferOutRefundPackageStruct struct {
RefundAmount *big.Int
RefundAddr common.Address
RefundReason uint32
}

var (
transferOutRefundPackageType, _ = abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{Name: "RefundAmount", Type: "uint256"},
{Name: "RefundAddr", Type: "address"},
{Name: "RefundReason", Type: "uint32"},
})

transferOutRefundPackageArgs = abi.Arguments{
{Type: transferOutRefundPackageType},
}
)

func (pkg *TransferOutRefundPackage) Serialize() ([]byte, error) {
return transferOutRefundPackageArgs.Pack(&TransferOutRefundPackageStruct{
SafeBigInt(pkg.RefundAmount),
common.BytesToAddress(pkg.RefundAddr),
pkg.RefundReason,
})
}

func DeserializeTransferOutRefundPackage(serializedPackage []byte) (*TransferOutRefundPackage, error) {
var tp TransferOutRefundPackage
err := rlp.DecodeBytes(serializedPackage, &tp)
unpacked, err := transferOutRefundPackageArgs.Unpack(serializedPackage)
if err != nil {
return nil, errors.Wrapf(ErrInvalidPackage, "deserialize transfer out refund package failed")
}

unpackedStruct := abi.ConvertType(unpacked[0], TransferOutRefundPackageStruct{})
pkgStruct, ok := unpackedStruct.(TransferOutRefundPackageStruct)
if !ok {
return nil, errors.Wrapf(ErrInvalidPackage, "reflect transfer out refund package failed")
}

tp := TransferOutRefundPackage{
pkgStruct.RefundAmount,
pkgStruct.RefundAddr.Bytes(),
pkgStruct.RefundReason,
}
return &tp, nil
}

Expand All @@ -54,12 +136,48 @@ type TransferInSynPackage struct {
RefundAddress sdk.AccAddress
}

type TransferInSynPackageStruct struct {
Amount *big.Int
ReceiverAddress common.Address
RefundAddress common.Address
}

var (
transferInSynPackageType, _ = abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{Name: "Amount", Type: "uint256"},
{Name: "ReceiverAddress", Type: "address"},
{Name: "RefundAddress", Type: "address"},
})

transferInSynPackageArgs = abi.Arguments{
{Type: transferInSynPackageType},
}
)

func (pkg *TransferInSynPackage) Serialize() ([]byte, error) {
return transferInSynPackageArgs.Pack(&TransferInSynPackageStruct{
SafeBigInt(pkg.Amount),
common.BytesToAddress(pkg.ReceiverAddress),
common.BytesToAddress(pkg.RefundAddress),
})
}

func DeserializeTransferInSynPackage(serializedPackage []byte) (*TransferInSynPackage, error) {
var tp TransferInSynPackage
err := rlp.DecodeBytes(serializedPackage, &tp)
unpacked, err := transferInSynPackageArgs.Unpack(serializedPackage)
if err != nil {
return nil, errors.Wrapf(ErrInvalidPackage, "deserialize transfer in package failed")
return nil, errors.Wrapf(ErrInvalidPackage, "deserialize transfer in sync package failed")
}

unpackedStruct := abi.ConvertType(unpacked[0], TransferInSynPackageStruct{})
pkgStruct, ok := unpackedStruct.(TransferInSynPackageStruct)
if !ok {
return nil, errors.Wrapf(ErrInvalidPackage, "reflect transfer in sync package failed")
}

tp := TransferInSynPackage{
pkgStruct.Amount,
pkgStruct.ReceiverAddress.Bytes(),
pkgStruct.RefundAddress.Bytes(),
}
return &tp, nil
}
Expand All @@ -69,3 +187,29 @@ type TransferInRefundPackage struct {
RefundAddress sdk.AccAddress
RefundReason uint32
}

type TransferInRefundPackageStruct struct {
RefundAmount *big.Int
RefundAddress common.Address
RefundReason uint32
}

var (
transferInRefundPackageType, _ = abi.NewType("tuple", "", []abi.ArgumentMarshaling{
{Name: "RefundAmount", Type: "uint256"},
{Name: "RefundAddr", Type: "address"},
{Name: "RefundReason", Type: "uint32"},
})

transferInRefundPackageArgs = abi.Arguments{
{Type: transferInRefundPackageType},
}
)

func (pkg *TransferInRefundPackage) Serialize() ([]byte, error) {
return transferInRefundPackageArgs.Pack(&TransferInRefundPackageStruct{
SafeBigInt(pkg.RefundAmount),
common.BytesToAddress(pkg.RefundAddress),
pkg.RefundReason,
})
}
4 changes: 2 additions & 2 deletions x/permission/types/common.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package types

import (
"github.com/bnb-chain/greenfield/types"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bnb-chain/greenfield/types"
)

func NewPrincipalWithAccount(addr sdk.AccAddress) *Principal {
Expand Down
19 changes: 3 additions & 16 deletions x/storage/keeper/cross_app_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bnb-chain/greenfield/x/storage/types"
Expand Down Expand Up @@ -50,11 +49,7 @@ func (app *BucketApp) ExecuteAckPackage(ctx sdk.Context, appCtx *sdk.CrossChainA
OperationType: operationType,
Package: result.Payload,
}
wrapPayloadBts, err := rlp.EncodeToBytes(wrapPayload)
if err != nil {
panic(err)
}
result.Payload = wrapPayloadBts
result.Payload = wrapPayload.MustSerialize()
}

return result
Expand Down Expand Up @@ -88,11 +83,7 @@ func (app *BucketApp) ExecuteFailAckPackage(ctx sdk.Context, appCtx *sdk.CrossCh
OperationType: operationType,
Package: result.Payload,
}
wrapPayloadBts, err := rlp.EncodeToBytes(wrapPayload)
if err != nil {
panic(err)
}
result.Payload = wrapPayloadBts
result.Payload = wrapPayload.MustSerialize()
}

return result
Expand Down Expand Up @@ -126,11 +117,7 @@ func (app *BucketApp) ExecuteSynPackage(ctx sdk.Context, appCtx *sdk.CrossChainA
OperationType: operationType,
Package: result.Payload,
}
wrapPayloadBts, err := rlp.EncodeToBytes(wrapPayload)
if err != nil {
panic(err)
}
result.Payload = wrapPayloadBts
result.Payload = wrapPayload.MustSerialize()
}

return result
Expand Down
Loading