Skip to content

Commit ace3fb1

Browse files
authored
Merge pull request #24 from yungwine/get-libs-balancer
Get libs, balancer
2 parents 71f301f + cb94cd7 commit ace3fb1

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

pytoniq/liteclient/balancer.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ def __init__(self, peers: typing.List[LiteClient], timeout: int = 10):
3333

3434
self._logger = logging.getLogger(self.__class__.__name__)
3535

36+
self.inited = False
3637
self.max_req_per_peer = 100
3738
self.max_retries = 1
3839
self.timeout = timeout
3940

4041
@property
41-
def inited(self):
42-
return bool(self._alive_peers)
42+
def last_mc_block(self):
43+
seqno = self._find_consensus_block()
44+
for p in self._peers:
45+
if p.last_mc_block.seqno == seqno:
46+
return p.last_mc_block
47+
return None
4348

4449
def set_max_retries(self, retries_num: int) -> None:
4550
self.max_retries = retries_num
@@ -66,6 +71,7 @@ async def f(): return connected
6671
await self._find_archives()
6772
self._checker = asyncio.create_task(self._check_peers())
6873
self._delete_unsync_peers()
74+
self.inited = True
6975

7076
async def _find_archives(self):
7177
tasks = []

pytoniq/liteclient/client.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -577,19 +577,18 @@ async def run_get_method_local(self, address: typing.Union[Address, str],
577577

578578
# set libs
579579
libs = []
580-
result_libs = {}
581580
self._find_libs(account.account.storage.state.state_init.code, libs)
582581
libs = [i for i in libs if i.hex() not in self.libs]
583-
libs = [libs[i:i + 16] for i in range(0, len(libs), 16)] # split libs into 16-element chunks
584-
for i in libs:
585-
result_libs |= await self.get_libraries(i)
586-
587-
def value_serializer(dest: Builder, src: Cell):
588-
if src is not None:
589-
dest.store_uint(0, 2).store_ref(src).store_maybe_ref(None)
590-
hm = HashMap(256, value_serializer=value_serializer)
591-
hm.map = self.libs
592-
if self.libs:
582+
if libs:
583+
self.libs |= await self.get_libraries(libs)
584+
585+
if libs and self.libs:
586+
def value_serializer(dest: Builder, src: Cell):
587+
if src is not None:
588+
dest.store_uint(0, 2).store_ref(src).store_maybe_ref(None)
589+
590+
hm = HashMap(256, value_serializer=value_serializer)
591+
hm.map = self.libs
593592
emulator.set_libraries(hm.serialize())
594593

595594
# todo set prev blocks info
@@ -1029,11 +1028,7 @@ async def get_config_params(self, params: typing.List[int], blk: typing.Optional
10291028
state_proof = Cell.one_from_boc(result['state_proof'])
10301029
return self.unpack_config(blk, config_proof, state_proof)
10311030

1032-
async def get_libraries(self, library_list: typing.List[typing.Union[bytes, str]]) -> typing.Dict[str, typing.Optional[Cell]]:
1033-
"""
1034-
:param library_list: list of library hashes in bytes or string hex form
1035-
:return: dict {library_hash_hex: library Cell or None if library not found}
1036-
"""
1031+
async def _get_libraries(self, library_list: typing.List[typing.Union[bytes, str]]) -> typing.Dict[str, typing.Optional[Cell]]:
10371032
if len(library_list) > 16:
10381033
raise LiteClientError('maximum libraries num could be requested is 16')
10391034
library_list = [lib.hex() if isinstance(lib, bytes) else lib for lib in library_list]
@@ -1054,6 +1049,17 @@ async def get_libraries(self, library_list: typing.List[typing.Union[bytes, str]
10541049

10551050
return result
10561051

1052+
async def get_libraries(self, library_list: typing.List[typing.Union[bytes, str]]) -> typing.Dict[str, typing.Optional[Cell]]:
1053+
"""
1054+
Get libraries from blockchain
1055+
1056+
:param library_list: list of library hashes in bytes or string hex form
1057+
:return: dict {library_hash_hex: library Cell or None if library not found}
1058+
"""
1059+
libs = [library_list[i:i + 16] for i in range(0, len(library_list), 16)] # split libs into 16-element chunks
1060+
result = await asyncio.gather(*[self._get_libraries(lib) for lib in libs])
1061+
return {k: v for d in result for k, v in d.items()}
1062+
10571063
async def get_out_msg_queue_sizes(self, wc: int = None, shard: int = None):
10581064
"""
10591065
If wc and shard are not None, returns queue size for all children shards of the provided shard.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="pytoniq",
8-
version="0.1.35",
8+
version="0.1.36",
99
author="Maksim Kurbatov",
1010
author_email="[email protected]",
1111
description="TON Blockchain SDK",

0 commit comments

Comments
 (0)