예제 #1
0
    def estimate_fee_per_kb(self, N):
        """ The argument N may be either a number of blocks target,
        for estimation of feerate by Core, or a number of satoshis
        per kilo-vbyte (see `fee_per_kb_has_been_manually_set` for
        how this is distinguished).
        In both cases it is prevented from falling below the current
        minimum feerate for tx to be accepted into node's mempool.
        In case of failure to connect, None is returned.
        In case of failure to source a specific minimum fee relay rate
        (which is used to sanity check user's chosen fee rate), 1000
        is used.
        In case of failure to source a feerate estimate for targeted
        number of blocks, a default of 10000 is returned.
        """

        # use the local bitcoin core minimum mempool fee as floor to avoid
        # relay problems
        rpc_result = self._rpc('getmempoolinfo', None)
        if not rpc_result:
            # in case of connection error:
            return None
        mempoolminfee_in_sat = btc.btc_to_sat(rpc_result['mempoolminfee'])
        mempoolminfee_in_sat_randomized = random.uniform(
            mempoolminfee_in_sat, mempoolminfee_in_sat * float(1.2))

        tx_fees_factor = float(jm_single().config.get('POLICY',
                                                      'tx_fees_factor'))
        if super().fee_per_kb_has_been_manually_set(N):
            N_res = random.uniform(N * float(1 - tx_fees_factor),
                                   N * float(1 + tx_fees_factor))
            if N_res < mempoolminfee_in_sat:
                log.info("Using this mempool min fee as tx feerate: " +
                         btc.fee_per_kb_to_str(mempoolminfee_in_sat) + ".")
                return int(mempoolminfee_in_sat_randomized)
            else:
                msg = "Using this manually set tx feerate"
                if tx_fees_factor != 0:
                    msg = msg + " (randomized for privacy)"
                log.info(msg + ": " + btc.fee_per_kb_to_str(N_res) + ".")
                return int(N_res)

        # Special bitcoin core case: sometimes the highest priority
        # cannot be estimated in that case the 2nd highest priority
        # should be used instead of falling back to hardcoded values
        tries = 2 if N == 1 else 1

        for i in range(tries):
            rpc_result = self._rpc('estimatesmartfee', [N + i])
            if not rpc_result:
                # in case of connection error:
                return None
            estimate = rpc_result.get('feerate')
            # `estimatesmartfee` will currently return in the format
            # `{'errors': ['Insufficient data or no feerate found'], 'blocks': N}`
            # if it is not able to make an estimate. We insist that
            # the 'feerate' key is found and contains a positive value:
            if estimate and estimate > 0:
                estimate_in_sat = btc.btc_to_sat(estimate)
                retval = random.uniform(
                    estimate_in_sat * float(1 - tx_fees_factor),
                    estimate_in_sat * float(1 + tx_fees_factor))
                break
        else:  # cannot get a valid estimate after `tries` tries:
            retval = 10000
            log.warn("Could not source a fee estimate from Core, " +
                     "falling back to default: " +
                     btc.fee_per_kb_to_str(retval) + ".")

        if retval < mempoolminfee_in_sat:
            log.info("Using this mempool min fee as tx feerate: " +
                     btc.fee_per_kb_to_str(mempoolminfee_in_sat) + ".")
            return int(mempoolminfee_in_sat_randomized)
        else:
            msg = "Using bitcoin network feerate for " + str(N) + \
                " block confirmation target"
            if tx_fees_factor != 0:
                msg = msg + " (randomized for privacy)"
            log.info(msg + ": " + btc.fee_per_kb_to_str(retval))
            return int(retval)
예제 #2
0
def test_btc_to_sat():
    assert (btc.btc_to_sat(Decimal("0.00000001")) == 1)
    assert (btc.btc_to_sat(Decimal("1.00000000")) == 100000000)