@@ -9,8 +9,8 @@ class Access(c.Module):
9
9
10
10
def __init__ (self ,
11
11
module : Union [c .Module , str ] = None , # the module or any python object
12
- network : str = 'subspace:main ' , # mainnet
13
- netuid : int = 0 , # subnet id
12
+ network : str = 'subspace' , # mainnet
13
+ netuid : int = 'all' , # subnet id
14
14
timescale :str = 'min' , # 'sec', 'min', 'hour', 'day'
15
15
stake2rate : int = 100.0 , # 1 call per every N tokens staked per timescale
16
16
max_rate : int = 1000.0 , # 1 call per every N tokens staked per timescale
@@ -25,84 +25,79 @@ def __init__(self,
25
25
26
26
self .set_config (locals ())
27
27
self .user_module = c .module ("user" )()
28
- self .address2key = c .address2key ()
29
- self .set_module (module )
30
28
self .state_path = state_path
31
29
if refresh :
32
30
self .rm_state ()
33
-
34
31
self .last_time_synced = c .time ()
35
32
self .state = {'sync_time' : 0 ,
36
33
'stake_from' : {},
37
34
'role2rate' : role2rate ,
38
35
'fn_info' : {}}
36
+
37
+ self .set_module (module )
39
38
40
39
c .thread (self .run_loop )
41
40
42
-
43
- def set_module (self , module : c .Module ):
44
- module = module or c .module ('module' )()
41
+ def set_module (self , module ):
45
42
if isinstance (module , str ):
46
43
module = c .module (module )()
47
44
self .module = module
48
- c .print (f'🚀 Access module set to { module } 🚀\033 ' , color = 'yellow' )
49
- self .whitelist = list (set (self .module .whitelist + c .whitelist ))
50
- self .blacklist = list (set (self .module .blacklist + c .blacklist ))
45
+ return module
51
46
52
- return {'success' : True , 'msg' : f'set module to { module } ' }
53
47
54
48
def run_loop (self ):
55
49
while True :
56
50
try :
57
51
r = self .sync_network ()
58
52
except Exception as e :
59
53
r = c .detailed_error (e )
54
+ c .print (r )
60
55
c .sleep (self .config .sync_interval )
61
56
57
+
62
58
def sync_network (self , update = False , max_age = None ):
63
59
state = self .get (self .state_path , {}, max_age = self .config .sync_interval )
64
60
time_since_sync = c .time () - state .get ('sync_time' , 0 )
65
61
self .key2address = c .key2address ()
66
62
self .address2key = c .address2key ()
67
- if time_since_sync > self .config .sync_interval :
68
- if 'subspace:' in self .config .network :
69
- self .config .network = self .config .network .replace ('subspace:' , '' )
70
- self .subspace = c .module ('subspace' )(network = self .config .network )
71
- max_age = max_age or self .config .max_age
72
- state ['stakes' ] = self .subspace .stakes (fmt = 'j' , netuid = 'all' , update = False , max_age = max_age )
73
- self .state = state
74
- self .put (self .state_path , self .state )
75
- c .print (f'🔄 Synced { self .state_path } 🔄\033 ' , color = 'yellow' )
76
-
77
- response = {'success' : True ,
78
- 'msg' : f'synced { self .state_path } ' ,
63
+ response = {'msg' : f'synced { self .state_path } ' ,
79
64
'until_sync' : int (self .config .sync_interval - time_since_sync ),
80
65
'time_since_sync' : int (time_since_sync )}
66
+
67
+ if time_since_sync < self .config .sync_interval :
68
+ response ['msg' ] = 'synced too earlly'
69
+ return response
70
+
71
+ self .subspace = c .module ('subspace' )(network = self .config .network )
72
+ max_age = max_age or self .config .max_age
73
+ state ['stakes' ] = self .subspace .stakes (fmt = 'j' , netuid = self .config .netuid , update = update , max_age = max_age )
74
+ self .state = state
75
+ self .put (self .state_path , self .state )
76
+ c .print (f'🔄 Synced { self .state_path } at { c .datetime ()} 🔄\033 ' , color = 'yellow' )
77
+
78
+
81
79
return response
82
80
83
- def verify (self ,
84
- address = '5FNBuR2yVf4A1v5nt3w5oi4ScorraGRjiSVzkXBVEsPHaGq1' ,
85
- fn : str = 'info' ,
86
- input :dict = None ) -> dict :
81
+ def forward (self , fn : str = 'info' , input :dict = None , address = None ) -> dict :
87
82
"""
88
83
input : dict
89
84
fn : str
90
85
address : str
91
86
92
87
returns : dict
93
88
"""
94
- if input is not None :
95
- address = input .get ('address' , address )
96
- fn = input .get ('fn' , fn )
89
+ input = input or {}
90
+ address = input .get ('address' , address )
91
+ assert address , f'address not in input or as an argument'
92
+ fn = input .get ('fn' , fn )
97
93
98
94
# ONLY THE ADMIN CAN CALL ANY FUNCTION, THIS IS A SECURITY FEATURE
99
95
# THE ADMIN KEYS ARE STORED IN THE CONFIG
100
96
if c .is_admin (address ):
101
97
return {'success' : True , 'msg' : f'is verified admin' }
102
-
103
98
104
- assert fn in self .whitelist , f"Function { fn } not in whitelist={ self .whitelist } "
105
- assert fn not in self .blacklist , f"Function { fn } is blacklisted={ self .blacklist } "
99
+ assert fn in self .module . whitelist , f"Function { fn } not in whitelist={ self . module .whitelist } "
100
+ assert fn not in self .module . blacklist , f"Function { fn } is blacklisted={ self . module .blacklist } "
106
101
107
102
if address in self .address2key :
108
103
return {'success' : True , 'msg' : f'address { address } is a local key' }
@@ -142,7 +137,6 @@ def verify(self,
142
137
143
138
rate_limit = (total_stake_score / stake2rate ) # convert the stake to a rate
144
139
145
-
146
140
# STEP 3: CHECK THE MAX RATE
147
141
max_rate = fn2info .get ('max_rate' , self .config .max_rate )
148
142
rate_limit = min (rate_limit , max_rate ) # cap the rate limit at the max rate
@@ -181,11 +175,7 @@ def verify(self,
181
175
self .state ['user_info' ][address ] = user_info
182
176
# check the rate limit
183
177
return user_info
184
-
185
- @classmethod
186
- def get_access_state (cls , module ):
187
- access_state = cls .get (module )
188
- return access_state
178
+ verify = forward
189
179
190
180
@classmethod
191
181
def test_whitelist (cls , key = 'vali::fam' , base_rate = 2 , fn = 'info' ):
@@ -194,7 +184,7 @@ def test_whitelist(cls, key='vali::fam', base_rate=2, fn='info'):
194
184
195
185
for i in range (base_rate * 3 ):
196
186
t1 = c .time ()
197
- result = module .verify (** {'address' : key .ss58_address , 'fn' : 'info' })
187
+ result = module .forward (** {'address' : key .ss58_address , 'fn' : 'info' })
198
188
t2 = c .time ()
199
189
c .print (f'🚨 { t2 - t1 } seconds... 🚨\033 ' , color = 'yellow' )
200
190
0 commit comments