<commit_before>#!/usr/bin/python
#License#
#bitHopper by <NAME> is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.

import os.path

from twisted.web import server, resource

class Scheduler(object):
   def __init__(self,bitHopper):
      self.bh = bitHopper
      self.initData()
   @classmethod
   def initData(self,):
      #self.bh.log_msg("<Scheduler> initData")
      return

   @classmethod
   def server_update(self,):
      return

   @classmethod
   def select_best_server(self,):
      return

   @classmethod
   def select_friendly_server(self):
      return

   @classmethod
   def select_backup_server(self,):
      return


class DefaultScheduler(Scheduler):
   def __init__(self,bitHopper):
      self.bh = bitHopper
      self.difficultyThreshold = 0.435
      self.initData()
      
   def initData(self,):
      if self.bh.options.threshold:
         self.bh.log_msg("Override difficulty threshold to: " + str(self.bh.options.threshold), cat='scheduler-default')
         self.difficultyThreshold = self.bh.options.threshold

   def select_best_server(self,):
      self.bh.log_dbg('select_best_server', cat='scheduler-default')
      server_name = None
      difficulty = self.bh.difficulty.get_difficulty()
      nmc_difficulty = self.bh.difficulty.get_nmc_difficulty()
      min_shares = difficulty * self.difficultyThreshold
        
      self.bh.log_dbg('min-shares: ' + str(min_shares), cat='scheduler-default')  
      for server in self.bh.pool.get_servers():
         info = self.bh.pool.get_entry(server)
         if info['role'] not in ['mine','mine_nmc','mine_slush','mine_friendly']:
            continue
         if info['role'] in ['mine', 'mine_friendly']:
            shares = info['shares']
         elif info['role'] == 'mine_slush':
            shares = info['shares'] * 4
         elif info['role'] == 'mine_nmc':
            shares = info['shares']*difficulty / nmc_difficulty
         else:
            shares = 100* info['shares']
         if shares< min_shares and info['lag'] == False:
            min_shares = shares
            self.bh.log_dbg('Selecting pool ' + str(server) + ' with shares ' + str(info['shares']), cat='scheduler-default')
            server_name = server
         
      if server_name == None:
         server_name = self.select_friendly_server()

      if server_name == None: return self.select_backup_server()
      else: return server_name   
   
   def select_friendly_server(self):
      server_name = None
      most_shares = self.bh.difficulty.get_difficulty() * 2
      for server in self.bh.pool.get_servers():
         info = self.bh.pool.get_entry(server)
         if info['role'] != 'mine_friendly':
            continue
         if info['shares'] > most_shares and info['lag'] == False:
            server_name = server
            most_shares = info['shares']
            self.bh.log_dbg('select_friendly_server: ' + str(server), cat='scheduler-default')
      
      return server_name
   
   def select_backup_server(self,):
      self.bh.log_dbg('select_backup_server', cat='scheduler-default')
      server_name = None
      reject_rate = 1
      for server in self.bh.pool.get_servers():
         info = self.bh.pool.get_entry(server)
         if info['role'] != 'backup':
            continue
         if info['lag'] == False:
            rr_server = float(info['rejects'])/(info['user_shares']+1)
            if  rr_server < reject_rate:
               server_name = server
               self.bh.log_dbg('select_backup_server: ' + str(server), cat='scheduler-default')
               reject_rate = rr_server

   
      if server_name == None:
         self.bh.log_dbg('Try another backup' + str(server), cat='scheduler-default')
         min_shares = 10**10
         for server in self.bh.pool.get_servers():
            info = self.bh.pool.get_entry(server)
            if info['role'] not in ['mine','mine_nmc','mine_slush']:
               continue
            if info['role'] == 'mine':
               shares = info['shares']
            elif info['role'] == 'mine_slush':
               shares = info['shares'] * 4
            elif info['role'] == 'mine_nmc':
               shares = info['shares']*difficulty / nmc_difficulty
            else:
               shares = info['shares']
            if shares < min_shares and info['lag'] == False:
               min_shares = shares
               self.bh.log_dbg('Selecting pool ' + str(server) + ' with shares ' + str(shares), cat='scheduler-default')
               server_name = server
      
      if server_name == None:
         self.bh.log_dbg('Try another backup pt2' + str(server), cat='scheduler-default')
         for server in self.bh.pool.get_servers():
            info = self.bh.pool.get_entry(server)
            if info['role'] != 'backup':
               continue
            server_name = server
            break
         
      return server_name


   def server_update(self,):
      self.bh.log_dbg('server_update', cat='scheduler-default')
      valid_roles = ['mine', 'mine_slush','mine_nmc']
      current_pool = self.bh.pool.get_entry(self.bh.pool.get_current())
      if current_pool['role'] not in valid_roles:
         return True

      current_role = current_pool['role']
      if current_role == 'mine' or current_role == 'mine_friendly':
         difficulty = self.bh.difficulty.get_difficulty()
      if current_role == 'mine_nmc':
         difficulty = self.bh.difficulty.get_nmc_difficulty()
      if current_role == 'mine_slush':
         difficulty = self.bh.difficulty.get_difficulty() * .25
      if current_pool['shares'] > (difficulty * self.difficultyThreshold):
         return True

      min_shares = 10**10

      for server in self.bh.pool.get_servers():
         pool = self.bh.pool.get_entry(server)
         if pool['shares'] < min_shares:
            min_shares = pool['shares']

      if min_shares < current_pool['shares']*.90:
        return True       
