Skip to content

Commit

Permalink
python: Introduce a new connection API that is a bit less stateful.
Browse files Browse the repository at this point in the history
  • Loading branch information
da-tanabe committed Mar 12, 2021
1 parent 8b8ebde commit 631e509
Show file tree
Hide file tree
Showing 32 changed files with 3,088 additions and 61 deletions.
1 change: 1 addition & 0 deletions python/dazl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
exercise_by_key,
)
from .ledger import Command
from .ledger.grpc import connect
from .pretty.table import write_acs
from .prim import ContractData, ContractId, DazlError, FrozenDict as frozendict, Party
from .util.logging import setup_default_logger
Expand Down
15 changes: 12 additions & 3 deletions python/dazl/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,19 @@ class async_network:
"""

def __init__(
self, url: "Optional[str]" = None, dars: "Optional[Union[Dar, Collection[Dar]]]" = None
self,
url: "Optional[str]" = None,
dars: "Optional[Union[Dar, Collection[Dar]]]" = None,
future_api: bool = False,
):
LOG.debug("async_network.__init__")
self.network = Network()
if future_api:
# inline import here to avoid import cycles
from ..compat.v8_network import Network as V8Network

self.network = V8Network()
else:
self.network = Network()
if url:
self.network.set_config(url=url)
self.dars = as_list(dars) # type: List[Dar]
Expand Down Expand Up @@ -294,7 +303,7 @@ def start_in_background(
"""
if validate_install_signal_handlers(install_signal_handlers):
self._impl.invoker.install_signal_handlers()
return self._impl.start(daemon)
return self._impl.start_in_background(daemon, install_signal_handlers=False)

def shutdown(self) -> "Optional[Awaitable[None]]":
"""
Expand Down
13 changes: 9 additions & 4 deletions python/dazl/client/state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from asyncio import Future, sleep
from asyncio import Future, get_event_loop, sleep
from collections import defaultdict
from dataclasses import dataclass
import datetime
Expand Down Expand Up @@ -60,7 +60,7 @@ class ContractContextualData:


class ActiveContractSet:
def __init__(self, invoker: "Invoker", lookup: "SymbolLookup"):
def __init__(self, invoker: "Optional[Invoker]", lookup: "SymbolLookup"):
self.invoker = invoker
self.lookup = lookup
self._tcdata = defaultdict(
Expand Down Expand Up @@ -207,8 +207,13 @@ def register_query(self, query: "PendingQuery") -> None:


class PendingQuery:
def __init__(self, invoker: "Invoker", match, min_count: int):
self.future = invoker.create_future() # type: Awaitable[Collection[ContractContextualData]]
def __init__(self, invoker: "Optional[Invoker]", match, min_count: int):
if invoker is not None:
self.future = (
invoker.create_future()
) # type: Awaitable[Collection[ContractContextualData]]
else:
self.future = get_event_loop().create_future()
self.match = match
self.min_count = min_count

Expand Down
9 changes: 9 additions & 0 deletions python/dazl/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

__all__ = ["AIOGlobalClient", "AIOPartyClient", "Network", "NotSupportedError"]


from .v8 import NotSupportedError
from .v8_client_aio import AIOGlobalClient, AIOPartyClient
from .v8_network import Network
16 changes: 16 additions & 0 deletions python/dazl/compat/_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
_NS_LEDGER_ID_DEPRECATED = (
"fetching ledger_id from an event is deprecated; instead get it from the connection (or "
"consider if knowing the ledger ID is important to your usecase)"
)

_NS_ACS_DEPRECATED = (
"acs functions are deprecated; you should keep your own store, or use contract keys to avoid "
"needing to work with local state"
)


COMMAND_ID = "command_id is no longer accessible in the new API"
WORKFLOW_ID = "workflow_id is no longer accessible in the new API"
EVENT_ID = "event_id is no longer accessible in the new API"
WITNESS_PARTIES = "witness_parties is no longer accessible in the new API"
PARTY = "reading `party` from a connection is ambiguous when multi-party submissions are being used; consider an alternate way of determining an appropriate Party in this context"
40 changes: 40 additions & 0 deletions python/dazl/compat/v8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""
This module contains symbols to aid in a gradual migration of v5-v7 code to v8. These symbols will
be marked as deprecated in v8, and be removed in v9.
"""
from typing import TypeVar

from ..protocols.core import AEventHandler, ArchiveEvent, CreateEvent, InitEvent, ReadyEvent

__all__ = ["ConnectionReuseWarning", "CallbackReturnValueWarning", "NotSupportedError"]

InitFn = TypeVar("InitFn", bound=AEventHandler[InitEvent])
ReadyFn = TypeVar("ReadyFn", bound=AEventHandler[ReadyEvent])
CreateFn = TypeVar("CreateFn", bound=AEventHandler[CreateEvent])
ArchiveFn = TypeVar("ArchiveFn", bound=AEventHandler[ArchiveEvent])
DEFAULT_TIMEOUT_SECONDS = 30


class NotSupportedError(Exception):
"""
Error raised when calling an API that exists on :class:`Network` but is not supported on
:class:`ConnectionFactory`.
"""


class ConnectionReuseWarning(DeprecationWarning):
"""
Warning raised when Network.aio_party or Network.simple_party are called more than once with
the same party literal. Connection sharing will no longer explicitly be provided by dazl in the
v8 API; any connection sharing must be instead managed by your application.
"""


class CallbackReturnValueWarning(DeprecationWarning):
"""
Warning raised when a callback returns a value instead of directly submitting a command itself.
This style of callback is not supported by the dazl v8 API.
"""
Loading

0 comments on commit 631e509

Please sign in to comment.