Skip to content

Commit 8737024

Browse files
feat(csm-v2): fix infinite recursion
1 parent b9f3364 commit 8737024

File tree

1 file changed

+73
-58
lines changed

1 file changed

+73
-58
lines changed

src/web3py/extensions/csm.py

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class CSM(Module):
3333
module: CSModuleContract
3434
params: CSParametersRegistryContract
3535

36+
CONTRACT_LOAD_MAX_RETRIES: int = 100
37+
CONTRACT_LOAD_RETRY_DELAY: int = 60
38+
3639
def __init__(self, w3: Web3) -> None:
3740
super().__init__(w3)
3841
self._load_contracts()
@@ -69,64 +72,76 @@ def get_curve_params(self, no_id: NodeOperatorId, blockstamp: BlockStamp) -> Cur
6972
return CurveParams(perf_coeffs, perf_leeway_data, reward_share_data, strikes_params)
7073

7174
def _load_contracts(self) -> None:
72-
try:
73-
self.module = cast(
74-
CSModuleContract,
75-
self.w3.eth.contract(
76-
address=variables.CSM_MODULE_ADDRESS, # type: ignore
77-
ContractFactoryClass=CSModuleContract,
78-
decode_tuples=True,
79-
),
80-
)
81-
82-
self.params = cast(
83-
CSParametersRegistryContract,
84-
self.w3.eth.contract(
85-
address=self.module.parameters_registry(),
86-
ContractFactoryClass=CSParametersRegistryContract,
87-
decode_tuples=True,
88-
),
89-
)
90-
91-
self.accounting = cast(
92-
CSAccountingContract,
93-
self.w3.eth.contract(
94-
address=self.module.accounting(),
95-
ContractFactoryClass=CSAccountingContract,
96-
decode_tuples=True,
97-
),
98-
)
99-
100-
self.fee_distributor = cast(
101-
CSFeeDistributorContract,
102-
self.w3.eth.contract(
103-
address=self.accounting.fee_distributor(),
104-
ContractFactoryClass=CSFeeDistributorContract,
105-
decode_tuples=True,
106-
),
107-
)
108-
109-
self.oracle = cast(
110-
CSFeeOracleContract,
111-
self.w3.eth.contract(
112-
address=self.fee_distributor.oracle(),
113-
ContractFactoryClass=CSFeeOracleContract,
114-
decode_tuples=True,
115-
),
116-
)
117-
118-
self.strikes = cast(
119-
CSStrikesContract,
120-
self.w3.eth.contract(
121-
address=self.oracle.strikes(),
122-
ContractFactoryClass=CSStrikesContract,
123-
decode_tuples=True,
124-
),
125-
)
126-
except Web3Exception as ex:
127-
logger.error({"msg": "Some of the contracts aren't healthy", "error": str(ex)})
128-
sleep(60)
129-
self._load_contracts()
75+
last_error = None
76+
77+
for attempt in range(self.CONTRACT_LOAD_MAX_RETRIES):
78+
try:
79+
self.module = cast(
80+
CSModuleContract,
81+
self.w3.eth.contract(
82+
address=variables.CSM_MODULE_ADDRESS, # type: ignore
83+
ContractFactoryClass=CSModuleContract,
84+
decode_tuples=True,
85+
),
86+
)
87+
88+
self.params = cast(
89+
CSParametersRegistryContract,
90+
self.w3.eth.contract(
91+
address=self.module.parameters_registry(),
92+
ContractFactoryClass=CSParametersRegistryContract,
93+
decode_tuples=True,
94+
),
95+
)
96+
97+
self.accounting = cast(
98+
CSAccountingContract,
99+
self.w3.eth.contract(
100+
address=self.module.accounting(),
101+
ContractFactoryClass=CSAccountingContract,
102+
decode_tuples=True,
103+
),
104+
)
105+
106+
self.fee_distributor = cast(
107+
CSFeeDistributorContract,
108+
self.w3.eth.contract(
109+
address=self.accounting.fee_distributor(),
110+
ContractFactoryClass=CSFeeDistributorContract,
111+
decode_tuples=True,
112+
),
113+
)
114+
115+
self.oracle = cast(
116+
CSFeeOracleContract,
117+
self.w3.eth.contract(
118+
address=self.fee_distributor.oracle(),
119+
ContractFactoryClass=CSFeeOracleContract,
120+
decode_tuples=True,
121+
),
122+
)
123+
124+
self.strikes = cast(
125+
CSStrikesContract,
126+
self.w3.eth.contract(
127+
address=self.oracle.strikes(),
128+
ContractFactoryClass=CSStrikesContract,
129+
decode_tuples=True,
130+
),
131+
)
132+
return
133+
except Web3Exception as e:
134+
last_error = e
135+
logger.error({
136+
"msg": f"Attempt {attempt + 1}/{self.CONTRACT_LOAD_MAX_RETRIES} failed to load contracts",
137+
"error": str(e)
138+
})
139+
sleep(self.CONTRACT_LOAD_RETRY_DELAY)
140+
else:
141+
raise Web3Exception(
142+
f"Failed to load contracts in CSM module "
143+
f"after {self.CONTRACT_LOAD_MAX_RETRIES} attempts"
144+
) from last_error
130145

131146

132147
class LazyCSM(CSM):

0 commit comments

Comments
 (0)