def test_vote_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 = SettingProposal(setting='my.config.setting', value='myvalue', nonce='somenonce') proposal_id = _to_hash(proposal.SerializeToString()) candidate = SettingCandidate( proposal_id=proposal_id, proposal=proposal, votes=[ SettingCandidate.VoteRecord(public_key='some_other_public_key', vote=SettingVote.ACCEPT), ]) candidates = SettingCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', SettingVote.REJECT) self._expect_get('sawtooth.settings.vote.authorized_keys', self._public_key + ',some_other_public_key') self._expect_get('sawtooth.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.settings.vote.approval_threshold', '2') # expect to update the proposals self._expect_get('sawtooth.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_set('sawtooth.settings.vote.proposals', base64.b64encode(EMPTY_CANDIDATES)) self._expect_ok()
def test_propose(self): """ Tests proposing a value in ballot mode. """ self._propose('my.config.setting', 'myvalue') self._expect_get('sawtooth.settings.vote.authorized_keys', self._public_key) self._expect_get('sawtooth.settings.vote.approval_threshold', '2') self._expect_get('sawtooth.settings.vote.proposals') proposal = SettingProposal(setting='my.config.setting', value='myvalue', nonce='somenonce') proposal_id = _to_hash(proposal.SerializeToString()) record = SettingCandidate.VoteRecord(public_key=self._public_key, vote=SettingVote.ACCEPT) candidate = SettingCandidate(proposal_id=proposal_id, proposal=proposal, votes=[record]) candidates = SettingCandidates(candidates=[candidate]) # Get's again to update the entry self._expect_get('sawtooth.settings.vote.proposals') self._expect_set('sawtooth.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_ok()
def test_vote_approved(self): """ Tests voting on a given setting, where the setting is approved """ proposal = SettingProposal(setting='my.config.setting', value='myvalue', nonce='somenonce') proposal_id = _to_hash(proposal.SerializeToString()) record = SettingCandidate.VoteRecord( public_key="some_other_public_key", vote=SettingVote.ACCEPT) candidate = SettingCandidate(proposal_id=proposal_id, proposal=proposal, votes=[record]) candidates = SettingCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', SettingVote.ACCEPT) self._expect_get('sawtooth.settings.vote.authorized_keys', self._public_key + ',some_other_public_key') self._expect_get('sawtooth.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.settings.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.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_set('sawtooth.settings.vote.proposals', base64.b64encode(EMPTY_CANDIDATES)) self._expect_ok()
def _apply_proposal(self, auth_keys, pubkey, setting_proposal_data, context): setting_proposal = SettingProposal() setting_proposal.ParseFromString(setting_proposal_data) proposal_id = hashlib.sha256(setting_proposal_data).hexdigest() approval_threshold = _get_approval_threshold(context) _validate_setting(auth_keys, setting_proposal.setting, setting_proposal.value) if approval_threshold > 1: setting_candidates = _get_setting_candidates(context) existing_candidate = _first( setting_candidates.candidates, lambda candidate: candidate.proposal_id == proposal_id) if existing_candidate is not None: raise InvalidTransaction('Duplicate proposal for {}'.format( setting_proposal.setting)) record = SettingCandidate.VoteRecord(public_key=pubkey, vote=SettingVote.ACCEPT) setting_candidates.candidates.add(proposal_id=proposal_id, proposal=setting_proposal, votes=[record]) LOGGER.debug('Proposal made to set %s to %s', setting_proposal.setting, setting_proposal.value) _save_setting_candidates(context, setting_candidates) else: _set_setting_value(context, setting_proposal.setting, setting_proposal.value)
def test_vote_counted(self): """ Tests voting on a given setting, where the vote is counted only. """ proposal = SettingProposal(setting='my.config.setting', value='myvalue', nonce='somenonce') proposal_id = _to_hash(proposal.SerializeToString()) record = SettingCandidate.VoteRecord( public_key="some_other_public_key", vote=SettingVote.ACCEPT) candidate = SettingCandidate(proposal_id=proposal_id, proposal=proposal, votes=[record]) candidates = SettingCandidates(candidates=[candidate]) self._vote(proposal_id, 'my.config.setting', SettingVote.ACCEPT) self._expect_get( 'sawtooth.settings.vote.authorized_keys', self._public_key + ',some_other_public_key,third_public_key') self._expect_get('sawtooth.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) self._expect_get('sawtooth.settings.vote.approval_threshold', '3') # expect to update the proposals self._expect_get('sawtooth.settings.vote.proposals', base64.b64encode(candidates.SerializeToString())) record = SettingCandidate.VoteRecord( public_key="some_other_public_key", vote=SettingVote.ACCEPT) new_record = SettingCandidate.VoteRecord(public_key=self._public_key, vote=SettingVote.ACCEPT) candidate = SettingCandidate(proposal_id=proposal_id, proposal=proposal, votes=[record, new_record]) updated_candidates = SettingCandidates(candidates=[candidate]) self._expect_set( 'sawtooth.settings.vote.proposals', base64.b64encode(updated_candidates.SerializeToString())) self._expect_add_event('sawtooth.settings.vote.proposals') self._expect_ok()