From d34f5c271237208fb2a4ba1fb1a20b13cdfb6616 Mon Sep 17 00:00:00 2001
From: Victor Shyba <victor1984@riseup.net>
Date: Mon, 13 Jan 2020 20:11:14 -0300
Subject: [PATCH] fix None in history, check records after wait

---
 lbry/wallet/ledger.py              | 7 +++++--
 tests/unit/wallet/test_database.py | 3 +++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/lbry/wallet/ledger.py b/lbry/wallet/ledger.py
index ce950f7a4..6721c6ff6 100644
--- a/lbry/wallet/ledger.py
+++ b/lbry/wallet/ledger.py
@@ -271,7 +271,7 @@ class Ledger(metaclass=LedgerRegistry):
     async def get_local_status_and_history(self, address, history=None):
         if not history:
             address_details = await self.db.get_address(address=address)
-            history = address_details['history'] or ''
+            history = (address_details['history'] if address_details else '') or ''
         parts = history.split(':')[:-1]
         return (
             hexlify(sha256(history.encode())).decode() if history else None,
@@ -616,9 +616,12 @@ class Ledger(metaclass=LedgerRegistry):
             )) for address_record in records
         ], timeout=timeout)
         if pending:
+            records = await self.db.get_addresses(address__in=addresses)
             for record in records:
                 found = False
-                _, local_history = await self.get_local_status_and_history(None, history=record['history'])
+                local_history = (await self.get_local_status_and_history(
+                    record['address'], history=record['history']
+                ))[1] if record['history'] else []
                 for txid, local_height in local_history:
                     if txid == tx.id and local_height >= height:
                         found = True
diff --git a/tests/unit/wallet/test_database.py b/tests/unit/wallet/test_database.py
index dd30db5f4..289d5b447 100644
--- a/tests/unit/wallet/test_database.py
+++ b/tests/unit/wallet/test_database.py
@@ -375,6 +375,9 @@ class TestQueries(AsyncioTestCase):
         self.assertListEqual([0, 3, 2, 1], [tx.height for tx in txs])
         self.assertListEqual([tx4.id, tx3.id, tx2.id, tx1.id], [tx.id for tx in txs])
 
+    async def test_empty_history(self):
+        self.assertEqual((None, []), await self.ledger.get_local_status_and_history(''))
+
 
 class TestUpgrade(AsyncioTestCase):