Skip to content

Commit

Permalink
replase eth_utils to_checksum_address with a cached wrapper (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Cezar authored Sep 24, 2021
1 parent e2adbde commit 98f0044
Show file tree
Hide file tree
Showing 29 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/monitoring_service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pkg_resources
import structlog
from eth_utils import to_checksum_address
from flask import Flask
from flask_restful import Resource
from gevent.pywsgi import WSGIServer
Expand All @@ -15,6 +14,7 @@
from monitoring_service.constants import API_PATH, DEFAULT_INFO_MESSAGE
from monitoring_service.service import MonitoringService
from raiden_libs.api import ApiWithErrorHandler
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion src/monitoring_service/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import click
import structlog
from eth_utils import to_checksum_address
from web3 import Web3
from web3.contract import Contract

Expand All @@ -31,6 +30,7 @@
DEFAULT_API_PORT_MS,
DEFAULT_POLL_INTERVALL,
)
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion src/monitoring_service/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional, Union

import structlog
from eth_utils import decode_hex, encode_hex, to_canonical_address, to_checksum_address, to_hex
from eth_utils import decode_hex, encode_hex, to_canonical_address, to_hex

from monitoring_service.events import (
ActionClaimRewardTriggeredEvent,
Expand All @@ -26,6 +26,7 @@
TransactionHash,
)
from raiden_libs.database import BaseDatabase, hex256
from utils import to_checksum_address

SubEvent = Union[ActionMonitoringTriggeredEvent, ActionClaimRewardTriggeredEvent]

Expand Down
3 changes: 2 additions & 1 deletion src/monitoring_service/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Iterable, Optional

from eth_typing.evm import HexAddress
from eth_utils import decode_hex, encode_hex, to_checksum_address
from eth_utils import decode_hex, encode_hex
from web3 import Web3

from raiden.constants import EMPTY_SIGNATURE
Expand All @@ -27,6 +27,7 @@
from raiden_contracts.utils.proofs import pack_balance_proof, pack_reward_proof
from raiden_contracts.utils.type_aliases import PrivateKey
from raiden_libs.states import BlockchainState
from utils import to_checksum_address


@dataclass
Expand Down
8 changes: 2 additions & 6 deletions src/pathfinding_service/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import marshmallow
import pkg_resources
import structlog
from eth_utils import (
is_checksum_address,
is_same_address,
to_canonical_address,
to_checksum_address,
)
from eth_utils import is_checksum_address, is_same_address, to_canonical_address
from flask import Flask, Response, request
from flask_restful import Resource
from gevent.pywsgi import WSGIServer
Expand Down Expand Up @@ -55,6 +50,7 @@
from raiden_libs.constants import UDC_SECURITY_MARGIN_FACTOR_PFS
from raiden_libs.exceptions import ApiException
from raiden_libs.marshmallow import ChecksumAddress, HexedBytes
from utils import to_checksum_address

log = structlog.get_logger(__name__)
T = TypeVar("T")
Expand Down
3 changes: 2 additions & 1 deletion src/pathfinding_service/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import click
import gevent
import structlog
from eth_utils import to_canonical_address, to_checksum_address
from eth_utils import to_canonical_address
from web3 import Web3
from web3.contract import Contract

Expand All @@ -30,6 +30,7 @@
DEFAULT_API_PORT_PFS,
DEFAULT_POLL_INTERVALL,
)
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion src/pathfinding_service/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import UUID

import structlog
from eth_utils import to_canonical_address, to_checksum_address
from eth_utils import to_canonical_address

from pathfinding_service.model import IOU
from pathfinding_service.model.channel import Channel
Expand All @@ -24,6 +24,7 @@
TokenNetworkAddress,
)
from raiden_libs.database import BaseDatabase, hex256
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion src/pathfinding_service/model/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import ClassVar, Tuple, Type

import marshmallow
from eth_utils import to_checksum_address
from marshmallow.fields import NaiveDateTime
from marshmallow_dataclass import add_schema

Expand All @@ -20,6 +19,7 @@
TokenNetworkAddress,
)
from raiden_libs.marshmallow import ChecksumAddress
from utils import to_checksum_address


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/pathfinding_service/model/token_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import networkx as nx
import structlog
from eth_utils import to_checksum_address
from networkx import DiGraph
from networkx.exception import NetworkXNoPath, NodeNotFound

