Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/util.py
Show First 20 Lines • Show All 243 Lines • ▼ Show 20 Lines | |||||
def str_to_b64str(string): | def str_to_b64str(string): | ||||
return b64encode(string.encode('utf-8')).decode('ascii') | return b64encode(string.encode('utf-8')).decode('ascii') | ||||
def satoshi_round(amount): | def satoshi_round(amount): | ||||
return Decimal(amount).quantize(Decimal('0.01'), rounding=ROUND_DOWN) | return Decimal(amount).quantize(Decimal('0.01'), rounding=ROUND_DOWN) | ||||
def wait_until(predicate, *, attempts=float('inf'), | def wait_until_helper(predicate, *, attempts=float('inf'), | ||||
timeout=float('inf'), lock=None, timeout_factor=1.0): | timeout=float('inf'), lock=None, timeout_factor=1.0): | ||||
"""Sleep until the predicate resolves to be True. | """Sleep until the predicate resolves to be True. | ||||
Warning: Note that this method is not recommended to be used in tests as it is | Warning: Note that this method is not recommended to be used in tests as it is | ||||
not aware of the context of the test framework. Using `wait_until()` counterpart | not aware of the context of the test framework. Using the `wait_until()` members | ||||
from `BitcoinTestFramework` or `P2PInterface` class ensures an understandable | from `BitcoinTestFramework` or `P2PInterface` class ensures the timeout is | ||||
amount of timeout and a common shared timeout_factor. Furthermore, `wait_until()` | properly scaled. Furthermore, `wait_until()` from `P2PInterface` class in | ||||
from `P2PInterface` class in `mininode.py` has a preset lock. | `p2p.py` has a preset lock. | ||||
""" | """ | ||||
if attempts == float('inf') and timeout == float('inf'): | if attempts == float('inf') and timeout == float('inf'): | ||||
timeout = 60 | timeout = 60 | ||||
timeout = timeout * timeout_factor | timeout = timeout * timeout_factor | ||||
attempt = 0 | attempt = 0 | ||||
time_end = time.time() + timeout | time_end = time.time() + timeout | ||||
while attempt < attempts and time.time() < time_end: | while attempt < attempts and time.time() < time_end: | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | for peer_id in peer_ids: | ||||
except JSONRPCException as e: | except JSONRPCException as e: | ||||
# If this node is disconnected between calculating the peer id | # If this node is disconnected between calculating the peer id | ||||
# and issuing the disconnect, don't worry about it. | # and issuing the disconnect, don't worry about it. | ||||
# This avoids a race condition if we're mass-disconnecting peers. | # This avoids a race condition if we're mass-disconnecting peers. | ||||
if e.error['code'] != -29: # RPC_CLIENT_NODE_NOT_CONNECTED | if e.error['code'] != -29: # RPC_CLIENT_NODE_NOT_CONNECTED | ||||
raise | raise | ||||
# wait to disconnect | # wait to disconnect | ||||
wait_until(lambda: not get_peer_ids(), timeout=5) | wait_until_helper(lambda: not get_peer_ids(), timeout=5) | ||||
def connect_nodes(from_node, to_node): | def connect_nodes(from_node, to_node): | ||||
host = to_node.host | host = to_node.host | ||||
if host is None: | if host is None: | ||||
host = '127.0.0.1' | host = '127.0.0.1' | ||||
ip_port = host + ':' + str(to_node.p2p_port) | ip_port = host + ':' + str(to_node.p2p_port) | ||||
from_node.addnode(ip_port, "onetry") | from_node.addnode(ip_port, "onetry") | ||||
# poll until version handshake complete to avoid race conditions | # poll until version handshake complete to avoid race conditions | ||||
# with transaction relaying | # with transaction relaying | ||||
# See comments in net_processing: | # See comments in net_processing: | ||||
# * Must have a version message before anything else | # * Must have a version message before anything else | ||||
# * Must have a verack message before anything else | # * Must have a verack message before anything else | ||||
wait_until( | wait_until_helper( | ||||
lambda: all( | lambda: all(peer['version'] != 0 | ||||
peer['version'] != 0 for peer in from_node.getpeerinfo())) | for peer in from_node.getpeerinfo())) | ||||
wait_until( | wait_until_helper( | ||||
lambda: all( | lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 | ||||
peer['bytesrecv_per_msg'].pop( | for peer in from_node.getpeerinfo())) | ||||
'verack', | |||||
0) == 24 for peer in from_node.getpeerinfo())) | |||||
# Transaction/Block functions | # Transaction/Block functions | ||||
############################# | ############################# | ||||
def find_output(node, txid, amount, *, blockhash=None): | def find_output(node, txid, amount, *, blockhash=None): | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 161 Lines • Show Last 20 Lines |