Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-p2p-avalanche.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2018 The Bitcoin developers | # Copyright (c) 2018 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.mininode import P2PInterface, mininode_lock | from test_framework.mininode import P2PInterface, mininode_lock | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
AvalancheVote, | AvalancheVote, | ||||
CInv, | CInv, | ||||
msg_avapoll, | msg_avapoll, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal, wait_until | from test_framework.util import ( | ||||
assert_equal, | |||||
sync_blocks, | |||||
wait_until, | |||||
) | |||||
from test_framework import schnorr | from test_framework import schnorr | ||||
BLOCK_ACCEPTED = 0 | BLOCK_ACCEPTED = 0 | ||||
BLOCK_REJECTED = 1 | BLOCK_REJECTED = 1 | ||||
BLOCK_UNKNOWN = -1 | BLOCK_UNKNOWN = -1 | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# Get our own node id so we can use it later. | # Get our own node id so we can use it later. | ||||
nodeid = node.getpeerinfo()[-1]['id'] | nodeid = node.getpeerinfo()[-1]['id'] | ||||
# Generate many block and poll for them. | # Generate many block and poll for them. | ||||
address = node.get_deterministic_priv_key().address | address = node.get_deterministic_priv_key().address | ||||
node.generatetoaddress(100, address) | node.generatetoaddress(100, address) | ||||
fork_node = self.nodes[1] | |||||
# Make sure the fork node has synced the blocks | |||||
sync_blocks([node, fork_node]) | |||||
# Get the key so we can verify signatures. | # Get the key so we can verify signatures. | ||||
avakey = bytes.fromhex(node.getavalanchekey()) | avakey = bytes.fromhex(node.getavalanchekey()) | ||||
self.log.info("Poll for the chain tip...") | self.log.info("Poll for the chain tip...") | ||||
best_block_hash = int(node.getbestblockhash(), 16) | best_block_hash = int(node.getbestblockhash(), 16) | ||||
poll_node.send_poll([best_block_hash]) | poll_node.send_poll([best_block_hash]) | ||||
poll_node.wait_for_avaresponse() | poll_node.wait_for_avaresponse() | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.log.info("Trigger polling from the node...") | self.log.info("Trigger polling from the node...") | ||||
# duplicate the deterministic sig test from src/test/key_tests.cpp | # duplicate the deterministic sig test from src/test/key_tests.cpp | ||||
privkey = bytes.fromhex( | privkey = bytes.fromhex( | ||||
"12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747") | "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747") | ||||
pubkey = schnorr.getpubkey(privkey, compressed=True) | pubkey = schnorr.getpubkey(privkey, compressed=True) | ||||
node.addavalanchepeer(nodeid, pubkey.hex()) | node.addavalanchepeer(nodeid, pubkey.hex()) | ||||
# Sanity check | # Make sure the fork node has synced the blocks | ||||
fork_node = self.nodes[1] | sync_blocks([node, fork_node]) | ||||
assert_equal(node.getbestblockhash(), fork_node.getbestblockhash()) | |||||
# Create a fork 2 blocks deep. This should trigger polling. | # Create a fork 2 blocks deep. This should trigger polling. | ||||
fork_node.invalidateblock(fork_node.getblockhash(100)) | fork_node.invalidateblock(fork_node.getblockhash(100)) | ||||
fork_address = fork_node.get_deterministic_priv_key().address | fork_address = fork_node.get_deterministic_priv_key().address | ||||
fork_node.generatetoaddress(2, fork_address) | fork_node.generatetoaddress(2, fork_address) | ||||
def can_find_block_in_poll(hash): | def can_find_block_in_poll(hash): | ||||
poll_node.wait_for_avapoll() | poll_node.wait_for_avapoll() | ||||
Show All 21 Lines |