Added check to submit() if worker is authorized
[stratum-mining.git] / mining / service.py
index f3a6861..5330662 100644 (file)
@@ -34,7 +34,18 @@ class MiningService(GenericService):
     
     def authorize(self, worker_name, worker_password):
         '''Let authorize worker on this connection.'''
-        return Interfaces.worker_manager.authorize(worker_name, worker_password)
+        
+        session = self.connection_ref().get_session()
+        session.setdefault('authorized', {})
+        
+        if Interfaces.worker_manager.authorize(worker_name, worker_password):
+            session['authorized'][worker_name] = worker_password
+            return True
+        
+        else:
+            if worker_name in session['authorized']:
+                del session['authorized'][worker_name]
+            return False
         
     def subscribe(self):
         '''Subscribe for receiving mining jobs. This will
@@ -70,6 +81,12 @@ class MiningService(GenericService):
         '''Try to solve block candidate using given parameters.'''
         
         session = self.connection_ref().get_session()
+        session.setdefault('authorized', {})
+        
+        # Check if worker is authorized to submit shares
+        if not Interfaces.worker_manager.authorize(worker_name,
+                        session['authorized'].get(worker_name)):
+            raise SubmitException("Worker is not authorized")
 
         # Check if extranonce1 is in connection session
         extranonce1_bin = session.get('extranonce1', None)
@@ -83,13 +100,10 @@ class MiningService(GenericService):
         (is_valid, reason, block_header, block_hash) = Interfaces.template_registry.submit_share(job_id,
                                                 worker_name, extranonce1_bin, extranonce2, ntime, nonce, difficulty,
                                                 Interfaces.share_manager.on_submit_block)
-        
-        if block_header != None:
-            # block header is missing when template registry was unable to build it
-            # from given parameters. Client side is probably broken, storing such
-            # submit don't have any sense.                   
-            Interfaces.share_manager.on_submit_share(worker_name, block_header, block_hash, difficulty,
-                                              Interfaces.timestamper.time(), is_valid)
+   
+        # block_header and block_hash may be None when submitted data are corrupted     
+        Interfaces.share_manager.on_submit_share(worker_name, block_header, block_hash, difficulty,
+                                                 Interfaces.timestamper.time(), is_valid)
         
         if not is_valid:
             raise SubmitException(reason)