Expand Down Expand Up @@ -34,6 +33,7 @@
TokenAmount,
TokenNetworkAddress,
)
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion src/raiden_libs/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from typing import Any, Dict, List

import structlog
from eth_utils import to_canonical_address, to_checksum_address
from eth_utils import to_canonical_address

from raiden.utils.typing import Address, BlockNumber, ChainID, TokenNetworkAddress
from raiden_libs.states import BlockchainState
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion src/raiden_libs/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from typing import Any, Dict

import structlog
from eth_utils import to_checksum_address, to_hex
from eth_utils import to_hex

from raiden.messages.abstract import Message
from raiden_libs.events import Event
from utils import to_checksum_address


def setup_logging(log_level: str, log_json: bool) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/raiden_libs/marshmallow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any

import marshmallow
from eth_utils import decode_hex, encode_hex, is_checksum_address, to_checksum_address
from eth_utils import decode_hex, encode_hex, is_checksum_address
from utils import to_checksum_address


class HexedBytes(marshmallow.fields.Field):
Expand Down
2 changes: 1 addition & 1 deletion src/raiden_libs/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import gevent
import structlog
from eth_utils import to_checksum_address
from gevent.event import AsyncResult, Event
from gevent.greenlet import Greenlet
from marshmallow import ValidationError
Expand Down Expand Up @@ -42,6 +41,7 @@
from raiden.utils.typing import Address, ChainID, Set
from raiden_contracts.utils.type_aliases import PrivateKey
from raiden_libs.utils import MultiClientUserAddressManager
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion src/raiden_libs/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
import structlog
from eth_typing import HexStr
from eth_utils import event_abi_to_log_topic, to_canonical_address, to_checksum_address, to_hex
from eth_utils import event_abi_to_log_topic, to_canonical_address, to_hex
from hexbytes import HexBytes
from requests import RequestException
from web3 import Web3
Expand All @@ -34,6 +34,7 @@
from raiden_libs.constants import CONFIRMATION_OF_UNDERSTANDING
from raiden_libs.contract_info import CONTRACT_MANAGER
from raiden_libs.utils import private_key_to_address
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion src/raiden_libs/user_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from uuid import UUID

import structlog
from eth_utils import to_checksum_address, to_normalized_address
from eth_utils import to_normalized_address
from gevent.event import Event
from matrix_client.errors import MatrixRequestError
from matrix_client.user import User
Expand All @@ -23,6 +23,7 @@
validate_userid_signature,
)
from raiden.utils.typing import Address, Any, FrozenSet, Set, Union
from utils import to_checksum_address

log = structlog.get_logger(__name__)

Expand Down
10 changes: 10 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from functools import lru_cache
from typing import Union

import eth_utils
from eth_typing import AnyAddress, ChecksumAddress


@lru_cache(maxsize=5000)
def to_checksum_address(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress:
return eth_utils.to_checksum_address(value)
3 changes: 2 additions & 1 deletion tests/monitoring/fixtures/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from eth_typing import BlockNumber
from eth_utils import decode_hex, to_checksum_address
from eth_utils import decode_hex

from monitoring_service.api import MSApi
from monitoring_service.constants import API_PATH
Expand All @@ -18,6 +18,7 @@
)
from raiden_contracts.utils.type_aliases import PrivateKey
from raiden_libs.constants import DEFAULT_API_HOST
from src.utils import to_checksum_address
from tests.libs.mocks.web3 import Web3Mock


Expand Down
3 changes: 2 additions & 1 deletion tests/monitoring/fixtures/factories.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
from eth_utils import encode_hex, to_checksum_address
from eth_utils import encode_hex

from monitoring_service.states import HashedBalanceProof
from raiden.messages.monitoring_service import RequestMonitoring
from raiden.utils.typing import ChannelID, Nonce, TokenAmount, TokenNetworkAddress
from raiden_contracts.tests.utils.address import get_random_privkey
from raiden_contracts.utils.type_aliases import ChainID, PrivateKey
from raiden_libs.utils import private_key_to_address
from src.utils import to_checksum_address
from tests.constants import TEST_CHAIN_ID, TEST_MSC_ADDRESS


Expand Down
2 changes: 1 addition & 1 deletion tests/monitoring/monitoring_service/test_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pkg_resources
import requests
from eth_utils import to_checksum_address

