Skip to content

Commit 181337e

Browse files
committed
fix: fork tests
1 parent f218b61 commit 181337e

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

.github/workflows/ci-daily-checks.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
CONSENSUS_CLIENT_URI: ${{ secrets.CONSENSUS_CLIENT_URI }}
2323
KEYS_API_URI: ${{ secrets.KEYS_API_URI }}
2424
LIDO_LOCATOR_ADDRESS: ${{ secrets.LIDO_LOCATOR_ADDRESS }}
25-
STAKING_MODULE_ADDRESS: ${{ secrets.CSM_MODULE_ADDRESS }}
25+
STAKING_MODULE_ADDRESS: ${{ secrets.STAKING_MODULE_ADDRESS }}
2626
PINATA_JWT: ${{ secrets.PINATA_JWT }}
2727
PINATA_DEDICATED_GATEWAY_URL: ${{ secrets.PINATA_DEDICATED_GATEWAY_URL }}
2828
PINATA_DEDICATED_GATEWAY_TOKEN: ${{ secrets.PINATA_DEDICATED_GATEWAY_TOKEN}}
@@ -39,7 +39,7 @@ jobs:
3939
CONSENSUS_CLIENT_URI=${CONSENSUS_CLIENT_URI}
4040
KEYS_API_URI=${KEYS_API_URI}
4141
LIDO_LOCATOR_ADDRESS=${LIDO_LOCATOR_ADDRESS}
42-
CSM_MODULE_ADDRESS=${CSM_MODULE_ADDRESS}
42+
STAKING_MODULE_ADDRESS=${STAKING_MODULE_ADDRESS}
4343
GW3_ACCESS_KEY=${GW3_ACCESS_KEY}
4444
GW3_SECRET_KEY=${GW3_SECRET_KEY}
4545
PINATA_JWT=${PINATA_JWT}

src/modules/oracles/staking_modules/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,9 @@ def collect_data(self) -> bool:
177177
def build_report(self, blockstamp: ReferenceBlockStamp) -> tuple:
178178
self.validate_state(blockstamp)
179179

180-
last_report = self._get_last_report(blockstamp)
180+
distribution, last_report = self.calculate_distribution(blockstamp)
181181
rewards_tree_root, rewards_cid = last_report.rewards_tree_root, last_report.rewards_tree_cid
182182

183-
distribution = self.calculate_distribution(blockstamp, last_report)
184-
185183
if distribution.total_rewards:
186184
rewards_tree = self.make_rewards_tree(distribution.total_rewards_map)
187185
rewards_tree_root = rewards_tree.root
@@ -218,10 +216,11 @@ def _get_last_report(self, blockstamp: ReferenceBlockStamp) -> LastReport:
218216
return LastReport.load(self.w3, blockstamp, current_frame)
219217

220218
@lru_cache(maxsize=1)
221-
def calculate_distribution(self, blockstamp: ReferenceBlockStamp, last_report: LastReport) -> DistributionResult:
219+
def calculate_distribution(self, blockstamp: ReferenceBlockStamp) -> tuple[DistributionResult, LastReport]:
220+
last_report = self._get_last_report(blockstamp)
222221
distribution = Distribution(self.w3, self.converter(blockstamp), self.state)
223222
result = distribution.calculate(blockstamp, last_report)
224-
return result
223+
return result, last_report
225224

226225
def is_main_data_submitted(self, blockstamp: BlockStamp) -> bool:
227226
last_ref_slot = self.w3.staking_module.get_last_processing_ref_slot(blockstamp)

tests/fork/test_csm_oracle_cycle.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from threading import Thread
22

33
import pytest
4+
import uvicorn
45

56
from unittest.mock import patch
67
from pathlib import Path
@@ -12,11 +13,13 @@
1213
from src.modules.sidecars.performance.collector.collector import PerformanceCollector
1314
from src.modules.common.types import FrameConfig
1415
from src.modules.sidecars.performance.common.db import Duty
15-
from src.modules.sidecars.performance.web.server import serve
16+
from src.modules.sidecars.performance.web.server import app
1617
from src.utils.range import sequence
1718
from src.web3py.types import Web3
1819
from tests.fork.conftest import first_slot_of_epoch
1920

21+
# pylint: disable=protected-access
22+
2023

2124
@pytest.fixture()
2225
def hash_consensus_bin():
@@ -40,12 +43,19 @@ def performance_local_db(testrun_path):
4043

4144
def mock_get_database_url(self):
4245
db_path = Path(testrun_path) / "test_duties.db"
46+
db_path.parent.mkdir(parents=True, exist_ok=True)
4347
return f"sqlite:///{db_path}"
4448

49+
def mock_build_engine(self, connect_timeout):
50+
return create_engine(
51+
self._get_database_url(),
52+
echo=False,
53+
pool_pre_ping=True,
54+
)
55+
4556
def mock_init(self, *args, **kwargs):
46-
# pylint: disable=protected-access
47-
self.engine = create_engine(self._get_database_url(), echo=False)
48-
# pylint: disable=protected-access
57+
self._statement_timeout_ms = kwargs.get('statement_timeout_ms')
58+
self.engine = self._build_engine(kwargs.get('connect_timeout'))
4959
self._setup_database()
5060

5161
table = Duty.__table__
@@ -54,8 +64,9 @@ def mock_init(self, *args, **kwargs):
5464
table.c[col_name].type = JSON()
5565

