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

🐛 Fix IndyVdrLedgerPool RuntimeWarning #3362

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
56 changes: 31 additions & 25 deletions acapy_agent/askar/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,25 @@ def __init__(
*,
profile_id: Optional[str] = None,
):
"""Create a new AskarProfile instance."""
"""Private constructor. Use 'create' to instantiate."""
super().__init__(context=context, name=opened.name, created=opened.created)
self.opened = opened
self.ledger_pool: Optional[IndyVdrLedgerPool] = None
self.profile_id = profile_id
self.init_ledger_pool()
self.bind_providers()

@classmethod
async def create(
cls,
opened: AskarOpenStore,
context: Optional[InjectionContext] = None,
*,
profile_id: Optional[str] = None,
) -> "AskarProfile":
"""Asynchronously create a new AskarProfile instance."""
profile = cls(opened, context, profile_id=profile_id)
await profile.init_ledger_pool()
await profile.bind_providers()
return profile

@property
def name(self) -> str:
Expand All @@ -64,7 +76,7 @@ async def remove(self):
if self.profile_id:
await self.store.remove_profile(self.profile_id)

def init_ledger_pool(self):
async def init_ledger_pool(self):
"""Initialize the ledger pool."""
if self.settings.get("ledger.disabled"):
LOGGER.info("Ledger support is disabled")
Expand All @@ -78,16 +90,16 @@ def init_ledger_pool(self):
LOGGER.warning("Note: setting ledger to read-only mode")
genesis_transactions = self.settings.get("ledger.genesis_transactions")
cache = self.context.injector.inject_or(BaseCache)
self.ledger_pool = IndyVdrLedgerPool(
pool_name,
self.ledger_pool = await IndyVdrLedgerPool.get_or_create(
name=pool_name,
keepalive=keepalive,
cache=cache,
genesis_transactions=genesis_transactions,
read_only=read_only,
socks_proxy=socks_proxy,
)

def bind_providers(self):
async def bind_providers(self):
"""Initialize the profile-level instance providers."""
injector = self._context.injector

Expand Down Expand Up @@ -134,23 +146,17 @@ def bind_providers(self):
settings=self.settings
)
cache = self.context.injector.inject_or(BaseCache)
ledger_pool = await IndyVdrLedgerPool.get_or_create(
name=write_ledger_config.get("pool_name")
or write_ledger_config.get("id"),
keepalive=write_ledger_config.get("keepalive"),
cache=cache,
genesis_transactions=write_ledger_config.get("genesis_transactions"),
read_only=write_ledger_config.get("read_only"),
socks_proxy=write_ledger_config.get("socks_proxy"),
)
injector.bind_provider(
BaseLedger,
ClassProvider(
IndyVdrLedger,
IndyVdrLedgerPool(
write_ledger_config.get("pool_name")
or write_ledger_config.get("id"),
keepalive=write_ledger_config.get("keepalive"),
cache=cache,
genesis_transactions=write_ledger_config.get(
"genesis_transactions"
),
read_only=write_ledger_config.get("read_only"),
socks_proxy=write_ledger_config.get("socks_proxy"),
),
ref(self),
),
BaseLedger, ClassProvider(IndyVdrLedger, ledger_pool, ref(self))
)
self.settings["ledger.write_ledger"] = write_ledger_config.get("id")
if (
Expand Down Expand Up @@ -327,7 +333,7 @@ async def provision(
opened = await store_config.open_store(
provision=True, in_memory=config.get("test")
)
return AskarProfile(opened, context)
return await AskarProfile.create(opened, context)

async def open(
self, context: InjectionContext, config: Mapping[str, Any] = None
Expand All @@ -337,7 +343,7 @@ async def open(
opened = await store_config.open_store(
provision=False, in_memory=config.get("test")
)
return AskarProfile(opened, context)
return await AskarProfile.create(opened, context)

@classmethod
async def generate_store_key(self, seed: Optional[str] = None) -> str:
Expand Down
56 changes: 31 additions & 25 deletions acapy_agent/askar/profile_anon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,25 @@ def __init__(
*,
profile_id: Optional[str] = None,
):
"""Create a new AskarProfile instance."""
"""Create a new AskarAnoncredsProfile instance."""
super().__init__(context=context, name=opened.name, created=opened.created)
self.opened = opened
self.ledger_pool: Optional[IndyVdrLedgerPool] = None
self.profile_id = profile_id
self.init_ledger_pool()
self.bind_providers()

@classmethod
async def create(
cls,
opened: AskarOpenStore,
context: Optional[InjectionContext] = None,
*,
profile_id: Optional[str] = None,
) -> "AskarAnoncredsProfile":
"""Asynchronously create a new AskarAnoncredsProfile instance."""
profile = cls(opened, context, profile_id=profile_id)
await profile.init_ledger_pool()
await profile.bind_providers()
return profile

@property
def name(self) -> str:
Expand All @@ -66,7 +78,7 @@ async def remove(self):
if self.profile_id:
await self.store.remove_profile(self.profile_id)

def init_ledger_pool(self):
async def init_ledger_pool(self):
"""Initialize the ledger pool."""
if self.settings.get("ledger.disabled"):
LOGGER.info("Ledger support is disabled")
Expand All @@ -80,16 +92,16 @@ def init_ledger_pool(self):
LOGGER.warning("Note: setting ledger to read-only mode")
genesis_transactions = self.settings.get("ledger.genesis_transactions")
cache = self.context.injector.inject_or(BaseCache)
self.ledger_pool = IndyVdrLedgerPool(
pool_name,
self.ledger_pool = await IndyVdrLedgerPool.get_or_create(
name=pool_name,
keepalive=keepalive,
cache=cache,
genesis_transactions=genesis_transactions,
read_only=read_only,
socks_proxy=socks_proxy,
)

def bind_providers(self):
async def bind_providers(self):
"""Initialize the profile-level instance providers."""
injector = self._context.injector

Expand Down Expand Up @@ -123,23 +135,17 @@ def bind_providers(self):
settings=self.settings
)
cache = self.context.injector.inject_or(BaseCache)
ledger_pool = await IndyVdrLedgerPool.get_or_create(
name=write_ledger_config.get("pool_name")
or write_ledger_config.get("id"),
keepalive=write_ledger_config.get("keepalive"),
cache=cache,
genesis_transactions=write_ledger_config.get("genesis_transactions"),
read_only=write_ledger_config.get("read_only"),
socks_proxy=write_ledger_config.get("socks_proxy"),
)
injector.bind_provider(
BaseLedger,
ClassProvider(
IndyVdrLedger,
IndyVdrLedgerPool(
write_ledger_config.get("pool_name")
or write_ledger_config.get("id"),
keepalive=write_ledger_config.get("keepalive"),
cache=cache,
genesis_transactions=write_ledger_config.get(
"genesis_transactions"
),
read_only=write_ledger_config.get("read_only"),
socks_proxy=write_ledger_config.get("socks_proxy"),
),
ref(self),
),
BaseLedger, ClassProvider(IndyVdrLedger, ledger_pool, ref(self))
)
self.settings["ledger.write_ledger"] = write_ledger_config.get("id")
if (
Expand Down Expand Up @@ -280,7 +286,7 @@ async def provision(
opened = await store_config.open_store(
provision=True, in_memory=config.get("test")
)
return AskarAnoncredsProfile(opened, context)
return await AskarAnoncredsProfile.create(opened, context)

async def open(
self, context: InjectionContext, config: Mapping[str, Any] = None
Expand All @@ -290,7 +296,7 @@ async def open(
opened = await store_config.open_store(
provision=False, in_memory=config.get("test")
)
return AskarAnoncredsProfile(opened, context)
return await AskarAnoncredsProfile.create(opened, context)

@classmethod
async def generate_store_key(self, seed: Optional[str] = None) -> str:
Expand Down
10 changes: 5 additions & 5 deletions acapy_agent/askar/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def open_store():

@pytest.mark.asyncio
async def test_init_success(open_store):
askar_profile = AskarProfile(
askar_profile = await AskarProfile.create(
open_store,
)

Expand Down Expand Up @@ -45,7 +45,7 @@ async def test_init_multi_ledger(open_store):
]
}
)
askar_profile = AskarProfile(
askar_profile = await AskarProfile.create(
open_store,
context=context,
)
Expand All @@ -68,7 +68,7 @@ async def test_remove_success(open_store):
"wallet.askar_profile": profile_id,
"ledger.genesis_transactions": mock.MagicMock(),
}
askar_profile = AskarProfile(openStore, context, profile_id=profile_id)
askar_profile = await AskarProfile.create(openStore, context, profile_id=profile_id)
remove_profile_stub = asyncio.Future()
remove_profile_stub.set_result(True)
openStore.store.remove_profile.return_value = remove_profile_stub
Expand All @@ -83,7 +83,7 @@ async def test_remove_profile_not_removed_if_wallet_type_not_askar_profile(open_
openStore = open_store
context = InjectionContext()
context.settings = {"multitenant.wallet_type": "basic"}
askar_profile = AskarProfile(openStore, context)
askar_profile = await AskarProfile.create(openStore, context)

await askar_profile.remove()

Expand All @@ -95,7 +95,7 @@ async def test_profile_manager_transaction():
profile = "profileId"

with mock.patch("acapy_agent.askar.profile.AskarProfile") as AskarProfile:
askar_profile = AskarProfile(None, True, profile_id=profile)
askar_profile = await AskarProfile.create(None, True, profile_id=profile)
askar_profile.profile_id = profile
askar_profile_transaction = mock.MagicMock()
askar_profile.store.transaction.return_value = askar_profile_transaction
Expand Down
Loading
Loading