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).
8 from twisted.internet import reactor, defer
11 log = stratum.logger.get_logger('interfaces')
13 class WorkerManagerInterface(object):
15 # Fire deferred when manager is ready
16 self.on_load = defer.Deferred()
17 self.on_load.callback(True)
19 def authorize(self, worker_name, worker_password):
22 class ShareLimiterInterface(object):
23 '''Implement difficulty adjustments here'''
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
30 - raise SubmitException for stop processing this request
31 - call mining.set_difficulty on connection to adjust the difficulty'''
34 class ShareManagerInterface(object):
36 # Fire deferred when manager is ready
37 self.on_load = defer.Deferred()
38 self.on_load.callback(True)
40 def on_network_block(self, prevhash):
41 '''Prints when there's new block coming from the network (possibly new round)'''
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))
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'))
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.'''
56 class PredictableTimestamperInterface(TimestamperInterface):
57 '''Predictable timestamper may be useful for unit testing.'''
58 start_time = 1345678900 # Some day in year 2012
63 return self.start_time + self.delta
65 class Interfaces(object):
70 template_registry = None
73 def set_worker_manager(cls, manager):
74 cls.worker_manager = manager
77 def set_share_manager(cls, manager):
78 cls.share_manager = manager
81 def set_share_limiter(cls, limiter):
82 cls.share_limiter = limiter
85 def set_timestamper(cls, manager):
86 cls.timestamper = manager
89 def set_template_registry(cls, registry):
90 cls.template_registry = registry