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.test_framework import BitcoinTestFramework | |||||
from test_framework.util import assert_equal, wait_until | |||||
from test_framework.mininode import P2PInterface, mininode_lock | from test_framework.mininode import P2PInterface, mininode_lock | ||||
from test_framework.messages import AvalancheVote, CInv, msg_avapoll | from test_framework.messages import AvalancheVote, CInv, msg_avapoll | ||||
from test_framework.test_framework import BitcoinTestFramework | |||||
from test_framework.util import assert_equal, wait_until | |||||
from test_framework import schnorr | |||||
class TestNode(P2PInterface): | class TestNode(P2PInterface): | ||||
def __init__(self): | def __init__(self): | ||||
self.last_avaresponse = None | self.last_avaresponse = None | ||||
super().__init__() | super().__init__() | ||||
Show All 28 Lines | def run_test(self): | ||||
poll_node = TestNode() | poll_node = TestNode() | ||||
node.add_p2p_connection(poll_node) | node.add_p2p_connection(poll_node) | ||||
poll_node.wait_for_verack() | poll_node.wait_for_verack() | ||||
poll_node.sync_with_ping() | poll_node.sync_with_ping() | ||||
# Generate many block and poll for them. | # Generate many block and poll for them. | ||||
node.generate(100) | node.generate(100) | ||||
# Get the key so we can verify signatures. | |||||
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() | ||||
def assert_response(response, expected): | def assert_response(response, expected): | ||||
votes = response.votes | r = response.response | ||||
assert_equal(r.cooldown, 0) | |||||
# Verify signature. | |||||
assert schnorr.verify(response.sig, avakey, r.get_hash()) | |||||
votes = r.votes | |||||
self.log.info("response: {}".format(repr(response))) | self.log.info("response: {}".format(repr(response))) | ||||
assert_equal(len(votes), len(expected)) | assert_equal(len(votes), len(expected)) | ||||
for i in range(0, len(votes)): | for i in range(0, len(votes)): | ||||
assert_equal(repr(votes[i]), repr(expected[i])) | assert_equal(repr(votes[i]), repr(expected[i])) | ||||
assert_response(poll_node.last_avaresponse, [ | assert_response(poll_node.last_avaresponse, [ | ||||
AvalancheVote(0, best_block_hash)]) | AvalancheVote(0, best_block_hash)]) | ||||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |