Initial commit
[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
9 import stratum.logger
10 log = stratum.logger.get_logger('interfaces')
11
12 class WorkerManagerInterface(object):
13     def authorize(self, worker_name, worker_password):
14         return True
15
16 class ShareManagerInterface(object):
17     def on_submit_share(self, worker_name, block_header, block_hash, shares, timestamp, is_valid):
18         log.info("%s %s %s" % ('Valid' if is_valid else 'INVALID', worker_name, block_hash))
19     
20     def on_submit_block(self, worker_name, block_header, block_hash, timestamp, is_accepted):
21         log.info("Block %s %s" % (block_hash, 'ACCEPTED' if is_accepted else 'REJECTED'))
22     
23 class TimestamperInterface(object):
24     '''This is the only source for current time in the application.
25     Override this for generating unix timestamp in different way.'''
26     def time(self):
27         return time.time()
28
29 class PredictableTimestamperInterface(TimestamperInterface):
30     '''Predictable timestamper may be useful for unit testing.'''
31     start_time = 1345678900 # Some day in year 2012
32     delta = 0
33     
34     def time(self):
35         self.delta += 1
36         return self.start_time + self.delta
37         
38 class Interfaces(object):
39     worker_manager = None
40     share_manager = None
41     timestamper = None
42     template_registry = None
43     
44     @classmethod
45     def set_worker_manager(cls, manager):
46         cls.worker_manager = manager    
47     
48     @classmethod        
49     def set_share_manager(cls, manager):
50         cls.share_manager = manager
51     
52     @classmethod
53     def set_timestamper(cls, manager):
54         cls.timestamper = manager
55         
56     @classmethod
57     def set_template_registry(cls, registry):
58         cls.template_registry = registry