diff --git a/backend/substrapp/ledger/api.py b/backend/substrapp/ledger/api.py index 88c46c91b..3be687f1a 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 'bookmark' in response: + results = response['results'] # first results + 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: + response = results + + return response + except Exception as e: error = e.__class__.__name__ raise 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)