From fd56dc68cdac7fc484e20d22cc6cfe6419203a49 Mon Sep 17 00:00:00 2001
From: Victor Shyba <victor1984@riseup.net>
Date: Thu, 4 Apr 2019 19:17:11 -0300
Subject: [PATCH] fix depth on resolve, add tests

---
 lbrynet/wallet/resolve.py                 | 12 +++---------
 lbrynet/wallet/server/session.py          |  2 +-
 tests/integration/test_resolve_command.py |  6 ++++++
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/lbrynet/wallet/resolve.py b/lbrynet/wallet/resolve.py
index 6f19b59d1..d3f8ebb4b 100644
--- a/lbrynet/wallet/resolve.py
+++ b/lbrynet/wallet/resolve.py
@@ -67,12 +67,9 @@ class Resolver:
             certificate_resolution_type = resolution['certificate']['resolution_type']
             if certificate_resolution_type == "winning" and certificate_response:
                 if 'height' in certificate_response:
-                    height = certificate_response['height']
-                    depth = self.height - height
                     certificate_response = _verify_proof(parsed_uri.name,
                                                        claim_trie_root,
                                                        certificate_response,
-                                                       height, depth,
                                                        ledger=self.ledger)
             elif certificate_resolution_type not in ['winning', 'claim_id', 'sequence']:
                 raise Exception(f"unknown response type: {certificate_resolution_type}")
@@ -85,12 +82,9 @@ class Resolver:
             claim_resolution_type = resolution['claim']['resolution_type']
             if claim_resolution_type == "winning" and claim_response:
                 if 'height' in claim_response:
-                    height = claim_response['height']
-                    depth = self.height - height
                     claim_response = _verify_proof(parsed_uri.name,
                                                    claim_trie_root,
                                                    claim_response,
-                                                   height, depth,
                                                    ledger=self.ledger)
             elif claim_resolution_type not in ["sequence", "winning", "claim_id"]:
                 raise Exception(f"unknown response type: {claim_resolution_type}")
@@ -260,7 +254,7 @@ class Resolver:
         return page_generator, upper_bound
 
 
-def _verify_proof(name, claim_trie_root, result, height, depth, ledger):
+def _verify_proof(name, claim_trie_root, result, ledger):
     """
     Verify proof for name claim
     """
@@ -276,8 +270,8 @@ def _verify_proof(name, claim_trie_root, result, height, depth, ledger):
             'nout': nOut,
             'amount': output.amount,
             'effective_amount': output.amount + support_amount,
-            'height': height,
-            'depth': depth,
+            'height': result['height'],
+            'depth': result['depth'],
             'claim_sequence': result['claim_sequence'],
             'address': output.get_address(ledger),
             'valid_at_height': result['valid_at_height'],
diff --git a/lbrynet/wallet/server/session.py b/lbrynet/wallet/server/session.py
index c95ec0a19..f7883e6fc 100644
--- a/lbrynet/wallet/server/session.py
+++ b/lbrynet/wallet/server/session.py
@@ -241,7 +241,7 @@ class LBRYElectrumX(ElectrumX):
             "txid": claim['txid'],
             "nout": claim['n'],
             "amount": amount,
-            "depth": self.db.db_height - height,
+            "depth": self.db.db_height - height + 1,
             "height": height,
             "value": hexlify(claim['value'].encode('ISO-8859-1')).decode(),
             "address": address,  # from index
diff --git a/tests/integration/test_resolve_command.py b/tests/integration/test_resolve_command.py
index ca81e4761..d64b5b6d7 100644
--- a/tests/integration/test_resolve_command.py
+++ b/tests/integration/test_resolve_command.py
@@ -1,3 +1,5 @@
+import json
+
 from integration.testcase import CommandTestCase
 
 
@@ -63,6 +65,10 @@ class ResolveCommand(CommandTestCase):
         self.assertEqual(claim['certificate']['name'], '@abc')
         self.assertEqual(claim['claims_in_channel'], 0)
 
+        # resolve has correct depth
+        tx_details = await self.blockchain.get_raw_transaction(claim['claim']['txid'])
+        self.assertEqual(claim['claim']['depth'], json.loads(tx_details)['confirmations'])
+
         # resolve handles invalid data
         txid = await self.blockchain_claim_name("gibberish", "cafecafe", "0.1")
         await self.generate(1)