<commit_msg>"backup_latehop" scheduler implemented: hops to the "backup_latehop" pool that has the most shares. If not applicable (?) behaves like the default "backup". Hopefully useful for helping out Scored/PPLNS/*PPS pools with long rounds.<commit_after>#!/usr/bin/python
#License#
#bitHopper by <NAME> is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.

import os.path

from twisted.web import server, resource

class Scheduler(object):
   def __init__(self,bitHopper):
      self.bh = bitHopper
      self.initData()
   @classmethod
   def initData(self,):
      #self.bh.log_msg("<Scheduler> initData")
      return

   @classmethod
   def server_update(self,):
      return

   @classmethod
   def select_best_server(self,):
      return

   @classmethod
   def select_friendly_server(self):
      return

   @classmethod
   def select_backup_server(self,):
      return


class DefaultScheduler(Scheduler):
   def __init__(self,bitHopper):
      self.bh = bitHopper
      self.difficultyThreshold = 0.435
      self.initData()
      
   def initData(self,):
      if self.bh.options.threshold:
         self.bh.log_msg("Override difficulty threshold to: " + str(self.bh.options.threshold), cat='scheduler-default')
         self.difficultyThreshold = self.bh.options.threshold

   def select_best_server(self,):
      self.bh.log_dbg('select_best_server', cat='scheduler-default')
      server_name = None
      difficulty = self.bh.difficulty.get_difficulty()
      nmc_difficulty = self.bh.difficulty.get_nmc_difficulty()
      min_shares = difficulty * self.difficultyThreshold
        
      self.bh.log_dbg('min-shares: ' + str(min_shares), cat='scheduler-default')  
      for server in self.bh.pool.get_servers():
         info = self.bh.pool.get_entry(server)
         if info['role'] not in ['mine','mine_nmc','mine_slush','mine_friendly']:
            continue
         if info['role'] in ['mine', 'mine_friendly']:
            shares = info['shares']
         elif info['role'] == 'mine_slush':
            shares = info['shares'] * 4
         elif info['role'] == 'mine_nmc':
            shares = info['shares']*difficulty / nmc_difficulty
         else:
            shares = 100* info['shares']
         if shares< min_shares and info['lag'] == False:
            min_shares = shares
            self.bh.log_dbg('Selecting pool ' + str(server) + ' with shares ' + str(info['shares']), cat='scheduler-default')
            server_name = server
         
      if server_name == None:
         server_name = self.select_friendly_server()

      if server_name == None: return self.select_backup_server()
      else: return server_name   
   
   def select_friendly_server(self):
      server_name = None
      most_shares = self.bh.difficulty.get_difficulty() * 2
      for server in self.bh.pool.get_servers():
         info = self.bh.pool.get_entry(server)
         if info['role'] != 'mine_friendly':
            continue
         if info['shares'] > most_shares and info['lag'] == False:
            server_name = server
            most_shares = info['shares']
            self.bh.log_dbg('select_friendly_server: ' + str(server), cat='scheduler-default')
      
      return server_name
   
   def select_latehop_server(self):
      server_name = None
      max_share_count = 1
      for server in self.bh.pool.get_servers():
         info = self.bh.pool.get_entry(server)
         if info['role'] != 'backup_latehop':
            continue
         if info['shares'] > max_share_count and info['lag'] == False:
            server_name = server
            max_share_count = info['shares']
            self.bh.log_dbg('select_latehop_server: ' + str(server), cat='scheduler-default')

      return server_name   

   def select_backup_server(self,):
      self.bh.log_dbg('select_backup_server', cat='scheduler-default')
      server_name = None
      reject_rate = 1

      server_name = self.select_latehop_server()

      if server_name == None:
         for server in self.bh.pool.get_servers():
            info = self.bh.pool.get_entry(server)
            if info['role'] != 'backup':
               continue
            if info['lag'] == False:
               rr_server = float(info['rejects'])/(info['user_shares']+1)
               if  rr_server < reject_rate:
                  server_name = server
                  self.bh.log_dbg('select_backup_server: ' + str(server), cat='scheduler-default')
                  reject_rate = rr_server

      if server_name == None:
         self.bh.log_dbg('Try another backup' + str(server), cat='scheduler-default')
         min_shares = 10**10
         for server in self.bh.pool.get_servers():
            info = self.bh.pool.get_entry(server)
            if info['role'] not in ['mine','mine_nmc','mine_slush']:
               continue
            if info['role'] == 'mine':
               shares = info['shares']
            elif info['role'] == 'mine_slush':
               shares = info['shares'] * 4
            elif info['role'] == 'mine_nmc':
               shares = info['shares']*difficulty / nmc_difficulty
            else:
               shares = info['shares']
            if shares < min_shares and info['lag'] == False:
               min_shares = shares
               self.bh.log_dbg('Selecting pool ' + str(server) + ' with shares ' + str(shares), cat='scheduler-default')
               server_name = server
      
      if server_name == None:
         self.bh.log_dbg('Try another backup pt2' + str(server), cat='scheduler-default')
         for server in self.bh.pool.get_servers():
            info = self.bh.pool.get_entry(server)
            if info['role'] != 'backup':
               continue
            server_name = server
            break

      return server_name


   def server_update(self,):
      self.bh.log_dbg('server_update', cat='scheduler-default')
      valid_roles = ['mine', 'mine_slush','mine_nmc']
      current_pool = self.bh.pool.get_entry(self.bh.pool.get_current())
      if current_pool['role'] not in valid_roles:
         return True

      current_role = current_pool['role']
      if current_role == 'mine' or current_role == 'mine_friendly':
         difficulty = self.bh.difficulty.get_difficulty()
      if current_role == 'mine_nmc':
         difficulty = self.bh.difficulty.get_nmc_difficulty()
      if current_role == 'mine_slush':
         difficulty = self.bh.difficulty.get_difficulty() * .25
      if current_pool['shares'] > (difficulty * self.difficultyThreshold):
         return True

      min_shares = 10**10

      for server in self.bh.pool.get_servers():
         pool = self.bh.pool.get_entry(server)
         if pool['shares'] < min_shares:
            min_shares = pool['shares']

      if min_shares < current_pool['shares']*.90:
        return True       

{'fraction_non_alphanumeric': 0.12010524686581024, 'fraction_numerical': 0.004256307073208482, 'mean_word_length': 2.407065647244925, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '597461', 'n_tokens_mistral': 3977, 'n_tokens_neox': 3868, 'n_words': 802}