forked from cculianu/Fulcrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMempool.cpp
35 lines (30 loc) · 1.14 KB
/
Mempool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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;
}