|
5 | 5 | import pytest |
6 | 6 |
|
7 | 7 | from iwa.plugins.gnosis.cow.types import OrderType |
| 8 | +from iwa.plugins.gnosis.cow_utils import CowApiUnavailableError |
8 | 9 |
|
9 | 10 | # ---- CowSwap.swap() tests ---- |
10 | 11 |
|
@@ -249,3 +250,140 @@ async def test_get_max_buy_amount_wei(self): |
249 | 250 |
|
250 | 251 | # 2000000 - 1.5% = 1970000 |
251 | 252 | assert result == 1970000 |
| 253 | + |
| 254 | + |
| 255 | +# ---- CowApiUnavailableError resilience tests ---- |
| 256 | + |
| 257 | + |
| 258 | +class TestCowApiUnavailableResilience: |
| 259 | + """CoW API down must never crash triton. All entry points must degrade gracefully.""" |
| 260 | + |
| 261 | + @staticmethod |
| 262 | + def _make_cowswap(mock_get_module): |
| 263 | + """Build a CowSwap instance with fully mocked cowpy.""" |
| 264 | + from iwa.plugins.gnosis.cow.swap import CowSwap |
| 265 | + |
| 266 | + chain = MagicMock() |
| 267 | + chain.chain_id = 100 |
| 268 | + chain.name = "gnosis" |
| 269 | + chain.get_token_address.return_value = "0xTokenAddr" |
| 270 | + |
| 271 | + with patch.object(CowSwap, "get_chain", return_value=MagicMock()): |
| 272 | + return CowSwap(private_key_or_signer=MagicMock(), chain=chain) |
| 273 | + |
| 274 | + @pytest.mark.asyncio |
| 275 | + @patch("iwa.plugins.gnosis.cow.swap.get_cowpy_module") |
| 276 | + async def test_sell_swap_returns_none_when_cow_api_down(self, mock_get_module): |
| 277 | + """Regression: CowSwap.swap(SELL) must return None when CoW API is down, not crash. |
| 278 | +
|
| 279 | + swap_tokens raises CowApiUnavailableError → caught by swap()'s except block. |
| 280 | + """ |
| 281 | + mock_chain_cls = MagicMock() |
| 282 | + mock_chain_cls.__iter__ = lambda self: iter([MagicMock(value=(MagicMock(),))]) |
| 283 | + |
| 284 | + def get_module(name): |
| 285 | + return { |
| 286 | + "SupportedChainId": MagicMock(), |
| 287 | + "OrderBookApi": MagicMock(), |
| 288 | + "OrderBookAPIConfigFactory": MagicMock(), |
| 289 | + "Chain": mock_chain_cls, |
| 290 | + "swap_tokens": AsyncMock( |
| 291 | + side_effect=CowApiUnavailableError("api.cow.fi unreachable") |
| 292 | + ), |
| 293 | + }.get(name, MagicMock()) |
| 294 | + |
| 295 | + mock_get_module.side_effect = get_module |
| 296 | + cow = self._make_cowswap(mock_get_module) |
| 297 | + |
| 298 | + result = await cow.swap( |
| 299 | + amount_wei=10**18, |
| 300 | + sell_token_name="olas", |
| 301 | + buy_token_name="wxdai", |
| 302 | + order_type=OrderType.SELL, |
| 303 | + wait_for_execution=False, |
| 304 | + ) |
| 305 | + |
| 306 | + assert result is None, "swap() must return None when CoW API is down, not crash" |
| 307 | + |
| 308 | + @pytest.mark.asyncio |
| 309 | + @patch("iwa.plugins.gnosis.cow.swap.get_cowpy_module") |
| 310 | + async def test_buy_swap_returns_none_when_cow_api_down(self, mock_get_module): |
| 311 | + """BUY path: CowApiUnavailableError from swap_tokens_to_exact_tokens → swap() returns None.""" |
| 312 | + mock_get_module.return_value = MagicMock() |
| 313 | + |
| 314 | + cow = self._make_cowswap(mock_get_module) |
| 315 | + |
| 316 | + with patch( |
| 317 | + "iwa.plugins.gnosis.cow.swap.CowSwap.swap_tokens_to_exact_tokens", |
| 318 | + new=AsyncMock(side_effect=CowApiUnavailableError("api.cow.fi unreachable")), |
| 319 | + ): |
| 320 | + result = await cow.swap( |
| 321 | + amount_wei=10**18, |
| 322 | + sell_token_name="olas", |
| 323 | + buy_token_name="wxdai", |
| 324 | + order_type=OrderType.BUY, |
| 325 | + wait_for_execution=False, |
| 326 | + ) |
| 327 | + |
| 328 | + assert result is None, "BUY swap() must return None when CoW API is down" |
| 329 | + |
| 330 | + @pytest.mark.asyncio |
| 331 | + async def test_get_max_sell_amount_raises_cow_api_unavailable(self): |
| 332 | + """get_max_sell_amount_wei propagates CowApiUnavailableError when API is down. |
| 333 | +
|
| 334 | + Callers (e.g. get_swap_execution_cost_pct in trader.py) have their own |
| 335 | + try/except and must handle this explicitly. |
| 336 | + """ |
| 337 | + import iwa.plugins.gnosis.cow.quotes as quotes_mod |
| 338 | + |
| 339 | + def raise_if_app_data(name): |
| 340 | + if name == "DEFAULT_APP_DATA_HASH": |
| 341 | + raise CowApiUnavailableError("api.cow.fi down") |
| 342 | + return MagicMock() |
| 343 | + |
| 344 | + with patch("iwa.plugins.gnosis.cow.quotes.get_cowpy_module", side_effect=raise_if_app_data): |
| 345 | + quotes_mod.get_order_quote = None # force re-fetch via get_cowpy_module |
| 346 | + |
| 347 | + with pytest.raises(CowApiUnavailableError): |
| 348 | + await quotes_mod.get_max_sell_amount_wei( |
| 349 | + amount_wei=10**18, |
| 350 | + sell_token="0xSell", |
| 351 | + buy_token="0xBuy", |
| 352 | + chain_id_val=100, |
| 353 | + account_address="0xAccount", |
| 354 | + ) |
| 355 | + |
| 356 | + @pytest.mark.asyncio |
| 357 | + async def test_get_max_buy_amount_raises_cow_api_unavailable(self): |
| 358 | + """get_max_buy_amount_wei propagates CowApiUnavailableError when API is down.""" |
| 359 | + import iwa.plugins.gnosis.cow.quotes as quotes_mod |
| 360 | + |
| 361 | + def raise_if_app_data(name): |
| 362 | + if name == "DEFAULT_APP_DATA_HASH": |
| 363 | + raise CowApiUnavailableError("api.cow.fi down") |
| 364 | + return MagicMock() |
| 365 | + |
| 366 | + with patch("iwa.plugins.gnosis.cow.quotes.get_cowpy_module", side_effect=raise_if_app_data): |
| 367 | + quotes_mod.get_order_quote = None # force re-fetch via get_cowpy_module |
| 368 | + |
| 369 | + with pytest.raises(CowApiUnavailableError): |
| 370 | + await quotes_mod.get_max_buy_amount_wei( |
| 371 | + sell_amount_wei=10**18, |
| 372 | + sell_token="0xSell", |
| 373 | + buy_token="0xBuy", |
| 374 | + chain_id_val=100, |
| 375 | + account_address="0xAccount", |
| 376 | + ) |
| 377 | + |
| 378 | + def test_server_preload_does_not_crash_when_cow_api_down(self): |
| 379 | + """Regression: api.cow.fi down at startup must NOT crash the web server. |
| 380 | +
|
| 381 | + _preload_cow_modules() catches CowApiUnavailableError and logs a warning. |
| 382 | + This prevents the triton reboot loop caused by the CoW DNS hijacking incident. |
| 383 | + """ |
| 384 | + from iwa.web import server as server_mod |
| 385 | + |
| 386 | + with patch.object(server_mod, "get_cowpy_module") as mock_get: |
| 387 | + mock_get.side_effect = CowApiUnavailableError("api.cow.fi unreachable at startup") |
| 388 | + # Must not raise — must only log a warning |
| 389 | + server_mod._preload_cow_modules() # no exception = pass |
0 commit comments