diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 43c67e0d7..9da91e320 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -36,6 +36,7 @@ from lbrynet.daemon.Downloader import GetStream from lbrynet.daemon.Publisher import Publisher from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager from lbrynet.daemon.auth.server import AuthJSONRPCServer +from lbrynet.daemon.claims_comparator import arrange_results from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager from lbrynet.core import utils, system_info from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob @@ -2293,6 +2294,7 @@ class Daemon(AuthJSONRPCServer): """ d = self.session.wallet.get_name_claims() + d.addCallback(arrange_results) d.addCallback(lambda claims: self._render_response(claims)) return d @@ -2331,7 +2333,8 @@ class Daemon(AuthJSONRPCServer): """ claims = yield self.session.wallet.get_claims_for_name(name) - defer.returnValue(claims) + result = arrange_results(claims) + defer.returnValue(result) @defer.inlineCallbacks def jsonrpc_claim_list_by_channel(self, page=0, page_size=10, uri=None, uris=[]): diff --git a/lbrynet/daemon/claims_comparator.py b/lbrynet/daemon/claims_comparator.py new file mode 100644 index 000000000..4a1ea9870 --- /dev/null +++ b/lbrynet/daemon/claims_comparator.py @@ -0,0 +1,36 @@ +_comparison_order = ['height', 'name', 'claim_id'] # TODO outpoint + + +def arrange_results(claims): + for claim in claims: + results = claim['result'] + sorted_results = sorted(results, cmp=_compare_results) + claim['result'] = sorted_results + return claims + + +def _compare_results(left, right): + """ + :type left: dict + :type right: dict + """ + result = 0 + + for attribute in _comparison_order: + left_value = left[attribute] + right_value = right[attribute] + sub_result = _cmp(left_value, right_value) + if sub_result is not 0: + result = sub_result + break + + return result + + +def _cmp(left, right): + if left == right: + return 0 + elif left < right: + return -1 + else: + return 1 diff --git a/lbrynet/tests/unit/daemon/__init__.py b/lbrynet/tests/unit/daemon/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/lbrynet/tests/unit/daemon/test_claims_comparator.py b/lbrynet/tests/unit/daemon/test_claims_comparator.py new file mode 100644 index 000000000..9651c2137 --- /dev/null +++ b/lbrynet/tests/unit/daemon/test_claims_comparator.py @@ -0,0 +1,52 @@ +import unittest + +from lbrynet.daemon.claims_comparator import arrange_results + + +class ClaimsComparatorTest(unittest.TestCase): + def test_arrange_results(self): + results = [ + { + 'height': 1, + 'name': 'res', + 'claim_id': 'ccc' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'aaa' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'bbb' + } + ] + data = {'result': results} + + expected = [ + { + 'height': 1, + 'name': 'res', + 'claim_id': 'aaa' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'bbb' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'ccc' + } + ] + claims = arrange_results([data]) + claim = claims[0] + actual = claim['result'] + + self.assertEqual(expected, actual) + + +if __name__ == '__main__': + unittest.main()