mirror of https://github.com/SChernykh/p2pool
BlockTemplate: non-ambiguous transaction order
Different nodes could pick different transactions with the same fee/byte which hurted compact broadcasts efficiencypull/226/head
parent
8a27a8cce4
commit
2ca428bbbb
|
@ -298,11 +298,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||||
m_mempoolTxs.clear();
|
m_mempoolTxs.clear();
|
||||||
}
|
}
|
||||||
else if (m_mempoolTxs.size() > max_transactions) {
|
else if (m_mempoolTxs.size() > max_transactions) {
|
||||||
std::nth_element(m_mempoolTxs.begin(), m_mempoolTxs.begin() + max_transactions, m_mempoolTxs.end(),
|
std::nth_element(m_mempoolTxs.begin(), m_mempoolTxs.begin() + max_transactions, m_mempoolTxs.end());
|
||||||
[](const TxMempoolData& tx_a, const TxMempoolData& tx_b)
|
|
||||||
{
|
|
||||||
return tx_a.fee * tx_b.weight > tx_b.fee * tx_a.weight;
|
|
||||||
});
|
|
||||||
m_mempoolTxs.resize(max_transactions);
|
m_mempoolTxs.resize(max_transactions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -387,13 +383,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
|
||||||
// Sometimes it even finds the optimal solution
|
// Sometimes it even finds the optimal solution
|
||||||
|
|
||||||
// Sort all transactions by fee per byte (highest to lowest)
|
// Sort all transactions by fee per byte (highest to lowest)
|
||||||
std::sort(m_mempoolTxsOrder.begin(), m_mempoolTxsOrder.end(),
|
std::sort(m_mempoolTxsOrder.begin(), m_mempoolTxsOrder.end(), [this](int a, int b) { return m_mempoolTxs[a] < m_mempoolTxs[b]; });
|
||||||
[this](int a, int b)
|
|
||||||
{
|
|
||||||
const TxMempoolData& tx_a = m_mempoolTxs[a];
|
|
||||||
const TxMempoolData& tx_b = m_mempoolTxs[b];
|
|
||||||
return tx_a.fee * tx_b.weight > tx_b.fee * tx_a.weight;
|
|
||||||
});
|
|
||||||
|
|
||||||
final_reward = base_reward;
|
final_reward = base_reward;
|
||||||
final_fees = 0;
|
final_fees = 0;
|
||||||
|
|
17
src/common.h
17
src/common.h
|
@ -329,6 +329,23 @@ struct TxMempoolData
|
||||||
{
|
{
|
||||||
FORCEINLINE TxMempoolData() : id(), blob_size(0), weight(0), fee(0), time_received(0) {}
|
FORCEINLINE TxMempoolData() : id(), blob_size(0), weight(0), fee(0), time_received(0) {}
|
||||||
|
|
||||||
|
FORCEINLINE bool operator<(const TxMempoolData& tx) const
|
||||||
|
{
|
||||||
|
const uint64_t a = fee * tx.weight;
|
||||||
|
const uint64_t b = tx.fee * weight;
|
||||||
|
|
||||||
|
// Prefer transactions with higher fee/byte
|
||||||
|
if (a > b) return true;
|
||||||
|
if (a < b) return false;
|
||||||
|
|
||||||
|
// If fee/byte is the same, prefer smaller transactions (they give smaller penalty when going above the median block size limit)
|
||||||
|
if (weight < tx.weight) return true;
|
||||||
|
if (weight > tx.weight) return false;
|
||||||
|
|
||||||
|
// If two transactions have exactly the same fee and weight, just order them by id
|
||||||
|
return id < tx.id;
|
||||||
|
}
|
||||||
|
|
||||||
hash id;
|
hash id;
|
||||||
uint64_t blob_size;
|
uint64_t blob_size;
|
||||||
uint64_t weight;
|
uint64_t weight;
|
||||||
|
|
Loading…
Reference in New Issue