88f5417fc6ab835382238379b0de55047b2a1fdf
[stratum-mining.git] / mining / interfaces.py
1 '''This module contains classes used by pool core to interact with the rest of the pool.
2    Default implementation do almost nothing, you probably want to override these classes
3    and customize references to interface instances in your launcher.
4    (see launcher_demo.tac for an example).
5 ''' 
6
7 import time
8 from twisted.internet import reactor, defer
9
10 import stratum.logger
11 log = stratum.logger.get_logger('interfaces')
12
13 class WorkerManagerInterface(object):
14     def __init__(self):
15         # Fire deferred when manager is ready
16         self.on_load = defer.Deferred()
17         self.on_load.callback(True)
18         
19     def authorize(self, worker_name, worker_password):
20         return True
21
22 class ShareManagerInterface(object):
23     def __init__(self):
24         # Fire deferred when manager is ready
25         self.on_load = defer.Deferred()
26         self.on_load.callback(True)
27
28     def on_submit_share(self, worker_name, block_header, block_hash, shares, timestamp, is_valid):
29         log.info("%s %s %s" % (block_hash, 'valid' if is_valid else 'INVALID', worker_name))
30     
31     def on_submit_block(self, worker_name, block_header, block_hash, timestamp, is_accepted):
32         log.info("Block %s %s" % (block_hash, 'ACCEPTED' if is_accepted else 'REJECTED'))
33     
34 class TimestamperInterface(object):
35     '''This is the only source for current time in the application.
36     Override this for generating unix timestamp in different way.'''
37     def time(self):
38         return time.time()
39
40 class PredictableTimestamperInterface(TimestamperInterface):
41     '''Predictable timestamper may be useful for unit testing.'''
42     start_time = 1345678900 # Some day in year 2012
43     delta = 0
44     
45     def time(self):
46         self.delta += 1
47         return self.start_time + self.delta
48         
49 class Interfaces(object):
50     worker_manager = None
51     share_manager = None
52     timestamper = None
53     template_registry = None
54     
55     @classmethod
56     def set_worker_manager(cls, manager):
57         cls.worker_manager = manager    
58     
59     @classmethod        
60     def set_share_manager(cls, manager):
61         cls.share_manager = manager
62     
63     @classmethod
64     def set_timestamper(cls, manager):
65         cls.timestamper = manager
66         
67     @classmethod
68     def set_template_registry(cls, registry):
69         cls.template_registry = registry