From 04be014c356e3c5cfbf93df41bdc12cd2eaaeb3b Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Fri, 10 Dec 2021 15:49:18 -0800 Subject: [PATCH] remove required dependencies from multi-ledger code; use consistent result format for get_ledger_for_identifier method Signed-off-by: Andrew Whitehead --- aries_cloudagent/core/conductor.py | 15 ++-- aries_cloudagent/core/profile.py | 4 +- .../indy/credx/tests/test_cred_issuance.py | 2 +- aries_cloudagent/indy/models/pres_preview.py | 28 ++++---- .../indy/models/tests/test_pres_preview.py | 22 +++--- .../indy/sdk/tests/test_verifier.py | 10 +-- aries_cloudagent/indy/tests/test_verifier.py | 25 ++++--- aries_cloudagent/indy/verifier.py | 14 ++-- aries_cloudagent/ledger/base.py | 2 +- .../tests/test_domain_txn_handler.py | 2 - .../ledger/multiple_ledger/base_manager.py | 11 ++- .../ledger_requests_executor.py | 18 ++--- .../multiple_ledger/manager_provider.py | 70 +++++++++++-------- .../tests/test_indy_ledger_requests.py | 11 ++- .../tests/test_indy_manager.py | 2 + .../tests/test_indy_vdr_manager.py | 2 + .../tests/test_manager_provider.py | 7 +- aries_cloudagent/ledger/routes.py | 27 ++----- aries_cloudagent/ledger/tests/test_routes.py | 14 ++-- .../credential_definitions/routes.py | 17 +---- .../tests/test_routes.py | 4 +- aries_cloudagent/messaging/schemas/routes.py | 18 ++--- .../messaging/schemas/tests/test_routes.py | 8 ++- .../issue_credential/v1_0/manager.py | 56 +++++++-------- .../v1_0/tests/test_manager.py | 2 +- .../v2_0/formats/indy/handler.py | 56 +++++++-------- .../v2_0/formats/indy/tests/test_handler.py | 2 +- .../present_proof/indy/pres_exch_handler.py | 42 +++++------ .../present_proof/v1_0/tests/test_manager.py | 2 +- .../present_proof/v2_0/tests/test_manager.py | 2 +- aries_cloudagent/resolver/default/indy.py | 14 ++-- .../resolver/default/tests/test_indy.py | 8 ++- aries_cloudagent/revocation/indy.py | 28 ++++---- .../revocation/tests/test_indy.py | 2 +- 34 files changed, 253 insertions(+), 294 deletions(-) diff --git a/aries_cloudagent/core/conductor.py b/aries_cloudagent/core/conductor.py index 6b99dcaffa..e66b502695 100644 --- a/aries_cloudagent/core/conductor.py +++ b/aries_cloudagent/core/conductor.py @@ -15,7 +15,6 @@ from ..admin.base_server import BaseAdminServer from ..admin.server import AdminResponder, AdminServer -from ..askar.profile import AskarProfile from ..config.default_context import ContextBuilder from ..config.injection_context import InjectionContext from ..config.provider import ClassProvider @@ -27,12 +26,9 @@ from ..config.logging import LoggingConfigurator from ..config.wallet import wallet_config from ..core.profile import Profile -from ..indy.sdk.profile import IndySdkProfile from ..indy.verifier import IndyVerifier from ..ledger.base import BaseLedger from ..ledger.error import LedgerConfigError, LedgerTransactionError -from ..ledger.indy import IndySdkLedger -from ..ledger.indy_vdr import IndyVdrLedger from ..ledger.multiple_ledger.base_manager import ( BaseMultipleLedgerManager, MultipleLedgerManagerError, @@ -67,6 +63,7 @@ from ..utils.task_queue import CompletedTask, TaskQueue from ..vc.ld_proofs.document_loader import DocumentLoader from ..wallet.did_info import DIDInfo + from .dispatcher import Dispatcher from .util import STARTUP_EVENT_TOPIC, SHUTDOWN_EVENT_TOPIC @@ -139,8 +136,9 @@ async def setup(self): BaseMultipleLedgerManager ).get_write_ledger() )[1] - if isinstance(self.root_profile, AskarProfile) and isinstance( - ledger, IndyVdrLedger + if ( + self.root_profile.BACKEND_NAME == "askar" + and ledger.BACKEND_NAME == "indy_vdr" ): context.injector.bind_instance(BaseLedger, ledger) context.injector.bind_provider( @@ -150,8 +148,9 @@ async def setup(self): self.root_profile, ), ) - elif isinstance(self.root_profile, IndySdkProfile) and isinstance( - ledger, IndySdkLedger + elif ( + self.root_profile.BACKEND_NAME == "indy" + and ledger.BACKEND_NAME == "indy" ): context.injector.bind_instance(BaseLedger, ledger) context.injector.bind_provider( diff --git a/aries_cloudagent/core/profile.py b/aries_cloudagent/core/profile.py index 6cbce6423f..e5de864d6e 100644 --- a/aries_cloudagent/core/profile.py +++ b/aries_cloudagent/core/profile.py @@ -21,8 +21,8 @@ class Profile(ABC): """Base abstraction for handling identity-related state.""" - BACKEND_NAME = None - DEFAULT_NAME = "default" + BACKEND_NAME: str = None + DEFAULT_NAME: str = "default" def __init__( self, diff --git a/aries_cloudagent/indy/credx/tests/test_cred_issuance.py b/aries_cloudagent/indy/credx/tests/test_cred_issuance.py index d35962ba35..0c23eda904 100644 --- a/aries_cloudagent/indy/credx/tests/test_cred_issuance.py +++ b/aries_cloudagent/indy/credx/tests/test_cred_issuance.py @@ -80,7 +80,7 @@ async def setUp(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=mock_ledger + return_value=(None, mock_ledger) ) ), ) diff --git a/aries_cloudagent/indy/models/pres_preview.py b/aries_cloudagent/indy/models/pres_preview.py index 28af5b5c1d..7ab771309f 100644 --- a/aries_cloudagent/indy/models/pres_preview.py +++ b/aries_cloudagent/indy/models/pres_preview.py @@ -352,14 +352,12 @@ def non_revoc(cred_def_id: str) -> IndyNonRevocationInterval: if cd_id: if profile: ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cd_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cd_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] if ledger: async with ledger: revoc_support = (await ledger.get_credential_definition(cd_id))[ @@ -413,14 +411,12 @@ def non_revoc(cred_def_id: str) -> IndyNonRevocationInterval: if cd_id: if profile: ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cd_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cd_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] if ledger: async with ledger: revoc_support = (await ledger.get_credential_definition(cd_id))[ diff --git a/aries_cloudagent/indy/models/tests/test_pres_preview.py b/aries_cloudagent/indy/models/tests/test_pres_preview.py index 9e3fc6d59c..eee3071e49 100644 --- a/aries_cloudagent/indy/models/tests/test_pres_preview.py +++ b/aries_cloudagent/indy/models/tests/test_pres_preview.py @@ -403,10 +403,13 @@ async def test_to_indy_proof_request_revo_default_interval(self): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = async_mock.MagicMock( - get_credential_definition=async_mock.CoroutineMock( - return_value={"value": {"revocation": {"...": "..."}}} - ) + mock_get_ledger.return_value = ( + None, + async_mock.MagicMock( + get_credential_definition=async_mock.CoroutineMock( + return_value={"value": {"revocation": {"...": "..."}}} + ) + ), ) indy_proof_req_revo = await pres_preview.indy_proof_request( **{k: INDY_PROOF_REQ[k] for k in ("name", "version", "nonce")}, @@ -443,10 +446,13 @@ async def test_to_indy_proof_request_revo(self): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = async_mock.MagicMock( - get_credential_definition=async_mock.CoroutineMock( - return_value={"value": {"revocation": {"...": "..."}}} - ) + mock_get_ledger.return_value = ( + None, + async_mock.MagicMock( + get_credential_definition=async_mock.CoroutineMock( + return_value={"value": {"revocation": {"...": "..."}}} + ) + ), ) indy_proof_req_revo = await pres_preview.indy_proof_request( **{k: INDY_PROOF_REQ[k] for k in ("name", "version", "nonce")}, diff --git a/aries_cloudagent/indy/sdk/tests/test_verifier.py b/aries_cloudagent/indy/sdk/tests/test_verifier.py index be02bb8ead..44784b1ad5 100644 --- a/aries_cloudagent/indy/sdk/tests/test_verifier.py +++ b/aries_cloudagent/indy/sdk/tests/test_verifier.py @@ -334,7 +334,7 @@ async def test_verify_presentation(self, mock_verify): ) as mock_non_revox, async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) INDY_PROOF_REQ_X = deepcopy(INDY_PROOF_REQ_PRED_NAMES) verified = await self.verifier.verify_presentation( INDY_PROOF_REQ_X, @@ -395,7 +395,7 @@ async def test_check_encoding_attr(self, mock_verify): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) mock_verify.return_value = True verified = await self.verifier.verify_presentation( INDY_PROOF_REQ_NAME, @@ -448,7 +448,7 @@ async def test_check_encoding_attr_tamper_encoded(self, mock_verify): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) verified = await self.verifier.verify_presentation( INDY_PROOF_REQ_NAME, INDY_PROOF_X, @@ -501,7 +501,7 @@ async def test_check_pred_names_tamper_pred_value(self, mock_verify): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) verified = await self.verifier.verify_presentation( deepcopy(INDY_PROOF_REQ_PRED_NAMES), INDY_PROOF_X, @@ -522,7 +522,7 @@ async def test_check_pred_names_tamper_pred_req_attr(self, mock_verify): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) verified = await self.verifier.verify_presentation( INDY_PROOF_REQ_X, INDY_PROOF_PRED_NAMES, diff --git a/aries_cloudagent/indy/tests/test_verifier.py b/aries_cloudagent/indy/tests/test_verifier.py index 58214f2202..7a00c7276d 100644 --- a/aries_cloudagent/indy/tests/test_verifier.py +++ b/aries_cloudagent/indy/tests/test_verifier.py @@ -341,7 +341,7 @@ async def test_check_timestamps(self): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) await self.verifier.check_timestamps( mock_profile, INDY_PROOF_REQ_NAME, @@ -353,13 +353,16 @@ async def test_check_timestamps(self): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = async_mock.MagicMock( - get_credential_definition=async_mock.CoroutineMock( - return_value={ - "...": "...", - "value": {"no": "revocation"}, - } - ) + mock_get_ledger.return_value = ( + None, + async_mock.MagicMock( + get_credential_definition=async_mock.CoroutineMock( + return_value={ + "...": "...", + "value": {"no": "revocation"}, + } + ) + ), ) with self.assertRaises(ValueError) as context: await self.verifier.check_timestamps( @@ -374,7 +377,7 @@ async def test_check_timestamps(self): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) proof_x = deepcopy(INDY_PROOF_NAME) proof_x["identifiers"][0]["timestamp"] = None proof_x["identifiers"][0]["rev_reg_id"] = None @@ -420,7 +423,7 @@ async def test_check_timestamps(self): ) as mock_logger, async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) pre_logger_calls = mock_logger.info.call_count await self.verifier.check_timestamps( mock_profile, @@ -437,7 +440,7 @@ async def test_check_timestamps(self): with async_mock.patch.object( IndyLedgerRequestsExecutor, "get_ledger_for_identifier" ) as mock_get_ledger: - mock_get_ledger.return_value = self.ledger + mock_get_ledger.return_value = (None, self.ledger) with self.assertRaises(ValueError) as context: await self.verifier.check_timestamps( mock_profile, diff --git a/aries_cloudagent/indy/verifier.py b/aries_cloudagent/indy/verifier.py index db6a2fadd3..7c219ec136 100644 --- a/aries_cloudagent/indy/verifier.py +++ b/aries_cloudagent/indy/verifier.py @@ -106,14 +106,12 @@ async def check_timestamps( if ident.get("timestamp"): cred_def_id = ident["cred_def_id"] ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cred_def_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cred_def_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: cred_def = await ledger.get_credential_definition(cred_def_id) if not cred_def["value"].get("revocation"): diff --git a/aries_cloudagent/ledger/base.py b/aries_cloudagent/ledger/base.py index 3a1868b6c1..3ceaa7e1f3 100644 --- a/aries_cloudagent/ledger/base.py +++ b/aries_cloudagent/ledger/base.py @@ -17,7 +17,7 @@ class BaseLedger(ABC, metaclass=ABCMeta): """Base class for ledger.""" - BACKEND_NAME = None + BACKEND_NAME: str = None async def __aenter__(self) -> "BaseLedger": """ diff --git a/aries_cloudagent/ledger/merkel_validation/tests/test_domain_txn_handler.py b/aries_cloudagent/ledger/merkel_validation/tests/test_domain_txn_handler.py index c8205e5a63..80c3adfaf4 100644 --- a/aries_cloudagent/ledger/merkel_validation/tests/test_domain_txn_handler.py +++ b/aries_cloudagent/ledger/merkel_validation/tests/test_domain_txn_handler.py @@ -6,8 +6,6 @@ from unittest import TestCase -from aries_cloudagent.ledger.multiple_ledger.base_manager import T - from ..domain_txn_handler import ( _extract_attr_typed_value, parse_attr_txn, diff --git a/aries_cloudagent/ledger/multiple_ledger/base_manager.py b/aries_cloudagent/ledger/multiple_ledger/base_manager.py index 0e34da32c1..49af0efe3c 100644 --- a/aries_cloudagent/ledger/multiple_ledger/base_manager.py +++ b/aries_cloudagent/ledger/multiple_ledger/base_manager.py @@ -1,12 +1,11 @@ """Manager for multiple ledger.""" from abc import ABC, abstractmethod -from typing import TypeVar, Optional, Tuple, Mapping +from typing import Optional, Tuple, Mapping from ...core.error import BaseError from ...core.profile import Profile - -T = TypeVar("T") +from ...ledger.base import BaseLedger class MultipleLedgerManagerError(BaseError): @@ -20,7 +19,7 @@ def __init__(self, profile: Profile): """Initialize Multiple Ledger Manager.""" @abstractmethod - async def get_write_ledger(self) -> Tuple[str, T]: + async def get_write_ledger(self) -> Tuple[str, BaseLedger]: """Return write ledger.""" @abstractmethod @@ -34,13 +33,13 @@ async def get_nonprod_ledgers(self) -> Mapping: @abstractmethod async def _get_ledger_by_did( self, ledger_id: str, did: str - ) -> Optional[Tuple[str, T, bool]]: + ) -> Optional[Tuple[str, BaseLedger, bool]]: """Build and submit GET_NYM request and process response.""" @abstractmethod async def lookup_did_in_configured_ledgers( self, did: str, cache_did: bool - ) -> Tuple[str, T]: + ) -> Tuple[str, BaseLedger]: """Lookup given DID in configured ledgers in parallel.""" def extract_did_from_identifier(self, identifier: str) -> str: diff --git a/aries_cloudagent/ledger/multiple_ledger/ledger_requests_executor.py b/aries_cloudagent/ledger/multiple_ledger/ledger_requests_executor.py index 701b058a14..a56d93dfef 100644 --- a/aries_cloudagent/ledger/multiple_ledger/ledger_requests_executor.py +++ b/aries_cloudagent/ledger/multiple_ledger/ledger_requests_executor.py @@ -1,11 +1,9 @@ """Ledger Request Executor.""" -from typing import Tuple, Union, Optional +from typing import Optional, Tuple from ...config.base import InjectionError from ...core.profile import Profile from ...ledger.base import BaseLedger -from ...ledger.indy import IndySdkLedger -from ...ledger.indy_vdr import IndyVdrLedger from ...ledger.multiple_ledger.base_manager import ( BaseMultipleLedgerManager, MultipleLedgerManagerError, @@ -41,16 +39,11 @@ def __init__( async def get_ledger_for_identifier( self, identifier: str, txn_record_type: int - ) -> Union[ - Optional[IndyVdrLedger], - Optional[IndySdkLedger], - Tuple[str, IndyVdrLedger], - Tuple[str, IndySdkLedger], - ]: + ) -> Tuple[Optional[str], Optional[BaseLedger]]: """Return ledger info given the record identifier.""" # For seqNo if identifier.isdigit(): - return self.profile.inject(BaseLedger) + return (None, self.profile.inject(BaseLedger)) elif ( self.profile.settings.get("ledger.ledger_config_list") and len(self.profile.settings.get("ledger.ledger_config_list")) > 0 @@ -66,6 +59,5 @@ async def get_ledger_for_identifier( extracted_did, cache_did=cache_did ) except (MultipleLedgerManagerError, InjectionError): - return self.profile.inject_or(BaseLedger) - else: - return self.profile.inject_or(BaseLedger) + pass + return (None, self.profile.inject_or(BaseLedger)) diff --git a/aries_cloudagent/ledger/multiple_ledger/manager_provider.py b/aries_cloudagent/ledger/multiple_ledger/manager_provider.py index 26a7496804..8ea62eac52 100644 --- a/aries_cloudagent/ledger/multiple_ledger/manager_provider.py +++ b/aries_cloudagent/ledger/multiple_ledger/manager_provider.py @@ -4,17 +4,13 @@ from collections import OrderedDict -from ...askar.profile import AskarProfile +from ...cache.base import BaseCache from ...config.provider import BaseProvider from ...config.settings import BaseSettings from ...config.injector import BaseInjector, InjectionError -from ...cache.base import BaseCache -from ...indy.sdk.profile import IndySdkProfile +from ...core.profile import Profile from ...ledger.base import BaseLedger -from ...utils.classloader import ClassLoader, ClassNotFoundError - -from ..indy import IndySdkLedgerPool, IndySdkLedger -from ..indy_vdr import IndyVdrLedgerPool, IndyVdrLedger +from ...utils.classloader import ClassNotFoundError, DeferLoad from .base_manager import MultipleLedgerManagerError @@ -24,39 +20,53 @@ class MultiIndyLedgerManagerProvider(BaseProvider): """Multiple Indy ledger support manager provider.""" - askar_manager_path = ( - "aries_cloudagent.ledger.multiple_ledger." - "indy_vdr_manager.MultiIndyVDRLedgerManager" - ) - basic_manager_path = ( - "aries_cloudagent.ledger.multiple_ledger." "indy_manager.MultiIndyLedgerManager" - ) MANAGER_TYPES = { - "basic": basic_manager_path, - "askar-profile": askar_manager_path, + "basic": ( + DeferLoad( + "aries_cloudagent.ledger.multiple_ledger." + "indy_manager.MultiIndyLedgerManager" + ) + ), + "askar-profile": ( + DeferLoad( + "aries_cloudagent.ledger.multiple_ledger." + "indy_vdr_manager.MultiIndyVDRLedgerManager" + ) + ), + } + LEDGER_TYPES = { + "basic": { + "pool": DeferLoad("aries_cloudagent.ledger.indy.IndySdkLedgerPool"), + "ledger": DeferLoad("aries_cloudagent.ledger.indy.IndySdkLedger"), + }, + "askar-profile": { + "pool": DeferLoad("aries_cloudagent.ledger.indy_vdr.IndyVdrLedgerPool"), + "ledger": DeferLoad("aries_cloudagent.ledger.indy_vdr.IndyVdrLedger"), + }, } def __init__(self, root_profile): """Initialize the multiple Indy ledger profile manager provider.""" self._inst = {} - self.root_profile = root_profile + self.root_profile: Profile = root_profile def provide(self, settings: BaseSettings, injector: BaseInjector): """Create the multiple Indy ledger manager instance.""" - if isinstance(self.root_profile, IndySdkProfile): + if self.root_profile.BACKEND_NAME == "indy": manager_type = "basic" - elif isinstance(self.root_profile, AskarProfile): + elif self.root_profile.BACKEND_NAME == "askar": manager_type = "askar-profile" else: raise MultipleLedgerManagerError( - "MultiIndyLedgerManagerProvider expects an IndySDKProfile [indy] " + "MultiIndyLedgerManagerProvider expects an IndySdkProfile [indy] " " or AskarProfile [indy_vdr] as root_profile" ) - manager_class = self.MANAGER_TYPES.get(manager_type) - - if manager_class not in self._inst: + if manager_type not in self._inst: + manager_class = self.MANAGER_TYPES.get(manager_type) + pool_class = self.LEDGER_TYPES[manager_type]["pool"] + ledger_class = self.LEDGER_TYPES[manager_type]["ledger"] LOGGER.info("Create multiple Indy ledger manager: %s", manager_type) try: if manager_type == "basic": @@ -74,7 +84,7 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): pool_name = config.get("pool_name") ledger_is_production = config.get("is_production") ledger_is_write = config.get("is_write") - ledger_pool = IndySdkLedgerPool( + ledger_pool = pool_class( pool_name, keepalive=keepalive, cache=cache, @@ -82,7 +92,7 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): read_only=read_only, socks_proxy=socks_proxy, ) - ledger_instance = IndySdkLedger( + ledger_instance = ledger_class( pool=ledger_pool, profile=self.root_profile, ) @@ -101,7 +111,7 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): indy_sdk_production_ledgers.move_to_end( ledger_id, last=False ) - self._inst[manager_class] = ClassLoader.load_class(manager_class)( + self._inst[manager_type] = manager_class( self.root_profile, production_ledgers=indy_sdk_production_ledgers, non_production_ledgers=indy_sdk_non_production_ledgers, @@ -122,7 +132,7 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): pool_name = config.get("pool_name") ledger_is_production = config.get("is_production") ledger_is_write = config.get("is_write") - ledger_pool = IndyVdrLedgerPool( + ledger_pool = pool_class( pool_name, keepalive=keepalive, cache=cache, @@ -130,7 +140,7 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): read_only=read_only, socks_proxy=socks_proxy, ) - ledger_instance = IndyVdrLedger( + ledger_instance = ledger_class( pool=ledger_pool, profile=self.root_profile, ) @@ -149,7 +159,7 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): indy_vdr_non_production_ledgers.move_to_end( ledger_id, last=False ) - self._inst[manager_class] = ClassLoader.load_class(manager_class)( + self._inst[manager_type] = manager_class( self.root_profile, production_ledgers=indy_vdr_production_ledgers, non_production_ledgers=indy_vdr_non_production_ledgers, @@ -160,4 +170,4 @@ def provide(self, settings: BaseSettings, injector: BaseInjector): f"Unknown multiple Indy ledger manager type: {manager_type}" ) from err - return self._inst[manager_class] + return self._inst[manager_type] diff --git a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_ledger_requests.py b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_ledger_requests.py index f2e8a60f61..52e6d52f41 100644 --- a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_ledger_requests.py +++ b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_ledger_requests.py @@ -54,7 +54,10 @@ async def test_get_ledger_for_identifier(self): assert ledger_inst.pool.name == "test_prod_1" async def test_get_ledger_for_identifier_is_digit(self): - ledger = await self.indy_ledger_requestor.get_ledger_for_identifier("123", 0) + ledger_id, ledger = await self.indy_ledger_requestor.get_ledger_for_identifier( + "123", 0 + ) + assert ledger_id is None assert ledger == self.ledger async def test_get_ledger_for_identifier_x(self): @@ -70,17 +73,19 @@ async def test_get_ledger_for_identifier_x(self): ), ) self.indy_ledger_requestor = IndyLedgerRequestsExecutor(self.profile) - ledger = await self.indy_ledger_requestor.get_ledger_for_identifier( + ledger_id, ledger = await self.indy_ledger_requestor.get_ledger_for_identifier( "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0", 0 ) + assert ledger_id is None assert ledger == self.ledger async def test_get_ledger_for_identifier_mult_ledger_not_set(self): self.profile.settings["ledger.ledger_config_list"] = None self.indy_ledger_requestor = IndyLedgerRequestsExecutor(self.profile) - ledger = await self.indy_ledger_requestor.get_ledger_for_identifier( + ledger_id, ledger = await self.indy_ledger_requestor.get_ledger_for_identifier( "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0", 0 ) + assert ledger_id is None assert ledger == self.ledger async def test_get_ledger_for_identifier_mult_ledger_not_cached(self): diff --git a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py index b59408c028..8b7d4740dc 100644 --- a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py +++ b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py @@ -1,5 +1,6 @@ import asyncio from copy import deepcopy +import pytest import json from asynctest import TestCase as AsyncTestCase @@ -21,6 +22,7 @@ from ..indy_manager import MultiIndyLedgerManager +@pytest.mark.indy class TestMultiIndyLedgerManager(AsyncTestCase): async def setUp(self): self.profile = InMemoryProfile.test_profile(bind={BaseCache: InMemoryCache()}) diff --git a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_vdr_manager.py b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_vdr_manager.py index 19f440ca38..78942ff83e 100644 --- a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_vdr_manager.py +++ b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_vdr_manager.py @@ -1,5 +1,6 @@ import asyncio import json +import pytest from asynctest import TestCase as AsyncTestCase from asynctest import mock as async_mock @@ -21,6 +22,7 @@ from ..indy_vdr_manager import MultiIndyVDRLedgerManager +@pytest.mark.indy_vdr class TestMultiIndyVDRLedgerManager(AsyncTestCase): async def setUp(self): self.profile = InMemoryProfile.test_profile(bind={BaseCache: InMemoryCache()}) diff --git a/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py b/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py index c050bd15d0..bf876a7a51 100644 --- a/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py +++ b/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py @@ -1,10 +1,11 @@ -from asynctest import TestCase as AsyncTestCase, mock as async_mock +import pytest -from aries_cloudagent.indy.sdk.profile import IndySdkProfile +from asynctest import TestCase as AsyncTestCase, mock as async_mock from ....askar.profile import AskarProfileManager from ....config.injection_context import InjectionContext from ....core.in_memory import InMemoryProfile +from ....indy.sdk.profile import IndySdkProfile from ....indy.sdk.wallet_setup import IndyOpenWallet, IndyWalletConfig from ....ledger.base import BaseLedger from ....ledger.indy import IndySdkLedgerPool, IndySdkLedger @@ -62,6 +63,7 @@ async def test_provide_invalid_manager(self): with self.assertRaises(MultipleLedgerManagerError): provider.provide(context.settings, context.injector) + @pytest.mark.indy async def test_provide_indy_manager(self): context = InjectionContext() profile = IndySdkProfile( @@ -84,6 +86,7 @@ async def test_provide_indy_manager(self): "MultiIndyLedgerManager", ) + @pytest.mark.askar async def test_provide_askar_manager(self): context = InjectionContext() profile = await AskarProfileManager().provision( diff --git a/aries_cloudagent/ledger/routes.py b/aries_cloudagent/ledger/routes.py index ad51fb3d0e..dda91013fd 100644 --- a/aries_cloudagent/ledger/routes.py +++ b/aries_cloudagent/ledger/routes.py @@ -242,18 +242,12 @@ async def get_nym_role(request: web.BaseRequest): if not did: raise web.HTTPBadRequest(reason="Request query must include DID") - ledger_id = None async with context.profile.session() as session: ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( did, txn_record_type=GET_NYM_ROLE, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No Indy ledger available" if not session.settings.get_value("wallet.type"): @@ -269,6 +263,7 @@ async def get_nym_role(request: web.BaseRequest): raise web.HTTPNotFound(reason=err.roll_up) except LedgerError as err: raise web.HTTPBadRequest(reason=err.roll_up) + if ledger_id: return web.json_response({"ledger_id": ledger_id, "role": role.name}) else: @@ -320,18 +315,12 @@ async def get_did_verkey(request: web.BaseRequest): if not did: raise web.HTTPBadRequest(reason="Request query must include DID") - ledger_id = None async with context.profile.session() as session: ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( did, txn_record_type=GET_KEY_FOR_DID, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No ledger available" if not session.settings.get_value("wallet.type"): @@ -345,6 +334,7 @@ async def get_did_verkey(request: web.BaseRequest): raise web.HTTPNotFound(reason=f"DID {did} is not on the ledger") except LedgerError as err: raise web.HTTPBadRequest(reason=err.roll_up) from err + if ledger_id: return web.json_response({"ledger_id": ledger_id, "verkey": result}) else: @@ -370,18 +360,12 @@ async def get_did_endpoint(request: web.BaseRequest): if not did: raise web.HTTPBadRequest(reason="Request query must include DID") - ledger_id = None async with context.profile.session() as session: ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( did, txn_record_type=GET_ENDPOINT_FOR_DID, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No Indy ledger available" if not session.settings.get_value("wallet.type"): @@ -396,6 +380,7 @@ async def get_did_endpoint(request: web.BaseRequest): r = await ledger.get_endpoint_for_did(did, endpoint_type) except LedgerError as err: raise web.HTTPBadRequest(reason=err.roll_up) from err + if ledger_id: return web.json_response({"ledger_id": ledger_id, "endpoint": r}) else: diff --git a/aries_cloudagent/ledger/tests/test_routes.py b/aries_cloudagent/ledger/tests/test_routes.py index ea085cfff4..8b2ed92a1a 100644 --- a/aries_cloudagent/ledger/tests/test_routes.py +++ b/aries_cloudagent/ledger/tests/test_routes.py @@ -45,7 +45,9 @@ async def test_missing_ledger(self): self.profile.context.injector.bind_instance( IndyLedgerRequestsExecutor, async_mock.MagicMock( - get_ledger_for_identifier=async_mock.CoroutineMock(return_value=None) + get_ledger_for_identifier=async_mock.CoroutineMock( + return_value=(None, None) + ) ), ) self.profile.context.injector.clear_binding(BaseLedger) @@ -79,7 +81,7 @@ async def test_get_verkey_a(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) @@ -141,7 +143,7 @@ async def test_get_verkey_x(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) @@ -155,7 +157,7 @@ async def test_get_endpoint(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) @@ -269,7 +271,7 @@ async def test_get_nym_role_a(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) @@ -346,7 +348,7 @@ async def test_get_nym_role_ledger_error(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) diff --git a/aries_cloudagent/messaging/credential_definitions/routes.py b/aries_cloudagent/messaging/credential_definitions/routes.py index 61f0624e6b..d547027d6d 100644 --- a/aries_cloudagent/messaging/credential_definitions/routes.py +++ b/aries_cloudagent/messaging/credential_definitions/routes.py @@ -357,18 +357,12 @@ async def credential_definitions_get_credential_definition(request: web.BaseRequ context: AdminRequestContext = request["context"] cred_def_id = request.match_info["cred_def_id"] - ledger_id = None async with context.profile.session() as session: ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( cred_def_id, txn_record_type=GET_CRED_DEF, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No ledger available" if not context.settings.get_value("wallet.type"): @@ -407,19 +401,13 @@ async def credential_definitions_fix_cred_def_wallet_record(request: web.BaseReq cred_def_id = request.match_info["cred_def_id"] - ledger_id = None async with context.profile.session() as session: storage = session.inject(BaseStorage) ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( cred_def_id, txn_record_type=GET_CRED_DEF, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No ledger available" if not context.settings.get_value("wallet.type"): @@ -445,6 +433,7 @@ async def credential_definitions_fix_cred_def_wallet_record(request: web.BaseReq await add_cred_def_non_secrets_record( session.profile, schema_id, iss_did, cred_def_id ) + if ledger_id: return web.json_response( {"ledger_id": ledger_id, "credential_definition": cred_def} diff --git a/aries_cloudagent/messaging/credential_definitions/tests/test_routes.py b/aries_cloudagent/messaging/credential_definitions/tests/test_routes.py index dd0ae0c716..a1e72ddf6e 100644 --- a/aries_cloudagent/messaging/credential_definitions/tests/test_routes.py +++ b/aries_cloudagent/messaging/credential_definitions/tests/test_routes.py @@ -350,7 +350,9 @@ async def test_get_credential_definition_no_ledger(self): self.profile_injector.bind_instance( IndyLedgerRequestsExecutor, async_mock.MagicMock( - get_ledger_for_identifier=async_mock.CoroutineMock(return_value=None) + get_ledger_for_identifier=async_mock.CoroutineMock( + return_value=(None, None) + ) ), ) self.request.match_info = {"cred_def_id": CRED_DEF_ID} diff --git a/aries_cloudagent/messaging/schemas/routes.py b/aries_cloudagent/messaging/schemas/routes.py index 5f73364ccd..5c7c6eb7fb 100644 --- a/aries_cloudagent/messaging/schemas/routes.py +++ b/aries_cloudagent/messaging/schemas/routes.py @@ -337,18 +337,12 @@ async def schemas_get_schema(request: web.BaseRequest): context: AdminRequestContext = request["context"] schema_id = request.match_info["schema_id"] - ledger_id = None async with context.profile.session() as session: ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( schema_id, txn_record_type=GET_SCHEMA, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No ledger available" if not context.settings.get_value("wallet.type"): @@ -360,6 +354,7 @@ async def schemas_get_schema(request: web.BaseRequest): schema = await ledger.get_schema(schema_id) except LedgerError as err: raise web.HTTPBadRequest(reason=err.roll_up) from err + if ledger_id: return web.json_response({"ledger_id": ledger_id, "schema": schema}) else: @@ -386,19 +381,13 @@ async def schemas_fix_schema_wallet_record(request: web.BaseRequest): schema_id = request.match_info["schema_id"] - ledger_id = None async with profile.session() as session: storage = session.inject(BaseStorage) ledger_exec_inst = session.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( + ledger_id, ledger = await ledger_exec_inst.get_ledger_for_identifier( schema_id, txn_record_type=GET_SCHEMA, ) - if isinstance(ledger_info, tuple): - ledger_id = ledger_info[0] - ledger = ledger_info[1] - else: - ledger = ledger_info if not ledger: reason = "No ledger available" if not context.settings.get_value("wallet.type"): @@ -420,6 +409,7 @@ async def schemas_fix_schema_wallet_record(request: web.BaseRequest): await add_schema_non_secrets_record(profile, schema_id) except LedgerError as err: raise web.HTTPBadRequest(reason=err.roll_up) from err + if ledger_id: return web.json_response({"ledger_id": ledger_id, "schema": schema}) else: diff --git a/aries_cloudagent/messaging/schemas/tests/test_routes.py b/aries_cloudagent/messaging/schemas/tests/test_routes.py index ca45372902..ff445535ff 100644 --- a/aries_cloudagent/messaging/schemas/tests/test_routes.py +++ b/aries_cloudagent/messaging/schemas/tests/test_routes.py @@ -311,7 +311,7 @@ async def test_get_schema_on_seq_no(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) @@ -327,7 +327,9 @@ async def test_get_schema_no_ledger(self): self.profile_injector.bind_instance( IndyLedgerRequestsExecutor, async_mock.MagicMock( - get_ledger_for_identifier=async_mock.CoroutineMock(return_value=None) + get_ledger_for_identifier=async_mock.CoroutineMock( + return_value=(None, None) + ) ), ) self.request.match_info = {"schema_id": SCHEMA_ID} @@ -344,7 +346,7 @@ async def test_get_schema_x_ledger(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) diff --git a/aries_cloudagent/protocols/issue_credential/v1_0/manager.py b/aries_cloudagent/protocols/issue_credential/v1_0/manager.py index 8dc8116888..eb79db2973 100644 --- a/aries_cloudagent/protocols/issue_credential/v1_0/manager.py +++ b/aries_cloudagent/protocols/issue_credential/v1_0/manager.py @@ -265,14 +265,12 @@ async def _create(cred_def_id): # vet attributes ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cred_def_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cred_def_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: schema_id = await ledger.credential_definition_id2schema_id(cred_def_id) schema = await ledger.get_schema(schema_id) @@ -405,14 +403,12 @@ async def create_request( async def _create(): ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - credential_definition_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + credential_definition_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: credential_definition = await ledger.get_credential_definition( credential_definition_id @@ -553,14 +549,12 @@ async def issue_credential( cred_offer_ser = cred_ex_record._credential_offer.ser cred_req_ser = cred_ex_record._credential_request.ser ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - schema_id, - txn_record_type=GET_SCHEMA, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + schema_id, + txn_record_type=GET_SCHEMA, + ) + )[1] async with ledger: schema = await ledger.get_schema(schema_id) credential_definition = await ledger.get_credential_definition( @@ -767,14 +761,12 @@ async def store_credential( raw_cred_serde = cred_ex_record._raw_credential revoc_reg_def = None ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - raw_cred_serde.de.cred_def_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + raw_cred_serde.de.cred_def_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: credential_definition = await ledger.get_credential_definition( raw_cred_serde.de.cred_def_id diff --git a/aries_cloudagent/protocols/issue_credential/v1_0/tests/test_manager.py b/aries_cloudagent/protocols/issue_credential/v1_0/tests/test_manager.py index 4e8f52b4b7..5084eebcca 100644 --- a/aries_cloudagent/protocols/issue_credential/v1_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/issue_credential/v1_0/tests/test_manager.py @@ -74,7 +74,7 @@ async def setUp(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/handler.py index 9604b9e278..2257a91cd7 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/handler.py @@ -202,14 +202,12 @@ async def _create(): return json.loads(offer_json) ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cred_def_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cred_def_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: schema_id = await ledger.credential_definition_id2schema_id(cred_def_id) schema = await ledger.get_schema(schema_id) @@ -265,14 +263,12 @@ async def create_request( async def _create(): ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cred_def_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cred_def_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: cred_def = await ledger.get_credential_definition(cred_def_id) @@ -335,14 +331,12 @@ async def issue_credential( rev_reg_id = None rev_reg = None ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - schema_id, - txn_record_type=GET_SCHEMA, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + schema_id, + txn_record_type=GET_SCHEMA, + ) + )[1] async with ledger: schema = await ledger.get_schema(schema_id) cred_def = await ledger.get_credential_definition(cred_def_id) @@ -481,14 +475,12 @@ async def store_credential( rev_reg_def = None ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cred["cred_def_id"], - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cred["cred_def_id"], + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: cred_def = await ledger.get_credential_definition(cred["cred_def_id"]) if cred.get("rev_reg_id"): diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/tests/test_handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/tests/test_handler.py index 55dfc0e378..9bffbad2a9 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/tests/test_handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/indy/tests/test_handler.py @@ -221,7 +221,7 @@ async def setUp(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) diff --git a/aries_cloudagent/protocols/present_proof/indy/pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/indy/pres_exch_handler.py index c2cce65d4b..56cec44dfd 100644 --- a/aries_cloudagent/protocols/present_proof/indy/pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/indy/pres_exch_handler.py @@ -94,14 +94,12 @@ async def return_presentation( for credential in credentials.values(): schema_id = credential["schema_id"] ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - schema_id, - txn_record_type=GET_SCHEMA, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + schema_id, + txn_record_type=GET_SCHEMA, + ) + )[1] async with ledger: if schema_id not in schemas: schemas[schema_id] = await ledger.get_schema(schema_id) @@ -130,14 +128,12 @@ async def return_presentation( continue rev_reg_id = credentials[credential_id]["rev_reg_id"] ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - rev_reg_id, - txn_record_type=GET_REVOC_REG_DELTA, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + rev_reg_id, + txn_record_type=GET_REVOC_REG_DELTA, + ) + )[1] async with ledger: reft_non_revoc_interval = precis.get("non_revoked") if reft_non_revoc_interval: @@ -227,14 +223,12 @@ async def process_pres_identifiers( schema_ids.append(identifier["schema_id"]) cred_def_ids.append(identifier["cred_def_id"]) ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - identifier["schema_id"], - txn_record_type=GET_SCHEMA, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + identifier["schema_id"], + txn_record_type=GET_SCHEMA, + ) + )[1] async with ledger: # Build schemas for anoncreds if identifier["schema_id"] not in schemas: diff --git a/aries_cloudagent/protocols/present_proof/v1_0/tests/test_manager.py b/aries_cloudagent/protocols/present_proof/v1_0/tests/test_manager.py index 8fb587e37a..4e0d9d3580 100644 --- a/aries_cloudagent/protocols/present_proof/v1_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/present_proof/v1_0/tests/test_manager.py @@ -267,7 +267,7 @@ async def setUp(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) diff --git a/aries_cloudagent/protocols/present_proof/v2_0/tests/test_manager.py b/aries_cloudagent/protocols/present_proof/v2_0/tests/test_manager.py index 150f1c7717..bf293f3cb6 100644 --- a/aries_cloudagent/protocols/present_proof/v2_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/present_proof/v2_0/tests/test_manager.py @@ -427,7 +427,7 @@ async def setUp(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), ) diff --git a/aries_cloudagent/resolver/default/indy.py b/aries_cloudagent/resolver/default/indy.py index 566e5ce22c..a7364306d9 100644 --- a/aries_cloudagent/resolver/default/indy.py +++ b/aries_cloudagent/resolver/default/indy.py @@ -45,14 +45,12 @@ def supported_did_regex(self) -> Pattern: async def _resolve(self, profile: Profile, did: str) -> dict: """Resolve an indy DID.""" ledger_exec_inst = profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - did, - txn_record_type=GET_KEY_FOR_DID, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + did, + txn_record_type=GET_KEY_FOR_DID, + ) + )[1] if not ledger: raise NoIndyLedger("No Indy ledger instance is configured.") diff --git a/aries_cloudagent/resolver/default/tests/test_indy.py b/aries_cloudagent/resolver/default/tests/test_indy.py index 21c8744644..f9d4ad944a 100644 --- a/aries_cloudagent/resolver/default/tests/test_indy.py +++ b/aries_cloudagent/resolver/default/tests/test_indy.py @@ -45,7 +45,9 @@ def profile(ledger): profile.context.injector.bind_instance( IndyLedgerRequestsExecutor, async_mock.MagicMock( - get_ledger_for_identifier=async_mock.CoroutineMock(return_value=ledger) + get_ledger_for_identifier=async_mock.CoroutineMock( + return_value=(None, ledger) + ) ), ) yield profile @@ -71,7 +73,9 @@ async def test_resolve_x_no_ledger( profile.context.injector.bind_instance( IndyLedgerRequestsExecutor, async_mock.MagicMock( - get_ledger_for_identifier=async_mock.CoroutineMock(return_value=None) + get_ledger_for_identifier=async_mock.CoroutineMock( + return_value=(None, None) + ) ), ) with pytest.raises(ResolverError): diff --git a/aries_cloudagent/revocation/indy.py b/aries_cloudagent/revocation/indy.py index 70c2877d5e..3dcab450f7 100644 --- a/aries_cloudagent/revocation/indy.py +++ b/aries_cloudagent/revocation/indy.py @@ -33,14 +33,12 @@ async def init_issuer_registry( ) -> "IssuerRevRegRecord": """Create a new revocation registry record for a credential definition.""" ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - cred_def_id, - txn_record_type=GET_CRED_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + cred_def_id, + txn_record_type=GET_CRED_DEF, + ) + )[1] async with ledger: cred_def = await ledger.get_credential_definition(cred_def_id) if not cred_def: @@ -111,14 +109,12 @@ async def get_ledger_registry(self, revoc_reg_id: str) -> "RevocationRegistry": return IndyRevocation.REV_REG_CACHE[revoc_reg_id] ledger_exec_inst = self._profile.inject(IndyLedgerRequestsExecutor) - ledger_info = await ledger_exec_inst.get_ledger_for_identifier( - revoc_reg_id, - txn_record_type=GET_REVOC_REG_DEF, - ) - if isinstance(ledger_info, tuple): - ledger = ledger_info[1] - else: - ledger = ledger_info + ledger = ( + await ledger_exec_inst.get_ledger_for_identifier( + revoc_reg_id, + txn_record_type=GET_REVOC_REG_DEF, + ) + )[1] async with ledger: rev_reg = RevocationRegistry.from_definition( await ledger.get_revoc_reg_def(revoc_reg_id), True diff --git a/aries_cloudagent/revocation/tests/test_indy.py b/aries_cloudagent/revocation/tests/test_indy.py index 2a4a0445cd..10708c6d28 100644 --- a/aries_cloudagent/revocation/tests/test_indy.py +++ b/aries_cloudagent/revocation/tests/test_indy.py @@ -35,7 +35,7 @@ def setUp(self): IndyLedgerRequestsExecutor, async_mock.MagicMock( get_ledger_for_identifier=async_mock.CoroutineMock( - return_value=self.ledger + return_value=(None, self.ledger) ) ), )