From 51720d9b736ca9cc99862324a65e5e259db26623 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 20 May 2019 14:11:53 -0400 Subject: [PATCH] add MAX() to claimtrie GROUP BY algo to make results consistent across SQLite versions --- lbrynet/wallet/server/db.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lbrynet/wallet/server/db.py b/lbrynet/wallet/server/db.py index 41a783190..21b9070d4 100644 --- a/lbrynet/wallet/server/db.py +++ b/lbrynet/wallet/server/db.py @@ -363,13 +363,15 @@ class SQLDB: if deleted_names: deleted_names_sql = f"OR normalized IN ({','.join('?' for _ in deleted_names)})" overtakes = self.execute(f""" - SELECT winner.normalized, winner.claim_hash, claimtrie.claim_hash AS current_winner FROM ( - SELECT normalized, claim_hash FROM claim + SELECT winner.normalized, winner.claim_hash, + claimtrie.claim_hash AS current_winner, + MAX(winner.effective_amount) + FROM ( + SELECT normalized, claim_hash, effective_amount FROM claim WHERE normalized IN ( - SELECT normalized FROM claim WHERE activation_height={height} {claim_hashes_sql} - ) {deleted_names_sql} - ORDER BY effective_amount ASC, height DESC, tx_position DESC - -- the order by is backwards, because GROUP BY picks last row + SELECT normalized FROM claim WHERE activation_height={height} {claim_hashes_sql} + ) {deleted_names_sql} + ORDER BY effective_amount DESC, height ASC, tx_position ASC ) AS winner LEFT JOIN claimtrie USING (normalized) GROUP BY winner.normalized HAVING current_winner IS NULL OR current_winner <> winner.claim_hash