From 4beeeb7ba85598a466be745e453413b4d21dea33 Mon Sep 17 00:00:00 2001 From: Loverush Date: Tue, 2 Aug 2022 18:38:22 +0800 Subject: [PATCH] Fix third review comments and update cross chain packages' data structs --- x/paramHub/genesis.go | 4 +- x/stake/cross_stake/cross_stake.go | 114 ++++++++++++++----------- x/stake/cross_stake/serialize.go | 92 +++++++++----------- x/stake/cross_stake/serialize_test.go | 7 +- x/stake/keeper/delegation.go | 28 +++--- x/stake/keeper/distribute_sidechain.go | 3 +- x/stake/types/cross_stake.go | 77 ++++++++++------- x/stake/types/cross_stake_test.go | 12 +-- 8 files changed, 179 insertions(+), 158 deletions(-) diff --git a/x/paramHub/genesis.go b/x/paramHub/genesis.go index dc73ac600..28ca132cf 100644 --- a/x/paramHub/genesis.go +++ b/x/paramHub/genesis.go @@ -73,8 +73,8 @@ const ( MiniListingFee = 8e8 // Cross stake fee - CrossDistributeRewardRelayFee = 2e6 // 0.02 BNB - CrossDistributeUndelegatedRelayFee = 2e6 // 0.02 BNB + CrossDistributeRewardRelayFee = 6e5 // 0.006 BNB + CrossDistributeUndelegatedRelayFee = 6e5 // 0.006 BNB ) var DefaultGenesisState = param.GenesisState{ diff --git a/x/stake/cross_stake/cross_stake.go b/x/stake/cross_stake/cross_stake.go index 86b838892..3edac55e5 100644 --- a/x/stake/cross_stake/cross_stake.go +++ b/x/stake/cross_stake/cross_stake.go @@ -20,26 +20,24 @@ func NewCrossStakeApp(stakeKeeper Keeper) *CrossStakeApp { } } -func (app *CrossStakeApp) ExecuteAckPackage(ctx sdk.Context, payload []byte) sdk.ExecuteResult { - if len(payload) == 0 { - app.stakeKeeper.Logger(ctx).Info("receive cross stake ack package") - return sdk.ExecuteResult{} - } - - pack, err := DeserializeCrossStakeAckPackage(payload) +func (app *CrossStakeApp) ExecuteSynPackage(ctx sdk.Context, payload []byte, relayFee int64) sdk.ExecuteResult { + app.stakeKeeper.Logger(ctx).Info("receive cross stake syn package") + pack, err := DeserializeCrossStakeSynPackage(payload) if err != nil { - app.stakeKeeper.Logger(ctx).Error("unmarshal cross stake ack package error", "err", err.Error(), "package", string(payload)) - panic("unmarshal cross stake ack package error") + app.stakeKeeper.Logger(ctx).Error("unmarshal cross stake sync claim error", "err", err.Error(), "claim", string(payload)) + panic("unmarshal cross stake claim error") } var result sdk.ExecuteResult switch p := pack.(type) { - case types.CrossStakeDistributeRewardSynPackage: - result, err = app.handleDistributeRewardRefund(ctx, p) - case types.CrossStakeDistributeUndelegatedSynPackage: - result, err = app.handleDistributeUndelegatedRefund(ctx, p) + case *types.CrossStakeDelegateSynPackage: + result, err = app.handleDelegate(ctx, p, relayFee) + case *types.CrossStakeUndelegateSynPackage: + result, err = app.handleUndelegate(ctx, p, relayFee) + case *types.CrossStakeRedelegateSynPackage: + result, err = app.handleRedelegate(ctx, p, relayFee) default: - panic("Unknown cross stake ack package type") + panic("Unknown cross stake syn package type") } if err != nil { panic(err) @@ -48,61 +46,77 @@ func (app *CrossStakeApp) ExecuteAckPackage(ctx sdk.Context, payload []byte) sdk return result } -func (app *CrossStakeApp) ExecuteFailAckPackage(ctx sdk.Context, payload []byte) sdk.ExecuteResult { +func (app *CrossStakeApp) ExecuteAckPackage(ctx sdk.Context, payload []byte) sdk.ExecuteResult { if len(payload) == 0 { - app.stakeKeeper.Logger(ctx).Info("receive cross stake fail ack package") + app.stakeKeeper.Logger(ctx).Info("receive cross stake ack package") return sdk.ExecuteResult{} } - pack, err := DeserializeCrossStakeFailAckPackage(payload) + pack, err := DeserializeCrossStakeRefundPackage(payload) if err != nil { - app.stakeKeeper.Logger(ctx).Error("unmarshal cross stake fail ack package error", "err", err.Error(), "package", string(payload)) - panic("unmarshal cross stake fail ack package error") + app.stakeKeeper.Logger(ctx).Error("unmarshal cross stake refund package error", "err", err.Error(), "package", string(payload)) + return sdk.ExecuteResult{} } var result sdk.ExecuteResult - switch p := pack.(type) { - case types.CrossStakeDistributeRewardSynPackage: - result, err = app.handleDistributeRewardRefund(ctx, p) - case types.CrossStakeDistributeUndelegatedSynPackage: - result, err = app.handleDistributeUndelegatedRefund(ctx, p) + switch pack.EventType { + case types.CrossStakeTypeDistributeReward: + result, err = app.handleDistributeRewardRefund(ctx, pack) + case types.CrossStakeTypeDistributeUndelegated: + result, err = app.handleDistributeUndelegatedRefund(ctx, pack) default: - panic("Unknown cross stake fail ack package type") + app.stakeKeeper.Logger(ctx).Error("unknown cross stake ack event type", "package", string(payload)) + return sdk.ExecuteResult{} } if err != nil { - panic(err) + app.stakeKeeper.Logger(ctx).Error("handle cross stake refund package error", "err", err.Error(), "package", string(payload)) + return sdk.ExecuteResult{} } return result } -func (app *CrossStakeApp) ExecuteSynPackage(ctx sdk.Context, payload []byte, relayFee int64) sdk.ExecuteResult { - app.stakeKeeper.Logger(ctx).Info("receive cross stake syn package") - pack, err := DeserializeCrossStakeSynPackage(payload) +func (app *CrossStakeApp) ExecuteFailAckPackage(ctx sdk.Context, payload []byte) sdk.ExecuteResult { + if len(payload) == 0 { + app.stakeKeeper.Logger(ctx).Info("receive cross stake fail ack package") + return sdk.ExecuteResult{} + } + + pack, err := DeserializeCrossStakeFailAckPackage(payload) if err != nil { - app.stakeKeeper.Logger(ctx).Error("unmarshal cross stake sync claim error", "err", err.Error(), "claim", string(payload)) - panic("unmarshal cross stake claim error") + app.stakeKeeper.Logger(ctx).Error("unmarshal cross stake fail ack package error", "err", err.Error(), "package", string(payload)) + return sdk.ExecuteResult{} } var result sdk.ExecuteResult switch p := pack.(type) { - case types.CrossStakeDelegateSynPackage: - result, err = app.handleDelegate(ctx, p, relayFee) - case types.CrossStakeUndelegateSynPackage: - result, err = app.handleUndelegate(ctx, p, relayFee) - case types.CrossStakeRedelegateSynPackage: - result, err = app.handleRedelegate(ctx, p, relayFee) + case *types.CrossStakeDistributeRewardSynPackage: + refundPackage := &types.CrossStakeRefundPackage{ + EventType: types.CrossStakeTypeDistributeReward, + Amount: p.Amount, + Recipient: p.Recipient, + } + result, err = app.handleDistributeRewardRefund(ctx, refundPackage) + case *types.CrossStakeDistributeUndelegatedSynPackage: + refundPackage := &types.CrossStakeRefundPackage{ + EventType: types.CrossStakeTypeDistributeUndelegated, + Amount: p.Amount, + Recipient: p.Recipient, + } + result, err = app.handleDistributeUndelegatedRefund(ctx, refundPackage) default: - panic("Unknown cross stake syn package type") + app.stakeKeeper.Logger(ctx).Error("unknown cross stake fail ack event type", "err", err.Error(), "package", string(payload)) + return sdk.ExecuteResult{} } if err != nil { - panic(err) + app.stakeKeeper.Logger(ctx).Error("handle cross stake fail ack package error", "err", err.Error(), "package", string(payload)) + return sdk.ExecuteResult{} } return result } -func (app *CrossStakeApp) handleDelegate(ctx sdk.Context, pack types.CrossStakeDelegateSynPackage, relayFee int64) (sdk.ExecuteResult, error) { +func (app *CrossStakeApp) handleDelegate(ctx sdk.Context, pack *types.CrossStakeDelegateSynPackage, relayFee int64) (sdk.ExecuteResult, error) { sideChainId := app.stakeKeeper.DestChainName if scCtx, err := app.stakeKeeper.ScKeeper.PrepareCtxForSideChain(ctx, sideChainId); err != nil { return sdk.ExecuteResult{}, err @@ -122,8 +136,7 @@ func (app *CrossStakeApp) handleDelegate(ctx sdk.Context, pack types.CrossStakeD sdkErr = types.ErrValidatorJailed(types.DefaultCodespace) errCode = CrossStakeErrValidatorJailed } - ackPack := types.NewCrossStakeDelegationAckPackage(&pack, types.CrossStakeTypeDelegate, errCode) - ackPack.Amount = bsc.ConvertBCAmountToBSCAmount(ackPack.Amount.Int64()) + ackPack := types.NewCrossStakeDelegationAckPackage(pack, types.CrossStakeTypeDelegate, errCode) ackBytes, err := rlp.EncodeToBytes(ackPack) if err != nil { return sdk.ExecuteResult{}, err @@ -178,7 +191,7 @@ func (app *CrossStakeApp) handleDelegate(ctx sdk.Context, pack types.CrossStakeD }, nil } -func (app *CrossStakeApp) handleUndelegate(ctx sdk.Context, pack types.CrossStakeUndelegateSynPackage, relayFee int64) (sdk.ExecuteResult, error) { +func (app *CrossStakeApp) handleUndelegate(ctx sdk.Context, pack *types.CrossStakeUndelegateSynPackage, relayFee int64) (sdk.ExecuteResult, error) { sideChainId := app.stakeKeeper.DestChainName if scCtx, err := app.stakeKeeper.ScKeeper.PrepareCtxForSideChain(ctx, sideChainId); err != nil { return sdk.ExecuteResult{}, err @@ -189,8 +202,7 @@ func (app *CrossStakeApp) handleUndelegate(ctx sdk.Context, pack types.CrossStak delAddr := types.GetStakeCAoB(pack.DelAddr[:], types.DelegateCAoBSalt) shares, sdkErr := app.stakeKeeper.ValidateUnbondAmount(ctx, delAddr, pack.Validator, pack.Amount.Int64()) if sdkErr != nil { - ackPack := types.NewCrossStakeUndelegateAckPackage(&pack, types.CrossStakeTypeUndelegate, CrossStakeErrBadDelegation) - ackPack.Amount = bsc.ConvertBCAmountToBSCAmount(ackPack.Amount.Int64()) + ackPack := types.NewCrossStakeUndelegateAckPackage(pack, types.CrossStakeTypeUndelegate, CrossStakeErrBadDelegation) ackBytes, err := rlp.EncodeToBytes(ackPack) if err != nil { return sdk.ExecuteResult{}, err @@ -232,7 +244,7 @@ func (app *CrossStakeApp) handleUndelegate(ctx sdk.Context, pack types.CrossStak }, nil } -func (app *CrossStakeApp) handleRedelegate(ctx sdk.Context, pack types.CrossStakeRedelegateSynPackage, relayFee int64) (sdk.ExecuteResult, error) { +func (app *CrossStakeApp) handleRedelegate(ctx sdk.Context, pack *types.CrossStakeRedelegateSynPackage, relayFee int64) (sdk.ExecuteResult, error) { sideChainId := app.stakeKeeper.DestChainName if scCtx, err := app.stakeKeeper.ScKeeper.PrepareCtxForSideChain(ctx, sideChainId); err != nil { return sdk.ExecuteResult{}, err @@ -251,8 +263,7 @@ func (app *CrossStakeApp) handleRedelegate(ctx sdk.Context, pack types.CrossStak sdkErr = types.ErrValidatorJailed(types.DefaultCodespace) errCode = CrossStakeErrValidatorJailed } - ackPack := types.NewCrossStakeRedelegationAckPackage(&pack, types.CrossStakeTypeRedelegate, errCode) - ackPack.Amount = bsc.ConvertBCAmountToBSCAmount(ackPack.Amount.Int64()) + ackPack := types.NewCrossStakeRedelegationAckPackage(pack, types.CrossStakeTypeRedelegate, errCode) ackBytes, err := rlp.EncodeToBytes(ackPack) if err != nil { return sdk.ExecuteResult{}, err @@ -266,8 +277,7 @@ func (app *CrossStakeApp) handleRedelegate(ctx sdk.Context, pack types.CrossStak delAddr := types.GetStakeCAoB(pack.DelAddr[:], types.DelegateCAoBSalt) shares, sdkErr := app.stakeKeeper.ValidateUnbondAmount(ctx, delAddr, pack.ValSrc, pack.Amount.Int64()) if sdkErr != nil { - ackPack := types.NewCrossStakeRedelegationAckPackage(&pack, types.CrossStakeTypeRedelegate, CrossStakeErrBadDelegation) - ackPack.Amount = bsc.ConvertBCAmountToBSCAmount(ackPack.Amount.Int64()) + ackPack := types.NewCrossStakeRedelegationAckPackage(pack, types.CrossStakeTypeRedelegate, CrossStakeErrBadDelegation) ackBytes, err := rlp.EncodeToBytes(ackPack) if err != nil { return sdk.ExecuteResult{}, err @@ -310,7 +320,7 @@ func (app *CrossStakeApp) handleRedelegate(ctx sdk.Context, pack types.CrossStak }, nil } -func (app *CrossStakeApp) handleDistributeRewardRefund(ctx sdk.Context, pack types.CrossStakeDistributeRewardSynPackage) (sdk.ExecuteResult, error) { +func (app *CrossStakeApp) handleDistributeRewardRefund(ctx sdk.Context, pack *types.CrossStakeRefundPackage) (sdk.ExecuteResult, error) { symbol := app.stakeKeeper.BondDenom(ctx) refundAmount := bsc.ConvertBSCAmountToBCAmount(pack.Amount) coins := sdk.Coins{sdk.NewCoin(symbol, refundAmount)} @@ -333,7 +343,7 @@ func (app *CrossStakeApp) handleDistributeRewardRefund(ctx sdk.Context, pack typ }, nil } -func (app *CrossStakeApp) handleDistributeUndelegatedRefund(ctx sdk.Context, pack types.CrossStakeDistributeUndelegatedSynPackage) (sdk.ExecuteResult, error) { +func (app *CrossStakeApp) handleDistributeUndelegatedRefund(ctx sdk.Context, pack *types.CrossStakeRefundPackage) (sdk.ExecuteResult, error) { symbol := app.stakeKeeper.BondDenom(ctx) refundAmount := bsc.ConvertBSCAmountToBCAmount(pack.Amount) coins := sdk.Coins{sdk.NewCoin(symbol, refundAmount)} diff --git a/x/stake/cross_stake/serialize.go b/x/stake/cross_stake/serialize.go index 14c8b0448..9e058dfbb 100644 --- a/x/stake/cross_stake/serialize.go +++ b/x/stake/cross_stake/serialize.go @@ -13,96 +13,86 @@ const ( CrossStakeErrBadDelegation uint8 = 3 ) -type CrossStakePackageFromBSC struct { - EventCode types.CrossStakePackageType +type CrossStakeSynPackageFromBSC struct { + PackageType types.CrossStakeEventType ParamsBytes []byte } func DeserializeCrossStakeSynPackage(serializedPackage []byte) (interface{}, error) { - var pack1 CrossStakePackageFromBSC + var pack1 CrossStakeSynPackageFromBSC err := rlp.DecodeBytes(serializedPackage, &pack1) if err != nil { return nil, err } - switch pack1.EventCode { + switch pack1.PackageType { case types.CrossStakeTypeDelegate: var pack2 types.CrossStakeDelegateSynPackage err := rlp.DecodeBytes(pack1.ParamsBytes, &pack2) if err != nil { return nil, err } - return pack2, nil + return &pack2, nil case types.CrossStakeTypeUndelegate: var pack2 types.CrossStakeUndelegateSynPackage err := rlp.DecodeBytes(pack1.ParamsBytes, &pack2) if err != nil { return nil, err } - return pack2, nil + return &pack2, nil case types.CrossStakeTypeRedelegate: var pack2 types.CrossStakeRedelegateSynPackage err := rlp.DecodeBytes(pack1.ParamsBytes, &pack2) if err != nil { return nil, err } - return pack2, nil + return &pack2, nil default: - return nil, fmt.Errorf("unrecognized package type") + return nil, fmt.Errorf("unrecognized cross stake event type: %d", pack1.PackageType) } } -func DeserializeCrossStakeAckPackage(serializedPackage []byte) (interface{}, error) { - var pack1 CrossStakePackageFromBSC - err := rlp.DecodeBytes(serializedPackage, &pack1) +func DeserializeCrossStakeRefundPackage(serializedPackage []byte) (*types.CrossStakeRefundPackage, error) { + var pack types.CrossStakeRefundPackage + err := rlp.DecodeBytes(serializedPackage, &pack) if err != nil { return nil, err } - switch pack1.EventCode { - case types.CrossStakeTypeDistributeReward: - var pack2 types.CrossStakeDistributeRewardSynPackage - err := rlp.DecodeBytes(pack1.ParamsBytes, &pack2) - if err != nil { - return nil, err - } - return pack2, nil - case types.CrossStakeTypeDistributeUndelegated: - var pack2 types.CrossStakeDistributeUndelegatedSynPackage - err := rlp.DecodeBytes(pack1.ParamsBytes, &pack2) - if err != nil { - return nil, err - } - return pack2, nil - default: - return nil, fmt.Errorf("unrecognized package type") - } + return &pack, nil } func DeserializeCrossStakeFailAckPackage(serializedPackage []byte) (interface{}, error) { - deserializeIntoUndelegatedPackage := func(serializedPackage []byte) (interface{}, error) { - var pack types.CrossStakeDistributeUndelegatedSynPackage - err := rlp.DecodeBytes(serializedPackage, &pack) - if err != nil { - return nil, err - } - return pack, nil - } - - deserializeIntoRewardPackage := func(serializedPackage []byte) (interface{}, error) { - var pack types.CrossStakeDistributeRewardSynPackage - err := rlp.DecodeBytes(serializedPackage, &pack) - if err != nil { - return nil, err - } - return pack, nil + deserializeFuncSet := []func(serializedPackage []byte) (interface{}, error){ + func(serializedPackage []byte) (interface{}, error) { + var pack types.CrossStakeDistributeRewardSynPackage + err := rlp.DecodeBytes(serializedPackage, &pack) + if err != nil { + return nil, err + } + if pack.EventType != types.CrossStakeTypeDistributeReward { + return nil, fmt.Errorf("wrong cross stake event type") + } + return &pack, nil + }, + func(serializedPackage []byte) (interface{}, error) { + var pack types.CrossStakeDistributeUndelegatedSynPackage + err := rlp.DecodeBytes(serializedPackage, &pack) + if err != nil { + return nil, err + } + if pack.EventType != types.CrossStakeTypeDistributeUndelegated { + return nil, fmt.Errorf("wrong cross stake event type") + } + return &pack, nil + }, } var pack interface{} - pack, err := deserializeIntoUndelegatedPackage(serializedPackage) - if err != nil { - pack, err = deserializeIntoRewardPackage(serializedPackage) - if err != nil { - return nil, err + var err error + for _, deserializeFunc := range deserializeFuncSet { + pack, err = deserializeFunc(serializedPackage) + if err == nil { + return pack, nil } } - return pack, nil + return nil, err } diff --git a/x/stake/cross_stake/serialize_test.go b/x/stake/cross_stake/serialize_test.go index dc7f8d946..b07c891ce 100644 --- a/x/stake/cross_stake/serialize_test.go +++ b/x/stake/cross_stake/serialize_test.go @@ -7,8 +7,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/stake/types" ) -func TestDeserializeDelegatePackage(t *testing.T) { - input := "f701b5f401945b38da6a701c568545dcfcb03fcb875f56beddc4940000000000000000000000000000000000001000880de0b6b3a7640000" +func TestDeserializeSynPackage(t *testing.T) { + input := "f84a03b847f845947de3642c66220e1136a42bf9897a6f8527ef9a0394beb218281ac3ebc4de700e7fcf23eea39010b8a3947de3642c66220e1136a42bf9897a6f8527ef9a038504c4b40000" packageBytes, err := hex.DecodeString(input) if err != nil { @@ -20,8 +20,7 @@ func TestDeserializeDelegatePackage(t *testing.T) { t.Fatal(err) } switch pack.(type) { - case types.CrossStakeDelegateSynPackage: - break + case *types.CrossStakeRedelegateSynPackage: default: t.Error("wrong event type") } diff --git a/x/stake/keeper/delegation.go b/x/stake/keeper/delegation.go index 19f71dd46..ff5c03d61 100644 --- a/x/stake/keeper/delegation.go +++ b/x/stake/keeper/delegation.go @@ -681,25 +681,31 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd var events sdk.Events if ubd.CrossStake { - _, err := k.BankKeeper.SendCoins(ctx, DelegationAccAddr, sdk.PegAccount, sdk.Coins{ubd.Balance}) + _, err := k.BankKeeper.SendCoins(ctx, DelegationAccAddr, ubd.DelegatorAddr, sdk.Coins{ubd.Balance}) + if err != nil { + return ubd, sdk.Events{}, err + } + bondDenom := k.BondDenom(ctx) + balance := k.BankKeeper.GetCoins(ctx, ubd.DelegatorAddr).AmountOf(bondDenom) + _, err = k.BankKeeper.SendCoins(ctx, ubd.DelegatorAddr, sdk.PegAccount, sdk.Coins{sdk.NewCoin(bondDenom, balance)}) if err != nil { return ubd, sdk.Events{}, err } - events, err = k.crossDistributeUndelegated(ctx, delAddr, valAddr, ubd.Balance) + events, err = k.crossDistributeUndelegated(ctx, delAddr, valAddr, balance) if err != nil { return ubd, sdk.Events{}, err } if k.AddrPool != nil { - k.AddrPool.AddAddrs([]sdk.AccAddress{sdk.PegAccount, DelegationAccAddr}) + k.AddrPool.AddAddrs([]sdk.AccAddress{sdk.PegAccount, ubd.DelegatorAddr}) } } else { _, err := k.BankKeeper.SendCoins(ctx, DelegationAccAddr, ubd.DelegatorAddr, sdk.Coins{ubd.Balance}) if err != nil { return ubd, sdk.Events{}, err } - if k.AddrPool != nil { - k.AddrPool.AddAddrs([]sdk.AccAddress{ubd.DelegatorAddr, DelegationAccAddr}) - } + } + if k.AddrPool != nil { + k.AddrPool.AddAddrs([]sdk.AccAddress{ubd.DelegatorAddr, DelegationAccAddr}) } k.RemoveUnbondingDelegation(ctx, ubd) return ubd, events, nil @@ -820,14 +826,14 @@ func (k Keeper) ValidateUnbondAmount( return shares, nil } -func (k Keeper) crossDistributeUndelegated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) (sdk.Events, sdk.Error) { +func (k Keeper) crossDistributeUndelegated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount int64) (sdk.Events, sdk.Error) { relayFeeCalc := fees.GetCalculator(types.CrossDistributeUndelegatedRelayFee) if relayFeeCalc == nil { return sdk.Events{}, sdk.ErrInternal("no fee calculator of distributeUndelegated") } relayFee := relayFeeCalc(nil) bscRelayFee := bsc.ConvertBCAmountToBSCAmount(relayFee.Tokens.AmountOf(k.BondDenom(ctx))) - bscTransferAmount := new(big.Int).Sub(bsc.ConvertBCAmountToBSCAmount(amount.Amount), bscRelayFee) + bscTransferAmount := new(big.Int).Sub(bsc.ConvertBCAmountToBSCAmount(amount), bscRelayFee) delBscAddrAcc := types.GetStakeCAoB(delAddr.Bytes(), types.DelegateCAoBSalt) delBscAddr := hex.EncodeToString(delBscAddrAcc.Bytes()) @@ -837,7 +843,7 @@ func (k Keeper) crossDistributeUndelegated(ctx sdk.Context, delAddr sdk.AccAddre } transferPackage := types.CrossStakeDistributeUndelegatedSynPackage{ - EventCode: types.CrossStakeTypeDistributeUndelegated, + EventType: types.CrossStakeTypeDistributeUndelegated, Amount: bscTransferAmount, Recipient: recipient, Validator: valAddr, @@ -864,7 +870,7 @@ func (k Keeper) crossDistributeUndelegated(ctx sdk.Context, delAddr sdk.AccAddre Type: types.CrossStakeDistributeUndelegatedType, From: DelegationAccAddr.String(), Denom: k.BondDenom(ctx), - To: []types.CrossReceiver{{sdk.PegAccount.String(), amount.Amount}}, + To: []types.CrossReceiver{{sdk.PegAccount.String(), amount}}, } k.PbsbServer.Publish(event) } else { @@ -877,7 +883,7 @@ func (k Keeper) crossDistributeUndelegated(ctx sdk.Context, delAddr sdk.AccAddre types.TagCrossStakeChannel, []byte{uint8(types.CrossStakeChannelID)}, types.TagCrossStakeSendSequence, []byte(strconv.FormatUint(sendSeq, 10)), ) - resultTags = append(resultTags, sdk.GetPegInTag(k.BondDenom(ctx), amount.Amount)) + resultTags = append(resultTags, sdk.GetPegInTag(k.BondDenom(ctx), amount)) events := sdk.Events{sdk.Event{ Type: types.EventTypeCrossStake, diff --git a/x/stake/keeper/distribute_sidechain.go b/x/stake/keeper/distribute_sidechain.go index c4aa9fa01..f54893efd 100644 --- a/x/stake/keeper/distribute_sidechain.go +++ b/x/stake/keeper/distribute_sidechain.go @@ -299,6 +299,7 @@ func (k Keeper) distributeSingleBatch(ctx sdk.Context, sideChainId string) sdk.E events = events.AppendEvents(event) changedAddrs = append(changedAddrs, sdk.PegAccount) } + reward.AccAddr = rewardCAoB } else { if _, _, err := k.BankKeeper.AddCoins(ctx, reward.AccAddr, sdk.Coins{sdk.NewCoin(bondDenom, reward.Amount)}); err != nil { panic(err) @@ -395,7 +396,7 @@ func crossDistributeReward(k Keeper, ctx sdk.Context, rewardCAoB sdk.AccAddress, } transferPackage := types.CrossStakeDistributeRewardSynPackage{ - EventCode: types.CrossStakeTypeDistributeReward, + EventType: types.CrossStakeTypeDistributeReward, Amount: bscTransferAmount, Recipient: recipient, } diff --git a/x/stake/types/cross_stake.go b/x/stake/types/cross_stake.go index 94fd6be27..afd15e345 100644 --- a/x/stake/types/cross_stake.go +++ b/x/stake/types/cross_stake.go @@ -3,11 +3,12 @@ package types import ( "math/big" + "github.com/cosmos/cosmos-sdk/bsc" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/tendermint/crypto/tmhash" ) -type CrossStakePackageType uint8 +type CrossStakeEventType uint8 const ( CrossStakeChannel = "crossStake" @@ -21,11 +22,11 @@ const ( CrossDistributeRewardRelayFee = "crossDistributeRewardRelayFee" CrossDistributeUndelegatedRelayFee = "crossDistributeUndelegatedRelayFee" - CrossStakeTypeDelegate CrossStakePackageType = 1 - CrossStakeTypeUndelegate CrossStakePackageType = 2 - CrossStakeTypeRedelegate CrossStakePackageType = 3 - CrossStakeTypeDistributeReward CrossStakePackageType = 4 - CrossStakeTypeDistributeUndelegated CrossStakePackageType = 5 + CrossStakeTypeDelegate CrossStakeEventType = 1 + CrossStakeTypeUndelegate CrossStakeEventType = 2 + CrossStakeTypeRedelegate CrossStakeEventType = 3 + CrossStakeTypeDistributeReward CrossStakeEventType = 4 + CrossStakeTypeDistributeUndelegated CrossStakeEventType = 5 CrossStakeDelegateType string = "CSD" CrossStakeDistributeRewardType string = "CSDR" @@ -45,16 +46,16 @@ type CrossStakeDelegateSynPackage struct { Amount *big.Int } -type CrossStakeDelegationAckPackage struct { - PackageType CrossStakePackageType - DelAddr sdk.SmartChainAddress - Validator sdk.ValAddress - Amount *big.Int - ErrorCode uint8 +type CrossStakeDelegateAckPackage struct { + EventType CrossStakeEventType + DelAddr sdk.SmartChainAddress + Validator sdk.ValAddress + Amount *big.Int + ErrorCode uint8 } -func NewCrossStakeDelegationAckPackage(synPack *CrossStakeDelegateSynPackage, packageType CrossStakePackageType, errCode uint8) *CrossStakeDelegationAckPackage { - return &CrossStakeDelegationAckPackage{packageType, synPack.DelAddr, synPack.Validator, synPack.Amount, errCode} +func NewCrossStakeDelegationAckPackage(synPack *CrossStakeDelegateSynPackage, eventType CrossStakeEventType, errCode uint8) *CrossStakeDelegateAckPackage { + return &CrossStakeDelegateAckPackage{eventType, synPack.DelAddr, synPack.Validator, bsc.ConvertBCAmountToBSCAmount(synPack.Amount.Int64()), errCode} } type CrossStakeUndelegateSynPackage struct { @@ -64,15 +65,15 @@ type CrossStakeUndelegateSynPackage struct { } type CrossStakeUndelegateAckPackage struct { - PackageType CrossStakePackageType - DelAddr sdk.SmartChainAddress - Validator sdk.ValAddress - Amount *big.Int - ErrorCode uint8 + EventType CrossStakeEventType + DelAddr sdk.SmartChainAddress + Validator sdk.ValAddress + Amount *big.Int + ErrorCode uint8 } -func NewCrossStakeUndelegateAckPackage(synPack *CrossStakeUndelegateSynPackage, packageType CrossStakePackageType, errCode uint8) *CrossStakeUndelegateAckPackage { - return &CrossStakeUndelegateAckPackage{packageType, synPack.DelAddr, synPack.Validator, synPack.Amount, errCode} +func NewCrossStakeUndelegateAckPackage(synPack *CrossStakeUndelegateSynPackage, eventType CrossStakeEventType, errCode uint8) *CrossStakeUndelegateAckPackage { + return &CrossStakeUndelegateAckPackage{eventType, synPack.DelAddr, synPack.Validator, bsc.ConvertBCAmountToBSCAmount(synPack.Amount.Int64()), errCode} } type CrossStakeRedelegateSynPackage struct { @@ -83,31 +84,45 @@ type CrossStakeRedelegateSynPackage struct { } type CrossStakeRedelegateAckPackage struct { - PackageType CrossStakePackageType - DelAddr sdk.SmartChainAddress - ValSrc sdk.ValAddress - ValDst sdk.ValAddress - Amount *big.Int - ErrorCode uint8 + EventType CrossStakeEventType + DelAddr sdk.SmartChainAddress + ValSrc sdk.ValAddress + ValDst sdk.ValAddress + Amount *big.Int + ErrorCode uint8 } -func NewCrossStakeRedelegationAckPackage(synPack *CrossStakeRedelegateSynPackage, packageType CrossStakePackageType, errCode uint8) *CrossStakeRedelegateAckPackage { - return &CrossStakeRedelegateAckPackage{packageType, synPack.DelAddr, synPack.ValSrc, synPack.ValDst, synPack.Amount, errCode} +func NewCrossStakeRedelegationAckPackage(synPack *CrossStakeRedelegateSynPackage, eventType CrossStakeEventType, errCode uint8) *CrossStakeRedelegateAckPackage { + return &CrossStakeRedelegateAckPackage{eventType, synPack.DelAddr, synPack.ValSrc, synPack.ValDst, bsc.ConvertBCAmountToBSCAmount(synPack.Amount.Int64()), errCode} } type CrossStakeDistributeRewardSynPackage struct { - EventCode CrossStakePackageType + EventType CrossStakeEventType Amount *big.Int Recipient sdk.SmartChainAddress } type CrossStakeDistributeUndelegatedSynPackage struct { - EventCode CrossStakePackageType + EventType CrossStakeEventType Amount *big.Int Recipient sdk.SmartChainAddress Validator sdk.ValAddress } +type RefundError uint32 + +const ( + DecodeFailed RefundError = 100 + WithdrawBNBFailed RefundError = 101 +) + +type CrossStakeRefundPackage struct { + EventType CrossStakeEventType + Amount *big.Int + Recipient sdk.SmartChainAddress + ErrorCode RefundError +} + func GetStakeCAoB(sourceAddr []byte, salt string) sdk.AccAddress { saltBytes := []byte("Staking" + salt + "Address Anchor") return sdk.XOR(tmhash.SumTruncated(saltBytes), sourceAddr) diff --git a/x/stake/types/cross_stake_test.go b/x/stake/types/cross_stake_test.go index 6b3951d4d..af76257b7 100644 --- a/x/stake/types/cross_stake_test.go +++ b/x/stake/types/cross_stake_test.go @@ -22,7 +22,7 @@ func TestGetStakeCAoB(t *testing.T) { } } -func TestRLP(t *testing.T) { +func TestAckRLP(t *testing.T) { delAddr, _ := sdk.NewSmartChainAddress("91D7deA99716Cbb247E81F1cfB692009164a967E") bcAddr := "bnb1dmrarep5fawa89shw0048syv3ek4tcm28tmqp6" @@ -39,11 +39,11 @@ func TestRLP(t *testing.T) { ackBytes, _ := rlp.EncodeToBytes(ackPack) type AckPackage struct { - PackageType CrossStakePackageType - DelAddr sdk.SmartChainAddress - Validator sdk.ValAddress - Amount *big.Int - ErrorCode uint8 + EventType CrossStakeEventType + DelAddr sdk.SmartChainAddress + Validator sdk.ValAddress + Amount *big.Int + ErrorCode uint8 } var pack AckPackage err := rlp.DecodeBytes(ackBytes, &pack)