Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_proof_inventory.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2021 The Bitcoin developers | # Copyright (c) 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 proof inventory relaying | Test proof inventory relaying | ||||
""" | """ | ||||
from test_framework.avatools import ( | from test_framework.avatools import ( | ||||
create_coinbase_stakes, | create_coinbase_stakes, | ||||
get_proof_ids, | get_proof_ids, | ||||
) | ) | ||||
from test_framework.key import ECKey, bytes_to_wif | from test_framework.key import ECKey, bytes_to_wif | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
AvalancheProof, | AvalancheProof, | ||||
FromHex, | FromHex, | ||||
MSG_AVA_PROOF, | |||||
MSG_TYPE_MASK, | |||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
P2PInterface, | P2PInterface, | ||||
mininode_lock, | mininode_lock, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
connect_nodes, | connect_nodes, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
class ProofInvStoreP2PInterface(P2PInterface): | |||||
def __init__(self): | |||||
super().__init__() | |||||
self.proof_invs = set() | |||||
def on_inv(self, message): | |||||
for i in message.inv: | |||||
if i.type & MSG_TYPE_MASK == MSG_AVA_PROOF: | |||||
self.proof_invs.add(i.hash) | |||||
class ProofInventoryTest(BitcoinTestFramework): | class ProofInventoryTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [['-enableavalanche=1', | self.extra_args = [['-enableavalanche=1', | ||||
'-avacooldown=0']] * self.num_nodes | '-avacooldown=0']] * self.num_nodes | ||||
def gen_proof(self, node): | def gen_proof(self, node): | ||||
blockhashes = node.generate(10) | blockhashes = node.generate(10) | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | def test_send_proof_inv(self): | ||||
def proof_inv_found(peer): | def proof_inv_found(peer): | ||||
with mininode_lock: | with mininode_lock: | ||||
return peer.last_message.get( | return peer.last_message.get( | ||||
"inv") and peer.last_message["inv"].inv[-1].hash == proofid | "inv") and peer.last_message["inv"].inv[-1].hash == proofid | ||||
wait_until(lambda: all(proof_inv_found(i) for i in node.p2ps)) | wait_until(lambda: all(proof_inv_found(i) for i in node.p2ps)) | ||||
# Add more proofs to our pool | |||||
for i in range(10): | |||||
_, proof = self.gen_proof(node) | |||||
node.sendavalancheproof(proof.serialize().hex()) | |||||
assert proof.proofid in get_proof_ids(node) | |||||
peer = ProofInvStoreP2PInterface() | |||||
node.add_p2p_connection(peer) | |||||
wait_until(lambda: set(get_proof_ids(node)) == peer.proof_invs) | |||||
def run_test(self): | def run_test(self): | ||||
self.test_send_proof_inv() | self.test_send_proof_inv() | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
ProofInventoryTest().main() | ProofInventoryTest().main() |