@@ -26,18 +26,22 @@ class NeuronReadOnlyDatabase(BaseDatabase):
2626 of modifying it.
2727 """
2828
29- def setup_neuron_models (self ):
29+ def __init__ (self , settings ):
30+ super ().__init__ (settings = settings )
31+
3032 # Register neuron models keyed by their version
31- self .models ["neuron" ] = {x .version : x for x in [NeuronModel210 (), NeuronModel211 ()]}
33+ self .models ["neuron" ] = {
34+ x .version : x for x in [NeuronModel210 (), NeuronModel211 ()]
35+ }
3236
3337 async def get_neuron (self , hotkey : str ) -> scmm .Neuron :
34- # Ensure the connection is up and running
38+ # Ensure Redis connection is established before any operation.
3539 await self .ensure_connection ()
3640
37- # Get a client
41+ # Get a connected Redis client, configured with the correct DB index and prefix.
3842 client = await self .get_client ()
3943
40- # Get the active versions
44+ # Get currently active versions of the "neuron" schema to use during read.
4145 _ , active = await self ._get_migration_status ("neuron" )
4246
4347 for version in reversed (active ):
@@ -63,13 +67,13 @@ async def get_neuron(self, hotkey: str) -> scmm.Neuron:
6367 return None
6468
6569 async def get_neurons (self ) -> typing .Dict [str , scmm .Neuron ]:
66- # Ensure the connection is up and running
70+ # Ensure Redis connection is established before any operation.
6771 await self .ensure_connection ()
6872
69- # Get a client
73+ # Get a connected Redis client, configured with the correct DB index and prefix.
7074 client = await self .get_client ()
7175
72- # Get the active versions
76+ # Get currently active versions of the "neuron" schema to use during read.
7377 _ , active = await self ._get_migration_status ("neuron" )
7478
7579 for version in reversed (active ):
@@ -98,10 +102,10 @@ async def get_neuron_last_updated(self):
98102 """
99103 Get the block of the last time the metagraph has been updated
100104 """
101- # Ensure the connection is up and running
105+ # Ensure Redis connection is established before any operation.
102106 await self .ensure_connection ()
103107
104- # Get a client
108+ # Get a connected Redis client, configured with the correct DB index and prefix.
105109 client = await self .get_client ()
106110
107111 try :
@@ -134,18 +138,14 @@ class NeuronDatabase(NeuronReadOnlyDatabase):
134138 Use this class in components responsible for managing or syncing neuron state.
135139 """
136140
137- def __init__ (self , settings ):
138- super ().__init__ (settings = settings )
139- self .setup_neuron_models ()
140-
141141 async def update_neurons (self , neurons : typing .List [scmm .Neuron ]):
142- # Ensure the connection is up and running
142+ # Ensure Redis connection is established before any operation.
143143 await self .ensure_connection ()
144144
145- # Get a client
145+ # Get a connected Redis client, configured with the correct DB index and prefix.
146146 client = await self .get_client ()
147147
148- # Get the active versions
148+ # Get currently active versions of the "neuron" schema to use during read.
149149 _ , active = await self ._get_migration_status ("neuron" )
150150
151151 for version in reversed (active ):
@@ -170,10 +170,10 @@ async def update_neurons(self, neurons: typing.List[scmm.Neuron]):
170170 return None
171171
172172 async def remove_neurons (self , neurons : list [Neuron ]):
173- # Ensure the connection is up and running
173+ # Ensure Redis connection is established before any operation.
174174 await self .ensure_connection ()
175175
176- # Get a client
176+ # Get a connected Redis client, configured with the correct DB index and prefix.
177177 client = await self .get_client ()
178178
179179 for version , model in self .models ["neuron" ].items ():
@@ -197,10 +197,10 @@ async def set_last_updated(self, block: int):
197197 """
198198 Set the block of the last time the metagraph has been updated
199199 """
200- # Ensure the connection is up and running
200+ # Ensure Redis connection is established before any operation.
201201 await self .ensure_connection ()
202202
203- # Get a client
203+ # Get a connected Redis client, configured with the correct DB index and prefix.
204204 client = await self .get_client ()
205205
206206 try :
@@ -225,20 +225,18 @@ async def mark_as_unready(self):
225225 await self ._set_state (state = "unready" )
226226
227227 async def notify_state (self ):
228- # Ensure the connection is up and running
228+ # Ensure Redis connection is established before any operation.
229229 await self .ensure_connection ()
230230
231- # Get a client
231+ # Get a connected Redis client, configured with the correct DB index and prefix.
232232 client = await self .get_client ()
233233
234234 try :
235235 # Get the current state of the metagraph
236236 state = await client .get (self ._key ("state:metagraph" ))
237237
238238 # Notify downstream via Redis stream
239- await client .xadd (
240- self ._key ("state:metagraph:stream" ), {"state" : state }
241- )
239+ await client .xadd (self ._key ("state:metagraph:stream" ), {"state" : state })
242240
243241 except Exception as ex :
244242 btul .logging .error (
@@ -255,10 +253,10 @@ def _key(self, key: str):
255253 return f"{ self .settings .key_prefix } :{ key } "
256254
257255 async def _set_state (self , state : str ):
258- # Ensure the connection is up and running
256+ # Ensure Redis connection is established before any operation.
259257 await self .ensure_connection ()
260258
261- # Get a client
259+ # Get a connected Redis client, configured with the correct DB index and prefix.
262260 client = await self .get_client ()
263261
264262 try :
0 commit comments