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.""" | ||||
import base64 | |||||
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, | ||||
) | ) | ||||
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 ( | ||||
AvalancheDelegation, | |||||
AvalancheProof, | |||||
FromHex, | |||||
) | |||||
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.test_node import ErrorMatch | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
append_config, | append_config, | ||||
assert_equal, | |||||
wait_until, | wait_until, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
) | ) | ||||
AVALANCHE_MAX_PROOF_STAKES = 1000 | AVALANCHE_MAX_PROOF_STAKES = 1000 | ||||
PROOF_DUST_THRESHOLD = 1.0 | PROOF_DUST_THRESHOLD = 1.0 | ||||
"""Minimum amount per UTXO in a proof (in coins, not in satoshis)""" | """Minimum amount per UTXO in a proof (in coins, not in satoshis)""" | ||||
Show All 30 Lines | def run_test(self): | ||||
"12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747"), True) | "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747"), True) | ||||
def get_hex_pubkey(privkey): | def get_hex_pubkey(privkey): | ||||
return privkey.get_pubkey().get_bytes().hex() | return privkey.get_pubkey().get_bytes().hex() | ||||
proof_master = get_hex_pubkey(privkey) | proof_master = get_hex_pubkey(privkey) | ||||
proof_sequence = 11 | proof_sequence = 11 | ||||
proof_expiration = 12 | proof_expiration = 12 | ||||
stakes = create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key) | |||||
proof = node.buildavalancheproof( | proof = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, | proof_sequence, proof_expiration, proof_master, stakes) | ||||
create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key)) | |||||
self.log.info("Test decodeavalancheproof RPC") | |||||
proofobj = FromHex(AvalancheProof(), proof) | |||||
decodedproof = node.decodeavalancheproof(proof) | |||||
assert_equal(decodedproof["sequence"], proof_sequence) | |||||
assert_equal(decodedproof["expiration"], proof_expiration) | |||||
assert_equal(decodedproof["master"], proof_master) | |||||
assert_equal(decodedproof["proofid"], f"{proofobj.proofid:0{64}x}") | |||||
assert_equal(decodedproof["stakes"][0]["txid"], stakes[0]["txid"]) | |||||
assert_equal(decodedproof["stakes"][0]["vout"], stakes[0]["vout"]) | |||||
assert_equal(decodedproof["stakes"][0]["height"], stakes[0]["height"]) | |||||
assert_equal( | |||||
decodedproof["stakes"][0]["iscoinbase"], | |||||
stakes[0]["iscoinbase"]) | |||||
assert_equal( | |||||
decodedproof["stakes"][0]["signature"], | |||||
base64.b64encode(proofobj.stakes[0].sig).decode("ascii")) | |||||
# Invalid hex (odd number of hex digits) | |||||
assert_raises_rpc_error(-22, "Proof must be an hexadecimal string", | |||||
node.decodeavalancheproof, proof[:-1]) | |||||
# Valid hex but invalid proof | |||||
assert_raises_rpc_error(-22, "Proof has invalid format", | |||||
node.decodeavalancheproof, proof[:-2]) | |||||
# Restart the node, making sure it is initially in IBD mode | # Restart the node, making sure it is initially in IBD mode | ||||
minchainwork = int(node.getblockchaininfo()["chainwork"], 16) + 1 | minchainwork = int(node.getblockchaininfo()["chainwork"], 16) + 1 | ||||
self.restart_node(0, self.extra_args[0] + [ | self.restart_node(0, self.extra_args[0] + [ | ||||
"-avaproof={}".format(proof), | "-avaproof={}".format(proof), | ||||
"-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", | "-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", | ||||
"-minimumchainwork=0x{:x}".format(minchainwork), | "-minimumchainwork=0x{:x}".format(minchainwork), | ||||
]) | ]) | ||||
▲ Show 20 Lines • Show All 210 Lines • Show Last 20 Lines |