#include "Mempool.h" #include #include auto Mempool::calcCompactFeeHistogram(double binSize) const -> FeeHistogramVec { // this algorithm is taken from: // https://github.com/Electron-Cash/electrumx/blob/fbd00416d804c286eb7de856e9399efb07a2ceaf/electrumx/server/mempool.py#L139 FeeHistogramVec ret; std::map> histogram; // sorted map, descending order by key for (const auto & [txid, tx] : txs) { const auto feeRate = unsigned(tx->fee / bitcoin::Amount::satoshi()) // sats / std::max(tx->sizeBytes, 1u); // per byte histogram[feeRate] += tx->sizeBytes; // accumulate size by feeRate } // now, compact the bins ret.reserve(8); unsigned cumSize = 0; double r = 0.; for (const auto & [feeRate, size] : histogram) { cumSize += size; if (cumSize + r > binSize) { ret.emplace_back(FeeHistogramItem{feeRate, cumSize}); r += double(cumSize) - binSize; cumSize = 0; binSize *= 1.1; } } ret.shrink_to_fit(); // save memory return ret; }