Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_compactproofs.py
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | def received_avaproofs(peer): | ||||
with p2p_lock: | with p2p_lock: | ||||
return peer.last_message.get("avaproofs") | return peer.last_message.get("avaproofs") | ||||
def test_send_outbound_getavaproofs(self): | def test_send_outbound_getavaproofs(self): | ||||
self.log.info( | self.log.info( | ||||
"Check we send a getavaproofs message to our avalanche outbound peers") | "Check we send a getavaproofs message to our avalanche outbound peers") | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
p2p_idx = 0 | |||||
non_avapeers = [] | non_avapeers = [] | ||||
for i in range(4): | for i in range(4): | ||||
peer = P2PInterface() | peer = P2PInterface() | ||||
node.add_outbound_p2p_connection( | node.add_outbound_p2p_connection( | ||||
peer, | peer, | ||||
p2p_idx=i, | p2p_idx=p2p_idx, | ||||
connection_type="outbound-full-relay", | connection_type="outbound-full-relay", | ||||
services=NODE_NETWORK, | services=NODE_NETWORK, | ||||
) | ) | ||||
non_avapeers.append(peer) | non_avapeers.append(peer) | ||||
p2p_idx += 1 | |||||
inbound_avapeers = [ | inbound_avapeers = [ | ||||
node.add_p2p_connection( | node.add_p2p_connection( | ||||
AvaP2PInterface()) for _ in range(4)] | AvaP2PInterface()) for _ in range(4)] | ||||
outbound_avapeers = [] | outbound_avapeers = [] | ||||
for i in range(4): | for i in range(4): | ||||
peer = P2PInterface() | peer = P2PInterface() | ||||
node.add_outbound_p2p_connection( | node.add_outbound_p2p_connection( | ||||
peer, | peer, | ||||
p2p_idx=16 + i, | p2p_idx=p2p_idx, | ||||
connection_type="avalanche", | connection_type="avalanche", | ||||
services=NODE_NETWORK | NODE_AVALANCHE, | services=NODE_NETWORK | NODE_AVALANCHE, | ||||
) | ) | ||||
outbound_avapeers.append(peer) | outbound_avapeers.append(peer) | ||||
p2p_idx += 1 | |||||
def all_peers_received_getavaproofs(): | def all_peers_received_getavaproofs(): | ||||
with p2p_lock: | with p2p_lock: | ||||
return all([p.last_message.get("getavaproofs") | return all([p.last_message.get("getavaproofs") | ||||
for p in outbound_avapeers]) | for p in outbound_avapeers]) | ||||
self.wait_until(all_peers_received_getavaproofs) | self.wait_until(all_peers_received_getavaproofs) | ||||
with p2p_lock: | with p2p_lock: | ||||
assert all([p.message_count.get( | assert all([p.message_count.get( | ||||
"getavaproofs", 0) >= 1 for p in outbound_avapeers]) | "getavaproofs", 0) >= 1 for p in outbound_avapeers]) | ||||
assert all([p.message_count.get( | assert all([p.message_count.get( | ||||
"getavaproofs", 0) == 0 for p in non_avapeers]) | "getavaproofs", 0) == 0 for p in non_avapeers]) | ||||
assert all([p.message_count.get( | assert all([p.message_count.get( | ||||
"getavaproofs", 0) == 0 for p in inbound_avapeers]) | "getavaproofs", 0) == 0 for p in inbound_avapeers]) | ||||
self.log.info( | self.log.info( | ||||
"Check we send periodic getavaproofs message to one of our peers") | "Check we send periodic getavaproofs message to some of our peers") | ||||
def count_outbounds_getavaproofs(): | def count_outbounds_getavaproofs(): | ||||
with p2p_lock: | with p2p_lock: | ||||
return sum([p.message_count.get("getavaproofs", 0) | return sum([p.message_count.get("getavaproofs", 0) | ||||
for p in outbound_avapeers]) | for p in outbound_avapeers]) | ||||
outbounds_getavaproofs = count_outbounds_getavaproofs() | outbounds_getavaproofs = count_outbounds_getavaproofs() | ||||
for i in range(12): | for i in range(12): | ||||
node.mockscheduler(AVALANCHE_MAX_PERIODIC_NETWORKING_INTERVAL) | node.mockscheduler(AVALANCHE_MAX_PERIODIC_NETWORKING_INTERVAL) | ||||
self.wait_until(lambda: count_outbounds_getavaproofs() | self.wait_until(lambda: count_outbounds_getavaproofs() | ||||
== outbounds_getavaproofs + 1) | == outbounds_getavaproofs + 3) | ||||
outbounds_getavaproofs += 1 | outbounds_getavaproofs += 3 | ||||
with p2p_lock: | |||||
assert all([p.message_count.get( | |||||
"getavaproofs", 0) == 0 for p in non_avapeers]) | |||||
assert all([p.message_count.get( | |||||
"getavaproofs", 0) == 0 for p in inbound_avapeers]) | |||||
self.log.info( | |||||
"After the first avaproofs has been received, all the peers are requested periodically") | |||||
responding_outbound_avapeers = P2PInterface() | |||||
sdulfari: nit: responding_outbound_avapeer (drop the plural) | |||||
node.add_outbound_p2p_connection( | |||||
responding_outbound_avapeers, | |||||
p2p_idx=p2p_idx, | |||||
connection_type="avalanche", | |||||
services=NODE_NETWORK | NODE_AVALANCHE, | |||||
) | |||||
p2p_idx += 1 | |||||
responding_outbound_avapeers_id = node.getpeerinfo()[-1]['id'] | |||||
outbound_avapeers.append(responding_outbound_avapeers) | |||||
self.wait_until(all_peers_received_getavaproofs) | |||||
# Register as an avalanche node for the avaproofs message to be counted | |||||
key, proof = gen_proof(node) | |||||
assert node.addavalanchenode( | |||||
responding_outbound_avapeers_id, | |||||
key.get_pubkey().get_bytes().hex(), | |||||
proof.serialize().hex()) | |||||
# Send the avaproofs message | |||||
avaproofs = msg_avaproofs() | |||||
avaproofs.key0 = random.randint(0, 2**64 - 1) | |||||
avaproofs.key1 = random.randint(0, 2**64 - 1) | |||||
avaproofs.prefilled_proofs = [] | |||||
avaproofs.shortids = [ | |||||
calculate_shortid( | |||||
avaproofs.key0, | |||||
avaproofs.key1, | |||||
proof.proofid)] | |||||
responding_outbound_avapeers.send_and_ping(avaproofs) | |||||
# Now the node will request from all its peers at each time period | |||||
outbounds_getavaproofs = count_outbounds_getavaproofs() | |||||
num_outbound_avapeers = len(outbound_avapeers) | |||||
for i in range(12): | |||||
node.mockscheduler(AVALANCHE_MAX_PERIODIC_NETWORKING_INTERVAL) | |||||
self.wait_until(lambda: count_outbounds_getavaproofs() | |||||
== outbounds_getavaproofs + num_outbound_avapeers) | |||||
outbounds_getavaproofs += num_outbound_avapeers | |||||
with p2p_lock: | with p2p_lock: | ||||
assert all([p.message_count.get( | assert all([p.message_count.get( | ||||
"getavaproofs", 0) == 0 for p in non_avapeers]) | "getavaproofs", 0) == 0 for p in non_avapeers]) | ||||
assert all([p.message_count.get( | assert all([p.message_count.get( | ||||
"getavaproofs", 0) == 0 for p in inbound_avapeers]) | "getavaproofs", 0) == 0 for p in inbound_avapeers]) | ||||
def test_send_manual_getavaproofs(self): | def test_send_manual_getavaproofs(self): | ||||
Show All 39 Lines | def test_respond_getavaproofs(self): | ||||
def send_getavaproof_check_shortid_len(peer, expected_len): | def send_getavaproof_check_shortid_len(peer, expected_len): | ||||
peer.send_message(msg_getavaproofs()) | peer.send_message(msg_getavaproofs()) | ||||
self.wait_until(lambda: self.received_avaproofs(peer)) | self.wait_until(lambda: self.received_avaproofs(peer)) | ||||
avaproofs = self.received_avaproofs(peer) | avaproofs = self.received_avaproofs(peer) | ||||
assert_equal(len(avaproofs.shortids), expected_len) | assert_equal(len(avaproofs.shortids), expected_len) | ||||
# Initially the node has 0 peer | # Initially the node has 0 peer | ||||
self.restart_node(0) | |||||
assert_equal(len(get_proof_ids(node)), 0) | assert_equal(len(get_proof_ids(node)), 0) | ||||
peer = node.add_p2p_connection(AvaP2PInterface()) | peer = node.add_p2p_connection(AvaP2PInterface()) | ||||
send_getavaproof_check_shortid_len(peer, 0) | send_getavaproof_check_shortid_len(peer, 0) | ||||
# Add some proofs | # Add some proofs | ||||
sending_peer = node.add_p2p_connection(AvaP2PInterface()) | sending_peer = node.add_p2p_connection(AvaP2PInterface()) | ||||
for _ in range(50): | for _ in range(50): | ||||
▲ Show 20 Lines • Show All 357 Lines • Show Last 20 Lines |
nit: responding_outbound_avapeer (drop the plural)