From d1e69e1559adf80accc3d0766d14645114d17a69 Mon Sep 17 00:00:00 2001
From: Anthony Fieroni <bvbfan@abv.bg>
Date: Mon, 6 Dec 2021 16:29:07 +0200
Subject: [PATCH] Prefer tx entry time order for a block

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
---
 src/miner.cpp |  4 ++--
 src/miner.h   | 10 ++++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/miner.cpp b/src/miner.cpp
index 1447e41c7a7..629ccc2844e 100644
--- a/src/miner.cpp
+++ b/src/miner.cpp
@@ -425,7 +425,7 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::ve
     // transactions for block inclusion.
     sortedEntries.clear();
     sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
-    std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
+    std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByEntryTime());
 }
 
 // This transaction selection algorithm orders the mempool based
@@ -993,4 +993,4 @@ void ThreadStaker::operator()(std::vector<ThreadStaker::Args> args, CChainParams
     }
 }
 
-}
\ No newline at end of file
+}
diff --git a/src/miner.h b/src/miner.h
index b20cc9adf96..b95ed88fc63 100644
--- a/src/miner.h
+++ b/src/miner.h
@@ -93,6 +93,16 @@ struct CompareTxIterByAncestorCount {
     }
 };
 
+// A comparator that sorts transactions based on tx entry time.
+struct CompareTxIterByEntryTime {
+    bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
+    {
+        if (a->GetTime() == b->GetTime())
+            return CompareTxIterByAncestorCount()(a, b);
+        return CompareTxMemPoolEntryByEntryTime()(*a, *b);
+    }
+};
+
 typedef boost::multi_index_container<
     CTxMemPoolModifiedEntry,
     boost::multi_index::indexed_by<