5666
with patch('src.modules.sidecars.performance.common.db.DutiesDB._get_database_url', mock_get_database_url):
57-
with patch('src.modules.sidecars.performance.common.db.DutiesDB.__init__', mock_init):
58-
yield mock_get_database_url, mock_init
67+
with patch('src.modules.sidecars.performance.common.db.DutiesDB._build_engine', mock_build_engine):
68+
with patch('src.modules.sidecars.performance.common.db.DutiesDB.__init__', mock_init):
69+
yield mock_get_database_url, mock_init
5970

6071

6172
@pytest.fixture()
@@ -65,7 +76,9 @@ def performance_collector(performance_local_db, web3: Web3, frame_config: FrameC
6576

6677
@pytest.fixture()
6778
def performance_web_server(performance_local_db):
68-
Thread(target=serve, daemon=True).start()
79+
Thread(
80+
target=uvicorn.run, args=(app,), kwargs={'host': '127.0.0.1', 'port': 9020, 'log_level': 'error'}, daemon=True
81+
).start()
6982
yield
7083

7184

@@ -98,19 +111,12 @@ def missed_initial_frame(frame_config: FrameConfig, cycle_iterations):
98111
@pytest.mark.fork
99112
@pytest.mark.parametrize(
100113
'module',
101-
[
102-
csm_module,
103-
# cm_module
104-
],
114+
[csm_module, cm_module],
105115
indirect=True,
106116
)
107117
@pytest.mark.parametrize(
108118
'running_finalized_slots',
109-
[
110-
# start_before_initial_epoch,
111-
start_after_initial_epoch,
112-
# missed_initial_frame
113-
],
119+
[start_before_initial_epoch, start_after_initial_epoch, missed_initial_frame],
114120
indirect=True,
115121
)
116122
def test_csm_module_report(

tests/modules/csm/test_csm_module.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,7 @@ class BuildReportTestParam:
756756
def test_build_report(module: CSPerformanceOracle, param: BuildReportTestParam):
757757
module.validate_state = Mock()
758758
module.report_contract.get_consensus_version = Mock(return_value=1)
759-
module._get_last_report = Mock(return_value=param.last_report)
760-
# mock current frame
761-
module.calculate_distribution = param.curr_distribution
759+
module.calculate_distribution = Mock(return_value=(param.curr_distribution(), param.last_report))
762760
module.make_rewards_tree = Mock(return_value=Mock(root=param.curr_rewards_tree_root))
763761
module.make_strikes_tree = Mock(return_value=Mock(root=param.curr_strikes_tree_root))
764762
module.publish_tree = Mock(
@@ -843,20 +841,23 @@ def test_calculate_distribution_lru_cache(module: CSPerformanceOracle):
843841

844842
module.converter = Mock()
845843
module.state = Mock()
844+
module._get_last_report = Mock(return_value=last_report)
846845

847-
result1 = module.calculate_distribution(blockstamp, last_report)
846+
result1, last_report1 = module.calculate_distribution(blockstamp)
848847

849-
result2 = module.calculate_distribution(blockstamp, last_report)
848+
result2, last_report2 = module.calculate_distribution(blockstamp)
850849

851850
assert result1 is result2
851+
assert last_report1 is last_report2
852852
assert result1 is mock_distribution_result
853+
assert last_report1 is last_report
853854

854855
assert MockDistribution.call_count == 1
855856
assert mock_distribution_instance.calculate.call_count == 1
856857

857858
module.calculate_distribution.cache_clear()
858859

859-
result3 = module.calculate_distribution(blockstamp, last_report)
860+
result3, last_report3 = module.calculate_distribution(blockstamp)
860861

861862
assert MockDistribution.call_count == 2
862863
assert result3 is mock_distribution_result

tests/providers_clients/test_keys_api_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ def test_get_used_module_operators_keys__csm_module__response_data_is_valid(
8080
empty_blockstamp,
8181
):
8282
csm_module_operators_keys = keys_api_client.get_used_module_operators_keys(
83-
module_address=variables.CSM_MODULE_ADDRESS, # type: ignore
83+
module_address=variables.STAKING_MODULE_ADDRESS, # type: ignore
8484
blockstamp=empty_blockstamp,
8585
)
8686

87-
assert csm_module_operators_keys['module']['stakingModuleAddress'] == variables.CSM_MODULE_ADDRESS
87+
assert csm_module_operators_keys['module']['stakingModuleAddress'] == variables.STAKING_MODULE_ADDRESS
8888
assert csm_module_operators_keys['module']['id'] >= 0
8989
assert len(csm_module_operators_keys['keys']) > 0
9090
assert len(csm_module_operators_keys['operators']) > 0
@@ -97,7 +97,7 @@ def test_get_used_module_operators_keys__csm_module__response_data_is_valid(
9797
for operator in csm_module_operators_keys['operators']:
9898
assert operator['index'] >= 0
9999
assert Web3.is_address(operator['rewardAddress'])
100-
assert operator['moduleAddress'] == variables.CSM_MODULE_ADDRESS
100+
assert operator['moduleAddress'] == variables.STAKING_MODULE_ADDRESS
101101

102102
def test_get_status__response_version_is_allowed(
103103
self,

0 commit comments

Comments
 (0)