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 decimal import Decimal | from decimal import Decimal | ||||
from test_framework.avatools import ( | from test_framework.avatools import ( | ||||
create_coinbase_stakes, | create_coinbase_stakes, | ||||
create_stakes, | create_stakes, | ||||
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 AvalancheDelegation | from test_framework.messages import ( | ||||
from test_framework.mininode import P2PInterface | AvalancheDelegation, | ||||
AvalancheProof, | |||||
FromHex, | |||||
) | |||||
from test_framework.mininode import P2PInterface, mininode_lock | |||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.test_node import ErrorMatch | from test_framework.test_node import ErrorMatch | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
append_config, | append_config, | ||||
wait_until, | wait_until, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
node.delegateavalancheproof, | node.delegateavalancheproof, | ||||
proof, | proof, | ||||
bytes_to_wif(privkey.get_bytes()), | bytes_to_wif(privkey.get_bytes()), | ||||
random_pubkey, | random_pubkey, | ||||
delegation, | delegation, | ||||
) | ) | ||||
# Test invalid proofs | # Test invalid proofs | ||||
self.log.info("Bad proof should be rejected at startup") | |||||
dust = node.buildavalancheproof( | dust = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, | proof_sequence, proof_expiration, proof_master, | ||||
create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key, amount="0")) | create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key, amount="0")) | ||||
dust_amount = Decimal(f"{PROOF_DUST_THRESHOLD * 0.9999:.4f}") | dust_amount = Decimal(f"{PROOF_DUST_THRESHOLD * 0.9999:.4f}") | ||||
dust2 = node.buildavalancheproof( | dust2 = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, | proof_sequence, proof_expiration, proof_master, | ||||
create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key, | create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key, | ||||
amount=str(dust_amount))) | amount=str(dust_amount))) | ||||
duplicate_stake = node.buildavalancheproof( | duplicate_stake = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, | proof_sequence, proof_expiration, proof_master, | ||||
create_coinbase_stakes(node, [blockhashes[0]] * 2, addrkey0.key)) | create_coinbase_stakes(node, [blockhashes[0]] * 2, addrkey0.key)) | ||||
bad_sig = ("0b000000000000000c0000000000000021030b4c866585dd868a9d62348" | bad_sig = ("0b000000000000000c0000000000000021030b4c866585dd868a9d62348" | ||||
"a9cd008d6a312937048fff31670e7e920cfc7a7440105c5f72f5d6da3085" | "a9cd008d6a312937048fff31670e7e920cfc7a7440105c5f72f5d6da3085" | ||||
"583e75ee79340eb4eff208c89988e7ed0efb30b87298fa30000000000f20" | "583e75ee79340eb4eff208c89988e7ed0efb30b87298fa30000000000f20" | ||||
"52a0100000003000000210227d85ba011276cf25b51df6a188b75e604b3" | "52a0100000003000000210227d85ba011276cf25b51df6a188b75e604b3" | ||||
"8770a462b2d0e9fb2fc839ef5d3faf07f001dd38e9b4a43d07d5d449cc0" | "8770a462b2d0e9fb2fc839ef5d3faf07f001dd38e9b4a43d07d5d449cc0" | ||||
"f7d2888d96b82962b3ce516d1083c0e031773487fc3c4f2e38acd1db974" | "f7d2888d96b82962b3ce516d1083c0e031773487fc3c4f2e38acd1db974" | ||||
"1321b91a79b82d1c2cfd47793261e4ba003cf5") | "1321b91a79b82d1c2cfd47793261e4ba003cf5") | ||||
self.log.info("Check the sendavalancheproof RPC") | |||||
# Restart to clear our proof pool | |||||
self.restart_node(0) | |||||
def check_sendavalancheproof_failure(proof, message): | |||||
assert_raises_rpc_error(-8, "The proof is invalid: " + message, | |||||
node.sendavalancheproof, proof) | |||||
assert FromHex(AvalancheProof(), | |||||
proof).proofid not in get_proof_ids(node) | |||||
check_sendavalancheproof_failure(no_stake, "no-stake") | |||||
check_sendavalancheproof_failure(dust, "amount-below-dust-threshold") | |||||
check_sendavalancheproof_failure(duplicate_stake, "duplicated-stake") | |||||
check_sendavalancheproof_failure(bad_sig, "invalid-signature") | |||||
if self.is_wallet_compiled(): | |||||
check_sendavalancheproof_failure(too_many_utxos, "too-many-utxos") | |||||
# Good proof | |||||
peer = node.add_p2p_connection(P2PInterface()) | |||||
proofid = FromHex(AvalancheProof(), proof).proofid | |||||
node.sendavalancheproof(proof) | |||||
assert proofid in get_proof_ids(node) | |||||
def inv_found(): | |||||
with mininode_lock: | |||||
return peer.last_message.get( | |||||
"inv") and peer.last_message["inv"].inv[-1].hash == proofid | |||||
wait_until(inv_found) | |||||
self.log.info("Bad proof should be rejected at startup") | |||||
self.stop_node(0) | self.stop_node(0) | ||||
node.assert_start_raises_init_error( | node.assert_start_raises_init_error( | ||||
self.extra_args[0] + [ | self.extra_args[0] + [ | ||||
"-avasessionkey=0", | "-avasessionkey=0", | ||||
], | ], | ||||
expected_msg="Error: the avalanche session key is invalid", | expected_msg="Error: the avalanche session key is invalid", | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |