X-Git-Url: https://git.novaco.in/?p=stratum-mining.git;a=blobdiff_plain;f=mining%2Fsubscription.py;h=3eea5ccb39fe8906af70dbdf4c246016d0201302;hp=d04c62eb9c0c0a796bf0a359c55fc26062ffdf33;hb=679245b565edcb2a03d865df3c08e86c0d6f402d;hpb=8a7474eb0eefdbc68e9b57371a68c25f33427f0d diff --git a/mining/subscription.py b/mining/subscription.py index d04c62e..3eea5cc 100644 --- a/mining/subscription.py +++ b/mining/subscription.py @@ -7,48 +7,48 @@ log = stratum.logger.get_logger('subscription') class MiningSubscription(Subscription): '''This subscription object implements logic for broadcasting new jobs to the clients.''' - + event = 'mining.notify' - + @classmethod def on_template(cls, is_new_block): '''This is called when TemplateRegistry registers new block which we have to broadcast clients.''' - + start = Interfaces.timestamper.time() - + clean_jobs = is_new_block (job_id, prevhash, coinb1, coinb2, merkle_branch, version, nbits, ntime, _) = \ Interfaces.template_registry.get_last_broadcast_args() - + # Push new job to subscribed clients cls.emit(job_id, prevhash, coinb1, coinb2, merkle_branch, version, nbits, ntime, clean_jobs) - + cnt = Pubsub.get_subscription_count(cls.event) log.info("BROADCASTED to %d connections in %.03f sec" % (cnt, (Interfaces.timestamper.time() - start))) - + def _finish_after_subscribe(self, result): '''Send new job to newly subscribed client''' - try: + try: (job_id, prevhash, coinb1, coinb2, merkle_branch, version, nbits, ntime, _) = \ Interfaces.template_registry.get_last_broadcast_args() except Exception: log.error("Template not ready yet") return result - + # Force set higher difficulty # TODO #self.connection_ref().rpc('mining.set_difficulty', [2,], is_notification=True) #self.connection_ref().rpc('client.get_version', []) - + # Force client to remove previous jobs if any (eg. from previous connection) clean_jobs = True self.emit_single(job_id, prevhash, coinb1, coinb2, merkle_branch, version, nbits, ntime, True) - + return result - + def after_subscribe(self, *args): '''This will send new job to the client *after* he receive subscription details. on_finish callback solve the issue that job is broadcasted *during* the subscription request and client receive messages in wrong order.''' - self.connection_ref().on_finish.addCallback(self._finish_after_subscribe) \ No newline at end of file + self.connection_ref().on_finish.addCallback(self._finish_after_subscribe)