Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add IsAutoUnDelegate field to CrossStakeDistributeUndelegatedSynPackage and prevent too many failed in auto refund #377

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions x/stake/endblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,17 @@ func handleMatureUnbondingDelegations(k keeper.Keeper, ctx sdk.Context) ([]types
}

const (
maxProcessedRefundCount = 10
maxProcessedRefundCount = 10
maxProcessedRefundFailed = 200
)

func handleRefundStake(ctx sdk.Context, sideChainPrefix []byte, k keeper.Keeper) sdk.Events {
sideChainCtx := ctx.WithSideChainKeyPrefix(sideChainPrefix)
iterator := k.IteratorAllDelegations(sideChainCtx)
defer iterator.Close()
var refundEvents sdk.Events
count := 0
succeedCount := 0
failedCount := 0
boundDenom := k.BondDenom(sideChainCtx)
bscSideChainId := k.ScKeeper.BscSideChainId(ctx)

Expand All @@ -304,15 +306,34 @@ func handleRefundStake(ctx sdk.Context, sideChainPrefix []byte, k keeper.Keeper)
SideChainId: bscSideChainId,
}, k)
refundEvents = refundEvents.AppendEvents(result.Events)
if !result.IsOK() {
ctx.Logger().Debug("handleRefundStake failed",
"delegator", delegation.DelegatorAddr.String(),
"validator", delegation.ValidatorAddr.String(),
"amount", delegation.GetShares().String(),
"sideChainId", bscSideChainId,
"result", fmt.Sprintf("%+v", result),
)
// this is to prevent too many delegation is in unbounded state
// if too many delegation is in unbounded state, it will cause too many iteration in the block
failedCount++
if failedCount >= maxProcessedRefundFailed {
break
}
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
}

if result.IsOK() && delegation.CrossStake {
k.SetAutoUnDelegate(sideChainCtx, delegation.DelegatorAddr, delegation.ValidatorAddr)
}

ctx.Logger().Info("handleRefundStake after SecondSunsetFork",
"delegator", delegation.DelegatorAddr.String(),
"validator", delegation.ValidatorAddr.String(),
"amount", delegation.GetShares().String(),
"sideChainId", bscSideChainId,
)
count++
if count >= maxProcessedRefundCount {
succeedCount++
if succeedCount >= maxProcessedRefundCount {
break
}
}
Expand Down
3 changes: 3 additions & 0 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func NewHandler(k keeper.Keeper, govKeeper gov.Keeper) sdk.Handler {
}
return handleMsgSideChainRedelegate(ctx, msg, k)
case types.MsgSideChainUndelegate:
if sdk.IsUpgrade(sdk.SecondSunsetFork) {
return sdk.ErrMsgNotSupported("").Result()
}
return handleMsgSideChainUndelegate(ctx, msg, k)
case types.MsgSideChainStakeMigration:
if !sdk.IsUpgrade(sdk.FirstSunsetFork) || sdk.IsUpgrade(sdk.SecondSunsetFork) {
Expand Down
23 changes: 19 additions & 4 deletions x/stake/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,20 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd
return ubd, events, nil
}

func (k Keeper) IsAutoUnDelegate(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) bool {
store := ctx.KVStore(k.storeKey)
key := GetAutoUnDelegateIndexKey(delAddr, valAddr)

return store.Has(key)
}

func (k Keeper) SetAutoUnDelegate(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)

key := GetAutoUnDelegateIndexKey(delAddr, valAddr)
store.Set(key, []byte{}) // index, store empty bytes
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
}

// complete unbonding an unbonding record
func (k Keeper) BeginRedelegation(ctx sdk.Context, delAddr sdk.AccAddress,
valSrcAddr, valDstAddr sdk.ValAddress, sharesAmount sdk.Dec) (types.Redelegation, sdk.Error) {
Expand Down Expand Up @@ -876,10 +890,11 @@ func (k Keeper) crossDistributeUndelegated(ctx sdk.Context, delAddr sdk.AccAddre
}

transferPackage := types.CrossStakeDistributeUndelegatedSynPackage{
EventType: types.CrossStakeTypeDistributeUndelegated,
Amount: bscTransferAmount,
Recipient: recipient,
Validator: valAddr,
EventType: types.CrossStakeTypeDistributeUndelegated,
Amount: bscTransferAmount,
Recipient: recipient,
Validator: valAddr,
IsAutoUnDelegate: k.IsAutoUnDelegate(ctx, delAddr, valAddr),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is a hardfork logic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
encodedPackage, err := rlp.EncodeToBytes(transferPackage)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions x/stake/keeper/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var (
// Keys for reward store prefix
RewardBatchKey = []byte{0x01} // key for batch of rewards
RewardValDistAddrKey = []byte{0x02} // key for rewards' validator <-> distribution address mapping

AutoUndelegateIndexKey = []byte{0x61} // prefix for each key for an auto undelegate, by validator operator
)

const (
Expand Down Expand Up @@ -340,3 +342,14 @@ func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddr
func GetValLatestUpdateConsAddrTimeKey(valAddr sdk.ValAddress) []byte {
return append(ValLatestUpdateConsAddrTimeKey, valAddr.Bytes()...)
}

// gets the prefix keyspace for the indexes of auto unDelegate delegations for a validator
func GetAutoUnDelegateByValIndexKey(valAddr sdk.ValAddress) []byte {
return append(AutoUndelegateIndexKey, valAddr.Bytes()...)
}

// gets the index-key for an auto unDelegate delegation, stored by validator-index
// VALUE: none (key rearrangement used)
func GetAutoUnDelegateIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
return append(GetAutoUnDelegateByValIndexKey(valAddr), delAddr.Bytes()...)
}
9 changes: 5 additions & 4 deletions x/stake/types/cross_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ type CrossStakeDistributeRewardSynPackage struct {
}

type CrossStakeDistributeUndelegatedSynPackage struct {
EventType CrossStakeEventType
Recipient sdk.SmartChainAddress
Validator sdk.ValAddress
Amount *big.Int
EventType CrossStakeEventType
Recipient sdk.SmartChainAddress
Validator sdk.ValAddress
Amount *big.Int
IsAutoUnDelegate bool
}

type RefundError uint32
Expand Down
Loading