commit a9222fcea717a541e1880e830f7fb58e751130cf Author: juga0 juga@riseup.net Date: Sat Dec 1 16:32:35 2018 +0000
v3bwfile: add consensus bandwidth and is_unmeasured
attributes to BWLine. Add methods to obtain them from results. --- sbws/lib/v3bwfile.py | 48 ++++++++++++++++++++++++++++++++++------- tests/unit/lib/test_v3bwfile.py | 1 + 2 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py index e08ef28..1cf7b63 100644 --- a/sbws/lib/v3bwfile.py +++ b/sbws/lib/v3bwfile.py @@ -51,7 +51,9 @@ BW_KEYVALUES_FILE = BW_KEYVALUES_BASIC + \ ['master_key_ed25519', 'nick', 'rtt', 'time', 'success', 'error_stream', 'error_circ', 'error_misc'] BW_KEYVALUES_EXTRA_BWS = ['bw_median', 'bw_mean', 'desc_bw_avg', 'desc_bw_bur', - 'desc_bw_obs_last', 'desc_bw_obs_mean'] + 'desc_bw_obs_last', 'desc_bw_obs_mean', + 'consensus_bandwidth', + 'consensus_bandwidth_is_unmeasured'] BW_KEYVALUES_EXTRA = BW_KEYVALUES_FILE + BW_KEYVALUES_EXTRA_BWS BW_KEYVALUES_INT = ['bw', 'rtt', 'success', 'error_stream', 'error_circ', 'error_misc'] + BW_KEYVALUES_EXTRA_BWS @@ -338,6 +340,11 @@ class V3BWLine(object): cls.desc_bw_avg_from_results(results_recent) kwargs['desc_bw_bur'] = \ cls.desc_bw_bur_from_results(results_recent) + kwargs['consensus_bandwidth'] = \ + cls.consensus_bandwidth_from_results(results_recent) + kwargs['consensus_bandwidth_is_unmeasured'] = \ + cls.consensus_bandwidth_is_unmeasured_from_results( + results_recent) kwargs['desc_bw_obs_last'] = \ cls.desc_bw_obs_last_from_results(results_recent) kwargs['desc_bw_obs_mean'] = \ @@ -438,6 +445,22 @@ class V3BWLine(object): return None
@staticmethod + def consensus_bandwidth_from_results(results): + """Obtain the last consensus bandwidth from the results.""" + for r in reversed(results): + if r.consensus_bandwidth is not None: + return r.consensus_bandwidth + return None + + @staticmethod + def consensus_bandwidth_is_unmeasured_from_results(results): + """Obtain the last consensus unmeasured flag from the results.""" + for r in reversed(results): + if r.consensus_bandwidth_is_unmeasured is not None: + return r.consensus_bandwidth_is_unmeasured + return None + + @staticmethod def desc_bw_obs_mean_from_results(results): desc_bw_obs_ls = [] for r in results: @@ -825,14 +848,23 @@ class V3BWFile(object): # descriptors' bandwidth-observed, because that penalises new # relays. # See https://trac.torproject.org/projects/tor/ticket/8494 - # just applying the formula above: desc_bw = min(desc_bw_obs, l.desc_bw_bur, l.desc_bw_avg) - bw_new = kb_round_x_sig_dig( - max( - l.bw_mean / mu, # ratio - max(l.bw_mean, mu) / muf # ratio filtered - ) * desc_bw, \ - digits=num_round_dig) # convert to KB + if l.consensus_bandwidth_is_unmeasured: + min_bandwidth = desc_bw + # If the relay is measured, use the minimum between the descriptors + # bandwidth and the consensus bandwidth, so that + # MaxAdvertisedBandwidth limits the consensus weight + # The consensus bandwidth in a measured relay has been obtained + # doing the same calculation as here + else: + min_bandwidth = min(desc_bw, l.consensus_bandwidth) + # Torflow's scaling + ratio_stream = l.bw_mean / mu + ratio_stream_filtered = max(l.bw_mean, mu) / muf + ratio = max(ratio_stream, ratio_stream_filtered) + bw_scaled = ratio * min_bandwidth + # round and convert to KB + bw_new = kb_round_x_sig_dig(bw_scaled, digits=num_round_dig) # Cap maximum bw if cap is not None: bw_new = min(hlimit, bw_new) diff --git a/tests/unit/lib/test_v3bwfile.py b/tests/unit/lib/test_v3bwfile.py index da2c8d8..31a5612 100644 --- a/tests/unit/lib/test_v3bwfile.py +++ b/tests/unit/lib/test_v3bwfile.py @@ -40,6 +40,7 @@ header_extra_str = LINE_SEP.join(header_extra_ls) + LINE_SEP
# Line produced without any scaling. raw_bwl_str = "bw=56 bw_mean=61423 bw_median=55656 "\ + "consensus_bandwidth=600000 consensus_bandwidth_is_unmeasured=False "\ "desc_bw_avg=1000000000 desc_bw_bur=123456 desc_bw_obs_last=524288 "\ "desc_bw_obs_mean=524288 error_circ=0 error_misc=0 error_stream=1 " \ "master_key_ed25519=g+Shk00y9Md0hg1S6ptnuc/wWKbADBgdjT0Kg+TSF3s " \
tor-commits@lists.torproject.org