Game doesn't accrued rewards for previous rebalance period in case if rebalanceBasket is called in next period
Game doesn't accrued rewards for previous rebalance period in case if `rebalanceBasket` is called in next period. Because of that user do not receive rewards for the previous period and in case if he calls `rebalanceBasket` each rebalance period, he will receive rewards only for last one.\\n```\\n  function addToTotalRewards(uint256 _basketId) internal onlyBasketOwner(_basketId) {\\n    if (baskets[_basketId].nrOfAllocatedTokens == 0) return;\\n\\n\\n    uint256 vaultNum = baskets[_basketId].vaultNumber;\\n    uint256 currentRebalancingPeriod = vaults[vaultNum].rebalancingPeriod;\\n    uint256 lastRebalancingPeriod = baskets[_basketId].lastRebalancingPeriod;\\n\\n\\n    if (currentRebalancingPeriod <= lastRebalancingPeriod) return;\\n\\n\\n    for (uint k = 0; k < chainIds.length; k++) {\\n      uint32 chain = chainIds[k];\\n      uint256 latestProtocol = latestProtocolId[chain];\\n      for (uint i = 0; i < latestProtocol; i++) {\\n        int256 allocation = basketAllocationInProtocol(_basketId, chain, i) / 1E18;\\n        if (allocation == 0) continue;\\n\\n\\n        int256 lastRebalanceReward = getRewardsPerLockedToken(\\n          vaultNum,\\n          chain,\\n          lastRebalancingPeriod,\\n          i\\n        );\\n        int256 currentReward = getRewardsPerLockedToken(\\n          vaultNum,\\n          chain,\\n          currentRebalancingPeriod,\\n          i\\n        );\\n        baskets[_basketId].totalUnRedeemedRewards +=\\n          (currentReward - lastRebalanceReward) *\\n          allocation;\\n      }\\n    }\\n  }\\n```\\n\\nThis function allows user to accrue rewards only when currentRebalancingPeriod > `lastRebalancingPeriod`. When user allocates, he allocates for the next period. And `lastRebalancingPeriod` is changed after `addToTotalRewards` is called, so after rewards for previous period accrued. And when allocations are sent to the xController, then new rebalance period is started. So actually rewards accruing for period that user allocated for is started once `pushAllocationsToController` is called. And at this point currentRebalancingPeriod == `lastRebalancingPeriod` which means that if user will call rebalanceBasket for next period, the rewards will not be accrued for him, but `lastRebalancingPeriod` will be incremented. So actually he will not receive rewards for previous period.\\nExample. 1.currentRebalancingPeriod is 10. 2.user calls `rebalanceBasket` with new allocation and `lastRebalancingPeriod` is set to 11 for him. 3.pushAllocationsToController is called, so `currentRebalancingPeriod` becomes 11. 4.settleRewards is called, so rewards for the 11th cycle are accrued. 5.now user can call `rebalanceBasket` for the next 12th cycle. `addToTotalRewards` is called, but `currentRebalancingPeriod == `lastRebalancingPeriod` == 11`, so rewards were not accrued for 11th cycle 6.new allocations is saved and `lastRebalancingPeriod` becomes 12. 7.the loop continues and every time when user allocates for next rewards his `lastRebalancingPeriod` is increased, but rewards are not added. 8.user will receive his rewards for previous cycle, only if he skip 1 rebalance period(he doesn't allocate on that period).\\nAs you can see this is very serious bug. Because of that, player that wants to adjust his allocation every rebalance period will loose all his rewards.
First of all, you need to allows to call `rebalanceBasket` only once per rebalance period, before new rebalancing period started and allocations are sent to xController. Then you need to change check inside `addToTotalRewards` to this `if (currentRebalancingPeriod < lastRebalancingPeriod) return;` in order to allow accruing for same period.
Player looses all his rewards
```\\n  function addToTotalRewards(uint256 _basketId) internal onlyBasketOwner(_basketId) {\\n    if (baskets[_basketId].nrOfAllocatedTokens == 0) return;\\n\\n\\n    uint256 vaultNum = baskets[_basketId].vaultNumber;\\n    uint256 currentRebalancingPeriod = vaults[vaultNum].rebalancingPeriod;\\n    uint256 lastRebalancingPeriod = baskets[_basketId].lastRebalancingPeriod;\\n\\n\\n    if (currentRebalancingPeriod <= lastRebalancingPeriod) return;\\n\\n\\n    for (uint k = 0; k < chainIds.length; k++) {\\n      uint32 chain = chainIds[k];\\n      uint256 latestProtocol = latestProtocolId[chain];\\n      for (uint i = 0; i < latestProtocol; i++) {\\n        int256 allocation = basketAllocationInProtocol(_basketId, chain, i) / 1E18;\\n        if (allocation == 0) continue;\\n\\n\\n        int256 lastRebalanceReward = getRewardsPerLockedToken(\\n          vaultNum,\\n          chain,\\n          lastRebalancingPeriod,\\n          i\\n        );\\n        int256 currentReward = getRewardsPerLockedToken(\\n          vaultNum,\\n          chain,\\n          currentRebalancingPeriod,\\n          i\\n        );\\n        baskets[_basketId].totalUnRedeemedRewards +=\\n          (currentReward - lastRebalanceReward) *\\n          allocation;\\n      }\\n    }\\n  }\\n```\\n