diff --git a/.gitmodules b/.gitmodules index bffa3e75..9f45b9a5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "diem"] +[submodule "diem_testnet_tag"] path = diem url = https://github.com/diem/diem.git diff --git a/diem b/diem index 3916c548..1a6910c2 160000 --- a/diem +++ b/diem @@ -1 +1 @@ -Subproject commit 3916c548380111f241d9ffbf7eecc09929bd2f4a +Subproject commit 1a6910c2fad633a09a38f2774596fa989ffcfed0 diff --git a/requirements.txt b/requirements.txt index 84d1244c..e713e87d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,10 +4,10 @@ numpy==1.18 protobuf==3.12.4 pytest==6.2.1 pytest-asyncio==0.14.0 -click==7.1 +click==8.1.3 aiohttp==3.7.4.post0 pylama -black +black==22.10.0 pyre-check pytest-cov mypy-protobuf diff --git a/src/diem/identifier/bech32.py b/src/diem/identifier/bech32.py index 01d82f07..c3de41d6 100644 --- a/src/diem/identifier/bech32.py +++ b/src/diem/identifier/bech32.py @@ -23,7 +23,7 @@ class Bech32Error(Exception): - """ Represents an error when creating a Diem address. """ + """Represents an error when creating a Diem address.""" pass diff --git a/src/diem/jsonrpc/async_client.py b/src/diem/jsonrpc/async_client.py index e4804717..97186904 100644 --- a/src/diem/jsonrpc/async_client.py +++ b/src/diem/jsonrpc/async_client.py @@ -564,6 +564,7 @@ async def execute_without_retry( "method": method, "params": params or [], } + json = {} try: json = await self._rs.send_request(self, request, ignore_stale_response or False) if "error" in json: diff --git a/src/diem/jsonrpc/client.py b/src/diem/jsonrpc/client.py index 455078c7..20babb3a 100644 --- a/src/diem/jsonrpc/client.py +++ b/src/diem/jsonrpc/client.py @@ -108,12 +108,7 @@ class RequestWithBackups(RequestStrategy): ``` """ - def __init__( - self, - backups: typing.List[str], - executor: ThreadPoolExecutor, - fallback: bool = False, - ) -> None: + def __init__(self, backups: typing.List[str], executor: ThreadPoolExecutor, fallback: bool = False) -> None: self._backups = backups self._executor = executor self._fallback = fallback @@ -268,16 +263,9 @@ def update_last_known_state(self, chain_id: int, version: int, timestamp_usecs: if curr.timestamp_usecs > timestamp_usecs: raise StaleResponseError(f"last known timestamp_usecs {curr.timestamp_usecs} > {timestamp_usecs}") - self._last_known_server_state = State( - chain_id=chain_id, - version=version, - timestamp_usecs=timestamp_usecs, - ) + self._last_known_server_state = State(chain_id=chain_id, version=version, timestamp_usecs=timestamp_usecs) - def get_metadata( - self, - version: typing.Optional[int] = None, - ) -> rpc.Metadata: + def get_metadata(self, version: typing.Optional[int] = None) -> rpc.Metadata: """get block metadata See [JSON-RPC API Doc](https://github.com/diem/diem/blob/master/json-rpc/docs/method_get_metadata.md) @@ -342,10 +330,7 @@ def get_account_transactions( return self.execute("get_account_transactions", params, _parse_list(lambda: rpc.Transaction())) def get_transactions( - self, - start_version: int, - limit: int, - include_events: typing.Optional[bool] = None, + self, start_version: int, limit: int, include_events: typing.Optional[bool] = None ) -> typing.List[rpc.Transaction]: """get transactions @@ -403,10 +388,7 @@ def support_diem_id(self) -> bool: tc_account = self.must_get_account(TREASURY_ADDRESS) return bool(tc_account.role.vasp_domain_events_key) - def submit( - self, - txn: typing.Union[diem_types.SignedTransaction, str], - ) -> None: + def submit(self, txn: typing.Union[diem_types.SignedTransaction, str]) -> None: """submit signed transaction See [JSON-RPC API Doc](https://github.com/diem/diem/blob/master/json-rpc/docs/method_submit.md) @@ -564,12 +546,9 @@ def execute_without_retry( Raises NetworkError if send http request failed, or received server response status is not 200. """ - request = { - "jsonrpc": "2.0", - "id": 1, - "method": method, - "params": params or [], - } + request = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params or []} + + json = {} try: json = self._rs.send_request(self, request, ignore_stale_response or False) if "error" in json: @@ -588,10 +567,7 @@ def execute_without_retry( raise InvalidServerResponse(f"Parse result failed: {e}, response: {json}") def _send_http_request( - self, - url: str, - request: typing.Dict[str, typing.Any], - ignore_stale_response: bool, + self, url: str, request: typing.Dict[str, typing.Any], ignore_stale_response: bool ) -> typing.Dict[str, typing.Any]: self._logger.debug("http request body: %s", request) response = self._session.post(url, json=request, timeout=self._timeout) @@ -605,9 +581,7 @@ def _send_http_request( # check stable response before check jsonrpc error try: self.update_last_known_state( - json.get("diem_chain_id"), - json.get("diem_ledger_version"), - json.get("diem_ledger_timestampusec"), + json.get("diem_chain_id"), json.get("diem_ledger_version"), json.get("diem_ledger_timestampusec") ) except StaleResponseError as e: if not ignore_stale_response: diff --git a/src/diem/offchain/command.py b/src/diem/offchain/command.py index 5ed4d650..6dc7377c 100644 --- a/src/diem/offchain/command.py +++ b/src/diem/offchain/command.py @@ -9,7 +9,7 @@ class Command(ABC): - """Command defines common interface for all commands """ + """Command defines common interface for all commands""" @abstractmethod def id(self) -> str: diff --git a/src/diem/offchain/types/__init__.py b/src/diem/offchain/types/__init__.py index 60d7b3b2..851c8f51 100644 --- a/src/diem/offchain/types/__init__.py +++ b/src/diem/offchain/types/__init__.py @@ -124,7 +124,7 @@ def _field_value_from_dict(field: dataclasses.Field, obj: typing.Any, field_path full_name = _join_field_path(field_path, field.name) field_type = field.type args = field.type.__args__ if hasattr(field.type, "__args__") else [] - is_optional = len(args) == 2 and isinstance(None, args[1]) # pyre-ignore + is_optional = len(args) == 2 and isinstance(None, args[1]) if is_optional: field_type = args[0] val = obj.get(field.name) @@ -166,14 +166,9 @@ def new_payment_object( return PaymentObject( reference_id=reference_id or str(uuid.uuid4()), sender=PaymentActorObject( - address=sender_account_id, - kyc_data=sender_kyc_data, - status=StatusObject(status=Status.needs_kyc_data), - ), - receiver=PaymentActorObject( - address=receiver_account_id, - status=StatusObject(status=Status.none), + address=sender_account_id, kyc_data=sender_kyc_data, status=StatusObject(status=Status.needs_kyc_data) ), + receiver=PaymentActorObject(address=receiver_account_id, status=StatusObject(status=Status.none)), action=PaymentActionObject(amount=amount, currency=currency), description=description, original_payment_reference_id=original_payment_reference_id, @@ -196,10 +191,7 @@ def replace_payment_actor( changes["additional_kyc_data"] = additional_kyc_data if status or abort_code or abort_message: changes["status"] = replace_payment_status( - actor.status, - status=status, - abort_code=abort_code, - abort_message=abort_message, + actor.status, status=status, abort_code=abort_code, abort_message=abort_message ) if metadata: if not isinstance(metadata, list): @@ -224,19 +216,11 @@ def replace_payment_status( return dataclasses.replace(status_obj, **changes) -def new_payment_request( - payment: PaymentObject, - cid: typing.Optional[str] = None, -) -> CommandRequestObject: +def new_payment_request(payment: PaymentObject, cid: typing.Optional[str] = None) -> CommandRequestObject: return CommandRequestObject( cid=cid or str(uuid.uuid4()), command_type=CommandType.PaymentCommand, - command=to_dict( - PaymentCommandObject( - _ObjectType=CommandType.PaymentCommand, - payment=payment, - ) - ), + command=to_dict(PaymentCommandObject(_ObjectType=CommandType.PaymentCommand, payment=payment)), ) @@ -254,17 +238,11 @@ def reply_request( def individual_kyc_data(**kwargs) -> KycDataObject: # pyre-ignore - return KycDataObject( - type=KycDataObjectType.individual, - **kwargs, - ) + return KycDataObject(type=KycDataObjectType.individual, **kwargs) def entity_kyc_data(**kwargs) -> KycDataObject: # pyre-ignore - return KycDataObject( - type=KycDataObjectType.entity, - **kwargs, - ) + return KycDataObject(type=KycDataObjectType.entity, **kwargs) def validate_write_once_fields(path: str, new: typing.Any, prior: typing.Any) -> None: # pyre-ignore diff --git a/src/diem/offchain/types/command_types.py b/src/diem/offchain/types/command_types.py index 38442301..de867225 100644 --- a/src/diem/offchain/types/command_types.py +++ b/src/diem/offchain/types/command_types.py @@ -98,7 +98,7 @@ class OffChainErrorType: class OffChainCommandResponseResultType: - """ Type of result in a CommandResponseObject""" + """Type of result in a CommandResponseObject""" ReferenceIDCommandResponse = "ReferenceIDCommandResponse" diff --git a/src/diem/testing/cli.py b/src/diem/testing/cli.py index c5408aa4..8c7f0710 100644 --- a/src/diem/testing/cli.py +++ b/src/diem/testing/cli.py @@ -14,7 +14,7 @@ log_format: str = "%(name)s [%(asctime)s] %(levelname)s: %(message)s" -click.option = functools.partial(click.option, show_default=True) # pyre-ignore +click.option = functools.partial(click.option, show_default=True) def coro(f): # pyre-ignore @@ -187,13 +187,7 @@ async def start_server( help="Before start test, the target wallet application host port should be ready for connection. This is wait timeout (seconds) for the port ready.", type=int, ) -@click.option( - "--include-offchain-v2", - type=bool, - is_flag=True, - default=False, - help="Include offchain v2 test suites", -) +@click.option("--include-offchain-v2", type=bool, is_flag=True, default=False, help="Include offchain v2 test suites") @click.help_option("-h", "--help") def test( target: str, diff --git a/src/diem/testing/miniwallet/app/app.py b/src/diem/testing/miniwallet/app/app.py index a0270726..37c9e427 100644 --- a/src/diem/testing/miniwallet/app/app.py +++ b/src/diem/testing/miniwallet/app/app.py @@ -149,7 +149,7 @@ async def worker() -> None: async def txn_metadata(self, txn: Transaction) -> Tuple[bytes, bytes]: if await self.offchain.is_under_dual_attestation_limit(txn.currency, txn.amount): if txn.refund_diem_txn_version and txn.refund_reason: - return self.diem_account.refund_metadata(txn.refund_diem_txn_version, txn.refund_reason) # pyre-ignore + return self.diem_account.refund_metadata(txn.refund_diem_txn_version, txn.refund_reason) if txn.subaddress_hex: return self.diem_account.general_metadata(txn.subaddress(), str(txn.payee)) if txn.reference_id: diff --git a/src/diem/testing/miniwallet/app/models.py b/src/diem/testing/miniwallet/app/models.py index 7c47b0f8..5e56ce42 100644 --- a/src/diem/testing/miniwallet/app/models.py +++ b/src/diem/testing/miniwallet/app/models.py @@ -26,7 +26,7 @@ def __post_init__(self) -> None: self.diem_id = create_diem_id(self.id, str(self.vasp_domain)) if self.vasp_domain else None def kyc_data_object(self) -> offchain.KycDataObject: - return self.kyc_data if self.kyc_data else offchain.individual_kyc_data() # pyre-ignore + return self.kyc_data if self.kyc_data else offchain.individual_kyc_data() @dataclass diff --git a/src/diem/testing/suites/basic/test_create_test_account.py b/src/diem/testing/suites/basic/test_create_test_account.py index e14dbead..e5991455 100644 --- a/src/diem/testing/suites/basic/test_create_test_account.py +++ b/src/diem/testing/suites/basic/test_create_test_account.py @@ -26,13 +26,7 @@ async def test_create_an_account_with_initial_deposit_balances( assert await account.balance(currency) == amount -@pytest.mark.parametrize( # pyre-ignore - "kyc_data", - [ - offchain.individual_kyc_data(), - offchain.entity_kyc_data(), - ], -) +@pytest.mark.parametrize("kyc_data", [offchain.individual_kyc_data(), offchain.entity_kyc_data()]) async def test_create_an_account_with_minimum_valid_kyc_data( target_client: RestClient, kyc_data: offchain.KycDataObject ) -> None: diff --git a/src/diem/testing/suites/basic/test_receive_payment.py b/src/diem/testing/suites/basic/test_receive_payment.py index 110d3e4b..7da15be8 100644 --- a/src/diem/testing/suites/basic/test_receive_payment.py +++ b/src/diem/testing/suites/basic/test_receive_payment.py @@ -74,10 +74,7 @@ async def test_receive_payment_with_invalid_metadata( @pytest.mark.parametrize("amount", [10_000, 1200_000, 125550_000]) async def test_receive_payment_with_general_metadata_and_valid_from_and_to_subaddresses( - stub_client: RestClient, - target_client: RestClient, - currency: str, - amount: int, + stub_client: RestClient, target_client: RestClient, currency: str, amount: int ) -> None: """ Test Plan: @@ -101,7 +98,7 @@ async def test_receive_payment_with_general_metadata_and_valid_from_and_to_subad await sender_account.log_events() -@pytest.mark.parametrize( # pyre-ignore +@pytest.mark.parametrize( "invalid_to_subaddress", [None, b"", b"bb4a3ba109a3175f", b"subaddress_more_than_8_bytes", b"too_short"] ) async def test_receive_payment_with_general_metadata_and_invalid_to_subaddress( @@ -163,7 +160,7 @@ async def test_receive_payment_with_general_metadata_and_invalid_to_subaddress( await sender_account.log_events() -@pytest.mark.parametrize( # pyre-ignore +@pytest.mark.parametrize( "invalid_from_subaddress", [None, b"", b"bb4a3ba109a3175f", b"subaddress_more_than_8_bytes", b"too_short"] ) async def test_receive_payment_with_general_metadata_and_invalid_from_subaddress( @@ -216,7 +213,7 @@ async def test_receive_payment_with_general_metadata_and_invalid_from_subaddress @pytest.mark.parametrize( "invalid_from_subaddress", [None, b"", b"bb4a3ba109a3175f", b"subaddress_more_than_8_bytes", b"too_short"] ) -@pytest.mark.parametrize( # pyre-ignore +@pytest.mark.parametrize( "invalid_to_subaddress", [None, b"", b"bb4a3ba109a3175f", b"subaddress_more_than_8_bytes", b"too_short"] ) async def test_receive_payment_with_general_metadata_and_invalid_subaddresses( diff --git a/src/diem/testing/suites/offchainv2/test_payment_command.py b/src/diem/testing/suites/offchainv2/test_payment_command.py index 5832b3f0..3fe785b6 100644 --- a/src/diem/testing/suites/offchainv2/test_payment_command.py +++ b/src/diem/testing/suites/offchainv2/test_payment_command.py @@ -264,11 +264,7 @@ async def test_payment_command_contains_invalid_actor_metadata_item_type( async def test_replay_the_same_payment_command( - currency: str, - travel_rule_threshold: int, - stub_wallet_app: App, - stub_client: RestClient, - target_client: RestClient, + currency: str, travel_rule_threshold: int, stub_wallet_app: App, stub_client: RestClient, target_client: RestClient ) -> None: """ Test Plan: @@ -285,9 +281,7 @@ async def test_replay_the_same_payment_command( target_kyc = await target_client.get_kyc_sample() stub_kyc = await stub_client.get_kyc_sample() sender_account = await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, - disable_background_tasks=True, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum, disable_background_tasks=True ) receiver_account = await target_client.create_account(kyc_data=stub_kyc.minimum) try: @@ -306,11 +300,7 @@ async def test_replay_the_same_payment_command( async def test_payment_command_sender_kyc_data_can_only_be_written_once( - currency: str, - travel_rule_threshold: int, - stub_wallet_app: App, - stub_client: RestClient, - target_client: RestClient, + currency: str, travel_rule_threshold: int, stub_wallet_app: App, stub_client: RestClient, target_client: RestClient ) -> None: """ Test Plan: @@ -330,9 +320,7 @@ async def test_payment_command_sender_kyc_data_can_only_be_written_once( target_kyc = await target_client.get_kyc_sample() stub_kyc = await stub_client.get_kyc_sample() sender_account = await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, - disable_background_tasks=True, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum, disable_background_tasks=True ) receiver_account = await target_client.create_account(kyc_data=stub_kyc.minimum) try: @@ -393,9 +381,7 @@ async def test_payment_command_sender_address_can_only_be_written_once( target_kyc = await target_client.get_kyc_sample() stub_kyc = await stub_client.get_kyc_sample() sender_account = await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, - disable_background_tasks=True, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum, disable_background_tasks=True ) receiver_account = await target_client.create_account(kyc_data=stub_kyc.minimum) try: @@ -431,14 +417,7 @@ async def test_payment_command_sender_address_can_only_be_written_once( await sender_account.log_events() -@pytest.mark.parametrize( - "field_name", - [ - "currency", - "amount", - "timestamp", - ], -) +@pytest.mark.parametrize("field_name", ["currency", "amount", "timestamp"]) async def test_payment_command_action_can_only_be_written_once( currency: str, travel_rule_threshold: int, @@ -466,9 +445,7 @@ async def test_payment_command_action_can_only_be_written_once( target_kyc = await target_client.get_kyc_sample() stub_kyc = await stub_client.get_kyc_sample() sender_account = await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, - disable_background_tasks=True, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum, disable_background_tasks=True ) receiver_account = await target_client.create_account(kyc_data=stub_kyc.minimum) try: @@ -486,6 +463,7 @@ async def test_payment_command_action_can_only_be_written_once( offchain_cmd = updated_cmd.to_offchain_command() # update action field value + field_value = "" if field_name == "currency": field_value = "XDX" if offchain_cmd.payment.action.currency == "XUS" else "XUS" elif field_name == "amount": @@ -535,12 +513,7 @@ async def assert_payment_command_field_error( set_field(request, full_field_name, field_value) status_code, resp = await send_request_json( - diem_client, - stub_config.account, - sender_address, - receiver_address, - json.dumps(request), - hrp, + diem_client, stub_config.account, sender_address, receiver_address, json.dumps(request), hrp ) assert status_code == 400 assert resp.status == "failure" diff --git a/src/diem/testing/suites/offchainv2/test_receive_payment.py b/src/diem/testing/suites/offchainv2/test_receive_payment.py index 7d7165d0..c13643bc 100644 --- a/src/diem/testing/suites/offchainv2/test_receive_payment.py +++ b/src/diem/testing/suites/offchainv2/test_receive_payment.py @@ -13,10 +13,7 @@ async def test_receive_payment_with_travel_rule_metadata_and_valid_reference_id( - stub_client: RestClient, - target_client: RestClient, - currency: str, - travel_rule_threshold: int, + stub_client: RestClient, target_client: RestClient, currency: str, travel_rule_threshold: int ) -> None: """ Test Plan: @@ -42,9 +39,7 @@ async def test_receive_payment_with_travel_rule_metadata_and_valid_reference_id( await sender_account.log_events() -@pytest.mark.parametrize( # pyre-ignore - "invalid_ref_id", [None, "", "ref_id_is_not_uuid", "6cd81d79-f041-4f28-867f-e4d54950833e"] -) +@pytest.mark.parametrize("invalid_ref_id", [None, "", "ref_id_is_not_uuid", "6cd81d79-f041-4f28-867f-e4d54950833e"]) async def test_receive_payment_with_travel_rule_metadata_and_invalid_reference_id( stub_client: RestClient, target_client: RestClient, @@ -115,10 +110,7 @@ async def test_receive_payment_with_travel_rule_metadata_and_invalid_reference_i async def test_receive_payment_meets_travel_rule_threshold_both_kyc_data_evaluations_are_accepted( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -134,8 +126,7 @@ async def test_receive_payment_meets_travel_rule_threshold_both_kyc_data_evaluat stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum ), receiver=await target_client.create_account(kyc_data=stub_kyc.minimum), payment_command_states=["S_INIT", "R_SEND", "READY"], @@ -145,11 +136,7 @@ async def test_receive_payment_meets_travel_rule_threshold_both_kyc_data_evaluat async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_rejected_by_the_receiver( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, - hrp: str, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient, hrp: str ) -> None: """ Test Plan: @@ -164,10 +151,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_re target_kyc = await target_client.get_kyc_sample() stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( - sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.reject, - ), + sender=await stub_client.create_account(balances={currency: travel_rule_threshold}, kyc_data=target_kyc.reject), receiver=await target_client.create_account(kyc_data=stub_kyc.minimum), payment_command_states=["S_INIT", "R_ABORT"], currency=currency, @@ -176,10 +160,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_re async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_rejected_by_the_sender( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -195,8 +176,7 @@ async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_ stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum ), receiver=await target_client.create_account(kyc_data=stub_kyc.reject), payment_command_states=["S_INIT", "R_SEND", "S_ABORT"], @@ -206,10 +186,7 @@ async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_then_accepted_after_reviewing_additional_kyc_data( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -225,8 +202,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.soft_match, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.soft_match ), receiver=await target_client.create_account(kyc_data=stub_kyc.minimum), payment_command_states=["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_SEND", "READY"], @@ -236,10 +212,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_soft_match_then_accepted_after_reviewing_additional_kyc_data( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -255,8 +228,7 @@ async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_ stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum ), receiver=await target_client.create_account(kyc_data=stub_kyc.soft_match), payment_command_states=["S_INIT", "R_SEND", "S_SOFT", "R_SOFT_SEND", "READY"], @@ -266,10 +238,7 @@ async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_then_rejected_after_reviewing_additional_kyc_data( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -285,8 +254,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.soft_reject, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.soft_reject ), receiver=await target_client.create_account(kyc_data=stub_kyc.minimum), payment_command_states=["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_ABORT"], @@ -296,10 +264,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_soft_match_then_rejected_after_reviewing_additional_kyc_data( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -315,8 +280,7 @@ async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_ stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.minimum, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.minimum ), receiver=await target_client.create_account(kyc_data=stub_kyc.soft_reject), payment_command_states=["S_INIT", "R_SEND", "S_SOFT", "R_SOFT_SEND", "S_ABORT"], @@ -326,10 +290,7 @@ async def test_receive_payment_meets_travel_rule_threshold_receiver_kyc_data_is_ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_then_receiver_aborts_for_sending_additional_kyc_data( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -358,10 +319,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so async def test_receive_payment_meets_travel_rule_threshold_sender_and_receiver_kyc_data_are_soft_match_then_accepted_after_reviewing_additional_kyc_data( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -377,8 +335,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_and_receiver_k stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.soft_match, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.soft_match ), receiver=await target_client.create_account(kyc_data=stub_kyc.soft_match), payment_command_states=["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_SEND", "S_SOFT", "R_SOFT_SEND", "READY"], @@ -388,10 +345,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_and_receiver_k async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_and_accepted_receiver_kyc_data_is_rejected( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -407,8 +361,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.soft_match, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.soft_match ), receiver=await target_client.create_account(kyc_data=stub_kyc.reject), payment_command_states=["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_SEND", "S_ABORT"], @@ -418,10 +371,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_and_accepted_receiver_kyc_data_is_soft_match_and_rejected( - currency: str, - travel_rule_threshold: int, - target_client: RestClient, - stub_client: RestClient, + currency: str, travel_rule_threshold: int, target_client: RestClient, stub_client: RestClient ) -> None: """ Test Plan: @@ -437,8 +387,7 @@ async def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_so stub_kyc = await stub_client.get_kyc_sample() await receive_payment_meets_travel_rule_threshold( sender=await stub_client.create_account( - balances={currency: travel_rule_threshold}, - kyc_data=target_kyc.soft_match, + balances={currency: travel_rule_threshold}, kyc_data=target_kyc.soft_match ), receiver=await target_client.create_account(kyc_data=stub_kyc.soft_reject), payment_command_states=["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_SEND", "S_SOFT", "R_SOFT_SEND", "S_ABORT"], diff --git a/src/diem/utils.py b/src/diem/utils.py index 76e354f6..c7c2c071 100644 --- a/src/diem/utils.py +++ b/src/diem/utils.py @@ -3,15 +3,13 @@ """Utilities for data type converting, construction and hashing.""" +import sys from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey, Ed25519PrivateKey import hashlib, typing, time, socket, asyncio from . import diem_types, jsonrpc, stdlib -from .constants import ( - SUB_ADDRESS_LEN, - DIEM_HASH_PREFIX, -) +from .constants import SUB_ADDRESS_LEN, DIEM_HASH_PREFIX class InvalidAccountAddressError(Exception): @@ -225,8 +223,9 @@ def shutdown_event_loop(loop: asyncio.events.AbstractEventLoop) -> None: try: asyncio.runners._cancel_all_tasks(loop) # pyre-ignore loop.run_until_complete(loop.shutdown_asyncgens()) - if hasattr(loop, "shutdown_default_executor"): - loop.run_until_complete(getattr(loop, "shutdown_default_executor")()) + if sys.version_info >= (3, 9): + if hasattr(loop, "shutdown_default_executor"): + loop.run_until_complete(getattr(loop, "shutdown_default_executor")()) finally: asyncio.set_event_loop(None) loop.close() @@ -238,7 +237,7 @@ async def with_retry(coroutine, max_retries=5, delay_secs=0.1, exception=Excepti tries += 1 try: return await coroutine() - except exception as e: # pyre-ignore + except exception as e: if tries < max_retries: # simplest backoff strategy: tries * delay await asyncio.sleep(delay_secs * tries)