-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeout modules on CSM failure #283
Open
hweawer
wants to merge
4
commits into
develop
Choose a base branch
from
timeout-modules
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/blockchain/deposit_strategy/deposit_module_recommender.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from datetime import datetime, timedelta | ||
from typing import Callable | ||
|
||
from web3 import Web3 | ||
|
||
_TIMEOUTS = { | ||
1: timedelta(minutes=10), | ||
2: timedelta(minutes=10), | ||
} | ||
|
||
|
||
class DepositModuleRecommender: | ||
def __init__(self, w3: Web3): | ||
self._w3 = w3 | ||
self._module_timeouts = dict() | ||
|
||
def get_preferred_to_deposit_modules(self, whitelist_modules: list[int]) -> list[int]: | ||
module_ids = self._w3.lido.staking_router.get_staking_module_ids() | ||
modules = self._w3.lido.staking_router.get_staking_module_digests(module_ids) | ||
|
||
depositable_modules = list(filter(self._get_module_depositable_filter(whitelist_modules), modules)) | ||
modules_ids = self.prioritize_modules(depositable_modules) | ||
return modules_ids | ||
|
||
def _get_module_depositable_filter(self, whitelist_modules: list[int]) -> Callable: | ||
def is_module_depositable(module: list) -> bool: | ||
module_id = module[2][0] | ||
|
||
if self._is_timeout_passed(module_id): | ||
self.reset_timeout(module_id) | ||
|
||
return ( | ||
module_id not in self._module_timeouts | ||
and module_id in whitelist_modules | ||
and self._w3.lido.staking_router.is_staking_module_active(module_id) | ||
and self._w3.lido.deposit_security_module.can_deposit(module_id) | ||
) | ||
|
||
return is_module_depositable | ||
|
||
@staticmethod | ||
def prioritize_modules(modules: list) -> list[int]: | ||
modules = sorted( | ||
modules, | ||
# totalDepositedValidators - totalExitedValidators | ||
key=lambda module: module[3][1] - module[3][0], | ||
) | ||
|
||
# module_ids | ||
return [module[2][0] for module in modules] | ||
|
||
def set_timeout(self, module_id: int): | ||
self._module_timeouts[module_id] = datetime.now() | ||
|
||
def reset_timeout(self, module_id: int): | ||
return self._module_timeouts.pop(module_id, None) | ||
|
||
def _is_timeout_passed(self, module_id: int) -> bool: | ||
if module_id not in _TIMEOUTS or module_id not in self._module_timeouts: | ||
return True | ||
init = self._module_timeouts[module_id] | ||
now = datetime.now() | ||
return abs(now - init) >= _TIMEOUTS[module_id] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/blockchain/deposit_strategy/test_deposit_module_recommender.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import unittest | ||
from datetime import datetime, timedelta | ||
from unittest.mock import MagicMock, patch | ||
|
||
from blockchain.deposit_strategy.deposit_module_recommender import DepositModuleRecommender | ||
|
||
|
||
class TestDepositModuleRecommender(unittest.TestCase): | ||
def setUp(self): | ||
# Create a mock Web3 instance with necessary Lido staking_router and deposit_security_module attributes | ||
self.mock_w3 = MagicMock() | ||
self.recommender = DepositModuleRecommender(w3=self.mock_w3) | ||
|
||
@patch('blockchain.deposit_strategy.deposit_module_recommender.datetime') | ||
def test_set_timeout(self, mock_datetime): | ||
"""Test setting a timeout for a module.""" | ||
mock_datetime.now.return_value = datetime(2023, 10, 1, 12, 0, 0) | ||
module_id = 1 | ||
|
||
self.recommender.set_timeout(module_id) | ||
self.assertIn(module_id, self.recommender._module_timeouts) | ||
self.assertEqual(self.recommender._module_timeouts[module_id], mock_datetime.now()) | ||
|
||
def test_reset_timeout(self): | ||
"""Test resetting a timeout for a module.""" | ||
module_id = 1 | ||
self.recommender._module_timeouts[module_id] = datetime.now() | ||
self.recommender.reset_timeout(module_id) | ||
self.assertNotIn(module_id, self.recommender._module_timeouts) | ||
|
||
@patch('blockchain.deposit_strategy.deposit_module_recommender.datetime') | ||
def test_is_timeout_passed(self, mock_datetime): | ||
"""Test that _is_timeout_passed correctly identifies passed timeouts.""" | ||
mock_datetime.now.return_value = datetime(2023, 10, 1, 12, 10, 0) | ||
module_id = 1 | ||
self.recommender._module_timeouts[module_id] = mock_datetime.now() - timedelta(minutes=15) | ||
|
||
# Timeout of 10 minutes for module_id 1 should be exceeded | ||
self.assertTrue(self.recommender._is_timeout_passed(module_id)) | ||
|
||
def test_prioritize_modules(self): | ||
"""Test prioritization of modules based on validator count.""" | ||
modules = [ | ||
['ModuleA', 'info', [1], [5, 10]], # Difference: 5 | ||
['ModuleB', 'info', [2], [15, 20]], # Difference: 5 | ||
['ModuleC', 'info', [3], [1, 5]], # Difference: 4 | ||
] | ||
prioritized = self.recommender.prioritize_modules(modules) | ||
self.assertEqual(prioritized, [3, 1, 2]) # Module C has smallest difference, followed by A and B | ||
|
||
def test_get_preferred_to_deposit_modules(self): | ||
"""Test retrieval of depositable modules based on whitelist and active status.""" | ||
whitelist_modules = [1, 2] | ||
self.mock_w3.lido.staking_router.get_staking_module_ids.return_value = [1, 2, 3] | ||
self.mock_w3.lido.staking_router.get_staking_module_digests.return_value = [ | ||
['ModuleA', 'info', [1], [10, 5]], # Module 1, whitelisted and active | ||
['ModuleB', 'info', [2], [20, 15]], # Module 2, whitelisted but inactive | ||
['ModuleC', 'info', [3], [5, 1]], # Module 3, not whitelisted | ||
] | ||
|
||
# Set module activity and deposit capability | ||
self.mock_w3.lido.staking_router.is_staking_module_active.side_effect = lambda module_id: module_id != 2 | ||
self.mock_w3.lido.deposit_security_module.can_deposit.side_effect = lambda module_id: module_id == 1 | ||
|
||
preferred_modules = self.recommender.get_preferred_to_deposit_modules(whitelist_modules) | ||
self.assertEqual(preferred_modules, [1]) | ||
|
||
@patch('blockchain.deposit_strategy.deposit_module_recommender.datetime') | ||
def test_depositable_filter_with_timeout(self, mock_datetime): | ||
"""Test the depositable filter with module timeout handling.""" | ||
mock_datetime.now.return_value = datetime(2023, 10, 1, 12, 0, 0) | ||
whitelist_modules = [1] | ||
module = ['ModuleA', 'info', [1], [10, 5]] | ||
|
||
# Set up a timeout that has not yet expired | ||
self.recommender.set_timeout(1) | ||
self.recommender._module_timeouts[1] = mock_datetime.now() - timedelta(minutes=5) | ||
self.assertFalse(self.recommender._get_module_depositable_filter(whitelist_modules)(module)) | ||
|
||
# Update time to simulate expiration of timeout | ||
self.recommender._module_timeouts[1] = mock_datetime.now() - timedelta(minutes=15) | ||
self.assertTrue(self.recommender._get_module_depositable_filter(whitelist_modules)(module)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Timeouts should apply for modules not by id, but by active validators count