From 3049941c7736b714fba2dd246448c350dfb7b221 Mon Sep 17 00:00:00 2001 From: Kelvin Moutet Date: Thu, 26 Nov 2020 17:40:39 +0100 Subject: [PATCH 1/5] Handle chaincode pagination --- backend/substrapp/ledger/api.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/substrapp/ledger/api.py b/backend/substrapp/ledger/api.py index 88c46c91b..68003db5b 100644 --- a/backend/substrapp/ledger/api.py +++ b/backend/substrapp/ledger/api.py @@ -168,7 +168,19 @@ def call_ledger(channel_name, call_type, fcn, *args, **kwargs): ts = time.time() error = None try: - return _call_ledger(channel_name, call_type, fcn, *args, **kwargs) + response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs) + + if isinstance(response, dict) and 'bookmarks' in response: + results = response['result'] # first result + while response['result']: + kwargs['args'] = {'bookmarks': response['bookmarks']} + response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs) + results.extend(response['result']) # following results + else: + response = results + + return response + except Exception as e: error = e.__class__.__name__ raise From 57f0c41d57b7328a15f51e6886b855245993cdef Mon Sep 17 00:00:00 2001 From: Kelvin Moutet Date: Fri, 27 Nov 2020 15:57:11 +0100 Subject: [PATCH 2/5] Update results to result --- backend/substrapp/ledger/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/substrapp/ledger/api.py b/backend/substrapp/ledger/api.py index 68003db5b..c6f88a98e 100644 --- a/backend/substrapp/ledger/api.py +++ b/backend/substrapp/ledger/api.py @@ -171,11 +171,11 @@ def call_ledger(channel_name, call_type, fcn, *args, **kwargs): response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs) if isinstance(response, dict) and 'bookmarks' in response: - results = response['result'] # first result - while response['result']: + results = response['results'] # first results + while response['results']: kwargs['args'] = {'bookmarks': response['bookmarks']} response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs) - results.extend(response['result']) # following results + results.extend(response['results']) # following results else: response = results From 8543e477d98f376dc8b49d43bc5e765a6ea3a49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Gasser?= Date: Fri, 27 Nov 2020 15:13:02 -0500 Subject: [PATCH 3/5] don't do an extra call --- backend/substrapp/ledger/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/substrapp/ledger/api.py b/backend/substrapp/ledger/api.py index c6f88a98e..70560456b 100644 --- a/backend/substrapp/ledger/api.py +++ b/backend/substrapp/ledger/api.py @@ -172,8 +172,8 @@ def call_ledger(channel_name, call_type, fcn, *args, **kwargs): if isinstance(response, dict) and 'bookmarks' in response: results = response['results'] # first results - while response['results']: - kwargs['args'] = {'bookmarks': response['bookmarks']} + while response['results'] and len(response['bookmark']) > 0: + kwargs['args'] = {'bookmark': response['bookmark']} response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs) results.extend(response['results']) # following results else: From 060a21541f76b659223e01375c8f9d8743be6a5b Mon Sep 17 00:00:00 2001 From: Kelvin Moutet Date: Mon, 7 Dec 2020 15:34:29 +0100 Subject: [PATCH 4/5] Fix typo --- backend/substrapp/ledger/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/substrapp/ledger/api.py b/backend/substrapp/ledger/api.py index 70560456b..3be687f1a 100644 --- a/backend/substrapp/ledger/api.py +++ b/backend/substrapp/ledger/api.py @@ -170,7 +170,7 @@ def call_ledger(channel_name, call_type, fcn, *args, **kwargs): try: response = _call_ledger(channel_name, call_type, fcn, *args, **kwargs) - if isinstance(response, dict) and 'bookmarks' in response: + if isinstance(response, dict) and 'bookmark' in response: results = response['results'] # first results while response['results'] and len(response['bookmark']) > 0: kwargs['args'] = {'bookmark': response['bookmark']} From fee959f6315a9a8e303b9a42b2acc2827ba44044 Mon Sep 17 00:00:00 2001 From: Kelvin Moutet Date: Tue, 8 Dec 2020 10:06:59 +0100 Subject: [PATCH 5/5] Add unit test for bookmark loop in call ledger. --- backend/substrapp/tests/tests_misc.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/substrapp/tests/tests_misc.py b/backend/substrapp/tests/tests_misc.py index 86ef16b1c..b86ea7cbb 100644 --- a/backend/substrapp/tests/tests_misc.py +++ b/backend/substrapp/tests/tests_misc.py @@ -7,7 +7,9 @@ from substrapp.ledger.exceptions import LedgerAssetNotFound, LedgerInvalidResponse from substrapp.ledger.api import get_object_from_ledger, log_fail_tuple, log_start_tuple, \ - log_success_tuple, query_tuples + log_success_tuple, query_tuples, call_ledger + +from .assets import traintuple import os @@ -136,3 +138,13 @@ def test_path_traversal(self): with self.assertRaises(Exception): model_dst_path = os.path.join(DIRECTORY, 'model/../../hackermodel') raise_if_path_traversal([model_dst_path], os.path.join(DIRECTORY, 'model/')) + + def test_call_ledger_with_bookmark(self): + + with patch('substrapp.ledger.api._call_ledger') as m_call_ledger: + m_call_ledger.side_effect = [ + {'results': traintuple[i:i + 2], 'bookmark': f'bookmark_{i}'} + for i in range(0, len(traintuple), 2) + ] + [{'results': "", 'bookmark': 'bookmark_end'}] + response = call_ledger(CHANNEL, 'query', 'queryTraintuples') + self.assertEqual(response, traintuple)