本文整理汇总了Python中eth_typing.Hash32方法的典型用法代码示例。如果您正苦于以下问题:Python eth_typing.Hash32方法的具体用法?Python eth_typing.Hash32怎么用?Python eth_typing.Hash32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eth_typing
的用法示例。
在下文中一共展示了eth_typing.Hash32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: recv_ping_v5
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def recv_ping_v5(
self,
node: kademlia.Node,
payload: Tuple[Any, ...],
message_hash: Hash32,
_: bytes,
) -> None:
# version, from, to, expiration, topics
_, _, _, _, topics = payload
self.logger.trace("<<< ping(v5) from %s, topics: %s", node, topics)
self.process_ping(node, message_hash)
topic_hash = keccak(rlp.encode(topics))
ticket_serial = self.topic_table.issue_ticket(node)
# TODO: Generate wait_periods list according to spec.
wait_periods = [60] * len(topics) # : List[int]
self.send_pong_v5(node, message_hash, topic_hash, ticket_serial, wait_periods)
示例2: send_pong_v5
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def send_pong_v5(
self,
node: kademlia.Node,
token: Hash32,
topic_hash: Hash32,
ticket_serial: int,
wait_periods: List[int],
) -> None:
self.logger.trace(">>> pong (v5) %s", node)
payload = (
node.address.to_endpoint(),
token,
_get_msg_expiration(),
topic_hash,
ticket_serial,
wait_periods,
)
message = _pack_v5(CMD_PONG_V5.id, payload, self.privkey)
self.send_v5(node, message)
示例3: process_pong_v5
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def process_pong_v5(
self,
remote: kademlia.Node,
token: Hash32,
raw_msg: bytes,
wait_periods: List[float],
) -> None:
pingid = self._mkpingid(token, remote)
try:
callback = self.pong_callbacks.get_callback(pingid)
except KeyError:
self.logger.debug(
"unexpected pong from %s (token == %s)", remote, encode_hex(token)
)
else:
callback(raw_msg, wait_periods)
示例4: _unpack_v4
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def _unpack_v4(
message: bytes
) -> Tuple[datatypes.PublicKey, int, Tuple[Any, ...], Hash32]:
"""Unpack a discovery v4 UDP message received from a remote node.
Returns the public key used to sign the message, the cmd ID, payload and hash.
"""
message_hash = message[:MAC_SIZE]
if message_hash != keccak(message[MAC_SIZE:]):
raise WrongMAC("Wrong msg mac")
signature = keys.Signature(message[MAC_SIZE:HEAD_SIZE])
signed_data = message[HEAD_SIZE:]
remote_pubkey = signature.recover_public_key_from_msg(signed_data)
cmd_id = message[HEAD_SIZE]
cmd = CMD_ID_MAP[cmd_id]
payload = tuple(rlp.decode(message[HEAD_SIZE + 1 :], strict=False))
# Ignore excessive list elements as required by EIP-8.
payload = payload[: cmd.elem_count]
return remote_pubkey, cmd_id, payload, message_hash
示例5: merkleize_with_cache
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def merkleize_with_cache(
chunks: Sequence[Hash32], cache: CacheObj, limit: int = None
) -> Tuple[Hash32, CacheObj]:
chunk_len = len(chunks)
if limit is None:
limit = chunk_len
chunk_depth, max_depth = _get_chunk_and_max_depth(chunks, limit, chunk_len)
if limit == 0:
return ZERO_HASHES[0], cache
return _get_merkleized_result(
chunks=chunks,
chunk_len=chunk_len,
chunk_depth=chunk_depth,
max_depth=max_depth,
cache=cache,
)
示例6: compute_hash_tree
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def compute_hash_tree(
chunks: Iterable[Hash32], chunk_count: Optional[int] = None
) -> RawHashTree:
validate_chunk_count(chunk_count)
chunks = pvector(chunks)
if not chunks:
raise ValueError("Number of chunks is 0")
if chunk_count is not None and len(chunks) > chunk_count:
raise ValueError(
f"Number of chunks ({len(chunks)}) exceeds chunk_count ({chunk_count})"
)
unpadded_chunk_tree = pvector(generate_hash_tree_layers(chunks))
return pad_hash_tree(unpadded_chunk_tree, chunk_count)
示例7: set_chunk_in_tree
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def set_chunk_in_tree(hash_tree: RawHashTree, index: int, chunk: Hash32) -> RawHashTree:
hash_tree_with_updated_chunk = hash_tree.transform((0, index), chunk)
parent_layer_indices = drop(1, range(len(hash_tree)))
parent_hash_indices = drop(
1, take(len(hash_tree), iterate(lambda index: index // 2, index))
)
update_functions = (
partial(recompute_hash_in_tree, layer_index=layer_index, hash_index=hash_index)
for layer_index, hash_index in zip(parent_layer_indices, parent_hash_indices)
)
hash_tree_with_updated_branch = pipe(
hash_tree_with_updated_chunk, *update_functions
)
if len(hash_tree_with_updated_branch[-1]) == 1:
return hash_tree_with_updated_branch
elif len(hash_tree_with_updated_branch[-1]) == 2:
return recompute_hash_in_tree(hash_tree_with_updated_branch, len(hash_tree), 0)
else:
raise Exception("Unreachable")
示例8: get_appended_chunks
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def get_appended_chunks(
*, appended_elements: Sequence[bytes], element_size: int, num_padding_elements: int
) -> Generator[Hash32, None, None]:
"""Get the sequence of appended chunks."""
if len(appended_elements) <= num_padding_elements:
return
elements_per_chunk = CHUNK_SIZE // element_size
chunk_partitioned_elements = partition(
elements_per_chunk,
appended_elements[num_padding_elements:],
pad=b"\x00" * element_size,
)
for elements_in_chunk in chunk_partitioned_elements:
yield Hash32(b"".join(elements_in_chunk))
示例9: __init__
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def __init__(
self,
privkey: datatypes.PrivateKey,
address: kademlia.Address,
bootstrap_nodes: Tuple[kademlia.Node, ...],
network_id: int,
cancel_token: CancelToken,
) -> None:
# this is a hack to keep the logging statements (quarkchain.utils.Logger only supports logging strings)
Logger.check_logger_set()
self.logger = Logger._qkc_logger
self.privkey = privkey
self.address = address
self.bootstrap_nodes = bootstrap_nodes
self.this_node = kademlia.Node(self.pubkey, address)
self.routing = kademlia.RoutingTable(self.this_node)
self.topic_table = TopicTable(self.logger)
self.pong_callbacks = CallbackManager()
self.ping_callbacks = CallbackManager()
self.neighbours_callbacks = CallbackManager()
self.topic_nodes_callbacks = CallbackManager()
self.parity_pong_tokens = {} # : Dict[Hash32, Hash32]
self.network_id = network_id
self.cancel_token = CancelToken("DiscoveryProtocol").chain(cancel_token)
self.QKC_ID_STRING = QKC_ID_STRING_TEMPLATE.format(self.network_id).encode()
self.V5_ID_STRING = V5_ID_STRING_TEMPLATE.format(self.network_id).encode()
self.HEAD_SIZE_V5 = len(self.V5_ID_STRING) + SIG_SIZE # 87
示例10: wait_pong_v4
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def wait_pong_v4(self, remote: kademlia.Node, token: Hash32) -> bool:
event = asyncio.Event()
callback = event.set
return await self._wait_pong(remote, token, event, callback)
示例11: _wait_pong
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def _wait_pong(
self,
remote: kademlia.Node,
token: Hash32,
event: asyncio.Event,
callback: Callable[..., Any],
) -> bool:
"""Wait for a pong from the given remote containing the given token.
This coroutine adds a callback to pong_callbacks and yields control until the given event
is set or a timeout (k_request_timeout) occurs. At that point it returns whether or not
a pong was received with the given pingid.
"""
pingid = self._mkpingid(token, remote)
with self.pong_callbacks.acquire(pingid, callback):
got_pong = False
try:
got_pong = await self.cancel_token.cancellable_wait(
event.wait(), timeout=kademlia.k_request_timeout
)
self.logger.trace("got expected pong with token %s", encode_hex(token))
except TimeoutError:
self.logger.trace(
"timed out waiting for pong from %s (token == %s)",
remote,
encode_hex(token),
)
return got_pong
示例12: _mkpingid
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def _mkpingid(self, token: Hash32, node: kademlia.Node) -> Hash32:
return token + node.pubkey.to_bytes()
示例13: recv_pong_v4
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def recv_pong_v4(
self, node: kademlia.Node, payload: Tuple[Any, ...], _: Hash32
) -> None:
# The pong payload should have 3 elements: to, token, expiration
_, token, _ = payload
self.logger.trace(
"<<< pong (v4) from %s (token == %s)", node, encode_hex(token)
)
self.process_pong_v4(node, token)
示例14: recv_ping_v4
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def recv_ping_v4(self, node: kademlia.Node, _: Any, message_hash: Hash32) -> None:
self.logger.trace("<<< ping(v4) from %s", node)
self.process_ping(node, message_hash)
self.send_pong_v4(node, message_hash)
示例15: recv_find_node_v4
# 需要导入模块: import eth_typing [as 别名]
# 或者: from eth_typing import Hash32 [as 别名]
def recv_find_node_v4(
self, node: kademlia.Node, payload: Tuple[Any, ...], _: Hash32
) -> None:
# The find_node payload should have 2 elements: node_id, expiration
self.logger.trace("<<< find_node from %s", node)
node_id, _ = payload
if node not in self.routing:
# FIXME: This is not correct; a node we've bonded before may have become unavailable
# and thus removed from self.routing, but once it's back online we should accept
# find_nodes from them.
self.logger.debug("Ignoring find_node request from unknown node %s", node)
return
self.update_routing_table(node)
found = self.routing.neighbours(big_endian_to_int(node_id))
self.send_neighbours_v4(node, found)