Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show First 20 Lines • Show All 890 Lines • ▼ Show 20 Lines | def deserialize(self, f): | ||||
self.stake.deserialize(f) | self.stake.deserialize(f) | ||||
self.sig = f.read(64) | self.sig = f.read(64) | ||||
def serialize(self) -> bytes: | def serialize(self) -> bytes: | ||||
return self.stake.serialize() + self.sig | return self.stake.serialize() + self.sig | ||||
class AvalancheProof: | class AvalancheProof: | ||||
__slots__ = ("sequence", "expiration", "master", "stakes", "proofid") | __slots__ = ( | ||||
"sequence", | |||||
"expiration", | |||||
"master", | |||||
"stakes", | |||||
"limited_proofid", | |||||
"proofid") | |||||
def __init__(self, sequence=0, expiration=0, | def __init__(self, sequence=0, expiration=0, | ||||
master=b"", signed_stakes=None): | master=b"", signed_stakes=None): | ||||
self.sequence: int = sequence | self.sequence: int = sequence | ||||
self.expiration: int = expiration | self.expiration: int = expiration | ||||
self.master: bytes = master | self.master: bytes = master | ||||
self.stakes: List[AvalancheSignedStake] = signed_stakes or [ | self.stakes: List[AvalancheSignedStake] = signed_stakes or [ | ||||
AvalancheSignedStake()] | AvalancheSignedStake()] | ||||
self.proofid: int = self.compute_proof_id() | |||||
def compute_proof_id(self) -> int: | self.limited_proofid: int = None | ||||
"""Return Bitcoin's 256-bit hash (double SHA-256) of the | self.proofid: int = None | ||||
self.compute_proof_id() | |||||
def compute_proof_id(self): | |||||
"""Compute Bitcoin's 256-bit hash (double SHA-256) of the | |||||
serialized proof data. | serialized proof data. | ||||
:return: bytes of length 32 | |||||
""" | """ | ||||
ss = struct.pack("<Qq", self.sequence, self.expiration) | ss = struct.pack("<Qq", self.sequence, self.expiration) | ||||
ss += ser_compact_size(len(self.stakes)) | ss += ser_compact_size(len(self.stakes)) | ||||
# Use unsigned stakes | # Use unsigned stakes | ||||
for s in self.stakes: | for s in self.stakes: | ||||
ss += s.stake.serialize() | ss += s.stake.serialize() | ||||
h = hash256(ss) | h = hash256(ss) | ||||
self.limited_proofid = uint256_from_str(h) | |||||
h += ser_string(self.master) | h += ser_string(self.master) | ||||
h = hash256(h) | h = hash256(h) | ||||
# make it an int, for comparing with Delegation.proofid | # make it an int, for comparing with Delegation.proofid | ||||
return uint256_from_str(h) | self.proofid = 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) | ||||
self.stakes = deser_vector(f, AvalancheSignedStake) | self.stakes = deser_vector(f, AvalancheSignedStake) | ||||
self.proofid = self.compute_proof_id() | self.compute_proof_id() | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<Q", self.sequence) | r += struct.pack("<Q", self.sequence) | ||||
r += struct.pack("<q", self.expiration) | r += struct.pack("<q", self.expiration) | ||||
r += ser_string(self.master) | r += ser_string(self.master) | ||||
r += ser_vector(self.stakes) | r += ser_vector(self.stakes) | ||||
return r | return r | ||||
▲ Show 20 Lines • Show All 1,126 Lines • Show Last 20 Lines |