Skip to content

Commit 939ad57

Browse files
author
fallentree
committed
fbshipit-source-id: 9e91a7723a1581a202cd5f8c3e82d454ee482062
1 parent 01826c5 commit 939ad57

39 files changed

+5730
-316
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ cmake-build-*
1010
# .gitignore file locally for IDE/Emacs/Vim generated files.
1111
**/target
1212
**/*.rs.bk
13-

python/lib/data.h

Lines changed: 209 additions & 111 deletions
Large diffs are not rendered by default.

python/setup.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Copyright (c) The Libra Core Contributors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
import sys
54
import platform
5+
import sys
66

7-
from setuptools import setup, Extension, Command, find_packages
7+
from setuptools import Command, Extension, find_packages, setup
88

99

1010
class VendorCommand(Command):
@@ -29,7 +29,9 @@ def run(self):
2929

3030

3131
# Require pytest-runner only when running tests
32-
pytest_runner = ["pytest-runner"] if any(arg in sys.argv for arg in ("pytest", "test")) else []
32+
pytest_runner = (
33+
["pytest-runner"] if any(arg in sys.argv for arg in ("pytest", "test")) else []
34+
)
3335
grpcio_tools = ["grpcio-tools"] if any(arg in sys.argv for arg in ["vendor"]) else []
3436

3537
LIBRA_INCLUDE_DIR = "lib"
@@ -38,15 +40,27 @@ def run(self):
3840

3941
extra_link_args = []
4042
if platform.system() == "Darwin":
41-
LIBRA_LIB_FILE = "%s/%s-%s.a" % (LIBRA_LIB_DIR, "liblibra_dev", "darwin-%s" % platform.machine())
43+
LIBRA_LIB_FILE = "%s/%s-%s.a" % (
44+
LIBRA_LIB_DIR,
45+
"liblibra_dev",
46+
"darwin-%s" % platform.machine(),
47+
)
4248
extra_link_args.extend(["-framework", "Security"])
4349
elif platform.system() == "Linux":
44-
LIBRA_LIB_FILE = "%s/%s-%s.a" % (LIBRA_LIB_DIR, "liblibra_dev", "linux-%s" % platform.machine())
50+
LIBRA_LIB_FILE = "%s/%s-%s.a" % (
51+
LIBRA_LIB_DIR,
52+
"liblibra_dev",
53+
"linux-%s" % platform.machine(),
54+
)
4555
extra_link_args.append("-ldl")
4656
extra_link_args.append("-lm")
4757
extra_link_args.append("-pthread")
4858
elif platform.system() == "Windows":
49-
LIBRA_LIB_FILE = "%s/%s-%s.a" % (LIBRA_LIB_DIR, "liblibra_dev", "windows-%s" % platform.machine())
59+
LIBRA_LIB_FILE = "%s/%s-%s.a" % (
60+
LIBRA_LIB_DIR,
61+
"liblibra_dev",
62+
"windows-%s" % platform.machine(),
63+
)
5064

5165

5266
exts = [
@@ -57,7 +71,7 @@ def run(self):
5771
extra_objects=[LIBRA_LIB_FILE],
5872
depends=["src/pylibra/capi.pxd", LIBRA_HEADER, LIBRA_LIB_FILE],
5973
extra_link_args=extra_link_args,
60-
),
74+
)
6175
]
6276

6377

python/src/pylibra/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
# pyre-strict
55

6-
# Must be at the top
76
from ._config import *
8-
7+
from ._jsonrpc_transport import *
98
from ._mint import *
10-
from ._types import *
119
from ._native import *
12-
from ._jsonrpc_transport import *
10+
from ._types import *
11+
1312

1413
__all__ = [
1514
# Constants

python/src/pylibra/_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
# pyre-strict
55
import typing
66

7+
78
NETWORK_TESTNET: str = "testnet"
89
NETWORK_DEV: str = "dev"
910

1011
NETWORK_DEFAULT: str = NETWORK_TESTNET
1112

1213
ENDPOINT_CONFIG: typing.Dict[str, typing.Dict[str, str]] = {
13-
NETWORK_TESTNET: {"json-rpc": "https://client.testnet.libra.org/", "faucet": "http://faucet.testnet.libra.org"},
14+
NETWORK_TESTNET: {
15+
"json-rpc": "https://client.testnet.libra.org/",
16+
"faucet": "http://faucet.testnet.libra.org",
17+
},
1418
NETWORK_DEV: {
1519
"json-rpc": "http://client.dev.aws.hlw3truzy4ls.com/",
1620
"faucet": "http://faucet.dev.aws.hlw3truzy4ls.com",

python/src/pylibra/_jsonrpc_transport.py

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,32 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import dataclasses
5-
import requests
65
import typing
76

8-
from ._config import NETWORK_DEFAULT, ENDPOINT_CONFIG, DEFAULT_CONNECT_TIMEOUT_SECS, DEFAULT_TIMEOUT_SECS
7+
import requests
8+
9+
from ._config import (
10+
DEFAULT_CONNECT_TIMEOUT_SECS,
11+
DEFAULT_TIMEOUT_SECS,
12+
ENDPOINT_CONFIG,
13+
NETWORK_DEFAULT,
14+
)
15+
from ._transport import BaseLibraNetwork, ClientError, SubmitTransactionError
916
from ._types import (
10-
SignedTransaction,
11-
Event,
12-
PaymentEvent,
13-
ToLBRExchangeRateUpdateEvent,
1417
AccountResource,
18+
ChildVASP,
1519
CurrencyInfo,
20+
Event,
1621
ParentVASP,
17-
ChildVASP,
22+
PaymentEvent,
23+
SignedTransaction,
24+
ToLBRExchangeRateUpdateEvent,
1825
)
19-
from ._transport import BaseLibraNetwork, ClientError, SubmitTransactionError
2026

2127

22-
EventsType = typing.List[typing.Union[Event, PaymentEvent, ToLBRExchangeRateUpdateEvent]]
28+
EventsType = typing.List[
29+
typing.Union[Event, PaymentEvent, ToLBRExchangeRateUpdateEvent]
30+
]
2331

2432

2533
@dataclasses.dataclass
@@ -150,7 +158,11 @@ def gas(self) -> int:
150158

151159
@property
152160
def metadata(self) -> bytes:
153-
return bytes.fromhex(self._transaction["script"]["metadata"]) if self.is_p2p else b""
161+
return (
162+
bytes.fromhex(self._transaction["script"]["metadata"])
163+
if self.is_p2p
164+
else b""
165+
)
154166

155167
@property
156168
def vm_status(self) -> int:
@@ -364,14 +376,11 @@ def make_json_rpc_request(
364376
timeout: typing.Tuple[float, float],
365377
method: str,
366378
params: typing.List[typing.Any],
367-
result_class: typing.Optional[typing.Type[typing.Union[GetAccountStateResp, GetMetadataResp, SubmitResp]]],
379+
result_class: typing.Optional[
380+
typing.Type[typing.Union[GetAccountStateResp, GetMetadataResp, SubmitResp]]
381+
],
368382
):
369-
req = {
370-
"jsonrpc": "2.0",
371-
"id": 1,
372-
"method": method,
373-
"params": params,
374-
}
383+
req = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params}
375384

376385
try:
377386
resp = session.post(url, json=req, timeout=timeout)
@@ -402,7 +411,10 @@ def as_events(resp_list: typing.List[typing.Dict], include: bool = True) -> Even
402411

403412
if include and resp_list:
404413
for e_dict in resp_list:
405-
if e_dict["data"]["type"] == "sentpayment" or e_dict["data"]["type"] == "receivedpayment":
414+
if (
415+
e_dict["data"]["type"] == "sentpayment"
416+
or e_dict["data"]["type"] == "receivedpayment"
417+
):
406418
events.append(JSONPaymentEvent(e_dict))
407419
elif e_dict["data"]["type"] == "to_lbr_exchange_rate_update":
408420
events.append(JSONToLBRExchangeRateUpdateEvent(e_dict))
@@ -442,19 +454,34 @@ def __del__(self):
442454

443455
def currentVersion(self) -> int:
444456
result = make_json_rpc_request(
445-
self._url, self._session, self._timeout, "get_metadata", ["NULL"], GetMetadataResp
457+
self._url,
458+
self._session,
459+
self._timeout,
460+
"get_metadata",
461+
["NULL"],
462+
GetMetadataResp,
446463
)
447464
return result.version
448465

449466
def currentTimestampUsecs(self) -> int:
450467
result = make_json_rpc_request(
451-
self._url, self._session, self._timeout, "get_metadata", ["NULL"], GetMetadataResp
468+
self._url,
469+
self._session,
470+
self._timeout,
471+
"get_metadata",
472+
["NULL"],
473+
GetMetadataResp,
452474
)
453475
return result.timestamp
454476

455477
def getAccount(self, address_hex: str) -> typing.Optional[AccountResource]:
456478
resp = make_json_rpc_request(
457-
self._url, self._session, self._timeout, "get_account", [address_hex], GetAccountStateResp
479+
self._url,
480+
self._session,
481+
self._timeout,
482+
"get_account",
483+
[address_hex],
484+
GetAccountStateResp,
458485
)
459486
if resp is None:
460487
return None
@@ -465,7 +492,12 @@ def getAccount(self, address_hex: str) -> typing.Optional[AccountResource]:
465492

466493
def sendTransaction(self, signed_transaction_bytes: bytes) -> None:
467494
resp = make_json_rpc_request(
468-
self._url, self._session, self._timeout, "submit", [signed_transaction_bytes.hex()], SubmitResp
495+
self._url,
496+
self._session,
497+
self._timeout,
498+
"submit",
499+
[signed_transaction_bytes.hex()],
500+
SubmitResp,
469501
)
470502
if resp is None:
471503
return None
@@ -478,7 +510,12 @@ def transactions_by_range(
478510
self, start_version: int, limit: int, include_events: bool = False
479511
) -> typing.List[typing.Tuple[SignedTransaction, EventsType]]:
480512
resp_dict = make_json_rpc_request(
481-
self._url, self._session, self._timeout, "get_transactions", [start_version, limit, include_events], None
513+
self._url,
514+
self._session,
515+
self._timeout,
516+
"get_transactions",
517+
[start_version, limit, include_events],
518+
None,
482519
)
483520

484521
results = []
@@ -496,7 +533,12 @@ def transaction_by_acc_seq(
496533
self, addr_hex: str, seq: int, include_events: bool = False
497534
) -> typing.Tuple[typing.Optional[SignedTransaction], EventsType]:
498535
resp_dict = make_json_rpc_request(
499-
self._url, self._session, self._timeout, "get_account_transaction", [addr_hex, seq, include_events], None
536+
self._url,
537+
self._session,
538+
self._timeout,
539+
"get_account_transaction",
540+
[addr_hex, seq, include_events],
541+
None,
500542
)
501543

502544
if resp_dict is None:
@@ -512,11 +554,18 @@ def transaction_by_acc_seq(
512554

513555
def get_events(self, key_hex: str, start: int, limit: int) -> EventsType:
514556
resp_list = make_json_rpc_request(
515-
self._url, self._session, self._timeout, "get_events", [key_hex, start, limit], None
557+
self._url,
558+
self._session,
559+
self._timeout,
560+
"get_events",
561+
[key_hex, start, limit],
562+
None,
516563
)
517564
return as_events(resp_list)
518565

519566
def get_currencies(self) -> typing.List[CurrencyInfo]:
520-
resp_list = make_json_rpc_request(self._url, self._session, self._timeout, "get_currencies", [], None)
567+
resp_list = make_json_rpc_request(
568+
self._url, self._session, self._timeout, "get_currencies", [], None
569+
)
521570
res = [CurrencyInfo(**x) for x in resp_list]
522571
return res

python/src/pylibra/_mint.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33

44
# pyre-strict
55

6-
import requests
76
import typing
7+
8+
import requests
89
from requests.exceptions import RequestException
910

10-
from ._config import NETWORK_DEFAULT, ENDPOINT_CONFIG, DEFAULT_CONNECT_TIMEOUT_SECS, DEFAULT_TIMEOUT_SECS
11+
from ._config import (
12+
DEFAULT_CONNECT_TIMEOUT_SECS,
13+
DEFAULT_TIMEOUT_SECS,
14+
ENDPOINT_CONFIG,
15+
NETWORK_DEFAULT,
16+
)
1117

1218

1319
class FaucetError(Exception):
@@ -26,7 +32,9 @@ def mint(
2632
amount: int,
2733
identifier: str = "LBR",
2834
session: typing.Optional[requests.Session] = None,
29-
timeout: typing.Optional[typing.Union[float, typing.Tuple[float, float]]] = None,
35+
timeout: typing.Optional[
36+
typing.Union[float, typing.Tuple[float, float]]
37+
] = None,
3038
) -> int:
3139
"""Request faucet to send libra to destination address."""
3240
if len(authkey_hex) != 64:
@@ -39,8 +47,14 @@ def mint(
3947
try:
4048
r = _session.post(
4149
self._baseurl,
42-
params={"amount": amount, "auth_key": authkey_hex, "currency_code": identifier},
43-
timeout=timeout if timeout else (DEFAULT_CONNECT_TIMEOUT_SECS, DEFAULT_TIMEOUT_SECS),
50+
params={
51+
"amount": amount,
52+
"auth_key": authkey_hex,
53+
"currency_code": identifier,
54+
},
55+
timeout=timeout
56+
if timeout
57+
else (DEFAULT_CONNECT_TIMEOUT_SECS, DEFAULT_TIMEOUT_SECS),
4458
)
4559
r.raise_for_status()
4660
if r.text:

python/src/pylibra/_native.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
# pyre-strict
55

6-
from ._types import SignedTransaction, AccountKey
76
import typing
87

8+
from ._types import AccountKey, SignedTransaction
9+
910
class TransactionUtils:
1011
@staticmethod
1112
def createSignedP2PTransaction(

python/src/pylibra/_transport.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
# pyre-strict
55

6+
import dataclasses
67
import typing
78
from abc import ABC
8-
import dataclasses
99

10-
from . import NETWORK_DEFAULT
11-
from . import AccountResource, SignedTransaction, Event, PaymentEvent, CurrencyInfo
10+
from . import (
11+
NETWORK_DEFAULT,
12+
AccountResource,
13+
CurrencyInfo,
14+
Event,
15+
PaymentEvent,
16+
SignedTransaction,
17+
)
1218

1319

1420
@dataclasses.dataclass
@@ -44,15 +50,22 @@ def sendTransaction(self, signed_transaction_bytes: bytes) -> None:
4450

4551
def transactions_by_range(
4652
self, start_version: int, limit: int, include_events: bool = False
47-
) -> typing.List[typing.Tuple[SignedTransaction, typing.List[typing.Union[Event, PaymentEvent]]]]:
53+
) -> typing.List[
54+
typing.Tuple[SignedTransaction, typing.List[typing.Union[Event, PaymentEvent]]]
55+
]:
4856
raise NotImplementedError()
4957

5058
def transaction_by_acc_seq(
5159
self, addr_hex: str, seq: int, include_events: bool = False
52-
) -> typing.Tuple[typing.Optional[SignedTransaction], typing.List[typing.Union[Event, PaymentEvent]]]:
60+
) -> typing.Tuple[
61+
typing.Optional[SignedTransaction],
62+
typing.List[typing.Union[Event, PaymentEvent]],
63+
]:
5364
raise NotImplementedError()
5465

55-
def get_events(self, key_hex: str, start: int, limit: int) -> typing.List[typing.Union[Event, PaymentEvent]]:
66+
def get_events(
67+
self, key_hex: str, start: int, limit: int
68+
) -> typing.List[typing.Union[Event, PaymentEvent]]:
5669
raise NotImplementedError()
5770

5871
def get_currencies(self) -> typing.List[CurrencyInfo]:

0 commit comments

Comments
 (0)