|
| 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) |
0 commit comments