Skip to content

Commit

Permalink
use transaction as async context for proper termination
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Jan 8, 2022
1 parent e953cf3 commit e8d90d4
Showing 1 changed file with 57 additions and 53 deletions.
110 changes: 57 additions & 53 deletions aries_cloudagent/indy/credx/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,63 +279,67 @@ async def create_credential(

if revoc_reg_id:
try:
txn = await self._profile.transaction()
rev_reg = await txn.handle.fetch(CATEGORY_REV_REG, revoc_reg_id)
rev_reg_info = await txn.handle.fetch(
CATEGORY_REV_REG_INFO, revoc_reg_id, for_update=True
)
rev_reg_def = await txn.handle.fetch(CATEGORY_REV_REG_DEF, revoc_reg_id)
rev_key = await txn.handle.fetch(
CATEGORY_REV_REG_DEF_PRIVATE, revoc_reg_id
)
if not rev_reg:
raise IndyIssuerError("Revocation registry not found")
if not rev_reg_info:
raise IndyIssuerError("Revocation registry metadata not found")
if not rev_reg_def:
raise IndyIssuerError("Revocation registry definition not found")
if not rev_key:
raise IndyIssuerError(
"Revocation registry definition private data not found"
async with self._profile.transaction() as txn:
rev_reg = await txn.handle.fetch(CATEGORY_REV_REG, revoc_reg_id)
rev_reg_info = await txn.handle.fetch(
CATEGORY_REV_REG_INFO, revoc_reg_id, for_update=True
)
# NOTE: we increment the index ahead of time to keep the
# transaction short. The revocation registry itself will NOT
# be updated because we always use ISSUANCE_BY_DEFAULT.
# If something goes wrong later, the index will be skipped.
# FIXME - double check issuance type in case of upgraded wallet?
rev_info = rev_reg_info.value_json
rev_reg_index = rev_info["curr_id"] + 1
try:
rev_reg_def = RevocationRegistryDefinition.load(
rev_reg_def.raw_value
rev_reg_def = await txn.handle.fetch(
CATEGORY_REV_REG_DEF, revoc_reg_id
)
except CredxError as err:
raise IndyIssuerError(
"Error loading revocation registry definition"
) from err
if rev_reg_index > rev_reg_def.max_cred_num:
raise IndyIssuerRevocationRegistryFullError(
"Revocation registry is full"
rev_key = await txn.handle.fetch(
CATEGORY_REV_REG_DEF_PRIVATE, revoc_reg_id
)
if not rev_reg:
raise IndyIssuerError("Revocation registry not found")
if not rev_reg_info:
raise IndyIssuerError("Revocation registry metadata not found")
if not rev_reg_def:
raise IndyIssuerError(
"Revocation registry definition not found"
)
if not rev_key:
raise IndyIssuerError(
"Revocation registry definition private data not found"
)
# NOTE: we increment the index ahead of time to keep the
# transaction short. The revocation registry itself will NOT
# be updated because we always use ISSUANCE_BY_DEFAULT.
# If something goes wrong later, the index will be skipped.
# FIXME - double check issuance type in case of upgraded wallet?
rev_info = rev_reg_info.value_json
rev_reg_index = rev_info["curr_id"] + 1
try:
rev_reg_def = RevocationRegistryDefinition.load(
rev_reg_def.raw_value
)
except CredxError as err:
raise IndyIssuerError(
"Error loading revocation registry definition"
) from err
if rev_reg_index > rev_reg_def.max_cred_num:
raise IndyIssuerRevocationRegistryFullError(
"Revocation registry is full"
)
rev_info["curr_id"] = rev_reg_index
await txn.handle.replace(
CATEGORY_REV_REG_INFO, revoc_reg_id, value_json=rev_info
)
rev_info["curr_id"] = rev_reg_index
await txn.handle.replace(
CATEGORY_REV_REG_INFO, revoc_reg_id, value_json=rev_info
)

issuer_cr_rec = IssuerCredRevRecord(
state=IssuerCredRevRecord.STATE_ISSUED,
cred_ex_id=cred_ex_id,
rev_reg_id=revoc_reg_id,
cred_rev_id=str(rev_reg_index),
)
await issuer_cr_rec.save(
txn,
reason=(
"Created issuer cred rev record for "
f"rev reg id {revoc_reg_id}, {rev_reg_index}"
),
)
await txn.commit()
issuer_cr_rec = IssuerCredRevRecord(
state=IssuerCredRevRecord.STATE_ISSUED,
cred_ex_id=cred_ex_id,
rev_reg_id=revoc_reg_id,
cred_rev_id=str(rev_reg_index),
)
await issuer_cr_rec.save(
txn,
reason=(
"Created issuer cred rev record for "
f"rev reg id {revoc_reg_id}, {rev_reg_index}"
),
)
await txn.commit()
except AskarError as err:
raise IndyIssuerError(
"Error updating revocation registry index"
Expand Down

0 comments on commit e8d90d4

Please sign in to comment.