Skip to content

Commit 94d6fac

Browse files
authored
feat: add FreezeTransaction (#716)
Signed-off-by: dosi <[email protected]>
1 parent 6b38081 commit 94d6fac

File tree

6 files changed

+635
-0
lines changed

6 files changed

+635
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Added
10+
- added FreezeTransaction class
11+
- added FreezeType class
1012
- Added `docs/sdk_developers/pylance.md`, a new guide explaining how to set up and use **Pylance** in VS Code for validating imports, file references, and methods before review. (#713)
1113
- docs: Add Google-style docstrings to `TokenId` class and its methods in `token_id.py`.
1214
- added Google-style docstrings to the `TransactionRecord` class including all dataclass fields, `__repr__`, `_from_proto()` & `_to_proto()` methods.

src/hiero_sdk_python/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@
140140
from .tokens.custom_royalty_fee import CustomRoyaltyFee
141141
from .transaction.custom_fee_limit import CustomFeeLimit
142142

143+
# System
144+
from .system.freeze_transaction import FreezeTransaction
145+
from .system.freeze_type import FreezeType
146+
143147
__all__ = [
144148
# Client
145149
"Client",
@@ -278,4 +282,8 @@
278282
"CustomFractionalFee",
279283
"CustomRoyaltyFee",
280284
"CustomFeeLimit",
285+
286+
# System
287+
"FreezeTransaction",
288+
"FreezeType",
281289
]
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
"""
2+
FreezeTransaction class for freezing the network.
3+
"""
4+
5+
from typing import Optional
6+
7+
from hiero_sdk_python.channels import _Channel
8+
from hiero_sdk_python.executable import _Method
9+
from hiero_sdk_python.file.file_id import FileId
10+
from hiero_sdk_python.hapi.services.freeze_pb2 import FreezeTransactionBody
11+
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
12+
SchedulableTransactionBody,
13+
)
14+
from hiero_sdk_python.hapi.services.transaction_pb2 import TransactionBody
15+
from hiero_sdk_python.system.freeze_type import FreezeType
16+
from hiero_sdk_python.timestamp import Timestamp
17+
from hiero_sdk_python.transaction.transaction import Transaction
18+
19+
20+
class FreezeTransaction(Transaction):
21+
"""
22+
Represents a freeze transaction on the network.
23+
24+
This transaction freezes the network with the specified parameters including
25+
start_time, file_id, file_hash, and freeze_type.
26+
27+
Inherits from the base Transaction class and implements the required methods
28+
to build and execute a freeze transaction.
29+
"""
30+
31+
def __init__(
32+
self,
33+
start_time: Optional[Timestamp] = None,
34+
file_id: Optional[FileId] = None,
35+
file_hash: Optional[bytes] = None,
36+
freeze_type: Optional[FreezeType] = None,
37+
):
38+
"""
39+
Initializes a new FreezeTransaction instance with the specified parameters.
40+
41+
Args:
42+
start_time (Optional[Timestamp]): The start time for the freeze.
43+
file_id (Optional[FileId]): The file ID containing the upgrade data.
44+
file_hash (Optional[bytes]): Hash of the file for verification.
45+
freeze_type (Optional[FreezeType]): The type of freeze to perform.
46+
"""
47+
super().__init__()
48+
self.start_time: Optional[Timestamp] = start_time
49+
self.file_id: Optional[FileId] = file_id
50+
self.file_hash: Optional[bytes] = file_hash
51+
self.freeze_type: Optional[FreezeType] = freeze_type
52+
53+
def set_start_time(self, start_time: Optional[Timestamp]) -> "FreezeTransaction":
54+
"""
55+
Sets the start time for this freeze transaction.
56+
57+
Args:
58+
start_time (Optional[Timestamp]): The start time for the freeze.
59+
60+
Returns:
61+
FreezeTransaction: This transaction instance.
62+
"""
63+
self._require_not_frozen()
64+
self.start_time = start_time
65+
return self
66+
67+
def set_file_id(self, file_id: Optional[FileId]) -> "FreezeTransaction":
68+
"""
69+
Sets the file ID for this freeze transaction.
70+
71+
Args:
72+
file_id (Optional[FileId]): The file ID containing the upgrade data.
73+
74+
Returns:
75+
FreezeTransaction: This transaction instance.
76+
"""
77+
self._require_not_frozen()
78+
self.file_id = file_id
79+
return self
80+
81+
def set_file_hash(self, file_hash: Optional[bytes]) -> "FreezeTransaction":
82+
"""
83+
Sets the file hash for this freeze transaction.
84+
85+
Args:
86+
file_hash (Optional[bytes]): Hash of the file for verification.
87+
88+
Returns:
89+
FreezeTransaction: This transaction instance.
90+
"""
91+
self._require_not_frozen()
92+
self.file_hash = file_hash
93+
return self
94+
95+
def set_freeze_type(self, freeze_type: Optional[FreezeType]) -> "FreezeTransaction":
96+
"""
97+
Sets the freeze type for this freeze transaction.
98+
99+
Args:
100+
freeze_type (Optional[FreezeType]): The type of freeze to perform.
101+
102+
Returns:
103+
FreezeTransaction: This transaction instance.
104+
"""
105+
self._require_not_frozen()
106+
self.freeze_type = freeze_type
107+
return self
108+
109+
def _build_proto_body(self) -> FreezeTransactionBody:
110+
"""
111+
Returns the protobuf body for the freeze transaction.
112+
113+
Returns:
114+
FreezeTransactionBody: The protobuf body for this transaction.
115+
"""
116+
return FreezeTransactionBody(
117+
start_time=self.start_time._to_protobuf() if self.start_time else None,
118+
update_file=self.file_id._to_proto() if self.file_id else None,
119+
file_hash=self.file_hash,
120+
freeze_type=self.freeze_type._to_proto() if self.freeze_type else None,
121+
)
122+
123+
def build_transaction_body(self) -> TransactionBody:
124+
"""
125+
Builds the transaction body for this freeze transaction.
126+
127+
Returns:
128+
TransactionBody: The built transaction body.
129+
"""
130+
freeze_body = self._build_proto_body()
131+
transaction_body = self.build_base_transaction_body()
132+
transaction_body.freeze.CopyFrom(freeze_body)
133+
return transaction_body
134+
135+
def build_scheduled_body(self) -> SchedulableTransactionBody:
136+
"""
137+
Builds the scheduled transaction body for this freeze transaction.
138+
139+
Returns:
140+
SchedulableTransactionBody: The schedulable transaction body.
141+
"""
142+
freeze_body = self._build_proto_body()
143+
schedulable_body = self.build_base_scheduled_body()
144+
schedulable_body.freeze.CopyFrom(freeze_body)
145+
return schedulable_body
146+
147+
def _get_method(self, channel: _Channel) -> _Method:
148+
"""
149+
Gets the method to execute the freeze transaction.
150+
151+
This internal method returns a _Method object containing the appropriate gRPC
152+
function to call when executing this transaction on the Hedera network.
153+
154+
Args:
155+
channel (_Channel): The channel containing service stubs
156+
157+
Returns:
158+
_Method: An object containing the transaction function to freeze the network.
159+
"""
160+
return _Method(transaction_func=channel.freeze.freeze, query_func=None)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Defines FreezeType enum for representing freeze types
3+
"""
4+
from enum import Enum
5+
6+
from hiero_sdk_python.hapi.services.freeze_type_pb2 import (
7+
FreezeType as proto_FreezeType,
8+
)
9+
10+
11+
class FreezeType(Enum):
12+
"""
13+
Freeze type:
14+
15+
• UNKNOWN_FREEZE_TYPE - not applicable
16+
• FREEZE_ONLY - Freeze only
17+
• PREPARE_UPGRADE - Prepare upgrade
18+
• FREEZE_UPGRADE - Freeze upgrade
19+
• FREEZE_ABORT - Freeze abort
20+
• TELEMETRY_UPGRADE - Telemetry upgrade
21+
"""
22+
23+
UNKNOWN_FREEZE_TYPE = 0
24+
FREEZE_ONLY = 1
25+
PREPARE_UPGRADE = 2
26+
FREEZE_UPGRADE = 3
27+
FREEZE_ABORT = 4
28+
TELEMETRY_UPGRADE = 5
29+
30+
@staticmethod
31+
def _from_proto(proto_obj: proto_FreezeType) -> "FreezeType":
32+
"""
33+
Converts a protobuf FreezeType to a FreezeType enum.
34+
35+
Args:
36+
proto_obj (proto_FreezeType): The protobuf FreezeType object.
37+
38+
Returns:
39+
FreezeType: The corresponding FreezeType enum value.
40+
"""
41+
if proto_obj == proto_FreezeType.FREEZE_ONLY:
42+
return FreezeType.FREEZE_ONLY
43+
elif proto_obj == proto_FreezeType.PREPARE_UPGRADE:
44+
return FreezeType.PREPARE_UPGRADE
45+
elif proto_obj == proto_FreezeType.FREEZE_UPGRADE:
46+
return FreezeType.FREEZE_UPGRADE
47+
elif proto_obj == proto_FreezeType.FREEZE_ABORT:
48+
return FreezeType.FREEZE_ABORT
49+
elif proto_obj == proto_FreezeType.TELEMETRY_UPGRADE:
50+
return FreezeType.TELEMETRY_UPGRADE
51+
return FreezeType.UNKNOWN_FREEZE_TYPE
52+
53+
def _to_proto(self) -> proto_FreezeType:
54+
"""
55+
Converts a FreezeType enum to a protobuf FreezeType object.
56+
57+
Args:
58+
self (FreezeType): The FreezeType enum value.
59+
60+
Returns:
61+
proto_FreezeType: The corresponding protobuf FreezeType object.
62+
"""
63+
if self == FreezeType.FREEZE_ONLY:
64+
return proto_FreezeType.FREEZE_ONLY
65+
elif self == FreezeType.PREPARE_UPGRADE:
66+
return proto_FreezeType.PREPARE_UPGRADE
67+
elif self == FreezeType.FREEZE_UPGRADE:
68+
return proto_FreezeType.FREEZE_UPGRADE
69+
elif self == FreezeType.FREEZE_ABORT:
70+
return proto_FreezeType.FREEZE_ABORT
71+
elif self == FreezeType.TELEMETRY_UPGRADE:
72+
return proto_FreezeType.TELEMETRY_UPGRADE
73+
return proto_FreezeType.UNKNOWN_FREEZE_TYPE
74+
75+
def __eq__(self, other: object) -> bool:
76+
if isinstance(other, FreezeType):
77+
return self.value == other.value
78+
elif isinstance(other, int):
79+
return self.value == other
80+
return False

0 commit comments

Comments
 (0)