Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove required dependencies from multi-ledger code #1550

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions aries_cloudagent/core/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/core/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/indy/credx/tests/test_cred_issuance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
),
)
Expand Down
28 changes: 12 additions & 16 deletions aries_cloudagent/indy/models/pres_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))[
Expand Down Expand Up @@ -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))[
Expand Down
22 changes: 14 additions & 8 deletions aries_cloudagent/indy/models/tests/test_pres_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
Expand Down Expand Up @@ -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")},
Expand Down
10 changes: 5 additions & 5 deletions aries_cloudagent/indy/sdk/tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
25 changes: 14 additions & 11 deletions aries_cloudagent/indy/tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions aries_cloudagent/indy/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/ledger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions aries_cloudagent/ledger/multiple_ledger/base_manager.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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))
Loading