From c28aae99138730fc868ca9ed9bd6933702c364f6 Mon Sep 17 00:00:00 2001
From: Jack Robison <jackrobison@lbry.io>
Date: Wed, 21 Jul 2021 12:53:51 -0400
Subject: [PATCH] fix expiring channels

---
 lbry/wallet/server/block_processor.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py
index b1648a615..a425284d3 100644
--- a/lbry/wallet/server/block_processor.py
+++ b/lbry/wallet/server/block_processor.py
@@ -643,8 +643,20 @@ class BlockProcessor:
             if (tx_num, position) not in self.txo_to_claim:
                 self._spend_claim_txo(txi, spent_claims)
         if expired:
-            # do this to follow the same content claim removing pathway as if a claim (possible channel) was abandoned
+            # abandon the channels last to handle abandoned signed claims in the same tx,
+            # see test_abandon_channel_and_claims_in_same_tx
+            expired_channels = {}
             for abandoned_claim_hash, (tx_num, nout, name) in spent_claims.items():
+                self._abandon_claim(abandoned_claim_hash, tx_num, nout, name)
+
+                if name.startswith('@'):
+                    expired_channels[abandoned_claim_hash] = (tx_num, nout, name)
+                else:
+                    # print(f"\texpire {abandoned_claim_hash.hex()} {tx_num} {nout}")
+                    self._abandon_claim(abandoned_claim_hash, tx_num, nout, name)
+
+            # do this to follow the same content claim removing pathway as if a claim (possible channel) was abandoned
+            for abandoned_claim_hash, (tx_num, nout, name) in expired_channels.items():
                 # print(f"\texpire {abandoned_claim_hash.hex()} {tx_num} {nout}")
                 self._abandon_claim(abandoned_claim_hash, tx_num, nout, name)