iwa 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -94,6 +94,9 @@ class ChainInterface:
94
94
  """Get the current active RPC URL."""
95
95
  if not self.chain.rpcs:
96
96
  return ""
97
+ # Ensure index is valid (could be stale after rpcs list changes or singleton reuse)
98
+ if self._current_rpc_index >= len(self.chain.rpcs):
99
+ self._current_rpc_index = 0
97
100
  return self.chain.rpcs[self._current_rpc_index]
98
101
 
99
102
  @property
@@ -1,5 +1,6 @@
1
1
  """Transaction service module."""
2
2
 
3
+ import threading
3
4
  from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
4
5
 
5
6
  from loguru import logger
@@ -220,12 +221,29 @@ class TransferLogger:
220
221
  class TransactionService:
221
222
  """Manages transaction lifecycle: signing, sending, retrying."""
222
223
 
224
+ # Class-level lock for managing per-address locks
225
+ _locks_lock = threading.Lock()
226
+ _address_locks: Dict[str, threading.Lock] = {}
227
+
223
228
  def __init__(self, key_storage: KeyStorage, account_service: AccountService, safe_service=None):
224
229
  """Initialize TransactionService."""
225
230
  self.key_storage = key_storage
226
231
  self.account_service = account_service
227
232
  self.safe_service = safe_service
228
233
 
234
+ @classmethod
235
+ def _get_address_lock(cls, address: str) -> threading.Lock:
236
+ """Get or create a lock for a specific address.
237
+
238
+ This prevents nonce collisions when sending multiple transactions
239
+ from the same address concurrently.
240
+ """
241
+ address_lower = address.lower()
242
+ with cls._locks_lock:
243
+ if address_lower not in cls._address_locks:
244
+ cls._address_locks[address_lower] = threading.Lock()
245
+ return cls._address_locks[address_lower]
246
+
229
247
  def _resolve_label(self, address: str, chain_name: str = "gnosis") -> str:
230
248
  """Resolve address to human-readable label."""
231
249
  if not address:
@@ -255,69 +273,84 @@ class TransactionService:
255
273
 
256
274
  Uses ChainInterface.with_retry() for consistent RPC rotation and retry logic.
257
275
  Gas errors are handled by increasing gas and retrying within the same mechanism.
