BlockTemplate: non-ambiguous transaction order

Different nodes could pick different transactions with the same fee/byte which hurted compact broadcasts efficiency
This commit is contained in:
SChernykh 2022-11-22 22:19:40 +01:00
parent 8a27a8cce4
commit 2ca428bbbb
2 changed files with 19 additions and 12 deletions

View file

@ -298,11 +298,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
m_mempoolTxs.clear();
}
else if (m_mempoolTxs.size() > max_transactions) {
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;
});
std::nth_element(m_mempoolTxs.begin(), m_mempoolTxs.begin() + max_transactions, m_mempoolTxs.end());
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
// Sort all transactions by fee per byte (highest to lowest)
std::sort(m_mempoolTxsOrder.begin(), m_mempoolTxsOrder.end(),
[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;
});
std::sort(m_mempoolTxsOrder.begin(), m_mempoolTxsOrder.end(), [this](int a, int b) { return m_mempoolTxs[a] < m_mempoolTxs[b]; });
final_reward = base_reward;
final_fees = 0;

View file

@ -329,6 +329,23 @@ struct TxMempoolData
{
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;
uint64_t blob_size;
uint64_t weight;