Skip to content

Commit d36fb72

Browse files
authored
feat(types): define types for height and round (#57)
1 parent c4d836b commit d36fb72

7 files changed

Lines changed: 53 additions & 5 deletions

File tree

examples/example_transfer_transaction_bls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from pactus.crypto.bls import PrivateKey
44
from pactus.transaction import Transaction
55
from pactus.types.amount import Amount
6+
from pactus.types.height import Height
67

78

89
def main() -> None:
910
HRP.use_testnet()
1011

11-
lock_time = 1_735_096
12+
lock_time = Height(1_735_096)
1213
memo = "This is a test transaction"
1314
amount = Amount.from_string("1.5")
1415
fee = Amount.from_string("0.01")

examples/example_transfer_transaction_ed25519.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from pactus.crypto.ed25519 import PrivateKey
44
from pactus.transaction import Transaction
55
from pactus.types.amount import Amount
6+
from pactus.types.height import Height
67

78

89
def main() -> None:
910
HRP.use_testnet()
1011

11-
lock_time = 1_735_096
12+
lock_time = Height(1_735_096)
1213
memo = "This is a test transaction"
1314
amount = Amount.from_string("1.5")
1415
fee = Amount.from_string("0.01")

pactus/transaction/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _get_unsigned_bytes(self, buf: bytes) -> bytes:
9595
"""
9696
encoding.append_uint8(buf, self.flags)
9797
encoding.append_uint8(buf, self.version)
98-
encoding.append_uint32(buf, self.lock_time)
98+
encoding.append_uint32(buf, self.lock_time.value)
9999
encoding.append_var_int(buf, self.fee.value)
100100
encoding.append_str(buf, self.memo)
101101
encoding.append_uint8(buf, self.payload.get_type().value)

pactus/types/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
from .amount import Amount
2+
from .height import Height
3+
from .round import Round
24

3-
__all__ = ["Amount"]
5+
__all__ = ["Amount", "Height", "Round"]

pactus/types/height.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Height:
2+
"""
3+
Height represents a block height in the Pactus blockchain.
4+
5+
It encapsulates a raw integer value representing a block height.
6+
It can be used for transaction lock times.
7+
"""
8+
9+
def __init__(self, value: int = 0) -> None:
10+
self.value = value
11+
12+
def __eq__(self, other: "Height") -> bool:
13+
if isinstance(other, Height):
14+
return self.value == other.value
15+
16+
return False
17+
18+
def __hash__(self) -> int:
19+
return hash(self.value)
20+
21+
def __str__(self) -> str:
22+
return str(self.value)

pactus/types/round.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Round:
2+
"""
3+
Round represents a consensus round in the Pactus blockchain.
4+
5+
It encapsulates a raw integer value representing a round number.
6+
"""
7+
8+
def __init__(self, value: int = 0) -> None:
9+
self.value = value
10+
11+
def __eq__(self, other: "Round") -> bool:
12+
if isinstance(other, Round):
13+
return self.value == other.value
14+
15+
return False
16+
17+
def __hash__(self) -> int:
18+
return hash(self.value)
19+
20+
def __str__(self) -> str:
21+
return str(self.value)

tests/test_transaction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pactus.crypto.bls import PrivateKey
55
from pactus.transaction import Transaction
66
from pactus.types.amount import Amount
7+
from pactus.types.height import Height
78

89

910
class TestTransaction(unittest.TestCase):
@@ -14,7 +15,7 @@ def test_sign_transfer(self):
1415
receiver = Address.from_string("pc1zt6qcdymkk48c5ds0fzfsaf6puwu8w8djn3ffpn")
1516
amount = Amount.from_pac(1.0) # 1 PAC
1617
fee = Amount.from_pac(0.001) # 0.001 PAC
17-
lock_time = 0x123456
18+
lock_time = Height(0x123456)
1819
memo = "test"
1920

2021
tx = Transaction.create_transfer_tx(

0 commit comments

Comments
 (0)