commit 7218845d7bfce16b9ae4a0812cc74cd54b07f7c2 Author: Damian Johnson atagar@torproject.org Date: Sun Sep 9 16:02:58 2012 -0700
Defaulting to consensus-method 1
As per the spec clarification in commit b829224, when the consensus-method or consensus-methods fields are missing we should default to method 1 (since in practice that's the only time the field doesn't appear). --- stem/descriptor/networkstatus.py | 11 ++++++++++- test/unit/descriptor/networkstatus/document.py | 14 +++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py index beafe89..1efcf58 100644 --- a/stem/descriptor/networkstatus.py +++ b/stem/descriptor/networkstatus.py @@ -305,22 +305,31 @@ class NetworkStatusDocument(stem.descriptor.Descriptor): raise ValueError("Expected a version 3 network status documents, got version '%s' instead" % self.version) elif keyword == 'vote-status': # "vote-status" type + # + # The consensus-method and consensus-methods fields are optional since + # they weren't included in version 1. Setting a default now that we + # know if we're a vote or not.
if value == 'consensus': self.is_consensus, self.is_vote = True, False + self.consensus_method = 1 elif value == 'vote': self.is_consensus, self.is_vote = False, True + self.consensus_methods = [1] elif validate: raise ValueError("A network status document's vote-status line can only be 'consensus' or 'vote', got '%s' instead" % value) elif keyword == 'consensus-methods': # "consensus-methods" IntegerList
+ consensus_methods = [] for entry in value.split(" "): if entry.isdigit(): - self.consensus_methods.append(int(entry)) + consensus_methods.append(int(entry)) elif validate: raise ValueError("A network status document's consensus-methods must be a list of integer values, but was '%s'" % value)
+ self.consensus_methods = consensus_methods + if validate and not (1 in self.consensus_methods): raise ValueError("Network status votes must include consensus-method version 1") elif keyword == 'consensus-method': diff --git a/test/unit/descriptor/networkstatus/document.py b/test/unit/descriptor/networkstatus/document.py index 5e3ec4b..2deeb35 100644 --- a/test/unit/descriptor/networkstatus/document.py +++ b/test/unit/descriptor/networkstatus/document.py @@ -267,6 +267,12 @@ class TestNetworkStatusDocument(unittest.TestCase):
document = NetworkStatusDocument(content, False) self.assertEquals(expected_consensus_methods, document.consensus_methods) + + # check that we default to including consensus-method 1 + content = get_network_status_document({"vote-status": "vote"}, ("consensus-methods",)) + document = NetworkStatusDocument(content) + self.assertEquals([1], document.consensus_methods) + self.assertEquals(None, document.consensus_method)
def test_invalid_consensus_method(self): """ @@ -286,7 +292,13 @@ class TestNetworkStatusDocument(unittest.TestCase): self.assertRaises(ValueError, NetworkStatusDocument, content)
document = NetworkStatusDocument(content, False) - self.assertEquals(None, document.consensus_method) + self.assertEquals(1, document.consensus_method) + + # check that we default to being consensus-method 1 + content = get_network_status_document(exclude = ("consensus-method",)) + document = NetworkStatusDocument(content) + self.assertEquals(1, document.consensus_method) + self.assertEquals([], document.consensus_methods)
def test_invalid_time_fields(self): """