# The EXPIRED judgment does not include the current block

Off-by-one in Babylon's GetStatus boundary lets a finality provider unbond in the same block its delegation expires, double-decrementing TotalBondedSat and halting the chain via panic.

- Payout: Audit contest
- Severity: high
- Program: Babylon
- Found by: LZ_security
- Disclosed: 2024-12-09
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-12-babylon-judging/issues/33
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-12-babylon%3AH-2
- Tags: area:web3 domain:web3 impact:dos impact:integrity kind:contest platform:sherlock sev:high tech:go technique:source-review vuln:accounting-error vuln:business-logic vuln:dos

**Target:** Cosmos SDK chain module (x/btcstaking GetStatus, x/finality power dist update)
**Root cause:** GetStatus uses strict `btcHeight+UnbondingTime > EndHeight` while the EXPIRED event fires at `EndHeight-UnbondingTime`, so at the exact boundary block the delegation still reports ACTIVE and BTCUndelegate succeeds alongside the EXPIRED event.
**Steps:**
1. Note EXPIRED power-dist event scheduled at `EndHeight - UnbondingTime` in AddBTCDelegation.
2. Observe GetStatus returns EXPIRED only when `btcHeight+UnbondingTime > EndHeight`, so at `btcHeight == EndHeight-UnbondingTime` it returns ACTIVE.
3. In that same block, submit `MsgBTCUndelegate` for the staking tx hash; GetStatus passes the UNBONDED/EXPIRED check.
4. BeginBlocker processes the EXPIRED event first; IsUnbondedEarly() is false because DelegatorUnbondingInfo not yet set, so processPowerDistUpdateEventUnbond runs.
5. BTCUndelegate then also processes unbond, causing subDelegationSat to run twice; TotalActiveSat goes negative -> ErrBTCDelegationRewardsTrackerNegativeAmount -> panic in MustProcessBtcDelegationUnbonded halts chain, or TotalBondedSat double-reduced.
**How found:** Code review of GetStatus boundary condition compared against the event scheduling height in AddBTCDelegation, spotting the strict-vs-inclusive inequality mismatch and tracing the resulting double-processing through ProcessAllPowerDistUpdateEvents.
**Exploitation:** Finality provider times a BTCUndelegate transaction to land in the exact block where its delegation expires, exploiting the ordering of BeginBlocker EXPIRED handling vs the message handler to trigger a duplicate unbond.
**Insight:** When a state machine schedules events at a computed height, verify the status predicate uses the same boundary (>= vs >) as the event scheduler; off-by-one at the exact boundary block lets a message handler and a BeginBlocker event both act on the same object.

## Proof of concept (copied exactly from the original report)

**Off-by-one in GetStatus EXPIRED check**

```
func (d *BTCDelegation) GetStatus(
	btcHeight uint32,
	covenantQuorum uint32,
) BTCDelegationStatus {
    ......

->   // btcHeight = currentBtcTipHeight 
->   // if btcHeight > d.EndHeight - d.UnbondingTime
->	if btcHeight+d.UnbondingTime > d.EndHeight {
		return BTCDelegationStatus_EXPIRED
	}
```

**BTCUndelegate rejects EXPIRED status**

```
btcDelStatus := btcDel.GetStatus(
		btcTip.Height,
		bsParams.CovenantQuorum,
	)

->	if btcDelStatus == types.BTCDelegationStatus_UNBONDED || btcDelStatus == types.BTCDelegationStatus_EXPIRED {
		return nil, types.ErrInvalidBTCUndelegateReq.Wrap("cannot unbond an unbonded BTC delegation")
	}
```

**EXPIRED event scheduled at EndHeight minus UnbondingTime**

```
expiredEvent := types.NewEventPowerDistUpdateWithBTCDel(&types.EventBTCDelegationStateUpdate{
        StakingTxHash: stakingTxHash.String(),
        NewState:      types.BTCDelegationStatus_EXPIRED,
    })

    // NOTE: we should have verified that EndHeight > btcTip.Height + unbonding_time
->  k.addPowerDistUpdateEvent(ctx, btcDel.EndHeight-btcDel.UnbondingTime, expiredEvent)
```

