# Rebalancing a Node

### Rebalancing Process

1. **Pre-Rebalance**
   * Ensure cooldown period has passed
   * Pay any pending management fees (must be outside window)
   * Verify component ratios sum to 100% with reserve
2. **Start Rebalance**

   ```solidity
   node.startRebalance()  // Updates totalAssets cache
   ```
3. **During Window**
   * Process pending redemptions from reserve or components
   * Invest excess reserve into components when:
     * Reserve is above target ratio
     * Component deviation exceeds maxDelta
     * Following target weights
     * Following liquidation queue order
4. **Window Closure**
   * Automatically ends after `rebalanceWindow`
   * Next window cannot start until `rebalanceCooldown` passes

## Example Flow

```solidity
// 1. Outside rebalance window
node.payManagementFees()  // Pay any pending fees
node.updateTotalAssets()  // Optional: update cache if needed

// 2. Start rebalance window
rebalancer.startRebalance()  // Also updates totalAssets cache

// 3. Process redemptions
node.fulfillRedeemFromReserve(controller)  // From reserve
// or
node.fulfillRedeemBatch(controllers)       // Batch process
// or
router.fulfillRedeemRequest(node, controller, component)  // From component

// 4. Invest excess reserve if:
//    - Reserve above target
//    - Component delta > maxDelta
//    - Following liquidation queue
router.invest(node, component, minSharesOut)      // For ERC4626
// or
router.investInAsyncComponent(node, component)    // For ERC7540

// 5. Window automatically closes after rebalanceWindow duration
```

This timing mechanism allows for controlled, periodic portfolio adjustments while maintaining flexibility for the rebalancer to choose optimal execution times.

## Rebalance Timing

1. The cooldown period sets a minimum time that must pass between rebalance windows
2. Once the cooldown period has passed, the rebalance window doesn't automatically start
3. The rebalancer must explicitly call startRebalance() to begin a new window
4. The rebalancer has discretion over when to start the window, as long as the cooldown has passed

## Rebalance Start Conditions

`startRebalance()` will revert if:

* Component target weights plus reserve ratio don't sum to 100%
* Cooldown period hasn't passed since last rebalance window
* Caller is not a whitelisted rebalancer

### Additional Notes

* Rebalancer decides optimal timing
* Management fees must be paid outside window
* totalAssets can be updated anytime
* Execution fees apply to investments, not withdrawals
* Node owner can optimize fees by:
  * Setting longer cooldown periods
  * Increasing maxDelta tolerances

This timing mechanism allows for controlled, periodic portfolio adjustments while maintaining flexibility for the rebalancer to choose optimal execution times.