from monitoring_service import metrics
from monitoring_service.api import MSApi
from monitoring_service.constants import DEFAULT_INFO_MESSAGE
from monitoring_service.service import MonitoringService
from src.utils import to_checksum_address


def test_get_info(api_url: str, ms_api_sut: MSApi, monitoring_service_mock: MonitoringService):
Expand Down
3 changes: 2 additions & 1 deletion tests/monitoring/monitoring_service/test_crash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List
from unittest.mock import Mock

from eth_utils import encode_hex, to_canonical_address, to_checksum_address
from eth_utils import encode_hex, to_canonical_address

from monitoring_service.events import ActionMonitoringTriggeredEvent
from monitoring_service.service import MonitoringService
Expand All @@ -27,6 +27,7 @@
from raiden_contracts.utils.type_aliases import PrivateKey
from raiden_libs.events import ReceiveChannelOpenedEvent, UpdatedHeadBlockEvent
from raiden_libs.states import BlockchainState
from src.utils import to_checksum_address
from tests.constants import TEST_MSC_ADDRESS

from ...libs.mocks.web3 import ContractMock, Web3Mock
Expand Down
3 changes: 1 addition & 2 deletions tests/monitoring/monitoring_service/test_database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import random

from eth_utils import to_checksum_address

from monitoring_service.database import Database
from monitoring_service.events import ActionMonitoringTriggeredEvent, ScheduledEvent
from monitoring_service.service import MonitoringService
Expand All @@ -16,6 +14,7 @@
TransactionHash,
)
from raiden_libs.database import hex256
from src.utils import to_checksum_address
from tests.monitoring.monitoring_service.factories import (
DEFAULT_TOKEN_NETWORK_ADDRESS,
create_channel,
Expand Down
2 changes: 1 addition & 1 deletion tests/monitoring/monitoring_service/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from unittest.mock import Mock, patch

import pytest
from eth_utils import to_checksum_address

from monitoring_service import metrics
from monitoring_service.database import Database
Expand Down Expand Up @@ -42,6 +41,7 @@
ReceiveNonClosingBalanceProofUpdatedEvent,
ReceiveTokenNetworkCreatedEvent,
)
from src.utils import to_checksum_address
from tests.libs.mocks.web3 import Web3Mock
from tests.monitoring.monitoring_service.factories import (
DEFAULT_CHANNEL_IDENTIFIER,
Expand Down
2 changes: 1 addition & 1 deletion tests/monitoring/request_collector/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from unittest.mock import Mock, patch

import pytest
from eth_utils import to_checksum_address

from monitoring_service.constants import CHANNEL_CLOSE_MARGIN
from monitoring_service.database import Database
from monitoring_service.states import Channel
from raiden.storage.serialization.serializer import DictSerializer
from raiden.utils.typing import Address
from src.utils import to_checksum_address


def test_invalid_request(ms_database, build_request_monitoring, request_collector):
Expand Down
3 changes: 2 additions & 1 deletion tests/pathfinding/fixtures/iou.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from eth_utils import encode_hex, to_canonical_address, to_checksum_address
from eth_utils import encode_hex, to_canonical_address
from web3.contract import Contract

from pathfinding_service.constants import MIN_IOU_EXPIRY
Expand All @@ -8,6 +8,7 @@
from raiden_contracts.utils.proofs import sign_one_to_n_iou
from raiden_contracts.utils.type_aliases import ChainID, PrivateKey
from raiden_libs.utils import private_key_to_address
from src.utils import to_checksum_address


@pytest.fixture
Expand Down
3 changes: 2 additions & 1 deletion tests/pathfinding/fixtures/network_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest.mock import Mock, patch

import pytest
from eth_utils import decode_hex, to_canonical_address, to_checksum_address
from eth_utils import decode_hex, to_canonical_address
from web3 import Web3
from web3.contract import Contract

Expand All @@ -27,6 +27,7 @@
from raiden_contracts.constants import CONTRACT_TOKEN_NETWORK_REGISTRY, CONTRACT_USER_DEPOSIT
from raiden_contracts.utils.type_aliases import PrivateKey
from raiden_libs.utils import private_key_to_address
from src.utils import to_checksum_address

from ...libs.mocks.web3 import Web3Mock
from ..utils import SimpleReachabilityContainer
Expand Down
Loading

0 comments on commit 98f0044

Please sign in to comment.