276
+
277
+ Note: Uses per-address locking to prevent nonce collisions when multiple
278
+ transactions are sent from the same address concurrently.
258
279
  """
259
280
  chain_interface = ChainInterfaces().get(chain_name)
260
281
  tx = dict(transaction)
261
282
 
262
- if not self._prepare_transaction(tx, signer_address_or_tag, chain_interface):
283
+ # Resolve signer first to get the address for locking
284
+ signer_account = self.account_service.resolve_account(signer_address_or_tag)
285
+ if not signer_account:
286
+ logger.error(f"Signer {signer_address_or_tag} not found")
263
287
  return False, {}
264
288
 
265
- # CHECK FOR SAFE TRANSACTION
266
- signer_account = self.account_service.resolve_account(signer_address_or_tag)
289
+ # CHECK FOR SAFE TRANSACTION (Safe has its own nonce management)
267
290
  if isinstance(signer_account, StoredSafeAccount):
268
291
  if not self.safe_service:
269
292
  logger.error("Attempted Safe transaction but SafeService is not initialized.")
270
293
  return False, {}
294
+ # Safe transactions don't need EOA nonce locking
295
+ if not self._prepare_transaction(tx, signer_address_or_tag, chain_interface):
296
+ return False, {}
271
297
  return self._execute_via_safe(tx, signer_account, chain_interface, chain_name, tags)
272
298
 
273
- # Mutable state for retry attempts
274
- state = {"gas_retries": 0, "max_gas_retries": 5}
299
+ # Acquire lock for this address to prevent nonce collisions
300
+ address_lock = self._get_address_lock(signer_account.address)
301
+ with address_lock:
302
+ if not self._prepare_transaction(tx, signer_address_or_tag, chain_interface):
303
+ return False, {}
275
304
 
276
- def _do_sign_send_wait() -> Tuple[bool, Dict, bytes]:
277
- """Inner operation wrapped by with_retry."""
278
- try:
279
- signed_txn = self.key_storage.sign_transaction(tx, signer_address_or_tag)
280
- txn_hash = chain_interface.web3.eth.send_raw_transaction(signed_txn.raw_transaction)
281
- receipt = chain_interface.web3.eth.wait_for_transaction_receipt(txn_hash)
305
+ # Mutable state for retry attempts
306
+ state = {"gas_retries": 0, "max_gas_retries": 5}
282
307
 
283
- status = getattr(receipt, "status", None)
284
- if status is None and isinstance(receipt, dict):
285
- status = receipt.get("status")
308
+ def _do_sign_send_wait() -> Tuple[bool, Dict, bytes]:
309
+ """Inner operation wrapped by with_retry."""
310
+ try:
311
+ signed_txn = self.key_storage.sign_transaction(tx, signer_address_or_tag)
312
+ txn_hash = chain_interface.web3.eth.send_raw_transaction(
313
+ signed_txn.raw_transaction
314
+ )
315
+ receipt = chain_interface.web3.eth.wait_for_transaction_receipt(txn_hash)
286
316
 
287
- if receipt and status == 1:
288
- return True, receipt, txn_hash
289
- # Transaction mined but reverted - don't retry
290
- logger.error("Transaction failed (status 0).")
291
- raise ValueError("Transaction reverted")
317
+ status = getattr(receipt, "status", None)
318
+ if status is None and isinstance(receipt, dict):
319
+ status = receipt.get("status")
292
320
 
293
- except web3_exceptions.Web3RPCError as e:
294
- # Handle gas errors by increasing gas and re-raising
295
- self._handle_gas_retry(e, tx, state)
296
- raise # Re-raise to trigger with_retry's retry mechanism
321
+ if receipt and status == 1:
322
+ return True, receipt, txn_hash
323
+ # Transaction mined but reverted - don't retry
324
+ logger.error("Transaction failed (status 0).")
325
+ raise ValueError("Transaction reverted")
297
326
 
298
- try:
299
- success, receipt, txn_hash = chain_interface.with_retry(
300
- _do_sign_send_wait,
301
- operation_name=f"sign_and_send to {tx.get('to', 'unknown')[:10]}...",
302
- )
303
- if success:
304
- signer_account = self.account_service.resolve_account(signer_address_or_tag)
305
- chain_interface.wait_for_no_pending_tx(signer_account.address)
306
- logger.info(f"Transaction sent successfully. Tx Hash: {txn_hash.hex()}")
307
- self._log_successful_transaction(
308
- receipt, tx, signer_account, chain_name, txn_hash, tags, chain_interface
327
+ except web3_exceptions.Web3RPCError as e:
328
+ # Handle gas errors by increasing gas and re-raising
329
+ self._handle_gas_retry(e, tx, state)
330
+ raise # Re-raise to trigger with_retry's retry mechanism
331
+
332
+ try:
333
+ success, receipt, txn_hash = chain_interface.with_retry(
334
+ _do_sign_send_wait,
335
+ operation_name=f"sign_and_send to {tx.get('to', 'unknown')[:10]}...",
309
336
  )
310
- return True, receipt
311
- return False, {}
312
- except ValueError as e:
313
- # Transaction reverted - already logged
314
- if "reverted" in str(e).lower():
337
+ if success:
338
+ chain_interface.wait_for_no_pending_tx(signer_account.address)
339
+ logger.info(f"Transaction sent successfully. Tx Hash: {txn_hash.hex()}")
340
+ self._log_successful_transaction(
341
+ receipt, tx, signer_account, chain_name, txn_hash, tags, chain_interface
342
+ )
343
+ return True, receipt
344
+ return False, {}
345
+ except ValueError as e:
346
+ # Transaction reverted - already logged
347
+ if "reverted" in str(e).lower():
348
+ return False, {}
349
+ logger.exception(f"Transaction failed: {e}")
350
+ return False, {}
351
+ except Exception as e:
352
+ logger.exception(f"Transaction failed after retries: {e}")
315
353
  return False, {}
316
- logger.exception(f"Transaction failed: {e}")
317
- return False, {}
318
- except Exception as e:
319
- logger.exception(f"Transaction failed after retries: {e}")
320
- return False, {}
321
354
 
322
355
  def _prepare_transaction(self, tx: dict, signer_tag: str, chain_interface) -> bool:
323
356
  """Ensure nonce and chainId are set."""
@@ -495,3 +495,138 @@ class StakingContract(ContractInstance):
495
495
  tx_params={"from": from_address},
496
496
  )
497
497
  return tx
498
+
499
+ def _fetch_events_chunked(
500
+ self,
501
+ event_type: str,
502
+ from_block: int,
503
+ to_block: int,
504
+ chunk_size: int = 500,
505
+ ) -> List:
506
+ """Fetch events in chunks to handle RPC block range limits.
507
+
508
+ Args:
509
+ event_type: Name of the event (Checkpoint, ServiceInactivityWarning, etc.)
510
+ from_block: Starting block number.
511
+ to_block: Ending block number.
512
+ chunk_size: Max blocks per request (default 500).
513
+
514
+ Returns:
515
+ List of event log entries.
516
+
517
+ """
518
+ all_logs: List = []
519
+ current_from = from_block
520
+
521
+ event = getattr(self.contract.events, event_type, None)
522
+ if event is None:
523
+ logger.debug(f"Event {event_type} not found in contract ABI")
524
+ return []
525
+
526
+ while current_from <= to_block:
527
+ current_to = min(current_from + chunk_size - 1, to_block)
528
+
529
+ try:
530
+ event_filter = event.create_filter(
531
+ from_block=current_from, to_block=current_to
532
+ )
533
+ logs = event_filter.get_all_entries()
534
+ all_logs.extend(logs)
535
+ except Exception as e:
536
+ error_msg = str(e).lower()
537
+ # If range too large, try smaller chunks
538
+ if "range" in error_msg or "limit" in error_msg or "10000" in error_msg:
539
+ if chunk_size > 100:
540
+ logger.debug(
541
+ "Block range too large, retrying with smaller chunks"
542
+ )
543
+ smaller_logs = self._fetch_events_chunked(
544
+ event_type, current_from, current_to, chunk_size // 2
545
+ )
546
+ all_logs.extend(smaller_logs)
547
+ else:
548
+ logger.warning(
549
+ f"Cannot fetch {event_type} events for blocks "
550
+ f"{current_from}-{current_to}: {e}"
551
+ )
552
+ else:
553
+ logger.debug(f"Error fetching {event_type} events: {e}")
554
+
555
+ current_from = current_to + 1
556
+
557
+ return all_logs
558
+
559
+ def get_checkpoint_events(
560
+ self, from_block: int, to_block: Optional[int] = None
561
+ ) -> Dict:
562
+ """Get checkpoint-related events from a block range.
563
+
564
+ Retrieves Checkpoint, ServiceInactivityWarning, and ServicesEvicted events
565
+ to determine which services received rewards and which got warnings.
566
+
567
+ Uses chunked fetching to handle RPCs that limit block ranges.
568
+
569
+ Args:
570
+ from_block: Starting block number.
571
+ to_block: Ending block number (defaults to latest).
572
+
573
+ Returns:
574
+ Dict with:
575
+ - epoch: int, the new epoch number
576
+ - rewarded_services: Dict[int, int] mapping service_id -> reward (wei)
577
+ - inactivity_warnings: List[int] of service IDs with warnings
578
+ - evicted_services: List[int] of evicted service IDs
579
+ - checkpoint_block: int, block where checkpoint occurred
580
+
581
+ """
582
+ if to_block is None:
583
+ to_block = self.chain_interface.web3.eth.block_number
584
+
585
+ result: Dict = {
586
+ "epoch": None,
587
+ "rewarded_services": {},
588
+ "inactivity_warnings": [],
589
+ "evicted_services": [],
590
+ "checkpoint_block": None,
591
+ }
592
+
593
+ try:
594
+ # Get Checkpoint events
595
+ checkpoint_logs = self._fetch_events_chunked(
596
+ "Checkpoint", from_block, to_block
597
+ )
598
+
599
+ if checkpoint_logs:
600
+ # Take the most recent checkpoint
601
+ latest = checkpoint_logs[-1]
602
+ result["epoch"] = latest.args.get("epoch")
603
+ result["checkpoint_block"] = latest.blockNumber
604
+
605
+ # Parse serviceIds and rewards arrays
606
+ service_ids = latest.args.get("serviceIds", [])
607
+ rewards = latest.args.get("rewards", [])
608
+
609
+ for sid, reward in zip(service_ids, rewards, strict=True):
610
+ result["rewarded_services"][sid] = reward
611
+
612
+ # Get ServiceInactivityWarning events
613
+ warning_logs = self._fetch_events_chunked(
614
+ "ServiceInactivityWarning", from_block, to_block
615
+ )
616
+ for log in warning_logs:
617
+ service_id = log.args.get("serviceId")
618
+ if service_id is not None:
619
+ result["inactivity_warnings"].append(service_id)
620
+
621
+ # Get ServicesEvicted events
622
+ evicted_logs = self._fetch_events_chunked(
623
+ "ServicesEvicted", from_block, to_block
624
+ )
625
+ for log in evicted_logs:
626
+ evicted_ids = log.args.get("serviceIds", [])
627
+ result["evicted_services"].extend(evicted_ids)
628
+
629
+ except Exception as e:
630
+ logger.error(f"Error fetching checkpoint events: {e}")
631
+
632
+ return result
@@ -271,3 +271,503 @@ def test_staking_contract(tmp_path): # noqa: C901
271
271
  # Verify new nonces fields
272
272
  assert info["current_safe_nonce"] == 5
273
273
  assert info["current_mech_requests"] == 3
274
+
275
+
276
+ def test_get_checkpoint_events():
277
+ """Test get_checkpoint_events method."""
278
+ with patch("builtins.open", side_effect=side_effect_open):
279
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
280
+ mock_chain = MagicMock()
281
+ mock_interfaces.return_value.get.return_value = mock_chain
282
+
283
+ mock_web3 = MagicMock()
284
+ mock_chain.web3 = mock_web3
285
+ mock_chain.web3._web3 = mock_web3
286
+ mock_web3.eth.block_number = 1000
287
+
288
+ mock_contract = MagicMock()
289
+ mock_web3.eth.contract.return_value = mock_contract
290
+
291
+ # Mock ActivityChecker calls
292
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
293
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
294
+
295
+ with patch(
296
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
297
+ ) as mock_call_base:
298
+
299
+ def init_side_effect(method, *args):
300
+ if method == "activityChecker":
301
+ return VALID_ADDR_4
302
+ if method == "stakingToken":
303
+ return VALID_ADDR_2
304
+ return 0
305
+
306
+ mock_call_base.side_effect = init_side_effect
307
+
308
+ staking = StakingContract(VALID_ADDR_1)
309
+
310
+ # Test 1: Checkpoint event with rewarded services
311
+ mock_checkpoint_log = MagicMock()
312
+ mock_checkpoint_log.args = {
313
+ "epoch": 42,
314
+ "serviceIds": [101, 102, 103],
315
+ "rewards": [1000, 2000, 3000],
316
+ }
317
+ mock_checkpoint_log.blockNumber = 999
318
+
319
+ mock_checkpoint_filter = MagicMock()
320
+ mock_checkpoint_filter.get_all_entries.return_value = [mock_checkpoint_log]
321
+ mock_contract.events.Checkpoint.create_filter.return_value = (
322
+ mock_checkpoint_filter
323
+ )
324
+
325
+ # No warnings or evictions
326
+ mock_warning_filter = MagicMock()
327
+ mock_warning_filter.get_all_entries.return_value = []
328
+ mock_contract.events.ServiceInactivityWarning.create_filter.return_value = (
329
+ mock_warning_filter
330
+ )
331
+
332
+ mock_evicted_filter = MagicMock()
333
+ mock_evicted_filter.get_all_entries.return_value = []
334
+ mock_contract.events.ServicesEvicted.create_filter.return_value = (
335
+ mock_evicted_filter
336
+ )
337
+
338
+ result = staking.get_checkpoint_events(from_block=900, to_block=1000)
339
+
340
+ assert result["epoch"] == 42
341
+ assert result["checkpoint_block"] == 999
342
+ assert result["rewarded_services"] == {101: 1000, 102: 2000, 103: 3000}
343
+ assert result["inactivity_warnings"] == []
344
+ assert result["evicted_services"] == []
345
+
346
+
347
+ def test_get_checkpoint_events_with_warnings():
348
+ """Test get_checkpoint_events with inactivity warnings."""
349
+ with patch("builtins.open", side_effect=side_effect_open):
350
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
351
+ mock_chain = MagicMock()
352
+ mock_interfaces.return_value.get.return_value = mock_chain
353
+
354
+ mock_web3 = MagicMock()
355
+ mock_chain.web3 = mock_web3
356
+ mock_chain.web3._web3 = mock_web3
357
+ mock_web3.eth.block_number = 1000
358
+
359
+ mock_contract = MagicMock()
360
+ mock_web3.eth.contract.return_value = mock_contract
361
+
362
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
363
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
364
+
365
+ with patch(
366
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
367
+ ) as mock_call_base:
368
+
369
+ def init_side_effect(method, *args):
370
+ if method == "activityChecker":
371
+ return VALID_ADDR_4
372
+ if method == "stakingToken":
373
+ return VALID_ADDR_2
374
+ return 0
375
+
376
+ mock_call_base.side_effect = init_side_effect
377
+
378
+ staking = StakingContract(VALID_ADDR_1)
379
+
380
+ # Checkpoint event
381
+ mock_checkpoint_log = MagicMock()
382
+ mock_checkpoint_log.args = {
383
+ "epoch": 10,
384
+ "serviceIds": [101, 102],
385
+ "rewards": [1000, 2000],
386
+ }
387
+ mock_checkpoint_log.blockNumber = 999
388
+
389
+ mock_checkpoint_filter = MagicMock()
390
+ mock_checkpoint_filter.get_all_entries.return_value = [mock_checkpoint_log]
391
+ mock_contract.events.Checkpoint.create_filter.return_value = (
392
+ mock_checkpoint_filter
393
+ )
394
+
395
+ # Inactivity warnings
396
+ mock_warning_log_1 = MagicMock()
397
+ mock_warning_log_1.args = {"serviceId": 101}
398
+ mock_warning_log_2 = MagicMock()
399
+ mock_warning_log_2.args = {"serviceId": 103}
400
+
401
+ mock_warning_filter = MagicMock()
402
+ mock_warning_filter.get_all_entries.return_value = [
403
+ mock_warning_log_1,
404
+ mock_warning_log_2,
405
+ ]
406
+ mock_contract.events.ServiceInactivityWarning.create_filter.return_value = (
407
+ mock_warning_filter
408
+ )
409
+
410
+ mock_evicted_filter = MagicMock()
411
+ mock_evicted_filter.get_all_entries.return_value = []
412
+ mock_contract.events.ServicesEvicted.create_filter.return_value = (
413
+ mock_evicted_filter
414
+ )
415
+
416
+ result = staking.get_checkpoint_events(from_block=900)
417
+
418
+ assert result["epoch"] == 10
419
+ assert result["inactivity_warnings"] == [101, 103]
420
+ assert result["evicted_services"] == []
421
+
422
+
423
+ def test_get_checkpoint_events_with_evictions():
424
+ """Test get_checkpoint_events with evicted services."""
425
+ with patch("builtins.open", side_effect=side_effect_open):
426
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
427
+ mock_chain = MagicMock()
428
+ mock_interfaces.return_value.get.return_value = mock_chain
429
+
430
+ mock_web3 = MagicMock()
431
+ mock_chain.web3 = mock_web3
432
+ mock_chain.web3._web3 = mock_web3
433
+ mock_web3.eth.block_number = 1000
434
+
435
+ mock_contract = MagicMock()
436
+ mock_web3.eth.contract.return_value = mock_contract
437
+
438
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
439
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
440
+
441
+ with patch(
442
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
443
+ ) as mock_call_base:
444
+
445
+ def init_side_effect(method, *args):
446
+ if method == "activityChecker":
447
+ return VALID_ADDR_4
448
+ if method == "stakingToken":
449
+ return VALID_ADDR_2
450
+ return 0
451
+
452
+ mock_call_base.side_effect = init_side_effect
453
+
454
+ staking = StakingContract(VALID_ADDR_1)
455
+
456
+ # Checkpoint event
457
+ mock_checkpoint_log = MagicMock()
458
+ mock_checkpoint_log.args = {
459
+ "epoch": 5,
460
+ "serviceIds": [102],
461
+ "rewards": [5000],
462
+ }
463
+ mock_checkpoint_log.blockNumber = 888
464
+
465
+ mock_checkpoint_filter = MagicMock()
466
+ mock_checkpoint_filter.get_all_entries.return_value = [mock_checkpoint_log]
467
+ mock_contract.events.Checkpoint.create_filter.return_value = (
468
+ mock_checkpoint_filter
469
+ )
470
+
471
+ # No warnings
472
+ mock_warning_filter = MagicMock()
473
+ mock_warning_filter.get_all_entries.return_value = []
474
+ mock_contract.events.ServiceInactivityWarning.create_filter.return_value = (
475
+ mock_warning_filter
476
+ )
477
+
478
+ # Evictions
479
+ mock_evicted_log = MagicMock()
480
+ mock_evicted_log.args = {"serviceIds": [101, 104]}
481
+
482
+ mock_evicted_filter = MagicMock()
483
+ mock_evicted_filter.get_all_entries.return_value = [mock_evicted_log]
484
+ mock_contract.events.ServicesEvicted.create_filter.return_value = (
485
+ mock_evicted_filter
486
+ )
487
+
488
+ result = staking.get_checkpoint_events(from_block=800)
489
+
490
+ assert result["epoch"] == 5
491
+ assert result["checkpoint_block"] == 888
492
+ assert result["rewarded_services"] == {102: 5000}
493
+ assert result["evicted_services"] == [101, 104]
494
+
495
+
496
+ def test_get_checkpoint_events_no_events():
497
+ """Test get_checkpoint_events when no checkpoint events found."""
498
+ with patch("builtins.open", side_effect=side_effect_open):
499
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
500
+ mock_chain = MagicMock()
501
+ mock_interfaces.return_value.get.return_value = mock_chain
502
+
503
+ mock_web3 = MagicMock()
504
+ mock_chain.web3 = mock_web3
505
+ mock_chain.web3._web3 = mock_web3
506
+ mock_web3.eth.block_number = 1000
507
+
508
+ mock_contract = MagicMock()
509
+ mock_web3.eth.contract.return_value = mock_contract
510
+
511
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
512
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
513
+
514
+ with patch(
515
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
516
+ ) as mock_call_base:
517
+
518
+ def init_side_effect(method, *args):
519
+ if method == "activityChecker":
520
+ return VALID_ADDR_4
521
+ if method == "stakingToken":
522
+ return VALID_ADDR_2
523
+ return 0
524
+
525
+ mock_call_base.side_effect = init_side_effect
526
+
527
+ staking = StakingContract(VALID_ADDR_1)
528
+
529
+ # No checkpoint events
530
+ mock_checkpoint_filter = MagicMock()
531
+ mock_checkpoint_filter.get_all_entries.return_value = []
532
+ mock_contract.events.Checkpoint.create_filter.return_value = (
533
+ mock_checkpoint_filter
534
+ )
535
+
536
+ mock_warning_filter = MagicMock()
537
+ mock_warning_filter.get_all_entries.return_value = []
538
+ mock_contract.events.ServiceInactivityWarning.create_filter.return_value = (
539
+ mock_warning_filter
540
+ )
541
+
542
+ mock_evicted_filter = MagicMock()
543
+ mock_evicted_filter.get_all_entries.return_value = []
544
+ mock_contract.events.ServicesEvicted.create_filter.return_value = (
545
+ mock_evicted_filter
546
+ )
547
+
548
+ result = staking.get_checkpoint_events(from_block=900)
549
+
550
+ assert result["epoch"] is None
551
+ assert result["checkpoint_block"] is None
552
+ assert result["rewarded_services"] == {}
553
+ assert result["inactivity_warnings"] == []
554
+ assert result["evicted_services"] == []
555
+
556
+
557
+ def test_get_checkpoint_events_handles_exceptions():
558
+ """Test get_checkpoint_events handles exceptions gracefully."""
559
+ with patch("builtins.open", side_effect=side_effect_open):
560
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
561
+ mock_chain = MagicMock()
562
+ mock_interfaces.return_value.get.return_value = mock_chain
563
+
564
+ mock_web3 = MagicMock()
565
+ mock_chain.web3 = mock_web3
566
+ mock_chain.web3._web3 = mock_web3
567
+ mock_web3.eth.block_number = 1000
568
+
569
+ mock_contract = MagicMock()
570
+ mock_web3.eth.contract.return_value = mock_contract
571
+
572
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
573
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
574
+
575
+ with patch(
576
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
577
+ ) as mock_call_base:
578
+
579
+ def init_side_effect(method, *args):
580
+ if method == "activityChecker":
581
+ return VALID_ADDR_4
582
+ if method == "stakingToken":
583
+ return VALID_ADDR_2
584
+ return 0
585
+
586
+ mock_call_base.side_effect = init_side_effect
587
+
588
+ staking = StakingContract(VALID_ADDR_1)
589
+
590
+ # Checkpoint filter raises an exception
591
+ mock_contract.events.Checkpoint.create_filter.side_effect = Exception(
592
+ "RPC error"
593
+ )
594
+
595
+ result = staking.get_checkpoint_events(from_block=900)
596
+
597
+ # Should return default empty result
598
+ assert result["epoch"] is None
599
+ assert result["rewarded_services"] == {}
600
+ assert result["inactivity_warnings"] == []
601
+ assert result["evicted_services"] == []
602
+
603
+
604
+ def test_fetch_events_chunked_splits_large_range():
605
+ """Test that _fetch_events_chunked splits large block ranges into chunks."""
606
+ with patch("builtins.open", side_effect=side_effect_open):
607
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
608
+ mock_chain = MagicMock()
609
+ mock_interfaces.return_value.get.return_value = mock_chain
610
+
611
+ mock_web3 = MagicMock()
612
+ mock_chain.web3 = mock_web3
613
+ mock_chain.web3._web3 = mock_web3
614
+ mock_web3.eth.block_number = 2000
615
+
616
+ mock_contract = MagicMock()
617
+ mock_web3.eth.contract.return_value = mock_contract
618
+
619
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
620
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
621
+
622
+ with patch(
623
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
624
+ ) as mock_call_base:
625
+
626
+ def init_side_effect(method, *args):
627
+ if method == "activityChecker":
628
+ return VALID_ADDR_4
629
+ if method == "stakingToken":
630
+ return VALID_ADDR_2
631
+ return 0
632
+
633
+ mock_call_base.side_effect = init_side_effect
634
+
635
+ staking = StakingContract(VALID_ADDR_1)
636
+
637
+ # Track calls to create_filter
638
+ call_ranges = []
639
+
640
+ def track_filter_calls(from_block, to_block):
641
+ call_ranges.append((from_block, to_block))
642
+ mock_filter = MagicMock()
643
+ mock_filter.get_all_entries.return_value = []
644
+ return mock_filter
645
+
646
+ mock_contract.events.Checkpoint.create_filter.side_effect = (
647
+ track_filter_calls
648
+ )
649
+
650
+ # Fetch 1200 blocks with chunk_size=500 -> should make 3 calls
651
+ staking._fetch_events_chunked("Checkpoint", 0, 1199, chunk_size=500)
652
+
653
+ assert len(call_ranges) == 3
654
+ assert call_ranges[0] == (0, 499)
655
+ assert call_ranges[1] == (500, 999)
656
+ assert call_ranges[2] == (1000, 1199)
657
+
658
+
659
+ def test_fetch_events_chunked_retries_with_smaller_chunks():
660
+ """Test that _fetch_events_chunked retries with smaller chunks on range error."""
661
+ with patch("builtins.open", side_effect=side_effect_open):
662
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
663
+ mock_chain = MagicMock()
664
+ mock_interfaces.return_value.get.return_value = mock_chain
665
+
666
+ mock_web3 = MagicMock()
667
+ mock_chain.web3 = mock_web3
668
+ mock_chain.web3._web3 = mock_web3
669
+ mock_web3.eth.block_number = 1000
670
+
671
+ mock_contract = MagicMock()
672
+ mock_web3.eth.contract.return_value = mock_contract
673
+
674
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
675
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
676
+
677
+ with patch(
678
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
679
+ ) as mock_call_base:
680
+
681
+ def init_side_effect(method, *args):
682
+ if method == "activityChecker":
683
+ return VALID_ADDR_4
684
+ if method == "stakingToken":
685
+ return VALID_ADDR_2
686
+ return 0
687
+
688
+ mock_call_base.side_effect = init_side_effect
689
+
690
+ staking = StakingContract(VALID_ADDR_1)
691
+
692
+ call_count = [0]
693
+
694
+ def fail_then_succeed(from_block, to_block):
695
+ call_count[0] += 1
696
+ # First call fails with range error, subsequent calls succeed
697
+ if call_count[0] == 1:
698
+ raise Exception("block range too large")
699
+ mock_filter = MagicMock()
700
+ mock_filter.get_all_entries.return_value = []
701
+ return mock_filter
702
+
703
+ mock_contract.events.Checkpoint.create_filter.side_effect = (
704
+ fail_then_succeed
705
+ )
706
+
707
+ # Should retry with smaller chunks
708
+ result = staking._fetch_events_chunked(
709
+ "Checkpoint", 0, 499, chunk_size=500
710
+ )
711
+
712
+ # First call fails, then retries with chunk_size=250 (2 calls)
713
+ assert call_count[0] >= 2
714
+ assert result == []
715
+
716
+
717
+ def test_fetch_events_chunked_aggregates_results():
718
+ """Test that _fetch_events_chunked aggregates events from all chunks."""
719
+ with patch("builtins.open", side_effect=side_effect_open):
720
+ with patch("iwa.core.contracts.contract.ChainInterfaces") as mock_interfaces:
721
+ mock_chain = MagicMock()
722
+ mock_interfaces.return_value.get.return_value = mock_chain
723
+
724
+ mock_web3 = MagicMock()
725
+ mock_chain.web3 = mock_web3
726
+ mock_chain.web3._web3 = mock_web3
727
+ mock_web3.eth.block_number = 1000
728
+
729
+ mock_contract = MagicMock()
730
+ mock_web3.eth.contract.return_value = mock_contract
731
+
732
+ mock_contract.functions.agentMech.return_value.call.return_value = VALID_ADDR_2
733
+ mock_contract.functions.livenessRatio.return_value.call.return_value = 10**18
734
+
735
+ with patch(
736
+ "iwa.plugins.olas.contracts.staking.ContractInstance.call"
737
+ ) as mock_call_base:
738
+
739
+ def init_side_effect(method, *args):
740
+ if method == "activityChecker":
741
+ return VALID_ADDR_4
742
+ if method == "stakingToken":
743
+ return VALID_ADDR_2
744
+ return 0
745
+
746
+ mock_call_base.side_effect = init_side_effect
747
+
748
+ staking = StakingContract(VALID_ADDR_1)
749
+
750
+ chunk_num = [0]
751
+
752
+ def return_different_events(from_block, to_block):
753
+ chunk_num[0] += 1
754
+ mock_filter = MagicMock()
755
+ # Each chunk returns a different event
756
+ event = MagicMock()
757
+ event.args = {"serviceId": 100 + chunk_num[0]}
758
+ mock_filter.get_all_entries.return_value = [event]
759
+ return mock_filter
760
+
761
+ mock_contract.events.ServiceInactivityWarning.create_filter.side_effect = (
762
+ return_different_events
763
+ )
764
+
765
+ # Fetch 600 blocks with chunk_size=200 -> 3 chunks
766
+ result = staking._fetch_events_chunked(
767
+ "ServiceInactivityWarning", 0, 599, chunk_size=200
768
+ )
769
+
770
+ # Should have 3 events aggregated
771
+ assert len(result) == 3
772
+ service_ids = [e.args["serviceId"] for e in result]
773
+ assert service_ids == [101, 102, 103]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: iwa
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: A secure, modular, and plugin-based framework for crypto agents and ops
5
5
  Requires-Python: <4.0,>=3.12
6
6
  Description-Content-Type: text/markdown
@@ -23,7 +23,7 @@ iwa/core/utils.py,sha256=FTYpIdQ1wnugD4lYU4TQ7d7_TlDs4CTUIhEpHGEJph4,4281
23
23
  iwa/core/wallet.py,sha256=xSGFOK5Wzh-ctLGhBMK1BySlXN0Ircpztyk1an21QiQ,13129
24
24
  iwa/core/chain/__init__.py,sha256=XJMmn0ed-_aVkY2iEMKpuTxPgIKBd41dexSVmEZTa-o,1604
25
25
  iwa/core/chain/errors.py,sha256=9SEbhxZ-qASPkzt-DoI51qq0GRJVqRgqgL720gO7a64,1275
26
- iwa/core/chain/interface.py,sha256=D_bDIQhQVfY9A2nLPpQ3bipmywoYsi2wVNPXPKutFec,30760
26
+ iwa/core/chain/interface.py,sha256=Z7AcLjlYRw8uBj1zklVGep8Z21T-uBCamW48df7w0ew,30952
27
27
  iwa/core/chain/manager.py,sha256=XHwn7ciapFCZVk0rPSJopUqM5Wu3Kpp6XrenkgTE1HA,1397
28
28
  iwa/core/chain/models.py,sha256=WUhAighMKcFdbAUkPU_3dkGbWyAUpRJqXMHLcWFC1xg,5261
29
29
  iwa/core/chain/rate_limiter.py,sha256=Ps1MrR4HHtylxgUAawe6DoC9tuqKagjQdKulqcJD2gs,9093
@@ -42,7 +42,7 @@ iwa/core/services/balance.py,sha256=MSCEzPRDPlHIjaWD1A2X2oIuiMz5MFJjD7sSHUxQ8OM,
42
42
  iwa/core/services/plugin.py,sha256=GNNlbtELyHl7MNVChrypF76GYphxXduxDog4kx1MLi8,3277
43
43
  iwa/core/services/safe.py,sha256=HG3yAN5IdNR45uuHfjAuaT4XVy_tiivoMTQLoEHlEwY,15701
44
44
  iwa/core/services/safe_executor.py,sha256=uZIoE_VeB0B9b-HhZ5jNWXna1kr8e_LZ8qEt63kGxIU,17082
45
- iwa/core/services/transaction.py,sha256=FrGRWn1xo5rbGIr2ToZ2kPzapr3zmWW38oycyB87TK8,19971
45
+ iwa/core/services/transaction.py,sha256=eh--51bAA3rbK1gPjfk7zA5Gm9GJ7CJ82uzG4O5A-Rs,21501
46
46
  iwa/core/services/transfer/__init__.py,sha256=7p22xtwrH0murXWTuTGNw0WRKBDqjJEnnVaUpWd8Vfo,5589
47
47
  iwa/core/services/transfer/base.py,sha256=sohz-Ss2i-pGYGl4x9bD93cnYKcSvsXaXyvyRawvgQs,9043
48
48
  iwa/core/services/transfer/erc20.py,sha256=e6RD1_QcI_-iJvP85kj7aw4p6NnCPjZcm-QSKi14RRA,10104
@@ -78,7 +78,7 @@ iwa/plugins/olas/contracts/mech.py,sha256=dXYtyORc-oiu9ga5PtTquOFkoakb6BLGKvlUst
78
78
  iwa/plugins/olas/contracts/mech_marketplace.py,sha256=hMADl5MQGvT2wLRKu4vHGe4RrAZVq8Y2M_EvXWWz528,1554
79
79
  iwa/plugins/olas/contracts/mech_marketplace_v1.py,sha256=ooF5uw1wxwYsoriGUGGxXxmaD8DtWZtK4TJBCUNTGtI,2501
80
80
  iwa/plugins/olas/contracts/service.py,sha256=BDQKeCTCnBNrwKD1a8rrlLytpKG3CAdjr-s0ec-dsFY,8243
81
- iwa/plugins/olas/contracts/staking.py,sha256=kKF4Uc2cFr9_drKG5OzFgzKTdgGyHIM_PUa-L0LX9gU,18486
81
+ iwa/plugins/olas/contracts/staking.py,sha256=Qkr6yS3an-yZe560Dwer_g1wyOL3e1VIAGaeSZLCgfs,23512
82
82
  iwa/plugins/olas/contracts/abis/activity_checker.json,sha256=HT0IMbyTLMO71ITBKwoS950rHe772suPP4b8eDAodJ0,2230
83
83
  iwa/plugins/olas/contracts/abis/mech.json,sha256=bMMCXInjE_2PTPnc_sIyS_H8pod5Sm_e-xTbKgZppKc,16369
84
84
  iwa/plugins/olas/contracts/abis/mech_marketplace.json,sha256=KPnF-H_UATb3wdb_7o6ky_hSp5xwgvckD-QqylsWJLg,32468
@@ -118,7 +118,7 @@ iwa/plugins/olas/tests/test_service_manager_mech.py,sha256=qG6qu5IPRNypXUsblU2OE
118
118
  iwa/plugins/olas/tests/test_service_manager_rewards.py,sha256=2YCrXBU5bEkPuhBoGBhjnO1nA2qwHxn5Ivrror18FHM,12248
119
119
  iwa/plugins/olas/tests/test_service_manager_validation.py,sha256=ajlfH5uc4mAHf8A7GLE5cW7X8utM2vUilM0JdGDdlVg,5382
120
120
  iwa/plugins/olas/tests/test_service_staking.py,sha256=exxWsile_wG_0rz_cGbCPG-_Ubq01Ofl4D_pi0plj5Y,18332
121
- iwa/plugins/olas/tests/test_staking_integration.py,sha256=QCBQf6P2ZmmsEGt2k8W2r53lG2aVRuoMJE-aFxVDLss,9701
121
+ iwa/plugins/olas/tests/test_staking_integration.py,sha256=q7zLQLrUyhtcnZf6MMymx2cX0Gmqaa7i1mRh1clnyj4,30198
122
122
  iwa/plugins/olas/tests/test_staking_validation.py,sha256=uug64jFcXYJ3Nw_lNa3O4fnhNr5wAWHHIrchSbR2MVE,4020
123
123
  iwa/plugins/olas/tui/__init__.py,sha256=5ZRsbC7J3z1xfkZRiwr4bLEklf78rNVjdswe2p7SlS8,28
124
124
  iwa/plugins/olas/tui/olas_view.py,sha256=dgZjfXCWsRRdHpygHfSOCJZFWZrgrVyieq-iYgDkK3w,37404
@@ -166,7 +166,7 @@ iwa/web/tests/test_web_endpoints.py,sha256=vA25YghHNB23sbmhD4ciesn_f_okSq0tjlkrS
166
166
  iwa/web/tests/test_web_olas.py,sha256=GunKEAzcbzL7FoUGMtEl8wqiqwYwA5lB9sOhfCNj0TA,16312
167
167
  iwa/web/tests/test_web_swap.py,sha256=7A4gBJFL01kIXPtW1E1J17SCsVc_0DmUn-R8kKrnnVA,2974
168
168
  iwa/web/tests/test_web_swap_coverage.py,sha256=zGNrzlhZ_vWDCvWmLcoUwFgqxnrp_ACbo49AtWBS_Kw,5584
169
- iwa-0.1.1.dist-info/licenses/LICENSE,sha256=eIubm_IlBHPYRQlLNZKbBNKhJUUP3JH0A2miZUhAVfI,1078
169
+ iwa-0.1.3.dist-info/licenses/LICENSE,sha256=eIubm_IlBHPYRQlLNZKbBNKhJUUP3JH0A2miZUhAVfI,1078
170
170
  tests/legacy_cow.py,sha256=oOkZvIxL70ReEoD9oHQbOD5GpjIr6AGNHcOCgfPlerU,8389
171
171
  tests/legacy_safe.py,sha256=AssM2g13E74dNGODu_H0Q0y412lgqsrYnEzI97nm_Ts,2972
172
172
  tests/legacy_transaction_retry_logic.py,sha256=D9RqZ7DBu61Xr2djBAodU2p9UE939LL-DnQXswX5iQk,1497
@@ -224,8 +224,8 @@ tests/test_utils.py,sha256=vkP49rYNI8BRzLpWR3WnKdDr8upeZjZcs7Rx0pjbQMo,1292
224
224
  tests/test_workers.py,sha256=MInwdkFY5LdmFB3o1odIaSD7AQZb3263hNafO1De5PE,2793
225
225
  tools/create_and_stake_service.py,sha256=1xwy_bJQI1j9yIQ968Oc9Db_F6mk1659LuuZntTASDE,3742
226
226
  tools/verify_drain.py,sha256=PkMjblyOOAuQge88FwfEzRtCYeEtJxXhPBmtQYCoQ-8,6743
227
- iwa-0.1.1.dist-info/METADATA,sha256=jQxFBAjZ8GWJqgloxJkCLzHmgC7rO2YmkwZOpb0EIIM,7336
228
- iwa-0.1.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
229
- iwa-0.1.1.dist-info/entry_points.txt,sha256=nwB6kscrfA7M00pYmL2j-sBH6eF6h2ga9IK1BZxdiyQ,241
230
- iwa-0.1.1.dist-info/top_level.txt,sha256=kedS9cRUbm4JE2wYeabIXilhHjN8KCw0IGbqqqsw0Bs,16
231
- iwa-0.1.1.dist-info/RECORD,,
227
+ iwa-0.1.3.dist-info/METADATA,sha256=uKemLrcWBfz_raFNxn3okTiQWiwF-lLQok838PpZXtI,7336
228
+ iwa-0.1.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
229
+ iwa-0.1.3.dist-info/entry_points.txt,sha256=nwB6kscrfA7M00pYmL2j-sBH6eF6h2ga9IK1BZxdiyQ,241
230
+ iwa-0.1.3.dist-info/top_level.txt,sha256=kedS9cRUbm4JE2wYeabIXilhHjN8KCw0IGbqqqsw0Bs,16
231
+ iwa-0.1.3.dist-info/RECORD,,
File without changes