Changeset View
Changeset View
Standalone View
Standalone View
src/policy/fees.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Copyright (c) 2018 The Bitcoin developers | // Copyright (c) 2018 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "policy/fees.h" | #include "policy/fees.h" | ||||
#include "amount.h" | #include "amount.h" | ||||
#include "feerate.h" | #include "feerate.h" | ||||
#include <algorithm> | |||||
#include <iostream> | |||||
void MempoolFeeEstimator::clear() { | |||||
m_maxHeap.resize(0); | |||||
m_minHeap.resize(0); | |||||
} | |||||
static bool maxComparor(const CFeeRate &a, const CFeeRate &b) { | |||||
return a < b; | |||||
} | |||||
static bool minComparor(const CFeeRate &a, const CFeeRate &b) { | |||||
return a > b; | |||||
} | |||||
void MempoolFeeEstimator::addFee(CFeeRate fee) { | |||||
auto maxHeapSize = m_maxHeap.size(); | |||||
auto minHeapSize = m_minHeap.size(); | |||||
// Add the fee to the appropriate half | |||||
if (maxHeapSize == 0 || fee < m_maxHeap.front()) { | |||||
m_maxHeap.push_back(fee); | |||||
std::push_heap(m_maxHeap.begin(), m_maxHeap.end(), maxComparor); | |||||
maxHeapSize++; | |||||
} else { | |||||
m_minHeap.push_back(fee); | |||||
std::push_heap(m_minHeap.begin(), m_minHeap.end(), minComparor); | |||||
minHeapSize++; | |||||
} | |||||
std::cout << "-------- Pre" << std::endl; | |||||
std::cout << "Max Heap:" << std::endl; | |||||
for (auto &&ent : m_maxHeap) { | |||||
std::cout << ent.ToString() << ", "; | |||||
} | |||||
std::cout << std::endl << "Min Heap:" << std::endl; | |||||
for (auto &&ent : m_minHeap) { | |||||
std::cout << ent.ToString() << ", "; | |||||
} | |||||
std::cout << std::endl; | |||||
std::cout << "-------- Post" << std::endl; | |||||
// Rebalance | |||||
if (maxHeapSize + 1 < minHeapSize) { | |||||
m_maxHeap.push_back(m_minHeap.front()); | |||||
std::push_heap(m_maxHeap.begin(), m_maxHeap.end(), maxComparor); | |||||
std::pop_heap(m_minHeap.begin(), m_minHeap.end()); | |||||
m_minHeap.pop_back(); | |||||
} else if (minHeapSize + 1 < maxHeapSize) { | |||||
m_minHeap.push_back(m_maxHeap.front()); | |||||
std::push_heap(m_minHeap.begin(), m_minHeap.end(), minComparor); | |||||
std::pop_heap(m_maxHeap.begin(), m_maxHeap.end()); | |||||
m_maxHeap.pop_back(); | |||||
} | |||||
// std::make_heap(m_maxHeap.begin(), m_maxHeap.end(), maxComparor); | |||||
std::make_heap(m_minHeap.begin(), m_minHeap.end(), minComparor); | |||||
schancel: Tests fail without this. However, I can't find any mistake above with respect to the… | |||||
std::cout << std::endl; | |||||
std::cout << "Max Heap:" << std::endl; | |||||
for (auto &&ent : m_maxHeap) { | |||||
std::cout << ent.ToString() << ", "; | |||||
} | |||||
std::cout << std::endl << "Min Heap:" << std::endl; | |||||
for (auto &&ent : m_minHeap) { | |||||
std::cout << ent.ToString() << ", "; | |||||
} | |||||
std::cout << std::endl; | |||||
} | |||||
CFeeRate MempoolFeeEstimator::estimateMedian() { | |||||
if (m_maxHeap.size() == m_minHeap.size()) { | |||||
return CFeeRate( | |||||
(m_minHeap.front().GetFeePerK() + m_maxHeap.front().GetFeePerK()) / | |||||
2); | |||||
} else if (m_maxHeap.size() > m_minHeap.size()) { | |||||
return m_maxHeap.front(); | |||||
} else { | |||||
return m_minHeap.front(); | |||||
} | |||||
} | |||||
FeeFilterRounder::FeeFilterRounder(const CFeeRate &minIncrementalFee) { | FeeFilterRounder::FeeFilterRounder(const CFeeRate &minIncrementalFee) { | ||||
Amount minFeeLimit = std::max(SATOSHI, minIncrementalFee.GetFeePerK() / 2); | Amount minFeeLimit = std::max(SATOSHI, minIncrementalFee.GetFeePerK() / 2); | ||||
feeset.insert(Amount::zero()); | feeset.insert(Amount::zero()); | ||||
for (double bucketBoundary = minFeeLimit / SATOSHI; | for (double bucketBoundary = minFeeLimit / SATOSHI; | ||||
bucketBoundary <= double(MAX_FEERATE / SATOSHI); | bucketBoundary <= double(MAX_FEERATE / SATOSHI); | ||||
bucketBoundary *= FEE_SPACING) { | bucketBoundary *= FEE_SPACING) { | ||||
feeset.insert(int64_t(bucketBoundary) * SATOSHI); | feeset.insert(int64_t(bucketBoundary) * SATOSHI); | ||||
} | } | ||||
Show All 11 Lines |
Tests fail without this. However, I can't find any mistake above with respect to the comparator and heap operations.