Commit f93a11ca authored by juga  's avatar juga Committed by Georg Koppen
Browse files

fix: v3bwfile: network means without relay type

This reverts commit fc3d3b99 and partialy
reverts a82c2618.

It was a mistake to think torflow was using the means by relay type,
it actually sets the same networks means for all relay types.

Closes #40080.
parent 888fb6e8
......@@ -189,14 +189,6 @@ MAX_RECENT_PRIORITY_RELAY_COUNT = (
MAX_RECENT_PRIORITY_LIST_COUNT * MAX_RELAYS_PER_PRIORITY_LIST
)
# Used by util/stem.py
G = 0
M = 1
E = 2
GE = 3
# Used by lib/scaling.py to calculate network means by relay type
RELAY_TYPES = [G, M, E, GE]
def fail_hard(*a, **kw):
""" Log something ... and then exit as fast as possible """
......
from statistics import mean
from sbws.globals import RELAY_TYPES
from sbws.util.stem import rs_relay_type
def bw_measurements_from_results(results):
return [
......@@ -30,23 +27,3 @@ def bw_filt(bw_measurements):
if bws_gte_mean:
return round(mean(bws_gte_mean))
return mu
def network_means_by_relay_type(bw_lines, router_statuses_d):
# Temporarily assign the type of relay to calculate network stream and
# filtered bandwidth by type
for line in bw_lines:
rs = None
if router_statuses_d:
rs = router_statuses_d.get(line.node_id.replace("$", ""), None)
line.set_relay_type(rs_relay_type(rs))
mu_type = muf_type = {}
for rt in RELAY_TYPES:
bw_lines_type = [line for line in bw_lines if line.relay_type == rt]
if len(bw_lines_type) > 0:
# Torflow does not round these values.
# Ensure they won't be 0 to avoid division by 0 Exception
mu_type[rt] = mean([line.bw_mean for line in bw_lines_type]) or 1
muf_type[rt] = mean([line.bw_filt for line in bw_lines_type]) or 1
return mu_type, muf_type
......@@ -1068,12 +1068,6 @@ class V3BWLine(object):
)
return bw_line_str
def set_relay_type(self, relay_type):
self.relay_type = relay_type
def del_relay_type(self):
delattr(self, "relay_type")
class V3BWFile(object):
"""
......@@ -1354,11 +1348,12 @@ class V3BWFile(object):
"""
log.info("Calculating relays' bandwidth using Torflow method.")
bw_lines_tf = copy.deepcopy(bw_lines)
mu_type, muf_type = scaling.network_means_by_relay_type(
bw_lines_tf, router_statuses_d
)
log.debug("mu %s", mu_type)
log.debug("muf %s", muf_type)
# mean (Torflow's strm_avg)
mu = mean([l.bw_mean for l in bw_lines])
# filtered mean (Torflow's filt_avg)
muf = mean([l.bw_filt for l in bw_lines])
log.debug("mu %s", mu)
log.debug("muf %s", muf)
# Torflow's ``tot_net_bw``, sum of the scaled bandwidth for the relays
# that are in the last consensus
......@@ -1423,12 +1418,10 @@ class V3BWFile(object):
continue
# Torflow's scaling
# relay_type is set in `network_means_by_relay_type` in the lines
# above
ratio_stream = l.bw_mean / mu_type[l.relay_type]
ratio_stream_filtered = l.bw_filt / muf_type[l.relay_type]
l.del_relay_type()
ratio_stream = l.bw_mean / mu
ratio_stream_filtered = l.bw_filt / muf
ratio = max(ratio_stream, ratio_stream_filtered)
# Assign it to an attribute, so it's not lost before capping and
# rounding
l.bw = ratio * min_bandwidth
......
......@@ -7,7 +7,6 @@ import socks
import stem.process
from stem import (
ControllerError,
Flag,
InvalidArguments,
InvalidRequest,
OperationFailed,
......@@ -21,13 +20,9 @@ from stem.control import Controller, Listener
from sbws import settings
from sbws.globals import (
GE,
TORRC_OPTIONS_CAN_FAIL,
TORRC_RUNTIME_OPTIONS,
TORRC_STARTING_POINT,
E,
G,
M,
fail_hard,
)
......@@ -368,21 +363,3 @@ def is_torrc_starting_point_set(tor_controller):
if not bad_options:
log.info("Tor is correctly configured to work with sbws.")
return bad_options
def rs_relay_type(rs):
# In torflow, the equivalent to the bw_lines is initialized to "", so when
# the relay is not in the previous consensus and it is not known which
# flags it has, it would return "Medium", as it's the fail case in
# Node.node_class().
# It is not known if it is a bug, or a desired side effect that they relays
# not in the consensus will end up in the Middle class
if not rs:
return M
if Flag.EXIT in rs.flags and Flag.GUARD in rs.flags:
return GE
if Flag.GUARD in rs.flags:
return G
if Flag.EXIT in rs.flags:
return E
return M
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment