Ticket #18035: updateFallbackDirs.py.patch

File updateFallbackDirs.py.patch, 4.1 KB (added by teor, 4 years ago)

starlight.2015q3's patch for updateFallbackDirs.py

  • updateFallbackDirs.py

    old new  
    2020#from bson import json_util
    2121
    2222import logging
    23 logging.basicConfig(level=logging.INFO)
     23logging.basicConfig(level=logging.DEBUG)
    2424
    2525ONIONOO = 'https://onionoo.torproject.org/'
    2626
    2727ADDRESS_AND_PORT_STABLE_DAYS = 120
    2828# What time-weighted-fraction of these flags must FallbackDirs:
    2929# Equal or Exceed?
     30MAX_AGE_DAYS = 120
    3031CUTOFF_RUNNING = .95
    3132CUTOFF_V2DIR = .95
    3233CUTOFF_GUARD = .95
     
    350351        logging.warn('Inconsistent value count in %s document for %s'%(p, which,))
    351352      for v in reversed(h['values']):
    352353        if (this_ts <= newest):
     354          agt1 = now - this_ts
     355          agt2 = interval
     356          agetmp1 = (agt1.microseconds + (agt1.seconds + agt1.days * 24 * 3600) * 10**6) / 10**6
     357          agetmp2 = (agt2.microseconds + (agt2.seconds + agt2.days * 24 * 3600) * 10**6) / 10**6
    353358          generic_history.append(
    354             { 'age': (now - this_ts).total_seconds(),
    355               'length': interval.total_seconds(),
     359            { 'age': agetmp1,
     360              'length': agetmp2,
    356361              'value': v
    357362            })
    358363          newest = this_ts
     
    365370    return generic_history
    366371
    367372  @staticmethod
    368   def _avg_generic_history(generic_history):
     373  def _avg_generic_history(generic_history, max_age):
    369374    a = []
    370375    for i in generic_history:
     376      if i['age'] > max_age:
     377        continue
    371378      w = i['length'] * math.pow(AGE_ALPHA, i['age']/(3600*24))
    372       a.append( (i['value'] * w, w) )
     379      if i['value'] is not None:
     380        a.append( (i['value'] * w, w) )
    373381
    374382    sv = math.fsum(map(lambda x: x[0], a))
    375383    sw = math.fsum(map(lambda x: x[1], a))
    376 
    377     return sv/sw
     384    if sw == 0.0:
     385      svw = 0.0
     386    else:
     387      svw = sv/sw
     388    return svw
    378389
    379390  def _add_generic_history(self, history):
    380391    periods = r['read_history'].keys()
     
    398409        logging.debug('No %s in flags for %s.'%(f, self._fpr,))
    399410        return
    400411
     412    MAX_AGE = MAX_AGE_DAYS * 24 * 3600
    401413    running = self._extract_generic_history(uptime['flags']['Running'], '%s-Running'%(self._fpr,))
    402414    guard = self._extract_generic_history(uptime['flags']['Guard'], '%s-Guard'%(self._fpr,))
    403415    v2dir = self._extract_generic_history(uptime['flags']['V2Dir'], '%s-V2Dir'%(self._fpr,))
    404416    if 'BadExit' in uptime['flags']:
    405417      badexit = self._extract_generic_history(uptime['flags']['BadExit'], '%s-BadExit'%(self._fpr,))
    406418
    407     self._running = self._avg_generic_history(running) / ONIONOO_SCALE_ONE
    408     self._guard = self._avg_generic_history(guard) / ONIONOO_SCALE_ONE
    409     self._v2dir = self._avg_generic_history(v2dir) / ONIONOO_SCALE_ONE
     419    self._running = self._avg_generic_history(running, MAX_AGE) / ONIONOO_SCALE_ONE
     420    self._guard = self._avg_generic_history(guard, MAX_AGE) / ONIONOO_SCALE_ONE
     421    self._v2dir = self._avg_generic_history(v2dir, MAX_AGE) / ONIONOO_SCALE_ONE
    410422    self._badexit = None
    411423    if 'BadExit' in uptime['flags']:
    412       self._badexit = self._avg_generic_history(badexit) / ONIONOO_SCALE_ONE
     424      self._badexit = self._avg_generic_history(badexit, MAX_AGE) / ONIONOO_SCALE_ONE
    413425
    414426  def is_candidate(self):
    415427    if self._data['last_changed_address_or_port'] > self.CUTOFF_ADDRESS_AND_PORT_STABLE:
     
    419431    if self._running < CUTOFF_RUNNING:
    420432      logging.debug('%s not a candidate: running avg too low (%lf)', self._fpr, self._running)
    421433      return False
    422     if self._guard < CUTOFF_GUARD:
    423       logging.debug('%s not a candidate: guard avg too low (%lf)', self._fpr, self._guard)
    424       return False
    425434    if self._v2dir < CUTOFF_V2DIR:
    426435      logging.debug('%s not a candidate: v2dir avg too low (%lf)', self._fpr, self._v2dir)
    427436      return False
     
    431440    # if the relay doesn't report a version, also exclude the relay
    432441    if not self._data.has_key('recommended_version') or not self._data['recommended_version']:
    433442      return False
     443    if self._guard < CUTOFF_GUARD:
     444      logging.debug('%s not a candidate: guard avg too low (%lf)', self._fpr, self._guard)
     445      return False
    434446    return True
    435447
    436448  @staticmethod