Bitcoin ABC  0.26.3
P2P Digital Currency
fees.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2018-2019 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <policy/fees.h>
8 
9 #include <feerate.h>
10 
11 static std::set<Amount> MakeFeeSet(const CFeeRate &min_incremental_fee,
12  const Amount &max_filter_fee_rate,
13  const double fee_filter_spacing) {
14  std::set<Amount> fee_set;
15 
16  Amount min_fee_limit =
17  std::max(SATOSHI, min_incremental_fee.GetFeePerK() / 2);
18  fee_set.insert(Amount::zero());
19  for (double bucket_boundary = min_fee_limit / SATOSHI;
20  bucket_boundary <= double(max_filter_fee_rate / SATOSHI);
21  bucket_boundary *= fee_filter_spacing) {
22  fee_set.insert(int64_t(bucket_boundary) * SATOSHI);
23  }
24 
25  return fee_set;
26 }
27 
29  FastRandomContext &rng)
30  : m_fee_set{MakeFeeSet(minIncrementalFee, MAX_FEERATE, FEE_SPACING)},
31  insecure_rand{rng} {}
32 
33 Amount FeeFilterRounder::round(const Amount currentMinFee) {
35 
36  auto it = m_fee_set.lower_bound(currentMinFee);
37  if (it == m_fee_set.end() ||
38  (it != m_fee_set.begin() &&
39  WITH_LOCK(m_insecure_rand_mutex, return insecure_rand.rand32()) % 3 !=
40  0)) {
41  --it;
42  }
43 
44  return *it;
45 }
static constexpr Amount SATOSHI
Definition: amount.h:143
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Amount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:54
Fast randomness source.
Definition: random.h:156
const std::set< Amount > m_fee_set
Definition: fees.h:42
Amount round(const Amount currentMinFee) EXCLUSIVE_LOCKS_REQUIRED(!m_insecure_rand_mutex)
Quantize a minimum fee for privacy purpose before broadcast.
Definition: fees.cpp:33
FeeFilterRounder(const CFeeRate &min_incremental_fee, FastRandomContext &rng)
Create new FeeFilterRounder.
Definition: fees.cpp:28
Mutex m_insecure_rand_mutex
Definition: fees.h:43
static std::set< Amount > MakeFeeSet(const CFeeRate &min_incremental_fee, const Amount &max_filter_fee_rate, const double fee_filter_spacing)
Definition: fees.cpp:11
static const double FEE_SPACING
Spacing of FeeRate buckets.
Definition: fees.h:27
static const Amount MAX_FEERATE(int64_t(1e7) *SATOSHI)
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
#define AssertLockNotHeld(cs)
Definition: sync.h:163
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357