Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Jan 6, 2024
1 parent 24e5a14 commit 735e428
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 86 deletions.
54 changes: 27 additions & 27 deletions x/stake/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}
8 changes: 4 additions & 4 deletions x/stake/client/cli/tx_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x/stake/cross_stake/cross_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}},
Expand Down
4 changes: 2 additions & 2 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 15 additions & 17 deletions x/stake/handler_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,33 +492,31 @@ 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()
}

// 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,
}

Expand All @@ -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()
}
Expand All @@ -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),
Expand All @@ -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}},
}
Expand All @@ -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))
Expand Down
30 changes: 14 additions & 16 deletions x/stake/stake_migration/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
}
}
6 changes: 4 additions & 2 deletions x/stake/stake_migration/stake_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
34 changes: 17 additions & 17 deletions x/stake/types/stake_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

MsgTypeSideChainStakeMigration = "stake_migration"

StakeMigrationRelayFee int64 = 1000000 // decimal 8
StakeMigrationRelayFee int64 = 500000 // decimal 8
)

type StakeMigrationSynPackage struct {
Expand All @@ -27,27 +27,27 @@ 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,
}
}

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 {
Expand All @@ -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")
Expand All @@ -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)}
}

0 comments on commit 735e428

Please sign in to comment.