Skip to content

Commit

Permalink
fix(encode): fix abi.encode error while *big.Int==nil, using SafeBigI…
Browse files Browse the repository at this point in the history
…nt instead of *big.Int
  • Loading branch information
cosinlink committed Jul 4, 2023
1 parent a759b06 commit 3d57436
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
15 changes: 11 additions & 4 deletions x/bridge/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ 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
Expand All @@ -45,7 +52,7 @@ var (

func (pkg *TransferOutSynPackage) Serialize() ([]byte, error) {
return transferOutSynPackageArgs.Pack(&TransferOutSynPackageStruct{
pkg.Amount,
SafeBigInt(pkg.Amount),
common.BytesToAddress(pkg.Recipient),
common.BytesToAddress(pkg.RefundAddress),
})
Expand Down Expand Up @@ -97,7 +104,7 @@ var (

func (pkg *TransferOutRefundPackage) Serialize() ([]byte, error) {
return transferOutRefundPackageArgs.Pack(&TransferOutRefundPackageStruct{
pkg.RefundAmount,
SafeBigInt(pkg.RefundAmount),
common.BytesToAddress(pkg.RefundAddr),
pkg.RefundReason,
})
Expand Down Expand Up @@ -149,7 +156,7 @@ var (

func (pkg *TransferInSynPackage) Serialize() ([]byte, error) {
return transferInSynPackageArgs.Pack(&TransferInSynPackageStruct{
pkg.Amount,
SafeBigInt(pkg.Amount),
common.BytesToAddress(pkg.ReceiverAddress),
common.BytesToAddress(pkg.RefundAddress),
})
Expand Down Expand Up @@ -201,7 +208,7 @@ var (

func (pkg *TransferInRefundPackage) Serialize() ([]byte, error) {
return transferInRefundPackageArgs.Pack(&TransferInRefundPackageStruct{
pkg.RefundAmount,
SafeBigInt(pkg.RefundAmount),
common.BytesToAddress(pkg.RefundAddress),
pkg.RefundReason,
})
Expand Down
52 changes: 40 additions & 12 deletions x/storage/types/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const (
OperationUpdateGroupMember uint8 = 4
)

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

type CrossChainPackage struct {
OperationType uint8
Package []byte
Expand Down Expand Up @@ -172,7 +179,7 @@ var (

func (pkg *MirrorBucketSynPackage) Serialize() ([]byte, error) {
return generalMirrorSynPackageArgs.Pack(&GeneralMirrorSynPackageStruct{
pkg.Id,
SafeBigInt(pkg.Id),
common.BytesToAddress(pkg.Owner),
})
}
Expand Down Expand Up @@ -205,7 +212,10 @@ func DeserializeMirrorBucketSynPackage(serializedPackage []byte) (interface{}, e
}

func (pkg *MirrorBucketAckPackage) Serialize() ([]byte, error) {
return generalMirrorAckPackageArgs.Pack(pkg)
return generalMirrorAckPackageArgs.Pack(&MirrorBucketAckPackage{
pkg.Status,
SafeBigInt(pkg.Id),
})
}

func DeserializeMirrorBucketAckPackage(serializedPackage []byte) (interface{}, error) {
Expand Down Expand Up @@ -235,7 +245,7 @@ type MirrorObjectAckPackage struct {

func (pkg *MirrorObjectSynPackage) Serialize() ([]byte, error) {
return generalMirrorSynPackageArgs.Pack(&GeneralMirrorSynPackageStruct{
pkg.Id,
SafeBigInt(pkg.Id),
common.BytesToAddress(pkg.Owner),
})
}
Expand All @@ -254,7 +264,10 @@ func DeserializeMirrorObjectSynPackage(serializedPackage []byte) (interface{}, e
}

func (pkg *MirrorObjectAckPackage) Serialize() ([]byte, error) {
return generalMirrorAckPackageArgs.Pack(pkg)
return generalMirrorAckPackageArgs.Pack(&MirrorObjectAckPackage{
pkg.Status,
SafeBigInt(pkg.Id),
})
}

func DeserializeMirrorObjectAckPackage(serializedPackage []byte) (interface{}, error) {
Expand Down Expand Up @@ -284,7 +297,7 @@ type MirrorGroupAckPackage struct {

func (pkg *MirrorGroupSynPackage) Serialize() ([]byte, error) {
return generalMirrorSynPackageArgs.Pack(&GeneralMirrorSynPackageStruct{
pkg.Id,
SafeBigInt(pkg.Id),
common.BytesToAddress(pkg.Owner),
})
}
Expand All @@ -303,7 +316,10 @@ func DeserializeMirrorGroupSynPackage(serializedPackage []byte) (interface{}, er
}

func (pkg *MirrorGroupAckPackage) Serialize() ([]byte, error) {
return generalMirrorAckPackageArgs.Pack(pkg)
return generalMirrorAckPackageArgs.Pack(&MirrorGroupAckPackage{
pkg.Status,
SafeBigInt(pkg.Id),
})
}

func DeserializeMirrorGroupAckPackage(serializedPackage []byte) (interface{}, error) {
Expand Down Expand Up @@ -452,7 +468,7 @@ var (
func (p CreateBucketAckPackage) MustSerialize() []byte {
encodedBytes, err := generalCreateAckPackageArgs.Pack(&GeneralCreateAckPackageStruct{
Status: p.Status,
Id: p.Id,
Id: SafeBigInt(p.Id),
Creator: common.BytesToAddress(p.Creator),
ExtraData: p.ExtraData,
})
Expand Down Expand Up @@ -556,7 +572,11 @@ var (
)

func (p DeleteBucketAckPackage) MustSerialize() []byte {
encodedBytes, err := generalCreateAckPackageArgs.Pack(&p)
encodedBytes, err := generalCreateAckPackageArgs.Pack(&DeleteBucketAckPackage{
p.Status,
SafeBigInt(p.Id),
p.ExtraData,
})
if err != nil {
panic("encode delete bucket ack package error")
}
Expand Down Expand Up @@ -639,7 +659,7 @@ type CreateGroupAckPackage struct {
func (p CreateGroupAckPackage) MustSerialize() []byte {
encodedBytes, err := generalCreateAckPackageArgs.Pack(&GeneralCreateAckPackageStruct{
Status: p.Status,
Id: p.Id,
Id: SafeBigInt(p.Id),
Creator: common.BytesToAddress(p.Creator),
ExtraData: p.ExtraData,
})
Expand Down Expand Up @@ -713,7 +733,11 @@ type DeleteObjectAckPackage struct {
}

func (p DeleteObjectAckPackage) MustSerialize() []byte {
encodedBytes, err := generalDeleteAckPackageArgs.Pack(&p)
encodedBytes, err := generalDeleteAckPackageArgs.Pack(&DeleteObjectAckPackage{
p.Status,
SafeBigInt(p.Id),
p.ExtraData,
})
if err != nil {
panic("encode delete object ack package error")
}
Expand Down Expand Up @@ -791,7 +815,11 @@ func DeserializeDeleteGroupAckPackage(serializedPackage []byte) (interface{}, er
}

func (p DeleteGroupAckPackage) MustSerialize() []byte {
encodedBytes, err := generalDeleteAckPackageArgs.Pack(&p)
encodedBytes, err := generalDeleteAckPackageArgs.Pack(&DeleteGroupAckPackage{
p.Status,
SafeBigInt(p.Id),
p.ExtraData,
})
if err != nil {
panic("encode delete group ack package error")
}
Expand Down Expand Up @@ -958,7 +986,7 @@ func (p UpdateGroupMemberAckPackage) MustSerialize() []byte {

encodedBytes, err := updateGroupMemberAckPackageArgs.Pack(&UpdateGroupMemberAckPackageStruct{
p.Status,
p.Id,
SafeBigInt(p.Id),
common.BytesToAddress(p.Operator),
p.OperationType,
members,
Expand Down

0 comments on commit 3d57436

Please sign in to comment.