Esempio n. 1
0
    def handle(self):
        super(RsmcResponsesMessage, self).handle()

        status = EnumResponseStatus.RESPONSE_OK
        try:
            # check the response status
            if not self.check_response_status(self.status):
                self.rollback_resource(self.channel_name, self.nonce, self.payment, self.status)
                return
            # common check
            self.check_channel_state(self.channel_name)
            self.verify()
            self.check_role(self.role_index)

            if 0 == self.role_index:
                need_update_balance, rsmc_sign_body = self.handle_partner_response()
            elif 1 == self.role_index:
                need_update_balance, rsmc_sign_body = self.handle_founder_response()
            else:
                raise GoTo(EnumResponseStatus.RESPONSE_FAIL, 'Invalid Role<{}> of RsmcResponse'.format(self.role_index))

            # send RsmcSign to peer
            if rsmc_sign_body:
                response_message = self.create_message_header(self.receiver, self.sender, self._message_name,
                                                              self.channel_name, self.asset_type, self.nego_nonce or self.nonce)
                response_message.update({'MessageBody': rsmc_sign_body})
                response_message.update({'Status': status.name})
                self.send(response_message)

            is_htlc_to_rsmc = self.is_hlock_to_rsmc(self.hashcode)
            if is_htlc_to_rsmc:
                # update the htlc transaction state.
                Channel.confirm_payment(self.channel_name, self.hashcode, is_htlc_to_rsmc)

            # update the channel balance
            if need_update_balance:
                APIStatistics.update_statistics(self.wallet.address, payment=self.payment, payer=(0==self.role_index))
                self.update_balance_for_channel(self.channel_name, self.asset_type, self.payer_address,
                                                self.payee_address, self.payment, is_htlc_to_rsmc)
        except GoTo as error:
            LOG.exception(error)
            status = error.reason
        except Exception as error:
            status = EnumResponseStatus.RESPONSE_EXCEPTION_HAPPENED
            LOG.exception('Failed to handle RsmcSign for channel<{}> nonce<{}>, role_index<{}>.Exception: {}' \
                      .format(self.channel_name, self.nonce, self.role_index, error))
        finally:
            # send error response
            if EnumResponseStatus.RESPONSE_OK != status:
                if 0 == self.role_index:
                    self.send_error_response(self.sender, self.receiver, self.channel_name,
                                             self.asset_type, self.nonce, status, kwargs={'RoleIndex': '1'})

                # need rollback some resources
                self.rollback_resource(self.channel_name, self.nonce, self.payment, status=self.status)

        return
Esempio n. 2
0
    def handle(self):
        super(RsmcResponsesMessage, self).handle()

        status = EnumResponseStatus.RESPONSE_OK
        try:
            # check the response status
            if not self.check_response_status(self.status):
                self.rollback_resource(self.channel_name, self.nonce,
                                       self.payment, self.status)
                return

            # common check
            self.check_channel_state(self.channel_name)
            self.verify()
            self.check_role(self.role_index)
            nonce = self.nonce if 0 == self.role_index else self.nonce + 1
            self.check_nonce(nonce, self.channel_name)

            is_htlc_to_rsmc = self.is_hlock_to_rsmc(self.hashcode)
            self.check_balance(self.channel_name,
                               self.asset_type,
                               self.payer,
                               self.sender_balance,
                               self.payee,
                               self.receiver_balance,
                               hlock_to_rsmc=is_htlc_to_rsmc,
                               payment=self.payment)

            sign_hashcode, sign_rcode = self.get_rcode(self.channel_name,
                                                       self.hashcode)
            self.check_signature(self.wallet,
                                 type_list=self._sign_type_list,
                                 value_list=[
                                     self.channel_name, nonce, self.payer,
                                     int(self.sender_balance), self.payee,
                                     int(self.receiver_balance), sign_hashcode,
                                     sign_rcode
                                 ],
                                 signature=self.commitment)

            if 0 == self.role_index:
                self.rsmc_sign(self.wallet, self.channel_name, self.asset_type,
                               self.nonce, self.sender, self.receiver,
                               self.payment, self.sender_balance,
                               self.receiver_balance, self.rsmc_sign_role,
                               self.hashcode, self.comments)

            # update transaction
            Channel.update_trade(self.channel_name,
                                 self.nonce,
                                 peer_commitment=self.commitment,
                                 state=EnumTradeState.confirmed.name)
            Channel.confirm_payment(self.channel_name, self.hashcode,
                                    is_htlc_to_rsmc)

            # update the channel balance
            self.update_balance_for_channel(self.channel_name, self.asset_type,
                                            self.payer, self.payee,
                                            self.payment, is_htlc_to_rsmc)
        except GoTo as error:
            LOG.error(error)
            status = error.reason
        except Exception as error:
            status = EnumResponseStatus.RESPONSE_EXCEPTION_HAPPENED
            LOG.error('Failed to handle RsmcSign for channel<{}> nonce<{}>, role_index<{}>.Exception: {}' \
                      .format(self.channel_name, self.nonce, self.role_index, error))
        finally:
            # send error response
            if EnumResponseStatus.RESPONSE_OK != status:
                if 0 == self.role_index:
                    self.send_error_response(self.sender,
                                             self.receiver,
                                             self.channel_name,
                                             self.asset_type,
                                             self.nonce,
                                             status,
                                             kwargs={'RoleIndex': '1'})

                # need rollback some resources
                self.rollback_resource(self.channel_name,
                                       self.nonce,
                                       self.payment,
                                       status=self.status)

        return