Skip to content

Commit a64def5

Browse files
committed
Add properties as well.
1 parent d80c1e6 commit a64def5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

async_substrate_interface/substrate_addons.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
"supports_rpc_method",
8282
]
8383

84+
RETRY_PROPS = ["properties", "version", "token_decimals", "token_symbol", "name"]
85+
8486

8587
class RetrySyncSubstrate(SubstrateInterface):
8688
def __init__(
@@ -132,6 +134,8 @@ def __init__(
132134
)
133135
for method in RETRY_METHODS:
134136
setattr(self, method, partial(self._retry, method))
137+
for property_ in RETRY_PROPS:
138+
setattr(self, property_, partial(self._retry_property, property_))
135139

136140
def _retry(self, method, *args, **kwargs):
137141
try:
@@ -148,6 +152,19 @@ def _retry(self, method, *args, **kwargs):
148152
)
149153
raise MaxRetriesExceeded
150154

155+
def _retry_property(self, property_):
156+
try:
157+
return getattr(self, property_)
158+
except (MaxRetriesExceeded, ConnectionError, ConnectionRefusedError) as e:
159+
try:
160+
self._reinstantiate_substrate(e)
161+
return self._retry_property(property_)
162+
except StopIteration:
163+
logging.error(
164+
f"Max retries exceeded with {self.url}. No more fallback chains."
165+
)
166+
raise MaxRetriesExceeded
167+
151168
def _reinstantiate_substrate(self, e: Optional[Exception] = None) -> None:
152169
next_network = next(self.fallback_chains)
153170
if e.__class__ == MaxRetriesExceeded:
@@ -207,6 +224,8 @@ def __init__(
207224
)
208225
for method in RETRY_METHODS:
209226
setattr(self, method, partial(self._retry, method))
227+
for property_ in RETRY_PROPS:
228+
setattr(self, property_, partial(self._retry_property, property_))
210229

211230
def _reinstantiate_substrate(self, e: Optional[Exception] = None) -> None:
212231
next_network = next(self.fallback_chains)
@@ -237,6 +256,7 @@ async def _retry(self, method, *args, **kwargs):
237256
except (MaxRetriesExceeded, ConnectionError, ConnectionRefusedError) as e:
238257
try:
239258
self._reinstantiate_substrate(e)
259+
await self.initialize()
240260
method_ = getattr(self, method)
241261
if asyncio.iscoroutinefunction(method_):
242262
return await method_(*args, **kwargs)
@@ -247,3 +267,16 @@ async def _retry(self, method, *args, **kwargs):
247267
f"Max retries exceeded with {self.url}. No more fallback chains."
248268
)
249269
raise MaxRetriesExceeded
270+
271+
async def _retry_property(self, property_):
272+
try:
273+
return await getattr(self, property_)
274+
except (MaxRetriesExceeded, ConnectionError, ConnectionRefusedError) as e:
275+
try:
276+
self._reinstantiate_substrate(e)
277+
return await self._retry_property(property_)
278+
except StopIteration:
279+
logging.error(
280+
f"Max retries exceeded with {self.url}. No more fallback chains."
281+
)
282+
raise MaxRetriesExceeded

0 commit comments

Comments
 (0)