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 _get_config_candidates(state): value = _get_config_value(state, 'sawtooth.config.vote.proposals') if not value: return ConfigCandidates(candidates={}) else: config_candidates = ConfigCandidates() config_candidates.ParseFromString(base64.b64decode(value)) return config_candidates
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()
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()
import unittest import hashlib import base64 from sawtooth_config_test.config_message_factory import ConfigMessageFactory from sawtooth_config.protobuf.config_pb2 import ConfigCandidates from sawtooth_config.protobuf.config_pb2 import ConfigCandidate from sawtooth_config.protobuf.config_pb2 import ConfigVote from sawtooth_config.protobuf.config_pb2 import ConfigProposal def _to_hash(value): return hashlib.sha256(value).hexdigest() EMPTY_CANDIDATES = ConfigCandidates(candidates=[]).SerializeToString() class TestConfig(unittest.TestCase): """ Set of tests to run in a test suite with an existing TPTester and transaction processor. """ def __init__(self, test_name, tester): super().__init__(test_name) self.tester = tester self.factory = ConfigMessageFactory() def _expect_get(self, key, value=None): received = self.tester.expect(