Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_rpc_avalancheproof.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 building avalanche proofs and using them to add avalanche peers.""" | """Test building avalanche proofs and using them to add avalanche peers.""" | ||||
from test_framework.avatools import get_stakes | from test_framework.avatools import get_stakes | ||||
from test_framework.key import ECKey | from test_framework.key import ECKey | ||||
from test_framework.mininode import P2PInterface | from test_framework.mininode import P2PInterface | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.test_node import ErrorMatch | |||||
from test_framework.util import ( | from test_framework.util import ( | ||||
append_config, | |||||
assert_equal, | assert_equal, | ||||
assert_raises_rpc_error, | |||||
) | ) | ||||
AVALANCHE_MAX_PROOF_STAKES = 1000 | AVALANCHE_MAX_PROOF_STAKES = 1000 | ||||
def add_interface_node(test_node) -> str: | def add_interface_node(test_node) -> str: | ||||
"""Create a mininode, connect it to test_node, return the nodeid | """Create a mininode, connect it to test_node, return the nodeid | ||||
of the mininode as registered by test_node. | of the mininode as registered by test_node. | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
addrkey0.address) | addrkey0.address) | ||||
too_many_stakes = get_stakes(node, blockhashes, addrkey0.key) | too_many_stakes = get_stakes(node, blockhashes, addrkey0.key) | ||||
maximum_stakes = get_stakes(node, blockhashes[:-1], addrkey0.key) | maximum_stakes = get_stakes(node, blockhashes[:-1], addrkey0.key) | ||||
good_proof = node.buildavalancheproof( | good_proof = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, | proof_sequence, proof_expiration, | ||||
proof_master, maximum_stakes) | proof_master, maximum_stakes) | ||||
peerid1 = add_interface_node(node) | peerid1 = add_interface_node(node) | ||||
node.addavalanchenode( | assert node.addavalanchenode(peerid1, proof_master, good_proof) | ||||
peerid1, proof_master, good_proof) | |||||
self.log.info("A proof using too many stakes should be rejected...") | self.log.info("A proof using too many stakes should be rejected...") | ||||
bad_proof = node.buildavalancheproof( | too_many_utxos = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, | proof_sequence, proof_expiration, | ||||
proof_master, too_many_stakes) | proof_master, too_many_stakes) | ||||
peerid2 = add_interface_node(node) | peerid2 = add_interface_node(node) | ||||
assert_raises_rpc_error(-32602, "Avalanche proof has too many UTXOs", | assert not node.addavalanchenode(peerid2, proof_master, too_many_utxos) | ||||
node.addavalanchenode, | |||||
peerid2, proof_master, bad_proof) | |||||
# Test invalid proofs | # Test invalid proofs | ||||
self.log.info("Bad proof should be rejected at startup") | self.log.info("Bad proof should be rejected at startup") | ||||
no_stake = node.buildavalancheproof( | no_stake = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, []) | proof_sequence, proof_expiration, proof_master, []) | ||||
dust = node.buildavalancheproof( | dust = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, | proof_sequence, proof_expiration, proof_master, | ||||
Show All 25 Lines | def run_test(self): | ||||
check_proof_init_error(no_stake, | check_proof_init_error(no_stake, | ||||
"the avalanche proof has no stake") | "the avalanche proof has no stake") | ||||
check_proof_init_error(dust, | check_proof_init_error(dust, | ||||
"the avalanche proof stake is too low") | "the avalanche proof stake is too low") | ||||
check_proof_init_error(duplicate_stake, | check_proof_init_error(duplicate_stake, | ||||
"the avalanche proof has duplicated stake") | "the avalanche proof has duplicated stake") | ||||
check_proof_init_error(bad_sig, | check_proof_init_error(bad_sig, | ||||
"the avalanche proof has invalid stake signatures") | "the avalanche proof has invalid stake signatures") | ||||
# The too many utxos case creates a proof which is that large that it | |||||
# cannot fit on the command line | |||||
append_config(node.datadir, ["avaproof={}".format(too_many_utxos)]) | |||||
node.assert_start_raises_init_error( | |||||
self.extra_args[0] + [ | |||||
"-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", | |||||
], | |||||
expected_msg="Error: the avalanche proof has too many utxos", | |||||
match=ErrorMatch.PARTIAL_REGEX, | |||||
) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
AvalancheProofTest().main() | AvalancheProofTest().main() |