def _get_setting_candidates(context): value = _get_setting_value(context, 'sawtooth.settings.vote.proposals') if not value: return SettingCandidates(candidates={}) setting_candidates = SettingCandidates() setting_candidates.ParseFromString(base64.b64decode(value)) return setting_candidates
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_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 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_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()
from sawtooth_settings.protobuf.settings_pb2 import SettingCandidate from sawtooth_settings.protobuf.settings_pb2 import SettingVote from sawtooth_settings.protobuf.settings_pb2 import SettingProposal from sawtooth_settings_test.settings_message_factory \ import SettingsMessageFactory from sawtooth_processor_test.transaction_processor_test_case \ import TransactionProcessorTestCase def _to_hash(value): return hashlib.sha256(value).hexdigest() EMPTY_CANDIDATES = SettingCandidates(candidates=[]).SerializeToString() class TestSettings(TransactionProcessorTestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.factory = SettingsMessageFactory() def _expect_get(self, key, value=None): received = self.validator.expect(self.factory.create_get_request(key)) self.validator.respond(self.factory.create_get_response(key, value), received) def _expect_set(self, key, expected_value): received = self.validator.expect(