예제 #1
0
파일: utils.py 프로젝트: wjlu/trinity-eth
def is_valid_deposit(asset_type, deposit, spv_wallet=False):
    """

    :param asset_type:
    :param deposit:
    :param spv_wallet:
    :return:
    """
    if len(asset_type) > 10:
        asset_type = get_asset_type_name(asset_type)
    else:
        asset_type = asset_type.upper()

    if not asset_type:
        LOG.error('Must specified the asset type. Current value is None')
        return False

    if spv_wallet:
        try:
            max_deposit_configure = Configure.get("Channel").get(
                asset_type).get("CommitMaxDeposit")
        except Exception as e:
            LOG.warn(str(e))
            max_deposit_configure = 0
        try:
            min_deposit_configure = Configure.get("Channel").get(
                asset_type.upper()).get("CommitMinDeposit")
        except Exception as e:
            LOG.warn(str(e))
            min_deposit_configure = 0

        max_deposit = TrinityNumber(str(max_deposit_configure)).number
        min_deposit = TrinityNumber(str(min_deposit_configure)).number

        if min_deposit > 0 and max_deposit > 0:
            return min_deposit <= deposit <= max_deposit, None

        elif 0 >= min_deposit:
            LOG.warn('CommitMinDeposit is set as an illegal value<{}>.'.format(
                str(min_deposit_configure)))
            return deposit <= max_deposit, None
        elif 0 >= max_deposit:
            LOG.warn('CommitMaxDeposit is set as an illegal value<{}>.'.format(
                str(max_deposit_configure)))
            return deposit >= min_deposit, None
    else:
        if asset_type == "TNC":
            deposit_l = DepositAuth.deposit_limit()
            deposit_cmp = TrinityNumber(str(deposit_l)).number

            if deposit <= deposit_cmp:
                return False, "Node wallet channel deposit should larger than {}, " \
                              "but now is {}".format(str(deposit_l),str(deposit/pow(10, 8)))
        return True, None
예제 #2
0
    def trigger_htlc_to_next_jump(self):
        """"""
        self.router, _ = self.exclude_wallet_from_router(
            self.wallet.url, self.router)
        if not self.check_if_the_last_router():
            next_router = self.next_jump
            LOG.debug('Get Next Router {} from {}'.format(
                str(next_router), self.router))

            if not next_router:
                raise GoTo(
                    EnumResponseStatus.RESPONSE_ROUTER_WITH_ILLEGAL_NEXT_JUMP,
                    'Illegal next jump<{}> in router<{}>'.format(
                        next_router, self.router))

            # to get channel between current wallet and next jump
            channel_set = Channel.get_channel(self.wallet.url,
                                              next_router,
                                              state=EnumChannelState.OPENED)
            if not (channel_set and channel_set[0]):
                raise GoTo(
                    EnumResponseStatus.RESPONSE_CHANNEL_NOT_FOUND,
                    'No OPENED channel is found between {} and {}.'.format(
                        self.wallet.url, next_router))
            channel_set = channel_set[0]

            if channel_set.channel == self.channel_name:
                LOG.warn('Has reached receiver of HTLC transaction.')
                return

            # calculate fee and transfer to next jump
            fee = TrinityNumber(str(self.get_fee(self.wallet.url))).number
            payment = self.big_number_calculate(self.payment, fee, False)
            receiver = next_router
            HtlcMessage.create(channel_set.channel,
                               self.asset_type,
                               self.wallet.url,
                               receiver,
                               payment,
                               self.hashcode,
                               self.router,
                               current_channel=self.channel_name,
                               comments=self.comments)

            # record channel of next jump in current htlc trade
            Channel.update_trade(self.channel_name,
                                 self.nonce,
                                 channel=channel_set.channel)

            APIStatistics.update_statistics(self.wallet.address, htlc_free=fee)
        else:
            # trigger RResponse
            Channel.update_payment(self.channel_name, self.hashcode)
            payment_trade = Channel.query_payment(self.channel_name,
                                                  self.hashcode)
            if not (payment_trade and payment_trade[0]):
                raise GoTo(
                    EnumResponseStatus.RESPONSE_TRADE_HASHR_NOT_FOUND,
                    'Rcode not found for hashcode<{}>'.format(self.hashcode))

            payment_trade = payment_trade[0]

            try:
                RResponse.create(self.channel_name, self.asset_type,
                                 self.nonce, self.wallet.url, self.sender,
                                 self.hashcode, payment_trade.rcode,
                                 self.comments)

                # update the htlc trade history
                Channel.update_trade(self.channel_name,
                                     self.nonce,
                                     rcode=payment_trade.rcode)
            except Exception as error:
                LOG.exception(
                    'Failed triggerring to send RResponse for HashR<{}>. Exception: {}'
                    .format(self.hashcode, error))