Skip to content

Commit dcadef6

Browse files
committed
refactor: encode methods accept and return buffer for clean chaining
All encode() signatures changed from to . This allows callers to pass a buffer through, appending in one fluid chain: buf = self.sender.encode(buf) buf = self.receiver.encode(buf) return self.amount.encode(buf) Updated: Amount, Height, Round, Hash, Address, all crypto Signatures/PublicKeys, all payloads, and Transaction._get_unsigned_bytes/sign/sign_bytes.
1 parent e372bd9 commit dcadef6

20 files changed

Lines changed: 57 additions & 89 deletions

pactus/crypto/address.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def is_account_address(self) -> bool:
8686
def is_validator_address(self) -> bool:
8787
return self.address_type() == AddressType.VALIDATOR
8888

89-
def encode(self) -> bytes:
90-
buf = b""
89+
def encode(self, buf: bytes) -> bytes:
9190
if self.is_treasury_address():
9291
return encoding.append_uint8(buf, AddressType.TREASURY.value)
9392
return encoding.append_fixed_bytes(buf, self.raw_bytes())

pactus/crypto/bls/public_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def string(self) -> str:
6161
self.raw_bytes(),
6262
)
6363

64-
def encode(self) -> bytes:
65-
return encoding.append_fixed_bytes(b"", self.raw_bytes())
64+
def encode(self, buf: bytes) -> bytes:
65+
return encoding.append_fixed_bytes(buf, self.raw_bytes())
6666

6767
@classmethod
6868
def decode(cls, buf: bytes) -> tuple:

pactus/crypto/bls/signature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def raw_bytes(self) -> bytes:
3939
def string(self) -> str:
4040
return self.raw_bytes().hex()
4141

42-
def encode(self) -> bytes:
43-
return encoding.append_fixed_bytes(b"", self.raw_bytes())
42+
def encode(self, buf: bytes) -> bytes:
43+
return encoding.append_fixed_bytes(buf, self.raw_bytes())
4444

4545
@classmethod
4646
def decode(cls, buf: bytes) -> tuple:

pactus/crypto/ed25519/public_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def string(self) -> str:
5050
self.raw_bytes(),
5151
)
5252

53-
def encode(self) -> bytes:
54-
return encoding.append_fixed_bytes(b"", self.raw_bytes())
53+
def encode(self, buf: bytes) -> bytes:
54+
return encoding.append_fixed_bytes(buf, self.raw_bytes())
5555

5656
@classmethod
5757
def decode(cls, buf: bytes) -> tuple:

pactus/crypto/ed25519/signature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def raw_bytes(self) -> bytes:
2626
def string(self) -> str:
2727
return self.sig.hex()
2828

29-
def encode(self) -> bytes:
30-
return encoding.append_fixed_bytes(b"", self.sig)
29+
def encode(self, buf: bytes) -> bytes:
30+
return encoding.append_fixed_bytes(buf, self.sig)
3131

3232
@classmethod
3333
def decode(cls, buf: bytes) -> tuple:

pactus/crypto/hash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def __str__(self) -> str:
2828
def is_undef(self) -> bool:
2929
return self.data == bytes(HASH_SIZE)
3030

31-
def encode(self) -> bytes:
32-
return encoding.append_fixed_bytes(b"", self.data)
31+
def encode(self, buf: bytes) -> bytes:
32+
return encoding.append_fixed_bytes(buf, self.data)
3333

3434
@classmethod
3535
def decode(cls, buf: bytes) -> tuple[Hash, bytes]:

pactus/crypto/public_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def string(self) -> str:
2222
pass
2323

2424
@abstractmethod
25-
def encode(self) -> bytes:
25+
def encode(self, buf: bytes) -> bytes:
2626
pass
2727

2828
@classmethod

pactus/crypto/secp256k1/public_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def string(self) -> str:
5050
self.raw_bytes(),
5151
)
5252

53-
def encode(self) -> bytes:
54-
return encoding.append_fixed_bytes(b"", self.raw_bytes())
53+
def encode(self, buf: bytes) -> bytes:
54+
return encoding.append_fixed_bytes(buf, self.raw_bytes())
5555

5656
@classmethod
5757
def decode(cls, buf: bytes) -> tuple:

pactus/crypto/secp256k1/signature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def raw_bytes(self) -> bytes:
2626
def string(self) -> str:
2727
return self.sig.hex()
2828

29-
def encode(self) -> bytes:
30-
return encoding.append_fixed_bytes(b"", self.sig)
29+
def encode(self, buf: bytes) -> bytes:
30+
return encoding.append_fixed_bytes(buf, self.sig)
3131

3232
@classmethod
3333
def decode(cls, buf: bytes) -> tuple:

pactus/crypto/signature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def string(self) -> str:
1818
pass
1919

2020
@abstractmethod
21-
def encode(self) -> bytes:
21+
def encode(self, buf: bytes) -> bytes:
2222
pass
2323

2424
@classmethod

0 commit comments

Comments
 (0)