Skip to content

Commit

Permalink
Fix third review comments and update cross chain packages' data structs
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Aug 2, 2022
1 parent 3f35003 commit 4beeeb7
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 158 deletions.
4 changes: 2 additions & 2 deletions x/paramHub/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
114 changes: 62 additions & 52 deletions x/stake/cross_stake/cross_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)}
Expand All @@ -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)}
Expand Down
92 changes: 41 additions & 51 deletions x/stake/cross_stake/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions x/stake/cross_stake/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
Loading

0 comments on commit 4beeeb7

Please sign in to comment.