Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_rpc_avalancheproof.py
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
addrkey0 = node.get_deterministic_priv_key() | addrkey0 = node.get_deterministic_priv_key() | ||||
blockhashes = node.generatetoaddress(100, addrkey0.address) | blockhashes = node.generatetoaddress(100, addrkey0.address) | ||||
self.log.info( | self.log.info( | ||||
"Make build a valid proof and restart the node to use it") | "Make build a valid proof and restart the node to use it") | ||||
privkey = ECKey() | privkey = ECKey() | ||||
privkey.set(bytes.fromhex( | privkey.set(bytes.fromhex( | ||||
"12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747"), True) | "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747"), True) | ||||
wif_privkey = bytes_to_wif(privkey.get_bytes()) | |||||
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) | stakes = create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key) | ||||
proof = node.buildavalancheproof( | proof = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, stakes) | proof_sequence, proof_expiration, wif_privkey, stakes) | ||||
self.log.info("Test decodeavalancheproof RPC") | self.log.info("Test decodeavalancheproof RPC") | ||||
proofobj = FromHex(AvalancheProof(), proof) | proofobj = FromHex(AvalancheProof(), proof) | ||||
decodedproof = node.decodeavalancheproof(proof) | decodedproof = node.decodeavalancheproof(proof) | ||||
limited_id_hex = f"{proofobj.limited_proofid:0{64}x}" | limited_id_hex = f"{proofobj.limited_proofid:0{64}x}" | ||||
assert_equal(decodedproof["sequence"], proof_sequence) | assert_equal(decodedproof["sequence"], proof_sequence) | ||||
assert_equal(decodedproof["expiration"], proof_expiration) | assert_equal(decodedproof["expiration"], proof_expiration) | ||||
assert_equal(decodedproof["master"], proof_master) | assert_equal(decodedproof["master"], proof_master) | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
) | ) | ||||
delegator_privkey = delegated_privkey | delegator_privkey = delegated_privkey | ||||
random_privkey = gen_privkey() | random_privkey = gen_privkey() | ||||
random_pubkey = get_hex_pubkey(random_privkey) | random_pubkey = get_hex_pubkey(random_privkey) | ||||
# Invalid proof | # Invalid proof | ||||
no_stake = node.buildavalancheproof(proof_sequence, proof_expiration, | no_stake = node.buildavalancheproof(proof_sequence, proof_expiration, | ||||
proof_master, []) | wif_privkey, []) | ||||
# Invalid privkey | # Invalid privkey | ||||
assert_raises_rpc_error(-5, "The private key is invalid", | assert_raises_rpc_error(-5, "The private key is invalid", | ||||
node.delegateavalancheproof, | node.delegateavalancheproof, | ||||
limited_id_hex, | limited_id_hex, | ||||
bytes_to_wif(bytes(32)), | bytes_to_wif(bytes(32)), | ||||
random_pubkey, | random_pubkey, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
limited_id_hex, | limited_id_hex, | ||||
bytes_to_wif(privkey.get_bytes()), | bytes_to_wif(privkey.get_bytes()), | ||||
random_pubkey, | random_pubkey, | ||||
"dead", | "dead", | ||||
) | ) | ||||
# Test invalid proofs | # Test invalid proofs | ||||
dust = node.buildavalancheproof( | dust = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, | proof_sequence, proof_expiration, wif_privkey, | ||||
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, wif_privkey, | ||||
create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key, | create_coinbase_stakes(node, [blockhashes[0]], addrkey0.key, | ||||
amount=str(dust_amount))) | amount=str(dust_amount))) | ||||
missing_stake = node.buildavalancheproof( | missing_stake = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, proof_master, [{ | proof_sequence, proof_expiration, wif_privkey, [{ | ||||
'txid': '0' * 64, | 'txid': '0' * 64, | ||||
'vout': 0, | 'vout': 0, | ||||
'amount': 10000000, | 'amount': 10000000, | ||||
'height': 42, | 'height': 42, | ||||
'iscoinbase': False, | 'iscoinbase': False, | ||||
'privatekey': addrkey0.key, | 'privatekey': addrkey0.key, | ||||
}] | }] | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# confirm the coinbase UTXOs | # confirm the coinbase UTXOs | ||||
node.generate(101) | node.generate(101) | ||||
too_many_stakes = create_stakes( | too_many_stakes = create_stakes( | ||||
node, new_blocks, AVALANCHE_MAX_PROOF_STAKES + 1) | node, new_blocks, AVALANCHE_MAX_PROOF_STAKES + 1) | ||||
maximum_stakes = too_many_stakes[:-1] | maximum_stakes = too_many_stakes[:-1] | ||||
good_proof = node.buildavalancheproof( | good_proof = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, | proof_sequence, proof_expiration, | ||||
proof_master, maximum_stakes) | wif_privkey, maximum_stakes) | ||||
too_many_utxos = node.buildavalancheproof( | too_many_utxos = node.buildavalancheproof( | ||||
proof_sequence, proof_expiration, | proof_sequence, proof_expiration, | ||||
proof_master, too_many_stakes) | wif_privkey, too_many_stakes) | ||||
assert node.verifyavalancheproof(good_proof) | assert node.verifyavalancheproof(good_proof) | ||||
for rpc in [node.verifyavalancheproof, node.sendavalancheproof]: | for rpc in [node.verifyavalancheproof, node.sendavalancheproof]: | ||||
assert_raises_rpc_error(-22, "Proof must be an hexadecimal string", | assert_raises_rpc_error(-22, "Proof must be an hexadecimal string", | ||||
rpc, "f00") | rpc, "f00") | ||||
assert_raises_rpc_error(-22, "Proof has invalid format", | assert_raises_rpc_error(-22, "Proof has invalid format", | ||||
rpc, "f00d") | rpc, "f00d") | ||||
def check_rpc_failure(proof, message): | def check_rpc_failure(proof, message): | ||||
assert_raises_rpc_error(-8, "The proof is invalid: " + message, | assert_raises_rpc_error(-8, "The proof is invalid: " + message, | ||||
rpc, proof) | rpc, proof) | ||||
check_rpc_failure(no_stake, "no-stake") | check_rpc_failure(no_stake, "no-stake") | ||||
check_rpc_failure(dust, "amount-below-dust-threshold") | check_rpc_failure(dust, "amount-below-dust-threshold") | ||||
check_rpc_failure(duplicate_stake, "duplicated-stake") | check_rpc_failure(duplicate_stake, "duplicated-stake") | ||||
check_rpc_failure(missing_stake, "utxo-missing-or-spent") | check_rpc_failure(missing_stake, "utxo-missing-or-spent") | ||||
check_rpc_failure(bad_sig, "invalid-signature") | check_rpc_failure(bad_sig, "invalid-signature") | ||||
check_rpc_failure(wrong_order, "wrong-stake-ordering") | check_rpc_failure(wrong_order, "wrong-stake-ordering") | ||||
if self.is_wallet_compiled(): | if self.is_wallet_compiled(): | ||||
check_rpc_failure(too_many_utxos, "too-many-utxos") | check_rpc_failure(too_many_utxos, "too-many-utxos") | ||||
conflicting_utxo = node.buildavalancheproof( | conflicting_utxo = node.buildavalancheproof( | ||||
proof_sequence + 1, proof_expiration, proof_master, stakes) | proof_sequence + 1, proof_expiration, wif_privkey, stakes) | ||||
assert_raises_rpc_error(-8, "The proof has conflicting utxo with an existing proof", | assert_raises_rpc_error(-8, "The proof has conflicting utxo with an existing proof", | ||||
node.sendavalancheproof, conflicting_utxo) | node.sendavalancheproof, conflicting_utxo) | ||||
# Good proof | # Good proof | ||||
assert node.verifyavalancheproof(proof) | assert node.verifyavalancheproof(proof) | ||||
peer = node.add_p2p_connection(P2PInterface()) | peer = node.add_p2p_connection(P2PInterface()) | ||||
▲ Show 20 Lines • Show All 139 Lines • Show Last 20 Lines |