mirror of
https://github.com/cculianu/Fulcrum.git
synced 2026-08-13 12:33:27 +02:00
35 lines
1.1 KiB
C++
35 lines
1.1 KiB
C++
#include "Mempool.h"
|
|
|
|
#include <functional>
|
|
#include <map>
|
|
|
|
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<unsigned, unsigned, std::greater<unsigned>> 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;
|
|
}
|