Skip to content

Commit

Permalink
Merge pull request #803 from binance-chain/enable-scripts-cross-chain…
Browse files Browse the repository at this point in the history
…-transfer

R4R: enable account scripts for cross chain transfer
  • Loading branch information
HaoyangLiu authored Dec 31, 2020
2 parents 0736700 + c6a4ba9 commit e78ca4c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
upgrade.Mgr.AddUpgradeHeight(upgrade.ListingRuleUpgrade, upgradeConfig.ListingRuleUpgradeHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.FixZeroBalance, upgradeConfig.FixZeroBalanceHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.LaunchBscUpgrade, upgradeConfig.LaunchBscUpgradeHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.EnableAccountScriptsForCrossChainTransfer, upgradeConfig.EnableAccountScriptsForCrossChainTransferHeight)

upgrade.Mgr.AddUpgradeHeight(upgrade.BEP8, upgradeConfig.BEP8Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP67, upgradeConfig.BEP67Height)
Expand Down
12 changes: 8 additions & 4 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ BEP82Height = {{ .UpgradeConfig.BEP82Height }}
BEP84Height = {{ .UpgradeConfig.BEP84Height }}
# Block height of FixFailAckPackage upgrade
FixFailAckPackageHeight = {{ .UpgradeConfig.FixFailAckPackageHeight }}
# Block height of EnableAccountScriptsForCrossChainTransferHeight upgrade
EnableAccountScriptsForCrossChainTransferHeight = {{ .UpgradeConfig.EnableAccountScriptsForCrossChainTransferHeight }}
[query]
# ABCI query interface black list, suggested value: ["custom/gov/proposals", "custom/timelock/timelocks", "custom/atomicSwap/swapcreator", "custom/atomicSwap/swaprecipient"]
Expand Down Expand Up @@ -504,10 +506,11 @@ type UpgradeConfig struct {
BEP67Height int64 `mapstructure:"BEP67Height"`
BEP70Height int64 `mapstructure:"BEP70Height"`

AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"`
BEP82Height int64 `mapstructure:"BEP82Height"`
BEP84Height int64 `mapstructure:"BEP84Height"`
FixFailAckPackageHeight int64 `mapstructure:"FixFailAckPackageHeight"`
AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"`
BEP82Height int64 `mapstructure:"BEP82Height"`
BEP84Height int64 `mapstructure:"BEP84Height"`
FixFailAckPackageHeight int64 `mapstructure:"FixFailAckPackageHeight"`
EnableAccountScriptsForCrossChainTransferHeight int64 `mapstructure:"EnableAccountScriptsForCrossChainTransferHeight"`
}

func defaultUpgradeConfig() *UpgradeConfig {
Expand All @@ -531,6 +534,7 @@ func defaultUpgradeConfig() *UpgradeConfig {
BEP82Height: math.MaxInt64,
BEP84Height: math.MaxInt64,
FixFailAckPackageHeight: math.MaxInt64,
EnableAccountScriptsForCrossChainTransferHeight: math.MaxInt64,
}
}

Expand Down
2 changes: 2 additions & 0 deletions common/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
// TODO: add upgrade name
LaunchBscUpgrade = sdk.LaunchBscUpgrade

EnableAccountScriptsForCrossChainTransfer = "EnableAccountScriptsForCrossChainTransfer"

//Nightingale upgrade
BEP8 = sdk.BEP8 // https://github.com/binance-chain/BEPs/pull/69 Mini token upgrade
BEP67 = "BEP67" // https://github.com/binance-chain/BEPs/pull/67 Expiry time upgrade
Expand Down
34 changes: 32 additions & 2 deletions plugins/bridge/cross_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"

Expand Down Expand Up @@ -350,6 +351,7 @@ func (app *TransferInApp) ExecuteSynPackage(ctx sdk.Context, payload []byte, rel
}
return sdk.ExecuteResult{
Payload: refundPackage,
Tags: types.GenerateTransferInTags(transferInPackage.ReceiverAddresses, symbol, transferInPackage.Amounts, true),
Err: types.ErrTokenBindRelationChanged("contract addr mismatch"),
}
}
Expand All @@ -362,6 +364,7 @@ func (app *TransferInApp) ExecuteSynPackage(ctx sdk.Context, payload []byte, rel
}
return sdk.ExecuteResult{
Payload: refundPackage,
Tags: types.GenerateTransferInTags(transferInPackage.ReceiverAddresses, symbol, transferInPackage.Amounts, true),
Err: types.ErrTransferInExpire("the package is expired"),
}
}
Expand All @@ -381,10 +384,37 @@ func (app *TransferInApp) ExecuteSynPackage(ctx sdk.Context, payload []byte, rel
}
return sdk.ExecuteResult{
Payload: refundPackage,
Tags: types.GenerateTransferInTags(transferInPackage.ReceiverAddresses, symbol, transferInPackage.Amounts, true),
Err: sdk.ErrInsufficientFunds("balance of peg account is insufficient"),
}
}

