Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show First 20 Lines • Show All 904 Lines • ▼ Show 20 Lines | class AvalancheProof: | ||||
def compute_proof_id(self) -> int: | def compute_proof_id(self) -> int: | ||||
"""Return Bitcoin's 256-bit hash (double SHA-256) of the | """Return Bitcoin's 256-bit hash (double SHA-256) of the | ||||
serialized proof data. | serialized proof data. | ||||
:return: bytes of length 32 | :return: bytes of length 32 | ||||
""" | """ | ||||
ss = struct.pack("<Qq", self.sequence, self.expiration) | ss = struct.pack("<Qq", self.sequence, self.expiration) | ||||
ss += ser_string(self.master) | ss += ser_string(self.master) | ||||
ss += ser_vector(self.stakes) | ss += ser_compact_size(len(self.stakes)) | ||||
# Use unsigned stakes | |||||
for s in self.stakes: | |||||
ss += s.stake.serialize() | |||||
h = hash256(ss) | h = hash256(ss) | ||||
# make it an int, for comparing with Delegation.proofid | # make it an int, for comparing with Delegation.proofid | ||||
return uint256_from_str(h) | return uint256_from_str(h) | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.sequence = struct.unpack("<Q", f.read(8))[0] | self.sequence = struct.unpack("<Q", f.read(8))[0] | ||||
self.expiration = struct.unpack("<q", f.read(8))[0] | self.expiration = struct.unpack("<q", f.read(8))[0] | ||||
self.master = deser_string(f) | self.master = deser_string(f) | ||||
▲ Show 20 Lines • Show All 1,080 Lines • ▼ Show 20 Lines | def __repr__(self): | ||||
return "msg_avahello(response={})".format(repr(self.hello)) | return "msg_avahello(response={})".format(repr(self.hello)) | ||||
class TestFrameworkMessages(unittest.TestCase): | class TestFrameworkMessages(unittest.TestCase): | ||||
def test_serialization_round_trip(self): | def test_serialization_round_trip(self): | ||||
"""Verify that messages and serialized objects are unchanged after | """Verify that messages and serialized objects are unchanged after | ||||
a round-trip of deserialization-serialization. | a round-trip of deserialization-serialization. | ||||
""" | """ | ||||
avaproof = AvalancheProof() | |||||
proof_hex = ( | proof_hex = ( | ||||
"2a00000000000000fff053650000000021030b4c866585dd868a9d62348a9cd00" | "2a00000000000000fff053650000000021030b4c866585dd868a9d62348a9cd00" | ||||
"8d6a312937048fff31670e7e920cfc7a74401b7fc19792583e9cb39843fc5e22a" | "8d6a312937048fff31670e7e920cfc7a74401b7fc19792583e9cb39843fc5e22a" | ||||
"4e3648ab1cb18a70290b341ee8d4f550ae2400000000102700000000000078881" | "4e3648ab1cb18a70290b341ee8d4f550ae2400000000102700000000000078881" | ||||
"4004104d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df" | "4004104d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df" | ||||
"42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c9764" | "42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c9764" | ||||
"8cb0ac3052d58da74de7404e84ebe2940ed2b0fe85578d8230788d8387aeaa618" | "8cb0ac3052d58da74de7404e84ebe2940ed2b0fe85578d8230788d8387aeaa618" | ||||
"274b0f2edc73679fd398f60e6315258c9ec348df7fcc09340ae1af37d009719b0" | "274b0f2edc73679fd398f60e6315258c9ec348df7fcc09340ae1af37d009719b0" | ||||
"665" | "665" | ||||
) | ) | ||||
avaproof.deserialize(BytesIO(bytes.fromhex(proof_hex))) | |||||
self.assertEqual(avaproof.serialize().hex(), proof_hex) | avaproof = FromHex(AvalancheProof(), proof_hex) | ||||
self.assertEqual(ToHex(avaproof), proof_hex) | |||||
self.assertEqual(f"{avaproof.proofid:x}", | |||||
"8ab9e9db85055cea7f541d464a84bd4fabaf284cc2815394868741bbe09b4735" | |||||
) | |||||
self.assertEqual(avaproof.sequence, 42) | |||||
self.assertEqual(avaproof.expiration, 1699999999) | |||||
# The master key is extracted from the key_tests.cpp. | |||||
# Associated privkey: | |||||
# hex: 12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747 | |||||
# WIF: cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN | |||||
self.assertEqual(avaproof.master, bytes.fromhex( | |||||
"030b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a744" | |||||
)) | |||||
self.assertEqual(len(avaproof.stakes), 1) | |||||
self.assertEqual(avaproof.stakes[0].sig, bytes.fromhex( | |||||
"c3052d58da74de7404e84ebe2940ed2b0fe85578d8230788d8387aeaa618274b" | |||||
"0f2edc73679fd398f60e6315258c9ec348df7fcc09340ae1af37d009719b0665" | |||||
)) | |||||
self.assertEqual(f"{avaproof.stakes[0].stake.utxo.hash:x}", | |||||
"24ae50f5d4e81e340b29708ab11cab48364e2ae2c53f8439cbe983257919fcb7" | |||||
) | |||||
self.assertEqual(avaproof.stakes[0].stake.utxo.n, 0) | |||||
self.assertEqual(avaproof.stakes[0].stake.amount, 10000) | |||||
self.assertEqual(avaproof.stakes[0].stake.height, 672828) | |||||
self.assertEqual(avaproof.stakes[0].stake.is_coinbase, False) | |||||
self.assertEqual(avaproof.stakes[0].stake.pubkey, bytes.fromhex( | |||||
"04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645" | |||||
"cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c97648cb0a" | |||||
)) | |||||
msg_proof = msg_avaproof() | msg_proof = msg_avaproof() | ||||
msg_proof.proof = avaproof | msg_proof.proof = avaproof | ||||
self.assertEqual(msg_proof.serialize().hex(), proof_hex) | self.assertEqual(ToHex(msg_proof), proof_hex) |