def test_propose_in_ballot_mode(self): """ Tests proposing a value in ballot mode. """ self._propose('my.config.setting', 'myvalue') self._expect_get('sawtooth.config.authorization_type', 'Ballot') self._expect_get('sawtooth.config.vote.authorized_keys', '') self._expect_get('sawtooth.config.vote.approval_threshold', '2') self._expect_get('sawtooth.config.vote.proposals') proposal = ConfigProposal( setting='my.config.setting', value='myvalue', nonce='somenonce' ) proposal_id = _to_hash(proposal.SerializeToString()) record = ConfigCandidate.VoteRecord( public_key=self._public_key, vote=ConfigVote.ACCEPT) candidate = ConfigCandidate( proposal_id=proposal_id, proposal=proposal, votes=[record]) candidates = ConfigCandidates(candidates=[candidate]) # Get's again to update the entry self._expect_get('sawtooth.config.vote.proposals') self._expect_set('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_ok()
def test_vote_in_ballot_mode_rejects_a_tie(self): """ Tests voting on a given setting, where there is a tie for accept and for reject, with no remaining auth keys. """ proposal = ConfigProposal(setting='my.config.setting', value='myvalue', nonce='somenonce') proposal_id = _to_hash(proposal.SerializeToString()) candidate = ConfigCandidate(proposal_id=proposal_id, proposal=proposal, votes=[ ConfigCandidate.VoteRecord( public_key='some_other_pubkey', vote=ConfigVote.ACCEPT), ]) candidates = ConfigCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', ConfigVote.REJECT) self._expect_get('sawtooth.config.authorization_type', 'Ballot') self._expect_get('sawtooth.config.vote.authorized_keys', '') self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.config.vote.approval_threshold', '2') # expect to update the proposals self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_set('sawtooth.config.vote.proposals', base64.b64encode(EMPTY_CANDIDATES)) self._expect_ok()
def test_vote_in_ballot_mode_approved(self): """ Tests voting on a given setting, where the setting is approved """ proposal = ConfigProposal(setting='my.config.setting', value='myvalue', nonce='somenonce') proposal_id = _to_hash(proposal.SerializeToString()) record = ConfigCandidate.VoteRecord(public_key="some_other_pubkey", vote=ConfigVote.ACCEPT) candidate = ConfigCandidate(proposal_id=proposal_id, proposal=proposal, votes=[record]) candidates = ConfigCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', ConfigVote.ACCEPT) self._expect_get('sawtooth.config.authorization_type', 'Ballot') self._expect_get('sawtooth.config.vote.authorized_keys', '') self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.config.vote.approval_threshold', '2') # the vote should pass self._expect_get('my.config.setting') self._expect_set('my.config.setting', 'myvalue') # expect to update the proposals self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_set('sawtooth.config.vote.proposals', base64.b64encode(EMPTY_CANDIDATES)) self._expect_ok()
def test_vote_counted(self): """ Tests voting on a given setting, where the vote is counted only. """ proposal = ConfigProposal( setting='my.config.setting', value='myvalue', nonce='somenonce' ) proposal_id = _to_hash(proposal.SerializeToString()) record = ConfigCandidate.VoteRecord( public_key="some_other_pubkey", vote=ConfigVote.ACCEPT) candidate = ConfigCandidate( proposal_id=proposal_id, proposal=proposal, votes=[record]) candidates = ConfigCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', ConfigVote.ACCEPT) self._expect_get('sawtooth.config.vote.authorized_keys', self._public_key + ',some_other_pubkey,third_pubkey') self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.config.vote.approval_threshold', '3') # expect to update the proposals self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) record = ConfigCandidate.VoteRecord( public_key="some_other_pubkey", vote=ConfigVote.ACCEPT) new_record = ConfigCandidate.VoteRecord( public_key=self._public_key, vote=ConfigVote.ACCEPT) candidate = ConfigCandidate( proposal_id=proposal_id, proposal=proposal, votes=[record, new_record]) updated_candidates = ConfigCandidates(candidates=[candidate]) self._expect_set( 'sawtooth.config.vote.proposals', base64.b64encode(updated_candidates.SerializeToString())) self._expect_ok()
def _apply_proposal(self, pubkey, config_proposal_data, state): config_proposal = ConfigProposal() config_proposal.ParseFromString(config_proposal_data) proposal_id = hashlib.sha256(config_proposal_data).hexdigest() approval_threshold = _get_approval_threshold(state) _validate_setting(config_proposal.setting, config_proposal.value) if approval_threshold >= 1: config_candidates = _get_config_candidates(state) existing_candidate = _first( config_candidates.candidates, lambda candidate: candidate.proposal_id == proposal_id) if existing_candidate is not None: raise InvalidTransaction('Duplicate proposal for {}'.format( config_proposal.setting)) record = ConfigCandidate.VoteRecord(public_key=pubkey, vote=ConfigVote.ACCEPT) config_candidates.candidates.add(proposal_id=proposal_id, proposal=config_proposal, votes=[record]) LOGGER.debug('Proposal made to set %s to %s', config_proposal.setting, config_proposal.value) _save_config_candidates(state, config_candidates) else: _set_config_value(state, config_proposal.setting, config_proposal.value)
def test_vote_rejected(self): """ Tests voting on a given setting, where the setting is rejected. """ proposal = ConfigProposal( setting='my.config.setting', value='myvalue', nonce='somenonce' ) proposal_id = _to_hash(proposal.SerializeToString()) candidate = ConfigCandidate( proposal_id=proposal_id, proposal=proposal, votes=[ ConfigCandidate.VoteRecord( public_key='some_other_pubkey', vote=ConfigVote.ACCEPT), ConfigCandidate.VoteRecord( public_key='a_rejectors_pubkey', vote=ConfigVote.REJECT) ]) candidates = ConfigCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', ConfigVote.REJECT) self._expect_get( 'sawtooth.config.vote.authorized_keys', self._public_key + ',some_other_pubkey,a_rejectors_pubkey') self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.config.vote.approval_threshold', '2') # expect to update the proposals self._expect_get('sawtooth.config.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_set('sawtooth.config.vote.proposals', base64.b64encode(EMPTY_CANDIDATES)) self._expect_ok()