for idx, receiverAddr := range transferInPackage.ReceiverAddresses {
amount := sdk.NewCoin(symbol, transferInPackage.Amounts[idx].Int64())
if sdk.IsUpgrade(upgrade.EnableAccountScriptsForCrossChainTransfer) {
sendMsg := bank.NewMsgSend(
[]bank.Input{bank.NewInput(types.PegAccount, sdk.Coins{amount})},
[]bank.Output{bank.NewOutput(receiverAddr, sdk.Coins{amount})})

for _, script := range sdk.GetRegisteredScripts(sendMsg.Type()) {
if script == nil {
continue
}
if script(ctx, sendMsg) != nil {
refundPackage, sdkErr := app.bridgeKeeper.RefundTransferIn(tokenInfo.GetContractDecimals(), transferInPackage, types.ForbidTransferToBPE12Addr)
if sdkErr != nil {
log.With("module", "bridge").Error("refund transfer in error", "err", sdkErr.Error())
panic(sdkErr)
}
return sdk.ExecuteResult{
Payload: refundPackage,
Tags: types.GenerateTransferInTags(transferInPackage.ReceiverAddresses, symbol, transferInPackage.Amounts, true),
Err: types.ErrScriptsExecutionError("account scripts execution error"),
}
}
}
}
}
for idx, receiverAddr := range transferInPackage.ReceiverAddresses {
amount := sdk.NewCoin(symbol, transferInPackage.Amounts[idx].Int64())
_, sdkErr = app.bridgeKeeper.BankKeeper.SendCoins(ctx, types.PegAccount, receiverAddr, sdk.Coins{amount})
Expand All @@ -409,10 +439,10 @@ func (app *TransferInApp) ExecuteSynPackage(ctx sdk.Context, payload []byte, rel

// emit peg related event
var totalAmount int64 = 0
var tags sdk.Tags
tags := types.GenerateTransferInTags(transferInPackage.ReceiverAddresses, symbol, transferInPackage.Amounts, false)
if totalTransferInAmount != nil {
totalAmount = totalTransferInAmount.AmountOf(symbol)
tags = sdk.Tags{sdk.GetPegOutTag(symbol, totalAmount)}
tags = tags.AppendTags(sdk.Tags{sdk.GetPegOutTag(symbol, totalAmount)})
}

return sdk.ExecuteResult{
Expand Down
5 changes: 5 additions & 0 deletions plugins/bridge/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
CodeDeserializePackageFailed sdk.CodeType = 14
CodeTokenBindRelationChanged sdk.CodeType = 15
CodeTransferInExpire sdk.CodeType = 16
CodeScriptsExecutionError sdk.CodeType = 17
)

//----------------------------------------
Expand Down Expand Up @@ -91,3 +92,7 @@ func ErrTokenBindRelationChanged(msg string) sdk.Error {
func ErrTransferInExpire(msg string) sdk.Error {
return sdk.NewError(DefaultCodespace, CodeTransferInExpire, msg)
}

func ErrScriptsExecutionError(msg string) sdk.Error {
return sdk.NewError(DefaultCodespace, CodeScriptsExecutionError, msg)
}
9 changes: 5 additions & 4 deletions plugins/bridge/types/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ func DeserializeTransferOutSynPackage(serializedPackage []byte) (*TransferOutSyn
type RefundReason uint32

const (
UnboundToken RefundReason = 1
Timeout RefundReason = 2
InsufficientBalance RefundReason = 3
Unknown RefundReason = 4
UnboundToken RefundReason = 1
Timeout RefundReason = 2
InsufficientBalance RefundReason = 3
Unknown RefundReason = 4
ForbidTransferToBPE12Addr RefundReason = 5
)

type TransferOutRefundPackage struct {
Expand Down
24 changes: 24 additions & 0 deletions plugins/bridge/types/tags.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
package types

import (
"fmt"
"math/big"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
TagSendSequence = "SendSequence"
TagChannel = "Channel"
TagRelayerFee = "relayerFee"

transferInSuccess = "transferInSuccess_%s_%s"
transferInRefund = "transferInRefund_%s_%s"
)

func GenerateTransferInTags(receiverAddresses []sdk.AccAddress, symbol string, amounts []*big.Int, isRefund bool) sdk.Tags {
tags := sdk.EmptyTags()
for idx, receiver := range receiverAddresses {
tags = tags.AppendTag(fmt.Sprintf(transferInSuccess, symbol, receiver.String()), []byte(strconv.FormatInt(amounts[idx].Int64(), 10)))
if isRefund {
tags = tags.AppendTag(fmt.Sprintf(transferInRefund, symbol, receiver.String()), []byte(strconv.FormatInt(amounts[idx].Int64(), 10)))
} else {
tags = tags.AppendTag(fmt.Sprintf(transferInSuccess, symbol, receiver.String()), []byte(strconv.FormatInt(amounts[idx].Int64(), 10)))
}
}
return tags
}

0 comments on commit e78ca4c

Please sign in to comment.