def update_voter(self, voter_id, public_key, name, created_at, type): """Updates new voter in state Args: voter_id (str): The email of the voter public_key (str): The public key of the voter name (str): The human-readable name of the voter created_at (int): Unix UTC timestamp of when the agent was created type (str): The type of user of the voter """ address = addresser.get_voter_address(public_key) container = voter_pb2.VoterContainer() state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) if state_entries: container.ParseFromString(state_entries[0].data) for voter in container.entries: if voter.public_key == public_key: voter.voter_id = voter_id voter.name = name voter.created_at = created_at voter.type = type data = container.SerializeToString() updated_state = {} updated_state[address] = data self._context.set_state(updated_state, timeout=self._timeout)
def make_update_election_transaction(transaction_signer, batch_signer, election_id, name, description, start_timestamp, end_timestamp, results_permission, can_change_vote, can_show_realtime, admin_id, status, timestamp): """Make a UpdateElectionAction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair election_id (str): Unique ID of the election name (str): Name of the election description (str): Description of the election start_timestamp (int): Unix UTC timestamp of when the election start end_timestamp (int): Unix UTC timestamp of when the election end results_permission (int): Defines if its possible to change the voting option of the election can_show_realtime (bool): Defines if the results of the election will be show realtime can_change_vote (bool): Defines if the results of the election will be presented admin_id (str): Unique ID of the administrator status (bool): Defines if the election is online or canceled timestamp (int): Unix UTC timestamp of when the election is created Returns: batch_pb2.Batch: The transaction wrapped in a batch """ inputs = [ addresser.get_voter_address( transaction_signer.get_public_key().as_hex()), addresser.get_election_address(election_id) ] outputs = [addresser.get_election_address(election_id)] action = payload_pb2.UpdateElectionAction( election_id=election_id, name=name, description=description, start_timestamp=start_timestamp, end_timestamp=end_timestamp, results_permission=results_permission, can_change_vote=can_change_vote, can_show_realtime=can_show_realtime, admin_id=admin_id, status=status) payload = payload_pb2.BevPayload( action=payload_pb2.BevPayload.UPDATE_ELECTION, update_election=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_update_voter_transaction(transaction_signer, batch_signer, voter_id, public_key, name, created_at, type): """Make a UpdateVoterTransaction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair voter_id (str): Unique ID of the voter public_key (str): Public Key of the voter name (str): Name of the voter created_at (int): Unix UTC timestamp of when the election is created type (str): Type of the voter Returns: batch_pb2.Batch: The transaction wrapped in a batch """ inputs = [ addresser.get_voter_address( transaction_signer.get_public_key().as_hex()) ] outputs = [addresser.get_voter_address(public_key)] action = payload_pb2.UpdateVoterAction(voter_id=voter_id, public_key=public_key, name=name, created_at=created_at, type=type) payload = payload_pb2.BevPayload( action=payload_pb2.BevPayload.UPDATE_VOTER, update_voter=action, timestamp=created_at) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_update_voting_option_status_transaction(transaction_signer, batch_signer, voting_option_id, name, description, election_id, status, timestamp): """Make a UpdateVotingOptionStatusAction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair voting_option_id (str): Unique ID of the voting option name (str): Name of the voting option description (str): Description of the voting option election_id (str): Unique ID of the election status (bool): Defines if the voting option is activated or deactivate timestamp (int): Unix UTC timestamp of when the vote is change Returns: batch_pb2.Batch: The transaction wrapped in a batch """ inputs = [ addresser.get_voter_address( transaction_signer.get_public_key().as_hex()), addresser.get_voting_option_address(voting_option_id) ] outputs = [addresser.get_voting_option_address(voting_option_id)] action = payload_pb2.UpdateVotingOptionAction( voting_option_id=voting_option_id, name=name, description=description, election_id=election_id, status=status) payload = payload_pb2.BevPayload( action=payload_pb2.BevPayload.UPDATE_VOTING_OPTION, update_voting_option=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_update_poll_book_status_transaction(transaction_signer, batch_signer, voter_id, name, election_id, status, timestamp): """Make a UpdatePollBookStatusAction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair voter_id (str): Unique ID of the voter name (str): Name of the voter election_id (str): Unique ID of the election status (bool): Defines if the user in poll registration is activated or disable timestamp (int): Unix UTC timestamp of when the election is created Returns: batch_pb2.Batch: The transaction wrapped in a batch """ inputs = [ addresser.get_voter_address( transaction_signer.get_public_key().as_hex()), addresser.get_poll_registration_address(voter_id) ] outputs = [addresser.get_poll_registration_address(voter_id)] action = payload_pb2.UpdatePollRegistrationAction(voter_id=voter_id, name=name, election_id=election_id, status=status) payload = payload_pb2.BevPayload( action=payload_pb2.BevPayload.UPDATE_POLL_REGISTRATION, update_poll_registration=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_create_vote_transaction(transaction_signer, batch_signer, vote_id, timestamp, voter_id, election_id, voting_option_id): """Make a CreateVoteAction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair vote_id (str): Unique ID of the vote timestamp (int): Unix UTC timestamp of when the agent is created voter_id (str): Unique ID of the voter election_id (str): Unique ID of the election voting_option_id (str): Unique ID of the voting option Returns: batch_pb2.Batch: The transaction wrapped in a batch """ inputs = [ addresser.get_voter_address( transaction_signer.get_public_key().as_hex()), addresser.get_vote_address(vote_id) ] outputs = [addresser.get_vote_address(vote_id)] action = payload_pb2.CreateVoteAction(vote_id=vote_id, timestamp=timestamp, voter_id=voter_id, election_id=election_id, voting_option_id=voting_option_id) payload = payload_pb2.BevPayload(action=payload_pb2.BevPayload.CREATE_VOTE, create_vote=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def get_voter(self, public_key): """Gets the voter associated with the public_key Args: public_key (str): The public key of the voter Returns: voter_pb2.Voter: Voter with the provided public_key """ address = addresser.get_voter_address(public_key) state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) if state_entries: container = voter_pb2.VoterContainer() container.ParseFromString(state_entries[0].data) for voter in container.entries: if voter.public_key == public_key: return voter return None