diff --git a/x/stake/client/cli/flags.go b/x/stake/client/cli/flags.go index c32178fc5..ae6464b12 100644 --- a/x/stake/client/cli/flags.go +++ b/x/stake/client/cli/flags.go @@ -8,16 +8,16 @@ import ( // nolint const ( - FlagAddressDelegator = "address-delegator" - FlagAddressValidator = "validator" - FlagAddressValidatorSrc = "addr-validator-source" - FlagAddressValidatorDst = "addr-validator-dest" - FlagAddressSmartChainOperator = "address-smart-chain-operator" - FlagAddressSmartChainDelegator = "address-smart-chain-delegator" - FlagPubKey = "pubkey" - FlagAmount = "amount" - FlagSharesAmount = "shares-amount" - FlagSharesPercent = "shares-percent" + FlagAddressDelegator = "address-delegator" + FlagAddressValidator = "validator" + FlagAddressValidatorSrc = "addr-validator-source" + FlagAddressValidatorDst = "addr-validator-dest" + FlagAddressSmartChainValidator = "address-smart-chain-validator" + FlagAddressSmartChainBeneficiary = "address-smart-chain-beneficiary" + FlagPubKey = "pubkey" + FlagAmount = "amount" + FlagSharesAmount = "shares-amount" + FlagSharesPercent = "shares-percent" FlagMoniker = "moniker" FlagIdentity = "identity" @@ -50,21 +50,21 @@ const ( // common flagsets to add to various functions var ( - fsPk = flag.NewFlagSet("", flag.ContinueOnError) - fsAmount = flag.NewFlagSet("", flag.ContinueOnError) - fsShares = flag.NewFlagSet("", flag.ContinueOnError) - fsDescriptionCreate = flag.NewFlagSet("", flag.ContinueOnError) - fsCommissionCreate = flag.NewFlagSet("", flag.ContinueOnError) - fsCommissionUpdate = flag.NewFlagSet("", flag.ContinueOnError) - fsDescriptionEdit = flag.NewFlagSet("", flag.ContinueOnError) - fsValidator = flag.NewFlagSet("", flag.ContinueOnError) - fsDelegator = flag.NewFlagSet("", flag.ContinueOnError) - fsRedelegation = flag.NewFlagSet("", flag.ContinueOnError) - fsSideChainFull = flag.NewFlagSet("", flag.ContinueOnError) - fsSideChainEdit = flag.NewFlagSet("", flag.ContinueOnError) - fsSideChainId = flag.NewFlagSet("", flag.ContinueOnError) - fsSmartChainOperator = flag.NewFlagSet("", flag.ContinueOnError) - fsSmartChainDelegator = flag.NewFlagSet("", flag.ContinueOnError) + fsPk = flag.NewFlagSet("", flag.ContinueOnError) + fsAmount = flag.NewFlagSet("", flag.ContinueOnError) + fsShares = flag.NewFlagSet("", flag.ContinueOnError) + fsDescriptionCreate = flag.NewFlagSet("", flag.ContinueOnError) + fsCommissionCreate = flag.NewFlagSet("", flag.ContinueOnError) + fsCommissionUpdate = flag.NewFlagSet("", flag.ContinueOnError) + fsDescriptionEdit = flag.NewFlagSet("", flag.ContinueOnError) + fsValidator = flag.NewFlagSet("", flag.ContinueOnError) + fsDelegator = flag.NewFlagSet("", flag.ContinueOnError) + fsRedelegation = flag.NewFlagSet("", flag.ContinueOnError) + fsSideChainFull = flag.NewFlagSet("", flag.ContinueOnError) + fsSideChainEdit = flag.NewFlagSet("", flag.ContinueOnError) + fsSideChainId = flag.NewFlagSet("", flag.ContinueOnError) + fsSmartChainValidator = flag.NewFlagSet("", flag.ContinueOnError) + fsSmartChainBeneficiary = flag.NewFlagSet("", flag.ContinueOnError) ) func init() { @@ -101,6 +101,6 @@ func init() { fsSideChainEdit.String(FlagBLSWalletDir, "", "Absolute path of BLS wallet, should be provided if the side vote address is provided") fsSideChainEdit.String(FlagBLSPassword, "", "Password for BLS wallet") fsSideChainId.String(FlagSideChainId, "", "Chain-id of the side chain the validator belongs to") - fsSmartChainOperator.String(FlagAddressSmartChainOperator, "", "Smart chain address of the operator") - fsSmartChainDelegator.String(FlagAddressSmartChainDelegator, "", "Smart chain address of the delegator") + fsSmartChainValidator.String(FlagAddressSmartChainValidator, "", "Smart chain operator address of the validator") + fsSmartChainBeneficiary.String(FlagAddressSmartChainBeneficiary, "", "Smart chain address of the delegation's beneficiary") } diff --git a/x/stake/client/cli/tx_sidechain.go b/x/stake/client/cli/tx_sidechain.go index cc8926879..af48dcbb6 100644 --- a/x/stake/client/cli/tx_sidechain.go +++ b/x/stake/client/cli/tx_sidechain.go @@ -315,11 +315,11 @@ func GetCmdSideChainStakeMigration(cdc *codec.Codec) *cobra.Command { if err != nil { return err } - operatorAddr, err := getSmartChainAddr(FlagAddressSmartChainOperator) + operatorAddr, err := getSmartChainAddr(FlagAddressSmartChainValidator) if err != nil { return err } - delAddr, err := getSmartChainAddr(FlagAddressSmartChainDelegator) + delAddr, err := getSmartChainAddr(FlagAddressSmartChainBeneficiary) if err != nil { return err } @@ -340,8 +340,8 @@ func GetCmdSideChainStakeMigration(cdc *codec.Codec) *cobra.Command { cmd.Flags().AddFlagSet(fsAmount) cmd.Flags().AddFlagSet(fsValidator) - cmd.Flags().AddFlagSet(fsSmartChainOperator) - cmd.Flags().AddFlagSet(fsSmartChainDelegator) + cmd.Flags().AddFlagSet(fsSmartChainValidator) + cmd.Flags().AddFlagSet(fsSmartChainBeneficiary) return cmd } diff --git a/x/stake/cross_stake/cross_stake.go b/x/stake/cross_stake/cross_stake.go index abd528f38..7ea761a6b 100644 --- a/x/stake/cross_stake/cross_stake.go +++ b/x/stake/cross_stake/cross_stake.go @@ -347,7 +347,7 @@ func (app *CrossStakeApp) handleDistributeRewardRefund(ctx sdk.Context, pack *ty return sdk.ExecuteResult{}, err } - // publish event + // publish event if app.stakeKeeper.PbsbServer != nil && ctx.IsDeliverTx() { app.stakeKeeper.AddrPool.AddAddrs([]sdk.AccAddress{sdk.PegAccount, refundAddr}) PublishCrossStakeEvent(ctx, app.stakeKeeper, sdk.PegAccount.String(), []pubsub.CrossReceiver{{refundAddr.String(), pack.Amount.Int64()}}, diff --git a/x/stake/handler.go b/x/stake/handler.go index 1391ace4c..cebd507d6 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -72,8 +72,8 @@ func NewHandler(k keeper.Keeper, govKeeper gov.Keeper) sdk.Handler { case types.MsgSideChainUndelegate: return handleMsgSideChainUndelegate(ctx, msg, k) case types.MsgSideChainStakeMigration: - if !sdk.IsUpgrade(sdk.FirstSunsetFork) { - return sdk.ErrMsgNotSupported("FirstSunset not activated yet").Result() + if !sdk.IsUpgrade(sdk.FirstSunsetFork) || sdk.IsUpgrade(sdk.SecondSunsetFork) { + return sdk.ErrMsgNotSupported("MsgSideChainStakeMigration is only enabled between FirstSunsetFork and SecondSunsetFork").Result() } return handleMsgSideChainStakeMigration(ctx, msg, k) default: diff --git a/x/stake/handler_sidechain.go b/x/stake/handler_sidechain.go index 9a51aa658..63281989f 100644 --- a/x/stake/handler_sidechain.go +++ b/x/stake/handler_sidechain.go @@ -492,23 +492,21 @@ func handleMsgSideChainStakeMigration(ctx sdk.Context, msg MsgSideChainStakeMigr return ErrBadDenom(k.Codespace()).Result() } - shares, sdkErr := k.ValidateUnbondAmount(ctx, msg.RefundAddress, msg.Validator, msg.Amount.Amount) + shares, sdkErr := k.ValidateUnbondAmount(ctx, msg.RefundAddr, msg.ValidatorSrcAddr, msg.Amount.Amount) if sdkErr != nil { return sdkErr.Result() } // unbond immediately - ubd, sdkErr := k.BeginUnbonding(ctx, msg.RefundAddress, msg.Validator, shares, false) - if sdkErr != nil { - return sdkErr.Result() - } - ubd, events, sdkErr := k.CompleteUnbonding(ctx, ubd.DelegatorAddr, ubd.ValidatorAddr) + ubd, events, sdkErr := k.UnboundDelegation(ctx, msg.RefundAddr, msg.ValidatorSrcAddr, shares) if sdkErr != nil { return sdkErr.Result() } // send coins to pegAccount - _, sdkErr = k.BankKeeper.SendCoins(ctx, msg.RefundAddress, sdk.PegAccount, sdk.Coins{ubd.Balance}) + relayFee := sdk.NewCoin(denom, types.StakeMigrationRelayFee) + transferAmt := sdk.Coins{ubd.Balance}.Plus(sdk.Coins{relayFee}) + _, sdkErr = k.BankKeeper.SendCoins(ctx, msg.RefundAddr, sdk.PegAccount, transferAmt) if sdkErr != nil { return sdkErr.Result() } @@ -516,9 +514,9 @@ func handleMsgSideChainStakeMigration(ctx sdk.Context, msg MsgSideChainStakeMigr // send cross-chain package bscAmount := bsc.ConvertBCAmountToBSCAmount(ubd.Balance.Amount) stakeMigrationSynPackage := types.StakeMigrationSynPackage{ - OperatorAddress: msg.OperatorAddress, - DelegatorAddress: msg.DelegatorAddress, - RefundAddress: msg.RefundAddress, + OperatorAddress: msg.ValidatorDstAddr, + DelegatorAddress: msg.DelegatorAddr, + RefundAddress: msg.RefundAddr, Amount: bscAmount, } @@ -527,9 +525,9 @@ func handleMsgSideChainStakeMigration(ctx sdk.Context, msg MsgSideChainStakeMigr return sdk.ErrInternal("encode stake migration package error").Result() } - relayFee := bsc.ConvertBCAmountToBSCAmount(types.StakeMigrationRelayFee) + bscRelayFee := bsc.ConvertBCAmountToBSCAmount(relayFee.Amount) sendSeq, sdkErr := k.IbcKeeper.CreateRawIBCPackageByIdWithFee(ctx, k.DestChainId, types.StakeMigrationChannelID, sdk.SynCrossChainPackageType, - encodedPackage, *relayFee) + encodedPackage, *bscRelayFee) if sdkErr != nil { return sdkErr.Result() } @@ -540,8 +538,8 @@ func handleMsgSideChainStakeMigration(ctx sdk.Context, msg MsgSideChainStakeMigr StakeEvent: types.StakeEvent{ IsFromTx: true, }, - Delegator: msg.RefundAddress, - Validator: msg.Validator, + Delegator: msg.RefundAddr, + Validator: msg.ValidatorSrcAddr, Amount: msg.Amount.Amount, Denom: msg.Amount.Denom, TxHash: ctx.Value(baseapp.TxHashKey).(string), @@ -554,7 +552,7 @@ func handleMsgSideChainStakeMigration(ctx sdk.Context, msg MsgSideChainStakeMigr ChainId: k.DestChainName, RelayerFee: types.StakeMigrationRelayFee, Type: types.TransferOutType, - From: msg.RefundAddress.String(), + From: msg.RefundAddr.String(), Denom: denom, To: []pubsub.CrossReceiver{{sdk.PegAccount.String(), ubd.Balance.Amount}}, } @@ -563,8 +561,8 @@ func handleMsgSideChainStakeMigration(ctx sdk.Context, msg MsgSideChainStakeMigr finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(ubd.MinTime) txTags := sdk.NewTags( - tags.Delegator, []byte(msg.RefundAddress.String()), - tags.SrcValidator, []byte(msg.Validator.String()), + tags.Delegator, []byte(msg.RefundAddr.String()), + tags.SrcValidator, []byte(msg.ValidatorSrcAddr.String()), tags.EndTime, finishTime, ) txTags = append(txTags, sdk.GetPegInTag(denom, ubd.Balance.Amount)) diff --git a/x/stake/stake_migration/pub.go b/x/stake/stake_migration/pub.go index 7e10c395b..2d5e36a07 100644 --- a/x/stake/stake_migration/pub.go +++ b/x/stake/stake_migration/pub.go @@ -7,24 +7,22 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake/cross_stake" ) -func PublishStakeMigrationEvent(ctx sdk.Context, keeper cross_stake.Keeper, from string, to []pubsub.CrossReceiver, symbol string, +func PublishStakeMigrationRefundEvent(ctx sdk.Context, keeper cross_stake.Keeper, from string, to []pubsub.CrossReceiver, symbol string, eventType string, relayerFee int64, ) { - if keeper.PbsbServer != nil { - txHash := ctx.Value(baseapp.TxHashKey) - if txHashStr, ok := txHash.(string); ok { - event := pubsub.CrossTransferEvent{ - TxHash: txHashStr, - ChainId: keeper.DestChainName, - RelayerFee: relayerFee, - Type: eventType, - From: from, - Denom: symbol, - To: to, - } - keeper.PbsbServer.Publish(event) - } else { - ctx.Logger().With("module", "stake").Error("failed to get txhash, will not publish cross transfer event ") + txHash := ctx.Value(baseapp.TxHashKey) + if txHashStr, ok := txHash.(string); ok { + event := pubsub.CrossTransferEvent{ + TxHash: txHashStr, + ChainId: keeper.DestChainName, + RelayerFee: relayerFee, + Type: eventType, + From: from, + Denom: symbol, + To: to, } + keeper.PbsbServer.Publish(event) + } else { + ctx.Logger().With("module", "stake").Error("failed to get txhash, will not publish cross transfer event ") } } diff --git a/x/stake/stake_migration/stake_migration.go b/x/stake/stake_migration/stake_migration.go index eddb11326..c743be9fb 100644 --- a/x/stake/stake_migration/stake_migration.go +++ b/x/stake/stake_migration/stake_migration.go @@ -73,9 +73,11 @@ func (app *StakeMigrationApp) handleRefund(ctx sdk.Context, pack *types.StakeMig } // publish event - if app.stakeKeeper.PbsbServer != nil && ctx.IsDeliverTx() { + if app.stakeKeeper.AddrPool != nil && ctx.IsDeliverTx() { app.stakeKeeper.AddrPool.AddAddrs([]sdk.AccAddress{sdk.PegAccount, pack.RefundAddress}) - PublishStakeMigrationEvent(ctx, app.stakeKeeper, sdk.PegAccount.String(), []pubsub.CrossReceiver{{pack.RefundAddress.String(), pack.Amount.Int64()}}, + } + if app.stakeKeeper.PbsbServer != nil && ctx.IsDeliverTx() { + PublishStakeMigrationRefundEvent(ctx, app.stakeKeeper, sdk.PegAccount.String(), []pubsub.CrossReceiver{{pack.RefundAddress.String(), pack.Amount.Int64()}}, app.stakeKeeper.BondDenom(ctx), types.TransferInType, 0) } diff --git a/x/stake/types/stake_migration.go b/x/stake/types/stake_migration.go index 0c6a2663e..7cd4ada07 100644 --- a/x/stake/types/stake_migration.go +++ b/x/stake/types/stake_migration.go @@ -16,7 +16,7 @@ const ( MsgTypeSideChainStakeMigration = "stake_migration" - StakeMigrationRelayFee int64 = 1000000 // decimal 8 + StakeMigrationRelayFee int64 = 500000 // decimal 8 ) type StakeMigrationSynPackage struct { @@ -27,19 +27,19 @@ type StakeMigrationSynPackage struct { } type MsgSideChainStakeMigration struct { - Validator sdk.ValAddress `json:"validator"` - OperatorAddress sdk.SmartChainAddress `json:"operator_address"` - DelegatorAddress sdk.SmartChainAddress `json:"delegator_address"` - RefundAddress sdk.AccAddress `json:"refund_address"` + ValidatorSrcAddr sdk.ValAddress `json:"validator_src_addr"` + ValidatorDstAddr sdk.SmartChainAddress `json:"ValidatorDstAddr"` + DelegatorAddr sdk.SmartChainAddress `json:"delegator_addr"` + RefundAddr sdk.AccAddress `json:"refund_addr"` Amount sdk.Coin `json:"amount"` } func NewMsgSideChainStakeMigration(valAddr sdk.ValAddress, operatorAddr, delegatorAddr sdk.SmartChainAddress, refundAddr sdk.AccAddress, amount sdk.Coin) MsgSideChainStakeMigration { return MsgSideChainStakeMigration{ - Validator: valAddr, - OperatorAddress: operatorAddr, - DelegatorAddress: delegatorAddr, - RefundAddress: refundAddr, + ValidatorSrcAddr: valAddr, + ValidatorDstAddr: operatorAddr, + DelegatorAddr: delegatorAddr, + RefundAddr: refundAddr, Amount: amount, } } @@ -47,7 +47,7 @@ func NewMsgSideChainStakeMigration(valAddr sdk.ValAddress, operatorAddr, delegat func (msg MsgSideChainStakeMigration) Route() string { return MsgRoute } func (msg MsgSideChainStakeMigration) Type() string { return MsgTypeSideChainStakeMigration } func (msg MsgSideChainStakeMigration) GetSigners() []sdk.AccAddress { - return []sdk.AccAddress{msg.RefundAddress} + return []sdk.AccAddress{msg.RefundAddr} } func (msg MsgSideChainStakeMigration) GetSignBytes() []byte { @@ -56,17 +56,17 @@ func (msg MsgSideChainStakeMigration) GetSignBytes() []byte { } func (msg MsgSideChainStakeMigration) ValidateBasic() sdk.Error { - if len(msg.Validator) != sdk.AddrLen { - return sdk.ErrInvalidAddress(fmt.Sprintf("Expected validator address length is %d, actual length is %d", sdk.AddrLen, len(msg.Validator))) + if len(msg.ValidatorSrcAddr) != sdk.AddrLen { + return sdk.ErrInvalidAddress(fmt.Sprintf("Expected validator address length is %d, actual length is %d", sdk.AddrLen, len(msg.ValidatorSrcAddr))) } - if msg.OperatorAddress.IsEmpty() { + if msg.ValidatorDstAddr.IsEmpty() { return sdk.ErrInvalidAddress("operator address is empty") } - if msg.DelegatorAddress.IsEmpty() { + if msg.DelegatorAddr.IsEmpty() { return sdk.ErrInvalidAddress("delegator address is empty") } - if len(msg.RefundAddress) != sdk.AddrLen { - return sdk.ErrInvalidAddress(fmt.Sprintf("Expected refund address length is %d, actual length is %d", sdk.AddrLen, len(msg.RefundAddress))) + if len(msg.RefundAddr) != sdk.AddrLen { + return sdk.ErrInvalidAddress(fmt.Sprintf("Expected refund address length is %d, actual length is %d", sdk.AddrLen, len(msg.RefundAddr))) } if msg.Amount.Amount <= 0 { return ErrBadDelegationAmount(DefaultCodespace, "stake migration amount must be positive") @@ -75,5 +75,5 @@ func (msg MsgSideChainStakeMigration) ValidateBasic() sdk.Error { } func (msg MsgSideChainStakeMigration) GetInvolvedAddresses() []sdk.AccAddress { - return []sdk.AccAddress{msg.RefundAddress, sdk.AccAddress(msg.Validator)} + return []sdk.AccAddress{msg.RefundAddr, sdk.AccAddress(msg.ValidatorSrcAddr)} }