Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_avalanche_peer_discovery.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2020-2021 The Bitcoin developers | # Copyright (c) 2020-2021 The Bitcoin developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test the peer discovery behavior of avalanche nodes. | """Test the peer discovery behavior of avalanche nodes. | ||||
This includes tests for the service flag, avahello handshake and | This includes tests for the service flag, avahello handshake and | ||||
proof exchange. | proof exchange. | ||||
""" | """ | ||||
import time | import time | ||||
from test_framework.address import ADDRESS_ECREG_UNSPENDABLE | from test_framework.address import ADDRESS_ECREG_UNSPENDABLE | ||||
from test_framework.avatools import ( | from test_framework.avatools import ( | ||||
AvaP2PInterface, | |||||
avalanche_proof_from_hex, | avalanche_proof_from_hex, | ||||
create_coinbase_stakes, | create_coinbase_stakes, | ||||
gen_proof, | gen_proof, | ||||
get_ava_p2p_interface, | get_ava_p2p_interface, | ||||
get_proof_ids, | get_proof_ids, | ||||
wait_for_proof, | wait_for_proof, | ||||
) | ) | ||||
from test_framework.key import ECKey, ECPubKey | from test_framework.key import ECKey, ECPubKey | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
MSG_AVA_PROOF, | MSG_AVA_PROOF, | ||||
MSG_TYPE_MASK, | |||||
NODE_AVALANCHE, | NODE_AVALANCHE, | ||||
NODE_NETWORK, | NODE_NETWORK, | ||||
CInv, | CInv, | ||||
msg_getdata, | msg_getdata, | ||||
) | ) | ||||
from test_framework.p2p import p2p_lock | from test_framework.p2p import p2p_lock | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal | from test_framework.util import assert_equal | ||||
from test_framework.wallet_util import bytes_to_wif | from test_framework.wallet_util import bytes_to_wif | ||||
UNCONDITIONAL_RELAY_DELAY = 2 * 60 | UNCONDITIONAL_RELAY_DELAY = 2 * 60 | ||||
class GetProofDataCountingInterface(AvaP2PInterface): | |||||
def __init__(self): | |||||
self.get_proof_data_count = 0 | |||||
super().__init__() | |||||
def on_getdata(self, message): | |||||
if message.inv.type & MSG_TYPE_MASK == MSG_AVA_PROOF: | |||||
self.get_proof_data_count += 1 | |||||
class AvalancheTest(BitcoinTestFramework): | class AvalancheTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [['-enableavalanche=1', | self.extra_args = [['-enableavalanche=1', | ||||
'-avaproofstakeutxoconfirmations=3', | '-avaproofstakeutxoconfirmations=3', | ||||
'-enableavalanchepeerdiscovery=1']] | '-enableavalanchepeerdiscovery=1']] | ||||
self.supports_cli = False | self.supports_cli = False | ||||
Show All 18 Lines | def run_test(self): | ||||
blockhashes = node.generatetoaddress(4, addrkey0.address) | blockhashes = node.generatetoaddress(4, addrkey0.address) | ||||
stakes = create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key) | stakes = create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key) | ||||
proof_sequence = 11 | proof_sequence = 11 | ||||
proof_expiration = 12 | proof_expiration = 12 | ||||
proof = node.buildavalancheproof( | proof = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, wif_privkey, stakes) | proof_sequence, proof_expiration, wif_privkey, stakes) | ||||
self.log.info("Test the avahello signature with no proof") | |||||
peer = get_ava_p2p_interface(node) | |||||
avahello = peer.wait_for_avahello().hello | |||||
assert_equal(avahello.delegation.limited_proofid, 0) | |||||
self.log.info( | |||||
"A delegation with all zero limited id indicates that the peer has no proof") | |||||
no_proof_peer = GetProofDataCountingInterface() | |||||
node.add_p2p_connection(no_proof_peer) | |||||
master_key = ECKey() | |||||
master_key.generate() | |||||
null_delegation = node.delegateavalancheproof( | |||||
f"{0:0{64}x}", | |||||
bytes_to_wif(privkey.get_bytes()), | |||||
master_key.get_pubkey().get_bytes().hex(), | |||||
) | |||||
no_proof_peer.send_avahello(null_delegation, privkey) | |||||
no_proof_peer.sync_send_with_ping() | |||||
# No proof is requested | |||||
with p2p_lock: | |||||
assert_equal(no_proof_peer.get_proof_data_count, 0) | |||||
# Restart the node | # Restart the node | ||||
self.restart_node(0, self.extra_args[0] + [ | self.restart_node(0, self.extra_args[0] + [ | ||||
"-avaproof={}".format(proof), | "-avaproof={}".format(proof), | ||||
"-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", | "-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", | ||||
]) | ]) | ||||
assert_equal( | assert_equal( | ||||
int(node.getnetworkinfo()['localservices'], 16) & NODE_AVALANCHE, | int(node.getnetworkinfo()['localservices'], 16) & NODE_AVALANCHE, | ||||
Show All 14 Lines | def run_test(self): | ||||
self.log.info( | self.log.info( | ||||
"Test the avahello signature with a generated delegation") | "Test the avahello signature with a generated delegation") | ||||
check_avahello([ | check_avahello([ | ||||
"-avaproof={}".format(proof), | "-avaproof={}".format(proof), | ||||
"-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN" | "-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN" | ||||
]) | ]) | ||||
master_key = ECKey() | |||||
master_key.generate() | |||||
limited_id = avalanche_proof_from_hex(proof).limited_proofid | limited_id = avalanche_proof_from_hex(proof).limited_proofid | ||||
delegation = node.delegateavalancheproof( | delegation = node.delegateavalancheproof( | ||||
f"{limited_id:0{64}x}", | f"{limited_id:0{64}x}", | ||||
bytes_to_wif(privkey.get_bytes()), | bytes_to_wif(privkey.get_bytes()), | ||||
master_key.get_pubkey().get_bytes().hex(), | master_key.get_pubkey().get_bytes().hex(), | ||||
) | ) | ||||
self.log.info("Test the avahello signature with a supplied delegation") | self.log.info("Test the avahello signature with a supplied delegation") | ||||
▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines |