Skip to content

Commit

Permalink
Merge pull request #24 from yungwine/get-libs-balancer
Browse files Browse the repository at this point in the history
Get libs, balancer
  • Loading branch information
yungwine authored May 9, 2024
2 parents 71f301f + cb94cd7 commit ace3fb1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
10 changes: 8 additions & 2 deletions pytoniq/liteclient/balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ def __init__(self, peers: typing.List[LiteClient], timeout: int = 10):

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

self.inited = False
self.max_req_per_peer = 100
self.max_retries = 1
self.timeout = timeout

@property
def inited(self):
return bool(self._alive_peers)
def last_mc_block(self):
seqno = self._find_consensus_block()
for p in self._peers:
if p.last_mc_block.seqno == seqno:
return p.last_mc_block
return None

def set_max_retries(self, retries_num: int) -> None:
self.max_retries = retries_num
Expand All @@ -66,6 +71,7 @@ async def f(): return connected
await self._find_archives()
self._checker = asyncio.create_task(self._check_peers())
self._delete_unsync_peers()
self.inited = True

async def _find_archives(self):
tasks = []
Expand Down
38 changes: 22 additions & 16 deletions pytoniq/liteclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,18 @@ async def run_get_method_local(self, address: typing.Union[Address, str],

# set libs
libs = []
result_libs = {}
self._find_libs(account.account.storage.state.state_init.code, libs)
libs = [i for i in libs if i.hex() not in self.libs]
libs = [libs[i:i + 16] for i in range(0, len(libs), 16)] # split libs into 16-element chunks
for i in libs:
result_libs |= await self.get_libraries(i)

def value_serializer(dest: Builder, src: Cell):
if src is not None:
dest.store_uint(0, 2).store_ref(src).store_maybe_ref(None)
hm = HashMap(256, value_serializer=value_serializer)
hm.map = self.libs
if self.libs:
if libs:
self.libs |= await self.get_libraries(libs)

if libs and self.libs:
def value_serializer(dest: Builder, src: Cell):
if src is not None:
dest.store_uint(0, 2).store_ref(src).store_maybe_ref(None)

hm = HashMap(256, value_serializer=value_serializer)
hm.map = self.libs
emulator.set_libraries(hm.serialize())

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

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

return result

async def get_libraries(self, library_list: typing.List[typing.Union[bytes, str]]) -> typing.Dict[str, typing.Optional[Cell]]:
"""
Get libraries from blockchain
:param library_list: list of library hashes in bytes or string hex form
:return: dict {library_hash_hex: library Cell or None if library not found}
"""
libs = [library_list[i:i + 16] for i in range(0, len(library_list), 16)] # split libs into 16-element chunks
result = await asyncio.gather(*[self._get_libraries(lib) for lib in libs])
return {k: v for d in result for k, v in d.items()}

async def get_out_msg_queue_sizes(self, wc: int = None, shard: int = None):
"""
If wc and shard are not None, returns queue size for all children shards of the provided shard.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pytoniq",
version="0.1.35",
version="0.1.36",
author="Maksim Kurbatov",
author_email="[email protected]",
description="TON Blockchain SDK",
Expand Down

0 comments on commit ace3fb1

Please sign in to comment.