remove dangling whitespace
[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 ShareLimiterInterface(object):
23     '''Implement difficulty adjustments here'''
24
25     def submit(self, connection_ref, current_difficulty, timestamp):
26         '''connection - weak reference to Protocol instance
27            current_difficulty - difficulty of the connection
28            timestamp - submission time of current share
29
30            - raise SubmitException for stop processing this request
31            - call mining.set_difficulty on connection to adjust the difficulty'''
32         pass
33
34 class ShareManagerInterface(object):
35     def __init__(self):
36         # Fire deferred when manager is ready
37         self.on_load = defer.Deferred()
38         self.on_load.callback(True)
39
40     def on_network_block(self, prevhash):
41         '''Prints when there's new block coming from the network (possibly new round)'''
42         pass
43
44     def on_submit_share(self, worker_name, block_header, block_hash, shares, timestamp, is_valid):
45         log.info("%s %s %s" % (block_hash, 'valid' if is_valid else 'INVALID', worker_name))
46
47     def on_submit_block(self, is_accepted, worker_name, block_header, block_hash, timestamp):
48         log.info("Block %s %s" % (block_hash, 'ACCEPTED' if is_accepted else 'REJECTED'))
49
50 class TimestamperInterface(object):
51     '''This is the only source for current time in the application.
52     Override this for generating unix timestamp in different way.'''
53     def time(self):
54         return time.time()
55
56 class PredictableTimestamperInterface(TimestamperInterface):
57     '''Predictable timestamper may be useful for unit testing.'''
58     start_time = 1345678900 # Some day in year 2012
59     delta = 0
60
61     def time(self):
62         self.delta += 1
63         return self.start_time + self.delta
64
65 class Interfaces(object):
66     worker_manager = None
67     share_manager = None
68     share_limiter = None
69     timestamper = None
70     template_registry = None
71
72     @classmethod
73     def set_worker_manager(cls, manager):
74         cls.worker_manager = manager
75
76     @classmethod
77     def set_share_manager(cls, manager):
78         cls.share_manager = manager
79
80     @classmethod
81     def set_share_limiter(cls, limiter):
82         cls.share_limiter = limiter
83
84     @classmethod
85     def set_timestamper(cls, manager):
86         cls.timestamper = manager
87
88     @classmethod
89     def set_template_registry(cls, registry):
90         cls.template_registry = registry