Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_avalanche.py
Show First 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
privkey = bytes.fromhex( | privkey = bytes.fromhex( | ||||
"12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747") | "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747") | ||||
pubkey = schnorr.getpubkey(privkey, compressed=True) | pubkey = schnorr.getpubkey(privkey, compressed=True) | ||||
# Activate the quorum. | # Activate the quorum. | ||||
for n in quorum: | for n in quorum: | ||||
node.addavalanchepeer(n.nodeid, pubkey.hex()) | node.addavalanchepeer(n.nodeid, pubkey.hex()) | ||||
# Make sure the fork node has synced the blocks | def can_find_block_in_poll(hash, resp=BLOCK_ACCEPTED): | ||||
self.sync_blocks([node, fork_node]) | |||||
# Create a fork 2 blocks deep. This should trigger polling. | |||||
fork_node.invalidateblock(fork_node.getblockhash(100)) | |||||
fork_address = fork_node.get_deterministic_priv_key().address | |||||
fork_node.generatetoaddress(2, fork_address) | |||||
# Because the new tip is a deep reorg, the node will not accept it | |||||
# right away, but poll for it. | |||||
def parked_block(blockhash): | |||||
for tip in node.getchaintips(): | |||||
if tip["hash"] == blockhash: | |||||
assert tip["status"] != "active" | |||||
return tip["status"] == "parked" | |||||
return False | |||||
fork_tip = fork_node.getbestblockhash() | |||||
wait_until(lambda: parked_block(fork_tip)) | |||||
self.log.info("Answer all polls to finalize...") | |||||
def can_find_block_in_poll(hash, resp): | |||||
found_hash = False | found_hash = False | ||||
for n in quorum: | for n in quorum: | ||||
poll = n.get_avapoll_if_available() | poll = n.get_avapoll_if_available() | ||||
# That node has not received a poll | # That node has not received a poll | ||||
if poll is None: | if poll is None: | ||||
continue | continue | ||||
Show All 9 Lines | def run_test(self): | ||||
found_hash = True | found_hash = True | ||||
votes.append(AvalancheVote(r, inv.hash)) | votes.append(AvalancheVote(r, inv.hash)) | ||||
n.send_avaresponse(poll.round, votes, privkey) | n.send_avaresponse(poll.round, votes, privkey) | ||||
return found_hash | return found_hash | ||||
# Now that we have a peer, we should start polling for the tip. | |||||
hash_tip = int(node.getbestblockhash(), 16) | |||||
wait_until(lambda: can_find_block_in_poll(hash_tip), timeout=5) | |||||
# Make sure the fork node has synced the blocks | |||||
self.sync_blocks([node, fork_node]) | |||||
# Create a fork 2 blocks deep. This should trigger polling. | |||||
fork_node.invalidateblock(fork_node.getblockhash(100)) | |||||
fork_address = fork_node.get_deterministic_priv_key().address | |||||
fork_node.generatetoaddress(2, fork_address) | |||||
# Because the new tip is a deep reorg, the node will not accept it | |||||
# right away, but poll for it. | |||||
def parked_block(blockhash): | |||||
for tip in node.getchaintips(): | |||||
if tip["hash"] == blockhash: | |||||
assert tip["status"] != "active" | |||||
return tip["status"] == "parked" | |||||
return False | |||||
fork_tip = fork_node.getbestblockhash() | |||||
wait_until(lambda: parked_block(fork_tip)) | |||||
self.log.info("Answer all polls to finalize...") | |||||
hash_to_find = int(fork_tip, 16) | hash_to_find = int(fork_tip, 16) | ||||
def has_accepted_new_tip(): | def has_accepted_new_tip(): | ||||
can_find_block_in_poll(hash_to_find, BLOCK_ACCEPTED) | can_find_block_in_poll(hash_to_find) | ||||
return node.getbestblockhash() == fork_tip | return node.getbestblockhash() == fork_tip | ||||
# Because everybody answers yes, the node will accept that block. | # Because everybody answers yes, the node will accept that block. | ||||
wait_until(has_accepted_new_tip, timeout=15) | wait_until(has_accepted_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() |