function _twapUpdate() internal {
    uint32 blockTimestamp = uint32(block.timestamp % 2 ** 32);
    uint32 timeElapsed = blockTimestamp - _BLOCK_TIMESTAMP_LAST_;
    if (timeElapsed > 0 && _BASE_RESERVE_ != 0 && _QUOTE_RESERVE_ != 0) {
        /// @dev It is desired and expected for this value to
        /// overflow once it has hit the max of `type.uint256`.
        unchecked {
            _BASE_PRICE_CUMULATIVE_LAST_ += getMidPrice() * timeElapsed;
        }
    }
    _BLOCK_TIMESTAMP_LAST_ = blockTimestamp;
}
function _resetTargetAndReserve() internal returns (uint256 baseBalance, uint256 quoteBalance) {
    baseBalance = _BASE_TOKEN_.balanceOf(address(this));
    quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
    if (baseBalance > type(uint112).max || quoteBalance > type(uint112).max) {
        revert ErrOverflow();
    }
    _BASE_RESERVE_ = uint112(baseBalance);
    _QUOTE_RESERVE_ = uint112(quoteBalance);
    _BASE_TARGET_ = uint112(baseBalance);
    _QUOTE_TARGET_ = uint112(quoteBalance);
    _RState_ = uint32(PMMPricing.RState.ONE);
    _twapUpdate();
}

function _setReserve(uint256 baseReserve, uint256 quoteReserve) internal {
    _BASE_RESERVE_ = baseReserve.toUint112();
    _QUOTE_RESERVE_ = quoteReserve.toUint112();
    _twapUpdate();
}
