Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_avalanche.py
Show All 20 Lines | |||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
BLOCK_ACCEPTED = 0 | BLOCK_ACCEPTED = 0 | ||||
BLOCK_REJECTED = 1 | BLOCK_INVALID = 1 | ||||
BLOCK_PARKED = 2 | |||||
BLOCK_FORK = 3 | |||||
BLOCK_UNKNOWN = -1 | BLOCK_UNKNOWN = -1 | ||||
BLOCK_MISSING = -2 | |||||
BLOCK_PENDING = -3 | |||||
class TestNode(P2PInterface): | class TestNode(P2PInterface): | ||||
def __init__(self): | def __init__(self): | ||||
self.round = 0 | self.round = 0 | ||||
self.avaresponses = [] | self.avaresponses = [] | ||||
self.avapolls = [] | self.avapolls = [] | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# We need to send the coin to a new address in order to make sure we do | # We need to send the coin to a new address in order to make sure we do | ||||
# not regenerate the same block. | # not regenerate the same block. | ||||
node.generatetoaddress( | node.generatetoaddress( | ||||
26, 'bchreg:pqv2r67sgz3qumufap3h2uuj0zfmnzuv8v7ej0fffv') | 26, 'bchreg:pqv2r67sgz3qumufap3h2uuj0zfmnzuv8v7ej0fffv') | ||||
node.reconsiderblock(invalidated_block) | node.reconsiderblock(invalidated_block) | ||||
poll_node.send_poll(various_block_hashes) | poll_node.send_poll(various_block_hashes) | ||||
assert_response([AvalancheVote(BLOCK_ACCEPTED, h) for h in various_block_hashes[:5]] + | assert_response([AvalancheVote(BLOCK_ACCEPTED, h) for h in various_block_hashes[:5]] + | ||||
[AvalancheVote(BLOCK_REJECTED, h) for h in various_block_hashes[-3:]]) | [AvalancheVote(BLOCK_FORK, h) for h in various_block_hashes[-3:]]) | ||||
self.log.info("Poll for unknown blocks...") | self.log.info("Poll for unknown blocks...") | ||||
various_block_hashes = [ | various_block_hashes = [ | ||||
int(node.getblockhash(0), 16), | int(node.getblockhash(0), 16), | ||||
int(node.getblockhash(25), 16), | int(node.getblockhash(25), 16), | ||||
int(node.getblockhash(42), 16), | int(node.getblockhash(42), 16), | ||||
various_block_hashes[5], | various_block_hashes[5], | ||||
various_block_hashes[6], | various_block_hashes[6], | ||||
various_block_hashes[7], | various_block_hashes[7], | ||||
random.randrange(1 << 255, (1 << 256) - 1), | random.randrange(1 << 255, (1 << 256) - 1), | ||||
random.randrange(1 << 255, (1 << 256) - 1), | random.randrange(1 << 255, (1 << 256) - 1), | ||||
random.randrange(1 << 255, (1 << 256) - 1), | random.randrange(1 << 255, (1 << 256) - 1), | ||||
] | ] | ||||
poll_node.send_poll(various_block_hashes) | poll_node.send_poll(various_block_hashes) | ||||
assert_response([AvalancheVote(BLOCK_ACCEPTED, h) for h in various_block_hashes[:3]] + | assert_response([AvalancheVote(BLOCK_ACCEPTED, h) for h in various_block_hashes[:3]] + | ||||
[AvalancheVote(BLOCK_REJECTED, h) for h in various_block_hashes[3:6]] + | [AvalancheVote(BLOCK_FORK, h) for h in various_block_hashes[3:6]] + | ||||
[AvalancheVote(BLOCK_UNKNOWN, h) for h in various_block_hashes[-3:]]) | [AvalancheVote(BLOCK_UNKNOWN, h) for h in various_block_hashes[-3:]]) | ||||
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 = ECKey() | privkey = ECKey() | ||||
privkey.set(bytes.fromhex( | privkey.set(bytes.fromhex( | ||||
"12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747"), True) | "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747"), True) | ||||
pubkey = privkey.get_pubkey() | pubkey = privkey.get_pubkey() | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
tip_to_park = node.getbestblockhash() | tip_to_park = node.getbestblockhash() | ||||
self.log.info(tip_to_park) | self.log.info(tip_to_park) | ||||
hash_to_find = int(tip_to_park, 16) | hash_to_find = int(tip_to_park, 16) | ||||
assert(tip_to_park != fork_tip) | assert(tip_to_park != fork_tip) | ||||
def has_parked_new_tip(): | def has_parked_new_tip(): | ||||
can_find_block_in_poll(hash_to_find, BLOCK_REJECTED) | can_find_block_in_poll(hash_to_find, BLOCK_PARKED) | ||||
return node.getbestblockhash() == fork_tip | return node.getbestblockhash() == fork_tip | ||||
# Because everybody answers no, the node will park that block. | # Because everybody answers no, the node will park that block. | ||||
wait_until(has_parked_new_tip, timeout=15) | wait_until(has_parked_new_tip, timeout=15) | ||||
assert_equal(node.getbestblockhash(), fork_tip) | assert_equal(node.getbestblockhash(), fork_tip) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
AvalancheTest().main() | AvalancheTest().main() |