Skip to content

Commit

Permalink
✨ Faster uuid generation (#2994)
Browse files Browse the repository at this point in the history
* ➕ add `uuid_utils`

Signed-off-by: ff137 <ff137@proton.me>

* ✨ replace default uuid method with 10x faster one

Signed-off-by: ff137 <ff137@proton.me>

* 🎨 organise imports after change

Signed-off-by: ff137 <ff137@proton.me>

* ✅ fix test

Signed-off-by: ff137 <ff137@proton.me>

* Update lock file

Signed-off-by: ff137 <ff137@proton.me>

---------

Signed-off-by: ff137 <ff137@proton.me>
  • Loading branch information
ff137 authored May 31, 2024
1 parent 854b7a5 commit 304106a
Show file tree
Hide file tree
Showing 30 changed files with 211 additions and 150 deletions.
9 changes: 3 additions & 6 deletions aries_cloudagent/admin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
import asyncio
import logging
import re
import uuid
import warnings
import weakref
from typing import Callable, Coroutine, Optional, Pattern, Sequence, cast

import aiohttp_cors
import jwt
from aiohttp import web
from aiohttp_apispec import (
setup_aiohttp_apispec,
validation_middleware,
)
from aiohttp_apispec import setup_aiohttp_apispec, validation_middleware
from uuid_utils import uuid4

from aries_cloudagent.wallet import singletons

Expand Down Expand Up @@ -548,7 +545,7 @@ async def websocket_handler(self, request):

ws = web.WebSocketResponse()
await ws.prepare(request)
socket_id = str(uuid.uuid4())
socket_id = str(uuid4())
queue = BasicMessageQueue()
loop = asyncio.get_event_loop()

Expand Down
16 changes: 7 additions & 9 deletions aries_cloudagent/anoncreds/default/legacy_indy/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import json
import logging
import re
import uuid
from asyncio import shield
from typing import List, Optional, Pattern, Sequence, Tuple

from base58 import alphabet
from uuid_utils import uuid4

from ....anoncreds.default.legacy_indy.author import get_endorser_info
from ....cache.base import BaseCache
Expand All @@ -32,9 +32,7 @@
TransactionManagerError,
)
from ....protocols.endorse_transaction.v1_0.util import is_author_role
from ....revocation_anoncreds.models.issuer_cred_rev_record import (
IssuerCredRevRecord,
)
from ....revocation_anoncreds.models.issuer_cred_rev_record import IssuerCredRevRecord
from ....revocation_anoncreds.recover import generate_ledger_rrrecovery_txn
from ....storage.error import StorageError
from ....utils import sentinel
Expand Down Expand Up @@ -267,7 +265,7 @@ async def register_schema(
# Need endorsement, so execute transaction flow
(schema_id, schema_def) = result

job_id = uuid.uuid4().hex
job_id = uuid4().hex
meta_data = {"context": {"job_id": job_id, "schema_id": schema_id}}

transaction_manager = TransactionManager(profile)
Expand Down Expand Up @@ -445,7 +443,7 @@ async def register_credential_definition(
)

# Need endorsement, so execute transaction flow
job_id = uuid.uuid4().hex
job_id = uuid4().hex

meta_data = {
"context": {
Expand Down Expand Up @@ -616,7 +614,7 @@ async def register_revocation_registry_definition(
# Need endorsement, so execute transaction flow
(rev_reg_def_id, reg_rev_def) = result

job_id = uuid.uuid4().hex
job_id = uuid4().hex
meta_data = {
"context": {
"job_id": job_id,
Expand Down Expand Up @@ -874,7 +872,7 @@ async def register_revocation_list(

(rev_reg_def_id, requested_txn) = result

job_id = uuid.uuid4().hex
job_id = uuid4().hex
meta_data = {
"context": {
"job_id": job_id,
Expand Down Expand Up @@ -987,7 +985,7 @@ async def update_revocation_list(

(rev_reg_def_id, requested_txn) = result

job_id = uuid.uuid4().hex
job_id = uuid4().hex
meta_data = {
"context": {
"job_id": job_id,
Expand Down
6 changes: 3 additions & 3 deletions aries_cloudagent/anoncreds/holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import logging
import re
import uuid
from typing import Dict, Optional, Sequence, Tuple, Union

from anoncreds import (
Expand All @@ -14,10 +13,11 @@
CredentialRevocationState,
Presentation,
PresentCredentials,
create_link_secret,
W3cCredential,
create_link_secret,
)
from aries_askar import AskarError, AskarErrorCode
from uuid_utils import uuid4

from ..anoncreds.models.anoncreds_schema import AnonCredsSchema
from ..askar.profile_anon import AskarAnoncredsProfile
Expand Down Expand Up @@ -238,7 +238,7 @@ async def _finish_store_credential(
f"Error parsing credential definition ID: {cred_def_id}"
)

credential_id = credential_id or str(uuid.uuid4())
credential_id = credential_id or str(uuid4())
tags = {
"schema_id": schema_id,
"schema_issuer_did": schema_id_parts[1],
Expand Down
7 changes: 2 additions & 5 deletions aries_cloudagent/anoncreds/revocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pathlib import Path
from typing import List, NamedTuple, Optional, Sequence, Tuple
from urllib.parse import urlparse
from uuid import uuid4

import base58
from anoncreds import (
Expand All @@ -23,13 +22,11 @@
)
from aries_askar.error import AskarError
from requests import RequestException, Session
from uuid_utils import uuid4

from aries_cloudagent.anoncreds.models.anoncreds_cred_def import CredDef

from ..askar.profile_anon import (
AskarAnoncredsProfile,
AskarAnoncredsProfileSession,
)
from ..askar.profile_anon import AskarAnoncredsProfile, AskarAnoncredsProfileSession
from ..core.error import BaseError
from ..core.event_bus import Event, EventBus
from ..core.profile import Profile, ProfileSession
Expand Down
9 changes: 4 additions & 5 deletions aries_cloudagent/config/ledger.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
"""Ledger configuration."""

from collections import OrderedDict
import logging
import re
import sys
from collections import OrderedDict
from typing import Optional
import uuid

import markdown
import prompt_toolkit
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
from prompt_toolkit.formatted_text import HTML
from uuid_utils import uuid4

from ..config.settings import Settings
from ..core.profile import Profile
from ..ledger.base import BaseLedger
from ..ledger.endpoint_type import EndpointType
from ..ledger.error import LedgerError
from ..utils.http import fetch, FetchError
from ..utils.http import FetchError, fetch
from ..wallet.base import BaseWallet

from .base import ConfigError

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,7 +87,7 @@ async def load_multiple_genesis_transactions_from_config(settings: Settings):
is_write_ledger = (
False if config.get("is_write") is None else config.get("is_write")
)
ledger_id = config.get("id") or str(uuid.uuid4())
ledger_id = config.get("id") or str(uuid4())
if is_write_ledger:
write_ledger_set = True
config_item = {
Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/indy/credx/holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import logging
import re
import uuid
from typing import Dict, Optional, Sequence, Tuple, Union

from aries_askar import AskarError, AskarErrorCode
Expand All @@ -17,6 +16,7 @@
Presentation,
PresentCredentials,
)
from uuid_utils import uuid4

from ...askar.profile import AskarProfile
from ...ledger.base import BaseLedger
Expand Down Expand Up @@ -195,7 +195,7 @@ async def store_credential(
f"Error parsing credential definition ID: {cred_def_id}"
)

credential_id = credential_id or str(uuid.uuid4())
credential_id = credential_id or str(uuid4())
tags = {
"schema_id": schema_id,
"schema_issuer_did": schema_id_parts[1],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Schema for configuring multiple ledgers."""

import uuid

from marshmallow import EXCLUDE, fields, pre_load
from uuid_utils import uuid4

from ...messaging.models.base import BaseModel, BaseModelSchema
from ...messaging.models.openapi import OpenAPISchema
Expand Down Expand Up @@ -56,7 +55,7 @@ class Meta:
def validate_id(self, data, **kwargs):
"""Check if id is present, if not then set to UUID4."""
if "id" not in data:
data["id"] = str(uuid.uuid4())
data["id"] = str(uuid4())
return data


Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/messaging/agent_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from collections import OrderedDict
from typing import Mapping, Optional, Text, Union
import uuid

from marshmallow import (
EXCLUDE,
Expand All @@ -13,6 +12,7 @@
pre_dump,
pre_load,
)
from uuid_utils import uuid4

from ..protocols.didcomm_prefix import DIDCommPrefix
from ..wallet.base import BaseWallet
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
self._message_id = _id
self._message_new_id = False
else:
self._message_id = str(uuid.uuid4())
self._message_id = str(uuid4())
self._message_new_id = True
self._message_decorators = (
_decorators if _decorators is not None else DecoratorSet()
Expand Down
10 changes: 5 additions & 5 deletions aries_cloudagent/messaging/decorators/attach_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import copy
import json
import uuid
from typing import Any, Mapping, Sequence, Tuple, Union

from marshmallow import EXCLUDE, fields, pre_load
from uuid_utils import uuid4

from ...did.did_key import DIDKey
from ...wallet.base import BaseWallet
Expand Down Expand Up @@ -620,7 +620,7 @@ def data_base64_string(
byte_count: optional attachment byte count
"""
return AttachDecorator(
ident=ident or str(uuid.uuid4()),
ident=ident or str(uuid4()),
description=description,
filename=filename,
mime_type="text/string",
Expand Down Expand Up @@ -655,7 +655,7 @@ def data_base64(
"""
return AttachDecorator(
ident=ident or str(uuid.uuid4()),
ident=ident or str(uuid4()),
description=description,
filename=filename,
mime_type="application/json",
Expand Down Expand Up @@ -692,7 +692,7 @@ def data_json(
"""
return AttachDecorator(
ident=ident or str(uuid.uuid4()),
ident=ident or str(uuid4()),
description=description,
filename=filename,
mime_type="application/json",
Expand Down Expand Up @@ -731,7 +731,7 @@ def data_links(
"""
return AttachDecorator(
ident=ident or str(uuid.uuid4()),
ident=ident or str(uuid4()),
description=description,
filename=filename,
mime_type=mime_type or "application/json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
import uuid
from copy import deepcopy
from datetime import datetime, timezone
from unittest import TestCase

import pytest
from uuid_utils import uuid4

from aries_cloudagent.wallet.base import BaseWallet

Expand Down Expand Up @@ -54,7 +54,7 @@
"rev_reg": None,
"witness": None,
}
IDENT = str(uuid.uuid4())
IDENT = str(uuid4())
DESCRIPTION = 'To one trained by "Bob," Truth can be found in a potato'
FILENAME = "potato.png"
MIME_TYPE = "image/png"
Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/messaging/models/base_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import json
import logging
import sys
import uuid
from datetime import datetime
from typing import Any, Mapping, Optional, Sequence, Type, TypeVar, Union

from marshmallow import fields
from uuid_utils import uuid4

from ...cache.base import BaseCache
from ...config.settings import BaseSettings
Expand Down Expand Up @@ -355,7 +355,7 @@ async def save(
new_record = False
else:
if not self._id:
self._id = str(uuid.uuid4())
self._id = str(uuid4())
self.created_at = self.updated_at
await storage.add_record(self.storage_record)
new_record = True
Expand Down
10 changes: 4 additions & 6 deletions aries_cloudagent/protocols/endorse_transaction/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import json
import logging
import uuid
from asyncio import shield

from uuid_utils import uuid4

from ....anoncreds.issuer import AnonCredsIssuer
from ....anoncreds.revocation import AnonCredsRevocation
from ....connections.models.conn_record import ConnRecord
Expand All @@ -22,10 +23,7 @@
from ....storage.error import StorageError, StorageNotFoundError
from ....transport.inbound.receipt import MessageReceipt
from ....wallet.base import BaseWallet
from ....wallet.util import (
notify_endorse_did_attrib_event,
notify_endorse_did_event,
)
from ....wallet.util import notify_endorse_did_attrib_event, notify_endorse_did_event
from .messages.cancel_transaction import CancelTransaction
from .messages.endorsed_transaction_response import EndorsedTransactionResponse
from .messages.refused_transaction_response import RefusedTransactionResponse
Expand Down Expand Up @@ -78,7 +76,7 @@ async def create_record(
"""

messages_attach_dict = {
"@id": str(uuid.uuid4()),
"@id": str(uuid4()),
"mime-type": "application/json",
"data": {"json": messages_attach},
}
Expand Down
Loading

0 comments on commit 304106a

Please sign in to comment.