Skip to content

Commit 76ad6d7

Browse files
committed
refactor: add Certificate.encode() and simplify hash()
1 parent 780960a commit 76ad6d7

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

pactus/block/certificate.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,17 @@ def decode(cls, buf: bytes) -> tuple:
4747
cert = cls(height, round_, committers, absentees, signature)
4848
return cert, buf
4949

50-
def hash(self) -> bytes:
51-
"""Return the certificate hash (blake2b-256 of encoded bytes)."""
52-
buf = self.height.encode(b"")
50+
def encode(self, buf: bytes) -> bytes:
51+
buf = self.height.encode(buf)
5352
buf = self.round.encode(buf)
5453
buf = encoding.append_var_int(buf, len(self.committers))
5554
for n in self.committers:
5655
buf = encoding.append_var_int(buf, n)
5756
buf = encoding.append_var_int(buf, len(self.absentees))
5857
for n in self.absentees:
5958
buf = encoding.append_var_int(buf, n)
60-
buf = self.signature.encode(buf)
61-
return hashlib.blake2b(buf, digest_size=32).digest()
59+
return self.signature.encode(buf)
60+
61+
def hash(self) -> bytes:
62+
"""Return the certificate hash (blake2b-256 of encoded bytes)."""
63+
return hashlib.blake2b(self.encode(b""), digest_size=32).digest()

0 commit comments

Comments
 (0)