Skip to content

Commit 9bdcf96

Browse files
feat(tck): implement deleteFile JSON-RPC method
Signed-off-by: Siddhartha Ganguly <gangulysiddhartha22@gmail.com>
1 parent 4c3df39 commit 9bdcf96

4 files changed

Lines changed: 58 additions & 30 deletions

File tree

src/hiero_sdk_python/file/file_delete_transaction.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Transaction to delete a file on the network."""
2-
32
from __future__ import annotations
43

4+
from typing import TYPE_CHECKING
5+
56
from hiero_sdk_python.channels import _Channel
67
from hiero_sdk_python.executable import _Method
78
from hiero_sdk_python.file.file_id import FileId
@@ -13,93 +14,77 @@
1314
from hiero_sdk_python.transaction.transaction import Transaction
1415

1516

16-
DEFAULT_TRANSACTION_FEE = Hbar(2).to_tinybars()
17-
17+
if TYPE_CHECKING:
18+
from hiero_sdk_python.hapi.services.transaction_pb2 import TransactionBody
1819

20+
DEFAULT_TRANSACTION_FEE = Hbar(2).to_tinybars()
1921
class FileDeleteTransaction(Transaction):
2022
"""
2123
Represents a file deletion transaction on the network.
22-
2324
This transaction deletes a specified file, rendering it inactive.
24-
2525
Inherits from the base Transaction class and implements the required methods
2626
to build and execute a file deletion transaction.
2727
"""
28-
2928
def __init__(self, file_id: FileId | None = None):
3029
"""
3130
Initializes a new FileDeleteTransaction instance with optional file_id.
32-
3331
Args:
3432
file_id (FileId, optional): The ID of the file to be deleted.
3533
"""
3634
super().__init__()
3735
self.file_id = file_id
3836
self._default_transaction_fee = DEFAULT_TRANSACTION_FEE
39-
4037
def set_file_id(self, file_id: FileId) -> FileDeleteTransaction:
4138
"""
4239
Sets the ID of the file to be deleted.
43-
4440
Args:
4541
file_id (FileId): The ID of the file to be deleted.
46-
4742
Returns:
4843
FileDeleteTransaction: Returns self for method chaining.
4944
"""
5045
self._require_not_frozen()
5146
self.file_id = file_id
5247
return self
53-
54-
def _build_proto_body(self):
48+
def _build_proto_body(self) -> FileDeleteTransactionBody:
5549
"""
5650
Returns the protobuf body for the file delete transaction.
5751
52+
If file_id is not set, the fileID field is left unset so the network
53+
responds with INVALID_FILE_ID rather than failing locally.
54+
5855
Returns:
5956
FileDeleteTransactionBody: The protobuf body for this transaction.
60-
61-
Raises:
62-
ValueError: If file_id is not set.
6357
"""
64-
if self.file_id is None:
65-
raise ValueError("Missing required FileID")
66-
67-
return FileDeleteTransactionBody(fileID=self.file_id._to_proto())
68-
69-
def build_transaction_body(self):
58+
return FileDeleteTransactionBody(
59+
fileID=self.file_id._to_proto() if self.file_id is not None else None
60+
)
61+
def build_transaction_body(self) -> TransactionBody:
7062
"""
7163
Builds and returns the protobuf transaction body for file deletion.
72-
7364
Returns:
7465
TransactionBody: The protobuf transaction body containing the file deletion details.
7566
"""
7667
file_delete_body = self._build_proto_body()
7768
transaction_body = self.build_base_transaction_body()
7869
transaction_body.fileDelete.CopyFrom(file_delete_body)
7970
return transaction_body
80-
8171
def build_scheduled_body(self) -> SchedulableTransactionBody:
8272
"""
8373
Builds the scheduled transaction body for this file delete transaction.
84-
8574
Returns:
8675
SchedulableTransactionBody: The built scheduled transaction body.
8776
"""
8877
file_delete_body = self._build_proto_body()
8978
schedulable_body = self.build_base_scheduled_body()
9079
schedulable_body.fileDelete.CopyFrom(file_delete_body)
9180
return schedulable_body
92-
9381
def _get_method(self, channel: _Channel) -> _Method:
9482
"""
9583
Gets the method to execute the file delete transaction.
96-
9784
This internal method returns a _Method object containing the appropriate gRPC
9885
function to call when executing this transaction on the network.
99-
10086
Args:
10187
channel (_Channel): The channel containing service stubs
102-
10388
Returns:
10489
_Method: An object containing the transaction function to delete a file.
10590
"""

tck/handlers/file.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
from hiero_sdk_python.file.file_contents_query import FileContentsQuery
44
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction
5+
from hiero_sdk_python.file.file_delete_transaction import FileDeleteTransaction
56
from hiero_sdk_python.file.file_id import FileId
67
from hiero_sdk_python.hbar import Hbar
78
from hiero_sdk_python.response_code import ResponseCode
89
from hiero_sdk_python.timestamp import Timestamp
910
from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt
1011
from tck.errors import JsonRpcError
1112
from tck.handlers.registry import rpc_method
12-
from tck.param.file import CreateFileParams, GetFileContentsParams
13-
from tck.response.file import CreateFileResponse, GetFileContentsResponse
13+
from tck.param.file import CreateFileParams, DeleteFileParams, GetFileContentsParams
14+
from tck.response.file import CreateFileResponse, DeleteFileResponse, GetFileContentsResponse
1415
from tck.util.client_utils import get_client
1516
from tck.util.constants import DEFAULT_GRPC_TIMEOUT
1617
from tck.util.key_utils import get_key_from_string
@@ -76,3 +77,22 @@ def get_file_contents(params: GetFileContentsParams) -> GetFileContentsResponse:
7677
decoded_contents = contents.decode("utf-8", errors="replace") if isinstance(contents, bytes) else str(contents)
7778

7879
return GetFileContentsResponse(contents=decoded_contents)
80+
81+
82+
@rpc_method("deleteFile")
83+
def delete_file(params: DeleteFileParams) -> DeleteFileResponse:
84+
"""Delete a file."""
85+
client = get_client(params.sessionId)
86+
87+
transaction = FileDeleteTransaction().set_grpc_deadline(DEFAULT_GRPC_TIMEOUT)
88+
89+
if params.fileId is not None:
90+
transaction.set_file_id(FileId.from_string(params.fileId))
91+
92+
if params.commonTransactionParams is not None:
93+
params.commonTransactionParams.apply_common_params(transaction, client)
94+
95+
response = transaction.execute(client, wait_for_receipt=False)
96+
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
97+
98+
return DeleteFileResponse(ResponseCode(receipt.status).name)

tck/param/file.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,19 @@ def parse_json_params(cls, params: dict) -> GetFileContentsParams:
5151
queryPayment=params.get("queryPayment"),
5252
maxQueryPayment=params.get("maxQueryPayment"),
5353
)
54+
55+
56+
@dataclass
57+
class DeleteFileParams(BaseTransactionParams):
58+
"""Parameters for deleting a file. Extends BaseTransactionParams to include common transaction parameters."""
59+
60+
fileId: str | None = None
61+
62+
@classmethod
63+
def parse_json_params(cls, params: dict) -> DeleteFileParams:
64+
65+
return cls(
66+
fileId=params.get("fileId"),
67+
sessionId=parse_session_id(params),
68+
commonTransactionParams=parse_common_transaction_params(params),
69+
)

tck/response/file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from dataclasses import dataclass
44

5+
from tck.response.base import StatusOnlyResponse
6+
57

68
@dataclass
79
class CreateFileResponse:
@@ -16,3 +18,8 @@ class GetFileContentsResponse:
1618
"""Response payload for getFileContents."""
1719

1820
contents: str | None = None
21+
22+
23+
@dataclass
24+
class DeleteFileResponse(StatusOnlyResponse):
25+
"""Response payload for deleteFile."""

0 commit comments

Comments
 (0)