Skip to content

Commit

Permalink
fix: handle race condition in delegation record update
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bowman committed Feb 10, 2024
1 parent 8c8fc77 commit c6249c4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions x/interchainstaking/keeper/ibc_packet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,18 @@ func (k *Keeper) HandleBeginRedelegate(ctx sdk.Context, msg sdk.Msg, completion
k.Logger(ctx).Error("unable to find delegation record", "chain", zone.ChainId, "source", redelegateMsg.ValidatorSrcAddress, "dst", redelegateMsg.ValidatorDstAddress, "epoch_number", epochNumber)
return fmt.Errorf("unable to find delegation record for chain %s, src: %s, dst: %s, at epoch %d", zone.ChainId, redelegateMsg.ValidatorSrcAddress, redelegateMsg.ValidatorDstAddress, epochNumber)
}
srcDelegation.Amount = srcDelegation.Amount.Sub(redelegateMsg.Amount)

srcDelegation.Amount, err = srcDelegation.Amount.SafeSub(redelegateMsg.Amount)
if err != nil {
if strings.Contains(err.Error(), "negative coin amount") {
// we received a negative srcDelegation. Obviously this cannot happen, but we can get a crossed re/un/delegation, all which fetch absolute values.
k.Logger(ctx).Error("possible race condition; unable to sub redelegation amount. requerying delegation anyway")
} else {
// we got some uother, unrecoverable err
return err
}
} else {
k.SetDelegation(ctx, zone.ChainId, srcDelegation)
}

valAddr, err = addressutils.ValAddressFromBech32(redelegateMsg.ValidatorDstAddress, zone.AccountPrefix+"valoper")
if err != nil {
Expand Down

0 comments on commit c6249c4

Please sign in to comment.