This is an automated email from the ASF dual-hosted git repository.

gstein pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/steve.git

commit a859bf237d970e5867e45944c04a35a5895757b1
Author: Greg Stein <[email protected]>
AuthorDate: Sun May 29 20:49:05 2022 -0500

    verify some Election states.
---
 v3/steve/election.py | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/v3/steve/election.py b/v3/steve/election.py
index 5599a7e..965aacd 100644
--- a/v3/steve/election.py
+++ b/v3/steve/election.py
@@ -52,9 +52,13 @@ class Election:
 
     def open(self):
 
-        # Double-check the election is in the editing state.
+        # Double-check the Election is in the editing state.
         assert self.is_editable()
 
+        # Add salts first. If this is gonna fail, then make sure it
+        # happens before we move to the "opened" state.
+        self.add_salts()
+
         edata = self.gather_election_data()
         print('EDATA:', edata)
         salt = crypto.gen_salt()
@@ -90,12 +94,18 @@ class Election:
     def close(self):
         "Close an election."
 
-        # Simple tweak of the metadata.
+        # The Election should be open.
+        assert self.is_open()
+
+        # Simple tweak of the metadata to close the Election.
         self.c_close.perform()
 
     def add_salts(self):
         "Set the SALT column in the ISSUES and RECORD tables."
 
+        # The Election should be editable.
+        assert self.is_editable()
+
         cur = self.db.conn.cursor()
 
         def for_table(table, mod_cursor):
@@ -117,9 +127,8 @@ class Election:
 
     def is_tampered(self):
 
-        # The election should be open.
-        md = self.q_metadata.first_row()
-        assert md.salt is not None and md.opened_key is not None
+        # The Election should be open.
+        assert self.is_opened()
 
         # Compute an opened_key based on the current data.
         edata = self.gather_election_data()
@@ -133,19 +142,19 @@ class Election:
         return opened_key != md.opened_key
 
     def is_editable(self):
-        "Can this election be edited?"
+        "Can this Election be edited?"
         md = self.q_metadata.first_row()
         return md.salt is None and md.opened_key is None
 
     def is_open(self):
-        "Is this election open for voting?"
+        "Is this Election open for voting?"
         md = self.q_metadata.first_row()
         return (md.salt is not None
                 and md.opened_key is not None
                 and md.closed in (None, 0))
 
     def is_closed(self):
-        "Has this election been closed?"
+        "Has this Election been closed?"
         md = self.q_metadata.first_row()
         return (md.salt is None
                 and md.opened_key is None
@@ -156,7 +165,7 @@ def new_eid():
     "Create a new ElectionID."
 
     # Use 4 bytes of a salt, for 32 bits.
-    b = crypto.gen_salt()[:4]
+    b = crypto.gen_salt()
 
     # Format into 8 hex characters.
     return f'{b[0]:02x}{b[1]:02x}{b[2]:02x}{b[3]:02x}'

Reply via email to