def loop(self): if self.authorization_failed: return super(GetworkSource, self).loop() thread = Thread(target=self.long_poll_thread) thread.daemon = True thread.start() while True: if self.should_stop: return if self.check_failback(): return True try: with self.switch.lock: miner = self.switch.updatable_miner() while miner: work = self.getwork() self.queue_work(work, miner) miner = self.switch.updatable_miner() self.process_result_queue() sleep(1) except Exception: say_exception("Unexpected error:") break
def ensure_connected(self, connection, proto, host): if connection != None and connection.sock != None: return connection, False if proto == 'https': connector = httplib.HTTPSConnection else: connector = httplib.HTTPConnection if not self.options.proxy: return connector(host, strict=True), True host, port = host.split(':') proxy_proto, user, pwd, proxy_host = self.options.proxy[:4] proxy_port = 9050 proxy_host = proxy_host.split(':') if len(proxy_host) > 1: proxy_port = int(proxy_host[1]); proxy_host = proxy_host[0] connection = connector(host, strict=True) connection.sock = socks.socksocket() proxy_type = socks.PROXY_TYPE_SOCKS5 if proxy_proto == 'http': proxy_type = socks.PROXY_TYPE_HTTP elif proxy_proto == 'socks4': proxy_type = socks.PROXY_TYPE_SOCKS4 connection.sock.setproxy(proxy_type, proxy_host, proxy_port, True, user, pwd) try: connection.sock.connect((host, int(port))) except socks.Socks5AuthError: say_exception('Proxy error:') self.stop() return connection, True
def __init__(self, options): self.lock = RLock() self.miners = [] self.options = options self.last_work = 0 self.update_time = True self.max_update_time = options.max_update_time self.backup_server_index = 1 self.errors = 0 self.failback_attempt_count = 0 self.server_index = -1 self.last_server = None self.server_map = {} self.user_agent = 'poclbm/' + options.version self.difficulty = 0 self.true_target = None self.last_block = '' self.sent = {} if self.options.proxy: self.options.proxy = self.parse_server(self.options.proxy, False) self.servers = [] for server in self.options.servers: try: self.servers.append(self.parse_server(server)) except ValueError: if self.options.verbose: say_exception() say_line("Ignored invalid server entry: %s", server) continue
def loop(self): super(StratumSource, self).loop() self.switch.update_time = True while True: if self.should_stop: return if self.current_job: miner = self.switch.updatable_miner() while miner: self.current_job = self.refresh_job(self.current_job) self.queue_work(self.current_job, miner) miner = self.switch.updatable_miner() if self.check_failback(): return True if not self.handler: try: #socket = ssl.wrap_socket(socket) address, port = self.server().host.split(':', 1) if not self.options.proxy: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((address, int(port))) else: self.socket = socks.socksocket() p = self.options.proxy self.socket.setproxy(p.type, p.host, p.port, True, p.user, p.pwd) try: self.socket.connect((address, int(port))) except socks.Socks5AuthError: say_exception('Proxy error:') self.stop() self.handler = Handler(self.socket, self.channel_map, self) thread = Thread(target=self.asyncore_thread) thread.daemon = True thread.start() if not self.subscribe(): say_line('Failed to subscribe') self.stop() elif not self.authorize(): self.stop() elif not self.suggest_difficulty(128): say_line('Failed to suggest_difficulty') except socket.error: say_exception() self.stop() continue with self.send_lock: self.process_result_queue() sleep(1)
def tokenize(option, name, default=[0], cast=int): if option: try: return [cast(x) for x in option.split(',')] except ValueError: say_exception('Invalid %s(s) specified: %s\n\n' % (name, option)) sys.exit() return default
def loop(self): super(StratumSource, self).loop() self.switch.update_time = True while True: if self.should_stop: return if self.current_job: miner = self.switch.updatable_miner() while miner: self.current_job = self.refresh_job(self.current_job) self.queue_work(self.current_job, miner) miner = self.switch.updatable_miner() if self.check_failback(): return True if not self.handler: try: #socket = ssl.wrap_socket(socket) address, port = self.server().host.split(':', 1) if not self.options.proxy: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((address, int(port))) else: self.socket = socks.socksocket() p = self.options.proxy self.socket.setproxy(p.type, p.host, p.port, True, p.user, p.pwd) try: self.socket.connect((address, int(port))) except socks.Socks5AuthError: say_exception('Proxy error:') self.stop() self.handler = Handler(self.socket, self.channel_map, self) thread = Thread(target=self.asyncore_thread) thread.daemon = True thread.start() if not self.subscribe(): say_line('Failed to subscribe') self.stop() elif not self.authorize(): self.stop() except socket.error: say_exception() self.stop() continue with self.send_lock: self.process_result_queue() sleep(1)
def getwork(self, data=None): try: self.connection = self.ensure_connected(self.connection, self.server().proto, self.server().host)[0] self.postdata['params'] = [data] if data else [] (self.connection, result) = self.request(self.connection, '/', self.headers, dumps(self.postdata)) self.switch.connection_ok() return result['result'] except (IOError, httplib.HTTPException, ValueError, socks.ProxyError, NotAuthorized, RPCError): self.stop() except Exception: say_exception()
def check(port, likely=True): result = False try: device = open_device(port) response = init_device(device) device.close() result = is_good_init(response) except SerialException: if likely: say_exception() if not likely and result: say_line('Found BitFORCE on %s', port) elif likely and not result: say_line('No valid response from BitFORCE on %s', port) return result
def long_poll_thread(self): last_host = None while True: if self.should_stop or self.authorization_failed: return url = self.long_poll_url if url != '': proto = self.server().proto host = self.server().host parsedUrl = urlsplit(url) if parsedUrl.scheme != '': proto = parsedUrl.scheme if parsedUrl.netloc != '': host = parsedUrl.netloc url = url[url.find(host) + len(host):] if url == '': url = '/' try: if host != last_host: self.close_lp_connection() self.lp_connection, changed = self.ensure_connected( self.lp_connection, proto, host) if changed: say_line("LP connected to %s", self.server().name) last_host = host self.long_poll_active = True response = self.request(self.lp_connection, url, self.headers, timeout=self.long_poll_timeout) self.long_poll_active = False if response: (self.lp_connection, result) = response self.queue_work(result['result']) if self.options.verbose: say_line('long poll: new block %s%s', (result['result']['data'][56:64], result['result']['data'][48:56])) except (IOError, httplib.HTTPException, ValueError, socks.ProxyError, NotAuthorized, RPCError): say_exception('long poll IO error') self.close_lp_connection() sleep(.5) except Exception: say_exception()
def send_message(self, message): data = dumps(message) + '\n' try: #self.handler.push(data) #there is some bug with asyncore's send mechanism #so we send data 'manually' #note that this is not thread safe if not self.handler: return False while data: sent = self.handler.send(data) data = data[sent:] return True except AttributeError: self.stop() except Exception: say_exception() self.stop()
def ensure_connected(self, connection, proto, host): if connection != None and connection.sock != None: return connection, False if proto == 'https': connector = httplib.HTTPSConnection else: connector = httplib.HTTPConnection if not self.options.proxy: return connector(host, strict=True), True host, port = host.split(':') connection = connector(host, strict=True) connection.sock = socks.socksocket() p = self.options.proxy connection.sock.setproxy(p.type, p.host, p.port, True, p.user, p.pwd) try: connection.sock.connect((host, int(port))) except socks.Socks5AuthError: say_exception('Proxy error:') self.stop() return connection, True
def long_poll_thread(self): last_host = None while True: if self.should_stop or self.authorization_failed: return url = self.long_poll_url if url != '': proto = self.server().proto host = self.server().host parsedUrl = urlsplit(url) if parsedUrl.scheme != '': proto = parsedUrl.scheme if parsedUrl.netloc != '': host = parsedUrl.netloc url = url[url.find(host) + len(host):] if url == '': url = '/' try: if host != last_host: self.close_lp_connection() self.lp_connection, changed = self.ensure_connected(self.lp_connection, proto, host) if changed: say_line("LP connected to %s", self.server().name) last_host = host self.long_poll_active = True response = self.request(self.lp_connection, url, self.headers, timeout=self.long_poll_timeout) self.long_poll_active = False if response: (self.lp_connection, result) = response self.queue_work(result['result']) if self.options.verbose: say_line('long poll: new block %s%s', (result['result']['data'][56:64], result['result']['data'][48:56])) except (IOError, httplib.HTTPException, ValueError, socks.ProxyError, NotAuthorized, RPCError): say_exception('long poll IO error') self.close_lp_connection() sleep(.5) except Exception: say_exception()
def ensure_connected(self, connection, proto, host): if connection != None and connection.sock != None: return connection, False if proto == 'https': connector = httplib.HTTPSConnection else: connector = httplib.HTTPConnection if not self.options.proxy: return connector(host, strict=True), True host, port = host.split(':') proxy_proto, user, pwd, proxy_host = self.options.proxy[:4] proxy_port = 9050 proxy_host = proxy_host.split(':') if len(proxy_host) > 1: proxy_port = int(proxy_host[1]) proxy_host = proxy_host[0] connection = connector(host, strict=True) connection.sock = socks.socksocket() proxy_type = socks.PROXY_TYPE_SOCKS5 if proxy_proto == 'http': proxy_type = socks.PROXY_TYPE_HTTP elif proxy_proto == 'socks4': proxy_type = socks.PROXY_TYPE_SOCKS4 connection.sock.setproxy(proxy_type, proxy_host, proxy_port, True, user, pwd) try: connection.sock.connect((host, int(port))) except socks.Socks5AuthError: say_exception('Proxy error:') self.stop() return connection, True
def handle_error(self): print('Handler.handle_error') say_exception() self.parent.stop()
def mining_thread(self): say_line('started BFL miner on %s', (self.id())) while not self.should_stop: try: self.device = open_device(self.port) response = init_device(self.device) if not is_good_init(response): say_line('Failed to initialize %s (response: %s), retrying...', (self.id(), response)) self.device.close() self.device = None sleep(1) continue last_rated = time() iterations = 0 self.job = None self.busy = False while not self.should_stop: if (not self.job) or (not self.work_queue.empty()): try: self.job = self.work_queue.get(True, 1) except Empty: if not self.busy: continue else: if not self.job and not self.busy: continue targetQ = self.job.targetQ self.job.original_time = self.job.time self.job.time_delta = np.uint32(time()) - self.job.time.byteswap() if not self.busy: self.put_job() else: result = self.check_result() if result: now = time() self.busy = False r = self.last_job job_duration = now - self.job_started self.put_job() self.min_interval = min(self.min_interval, job_duration) iterations += 4294967296 t = now - last_rated if t > self.options.rate: self.update_rate(now, iterations, t, targetQ) last_rated = now; iterations = 0 if result != b'NO-NONCE\n': r.nonces = result self.switch.put(r) sleep(self.min_interval - (CHECK_INTERVAL * 2)) else: if result is None: self.check_interval = min(self.check_interval * 2, 1) sleep(self.check_interval) except Exception: say_exception() if self.device: self.device.close() self.device = None sleep(1)
def handle_error(self): say_exception() self.parent.stop()
def loop(self): super(StratumSource, self).loop() self.switch.update_time = True while True: if self.should_stop: return if self.current_job: miner = self.switch.updatable_miner() while miner: self.current_job = self.refresh_job(self.current_job) self.queue_work(self.current_job, miner) miner = self.switch.updatable_miner() if self.check_failback(): return True if not self.handler: try: #socket = ssl.wrap_socket(socket) address, port = self.server().host.split(':', 1) if not self.options.proxy: self.socket = socket.nodelay_socket( socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((address, int(port))) else: proxy_proto, user, pwd, proxy_host = self.options.proxy[: 4] proxy_port = 9050 proxy_host = proxy_host.split(':') if len(proxy_host) > 1: proxy_port = int(proxy_host[1]) proxy_host = proxy_host[0] self.socket = socks.socksocket() proxy_type = socks.PROXY_TYPE_SOCKS5 if proxy_proto == 'http': proxy_type = socks.PROXY_TYPE_HTTP elif proxy_proto == 'socks4': proxy_type = socks.PROXY_TYPE_SOCKS4 self.socket.setproxy(proxy_type, proxy_host, proxy_port, True, user, pwd) try: self.socket.connect((address, int(port))) except socks.Socks5AuthError: say_exception('Proxy error:') self.stop() self.handler = Handler(self.socket, self.channel_map, self) thread = Thread(target=self.asyncore_thread) thread.daemon = True thread.start() if not self.subscribe(): say_line('Failed to subscribe') self.stop() elif not self.authorize(): self.stop() except socket.error: say_exception() self.stop() continue with self.send_lock: self.process_result_queue() sleep(1)