Esempio n. 1
0
    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()
Esempio n. 2
0
    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)
Esempio n. 3
0
    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()
Esempio n. 4
0
    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()
Esempio n. 5
0
    def _create_proposal(self, block_header, pbft_enclave_module):
        """
        proposal request
        """
        public_key_hash = hashlib.sha256(block_header.signer_public_key.encode()).hexdigest()
        nonce = SignupInfo.block_id_to_nonce(block_header.previous_block_id)

        setting = 'sawtooth.consensus.pbft.max_log_size'
        if False:
            # try to set pbft params
            
            proposal = SettingProposal(
                 setting=setting,
                 value='1003',
                 nonce=nonce)
            payload = SettingsPayload(data=proposal.SerializeToString(),action=SettingsPayload.PROPOSE)
            serialized = payload.SerializeToString()
            input_addresses = _config_inputs(setting) 
            output_addresses = _config_outputs(setting)

            header = txn_pb.TransactionHeader(
                signer_public_key=block_header.signer_public_key,
                family_name='sawtooth_settings',
                family_version='1.0',
                inputs=input_addresses,
                outputs=output_addresses,
                dependencies=[],
                payload_sha512=hashlib.sha512(serialized).hexdigest(),
                batcher_public_key=block_header.signer_public_key,
                nonce=hex(random.randint(0, 2**64))).SerializeToString()

            signature = self._batch_publisher.identity_signer.sign(header)

            transaction = txn_pb.Transaction(
                    header=header,
                    payload=serialized,
                    header_signature=signature)

            LOGGER.info('payload action=%s nonce=%s',
                payload.action,
                nonce)

            self._batch_publisher.send([transaction])
        else:
            # get setting
            pass
    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()
    def create_proposal_transaction(self, setting, value, nonce):
        proposal = SettingProposal(setting=setting, value=value, nonce=nonce)
        payload = SettingsPayload(action=SettingsPayload.PROPOSE,
                                  data=proposal.SerializeToString())

        return self._create_tp_process_request(setting, payload)