Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_avalanche.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 resolution of forks via avalanche.""" | """Test the resolution of forks via avalanche.""" | ||||
import random | import random | ||||
from test_framework.avatools import create_coinbase_stakes | from test_framework.avatools import create_coinbase_stakes | ||||
from test_framework.key import ( | from test_framework.key import ( | ||||
ECKey, | ECKey, | ||||
ECPubKey, | ECPubKey, | ||||
) | ) | ||||
from test_framework.mininode import P2PInterface, mininode_lock | from test_framework.mininode import P2PInterface, p2p_lock | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
AvalancheResponse, | AvalancheResponse, | ||||
AvalancheVote, | AvalancheVote, | ||||
CInv, | CInv, | ||||
msg_avapoll, | msg_avapoll, | ||||
msg_tcpavaresponse, | msg_tcpavaresponse, | ||||
NODE_AVALANCHE, | NODE_AVALANCHE, | ||||
NODE_NETWORK, | NODE_NETWORK, | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | def send_avaresponse(self, round, votes, privkey): | ||||
msg = msg_tcpavaresponse() | msg = msg_tcpavaresponse() | ||||
msg.response = TCPAvalancheResponse(response, sig) | msg.response = TCPAvalancheResponse(response, sig) | ||||
self.send_message(msg) | self.send_message(msg) | ||||
def wait_for_avaresponse(self, timeout=5): | def wait_for_avaresponse(self, timeout=5): | ||||
wait_until( | wait_until( | ||||
lambda: len(self.avaresponses) > 0, | lambda: len(self.avaresponses) > 0, | ||||
timeout=timeout, | timeout=timeout, | ||||
lock=mininode_lock) | lock=p2p_lock) | ||||
with mininode_lock: | with p2p_lock: | ||||
return self.avaresponses.pop(0) | return self.avaresponses.pop(0) | ||||
def send_poll(self, hashes): | def send_poll(self, hashes): | ||||
msg = msg_avapoll() | msg = msg_avapoll() | ||||
msg.poll.round = self.round | msg.poll.round = self.round | ||||
self.round += 1 | self.round += 1 | ||||
for h in hashes: | for h in hashes: | ||||
msg.poll.invs.append(CInv(2, h)) | msg.poll.invs.append(CInv(2, h)) | ||||
self.send_message(msg) | self.send_message(msg) | ||||
def get_avapoll_if_available(self): | def get_avapoll_if_available(self): | ||||
with mininode_lock: | with p2p_lock: | ||||
return self.avapolls.pop(0) if len(self.avapolls) > 0 else None | return self.avapolls.pop(0) if len(self.avapolls) > 0 else None | ||||
def wait_for_avahello(self, timeout=5): | def wait_for_avahello(self, timeout=5): | ||||
wait_until( | wait_until( | ||||
lambda: self.avahello is not None, | lambda: self.avahello is not None, | ||||
timeout=timeout, | timeout=timeout, | ||||
lock=mininode_lock) | lock=p2p_lock) | ||||
with mininode_lock: | with p2p_lock: | ||||
return self.avahello | return self.avahello | ||||
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 = 2 | self.num_nodes = 2 | ||||
self.extra_args = [ | self.extra_args = [ | ||||
▲ Show 20 Lines • Show All 251 Lines • Show Last 20 Lines |