Ejemplo n.º 1
0
    async def write_batch(self,
                          msg_batch,
                          batch_seq_nums=None,
                          m_of_n_quorum=None):
        if not self.comm_prepared:
            await self._comm_prepare()

        cid = str(self.req_seq_num.next())
        batch_size = len(msg_batch)

        if batch_seq_nums is None:
            batch_seq_nums = []
            for n in range(batch_size):
                batch_seq_nums.append(self.req_seq_num.next())

        msg_data = b''
        for n in range(batch_size):
            msg = msg_batch[n]
            msg_seq_num = batch_seq_nums[n]
            msg_cid = str(msg_seq_num)
            msg_data = b''.join([
                msg_data,
                bft_msgs.pack_request(self.client_id, msg_seq_num, False,
                                      self.config.req_timeout_milli, msg_cid,
                                      msg, True)
            ])

        data = bft_msgs.pack_batch_request(self.client_id, batch_size,
                                           msg_data, cid)

        if m_of_n_quorum is None:
            m_of_n_quorum = MofNQuorum.LinearizableQuorum(
                self.config, [r.id for r in self.replicas])

        # Raise a trio.TooSlowError exception if a quorum of replies
        try:
            with trio.fail_after(batch_size * self.config.req_timeout_milli /
                                 1000):
                self._reset_on_new_request(batch_seq_nums)
                return await self._send_receive_loop(
                    data, False, m_of_n_quorum,
                    batch_size * self.config.retry_timeout_milli / 1000)
        except trio.TooSlowError:
            print(
                f"TooSlowError thrown from client_id {self.client_id}, for batch msg {cid} {batch_seq_nums}"
            )
            raise trio.TooSlowError
        finally:
            pass
Ejemplo n.º 2
0
    async def write_batch(self, msg_batch, batch_seq_nums=None, m_of_n_quorum=None, corrupt_params=None, no_retries=False):
        if not self.comm_prepared:
            await self._comm_prepare()

        cid = str(self.req_seq_num.next())
        batch_size = len(msg_batch)

        if batch_seq_nums is None:
            batch_seq_nums = []
            for n in range(batch_size):
                batch_seq_nums.append(self.req_seq_num.next())

        msg_data = b''
        req_index_to_corrupt = random.randint(1, batch_size-1) # don't corrupt the 1st
        for n in range(batch_size):
            msg = msg_batch[n]
            msg_seq_num = batch_seq_nums[n]
            msg_cid = str(msg_seq_num)

            signature = b''
            client_id = self.client_id
            if self.signing_key:
                h = SHA256.new(msg)
                signature = pkcs1_15.new(self.signing_key).sign(h)
                if corrupt_params and (req_index_to_corrupt == n):
                    msg, signature, client_id = self._corrupt_signing_params(msg, signature, client_id, corrupt_params)

            msg_data = b''.join([msg_data, bft_msgs.pack_request(
                self.client_id, msg_seq_num, False, self.config.req_timeout_milli, msg_cid, msg, True,
                reconfiguration=False, span_context=b'', signature=signature)])

        data = bft_msgs.pack_batch_request(
            self.client_id, batch_size, msg_data, cid)

        if m_of_n_quorum is None:
            m_of_n_quorum = MofNQuorum.LinearizableQuorum(self.config, [r.id for r in self.replicas])

        # Raise a trio.TooSlowError exception if a quorum of replies
        try:
            with trio.fail_after(batch_size * self.config.req_timeout_milli / 1000):
                self._reset_on_new_request(batch_seq_nums)
                return await self._send_receive_loop(data, False, m_of_n_quorum,
                    batch_size * self.config.retry_timeout_milli / 1000, no_retries=no_retries)
        except trio.TooSlowError:
            print(f"TooSlowError thrown from client_id {self.client_id}, for batch msg {cid} {batch_seq_nums}")
            raise trio.TooSlowError
        finally:
            pass