-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathnode_create_transaction.py
More file actions
327 lines (268 loc) · 12.7 KB
/
Copy pathnode_create_transaction.py
File metadata and controls
327 lines (268 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""NodeCreateTransaction class."""
from __future__ import annotations
from dataclasses import dataclass, field
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.address_book.endpoint import Endpoint
from hiero_sdk_python.channels import _Channel
from hiero_sdk_python.crypto.public_key import PublicKey
from hiero_sdk_python.executable import _Method
from hiero_sdk_python.hapi.services.node_create_pb2 import NodeCreateTransactionBody
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
SchedulableTransactionBody,
)
from hiero_sdk_python.hapi.services.transaction_pb2 import TransactionBody
from hiero_sdk_python.transaction.transaction import Transaction
@dataclass
class NodeCreateParams:
"""
Represents node attributes that can be set on creation.
Attributes:
account_id (AccountId, optional): The account ID of the node.
description (str, optional): The description of the node.
gossip_endpoints (list[Endpoint]): The gossip endpoints of the node.
service_endpoints (list[Endpoint]): The service endpoints of the node.
gossip_ca_certificate (bytes, optional): The gossip ca certificate of the node.
grpc_certificate_hash (bytes, optional): The grpc certificate hash of the node.
admin_key (PublicKey, optional): The admin key of the node.
decline_reward (bool, optional): The decline reward of the node.
grpc_web_proxy_endpoint (Endpoint, optional): The grpc web proxy endpoint of the node.
"""
account_id: AccountId | None = None
description: str | None = None
gossip_endpoints: list[Endpoint] = field(default_factory=list)
service_endpoints: list[Endpoint] = field(default_factory=list)
gossip_ca_certificate: bytes | None = None
grpc_certificate_hash: bytes | None = None
admin_key: PublicKey | None = None
decline_reward: bool | None = None
grpc_web_proxy_endpoint: Endpoint | None = None
associated_registered_nodes: list[int] = field(default_factory=list)
class NodeCreateTransaction(Transaction):
"""
Represents a node create transaction on the network.
This transaction creates a new node on the network with the specified account ID,
description, endpoints, certificates, admin key, and other node properties.
Inherits from the base Transaction class and implements the required methods
to build and execute a node create transaction.
"""
def __init__(self, node_create_params: NodeCreateParams | None = None):
"""
Initializes a new NodeCreateTransaction instance with the specified parameters.
Args:
node_create_params (NodeCreateParams, optional):
The parameters for the node create transaction.
"""
super().__init__()
node_create_params = node_create_params or NodeCreateParams()
self.account_id: AccountId | None = node_create_params.account_id
self.description: str | None = node_create_params.description
self.gossip_endpoints: list[Endpoint] = node_create_params.gossip_endpoints
self.service_endpoints: list[Endpoint] = node_create_params.service_endpoints
self.gossip_ca_certificate: bytes | None = node_create_params.gossip_ca_certificate
self.grpc_certificate_hash: bytes | None = node_create_params.grpc_certificate_hash
self.admin_key: PublicKey | None = node_create_params.admin_key
self.decline_reward: bool | None = node_create_params.decline_reward
self.grpc_web_proxy_endpoint: Endpoint | None = node_create_params.grpc_web_proxy_endpoint
self.associated_registered_nodes: list[int] = node_create_params.associated_registered_nodes
def set_account_id(self, account_id: AccountId | None) -> NodeCreateTransaction:
"""
Sets the account id for this node create transaction.
Args:
account_id (AccountId):
The account id of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.account_id = account_id
return self
def set_description(self, description: str | None) -> NodeCreateTransaction:
"""
Sets the description for this node create transaction.
Args:
description (str):
The description of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.description = description
return self
def set_gossip_endpoints(self, gossip_endpoints: list[Endpoint] | None) -> NodeCreateTransaction:
"""
Sets the gossip endpoints for this node create transaction.
Args:
gossip_endpoints (list[Endpoint] | None):
The gossip endpoints of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.gossip_endpoints = gossip_endpoints
return self
def set_service_endpoints(self, service_endpoints: list[Endpoint] | None) -> NodeCreateTransaction:
"""
Sets the service endpoints for this node create transaction.
Args:
service_endpoints (list[Endpoint] | None):
The service endpoints of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.service_endpoints = service_endpoints
return self
def set_gossip_ca_certificate(self, gossip_ca_certificate: bytes | None) -> NodeCreateTransaction:
"""
Sets the gossip ca certificate for this node create transaction.
Args:
gossip_ca_certificate (bytes):
The gossip ca certificate of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.gossip_ca_certificate = gossip_ca_certificate
return self
def set_grpc_certificate_hash(self, grpc_certificate_hash: bytes | None) -> NodeCreateTransaction:
"""
Sets the grpc certificate hash for this node create transaction.
Args:
grpc_certificate_hash (bytes):
The grpc certificate hash of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.grpc_certificate_hash = grpc_certificate_hash
return self
def set_admin_key(self, admin_key: PublicKey | None) -> NodeCreateTransaction:
"""
Sets the admin key for this node create transaction.
Args:
admin_key (PublicKey):
The admin key of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.admin_key = admin_key
return self
def set_decline_reward(self, decline_reward: bool | None) -> NodeCreateTransaction:
"""
Sets the decline reward for this node create transaction.
Args:
decline_reward (bool):
The decline reward of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.decline_reward = decline_reward
return self
def set_grpc_web_proxy_endpoint(self, grpc_web_proxy_endpoint: Endpoint | None) -> NodeCreateTransaction:
"""
Sets the grpc web proxy endpoint for this node create transaction.
Args:
grpc_web_proxy_endpoint (Endpoint):
The grpc web proxy endpoint of the node.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.grpc_web_proxy_endpoint = grpc_web_proxy_endpoint
return self
def set_associated_registered_nodes(self, registered_node_ids: list[int]) -> NodeCreateTransaction:
"""
Sets the associated registered node IDs for this node create transaction.
Args:
registered_node_ids (list[int]): The registered node IDs to associate.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.associated_registered_nodes = registered_node_ids
return self
def add_associated_registered_node(self, registered_node_id: int) -> NodeCreateTransaction:
"""
Adds an associated registered node ID to this node create transaction.
Args:
registered_node_id (int): The registered node ID to add.
Returns:
NodeCreateTransaction: This transaction instance.
"""
self._require_not_frozen()
self.associated_registered_nodes.append(registered_node_id)
return self
def _build_proto_body(self) -> NodeCreateTransactionBody:
"""
Returns the protobuf body for the node create transaction.
Returns:
NodeCreateTransactionBody: The protobuf body for this transaction.
"""
return NodeCreateTransactionBody(
account_id=self.account_id._to_proto() if self.account_id else None,
description=self.description,
gossip_endpoint=[endpoint._to_proto() for endpoint in self.gossip_endpoints or []],
service_endpoint=[endpoint._to_proto() for endpoint in self.service_endpoints or []],
gossip_ca_certificate=self.gossip_ca_certificate,
grpc_certificate_hash=self.grpc_certificate_hash,
admin_key=self.admin_key._to_proto() if self.admin_key else None,
decline_reward=self.decline_reward,
grpc_proxy_endpoint=(self.grpc_web_proxy_endpoint._to_proto() if self.grpc_web_proxy_endpoint else None),
associated_registered_node=self.associated_registered_nodes,
)
def build_transaction_body(self) -> TransactionBody:
"""
Builds the transaction body for node create transaction.
Returns:
TransactionBody: The built transaction body.
"""
node_create_body = self._build_proto_body()
transaction_body = self.build_base_transaction_body()
transaction_body.nodeCreate.CopyFrom(node_create_body)
return transaction_body
def build_scheduled_body(self) -> SchedulableTransactionBody:
"""
Builds the scheduled transaction body for node create transaction.
Returns:
SchedulableTransactionBody: The built scheduled transaction body.
"""
node_create_body = self._build_proto_body()
scheduled_body = self.build_base_scheduled_body()
scheduled_body.nodeCreate.CopyFrom(node_create_body)
return scheduled_body
def _get_method(self, channel: _Channel) -> _Method:
"""
Gets the method to execute the node create transaction.
This internal method returns a _Method object containing the appropriate gRPC
function to call when executing this transaction on the Hedera network.
Args:
channel (_Channel): The channel containing service stubs
Returns:
_Method: An object containing the transaction function to create a node.
"""
return _Method(transaction_func=channel.address_book.createNode, query_func=None)
@classmethod
def _from_protobuf(cls, transaction_body):
transaction = super()._from_protobuf(transaction_body)
if transaction_body.HasField("nodeCreate"):
pb = transaction_body.nodeCreate
if pb.HasField("account_id"):
transaction.account_id = AccountId._from_proto(pb.account_id)
if pb.description:
transaction.description = pb.description
transaction.gossip_endpoints = [Endpoint._from_proto(ep) for ep in pb.gossip_endpoint]
transaction.service_endpoints = [Endpoint._from_proto(ep) for ep in pb.service_endpoint]
if pb.gossip_ca_certificate:
transaction.gossip_ca_certificate = pb.gossip_ca_certificate
if pb.grpc_certificate_hash:
transaction.grpc_certificate_hash = pb.grpc_certificate_hash
if pb.HasField("admin_key"):
transaction.admin_key = PublicKey._from_proto(pb.admin_key)
if pb.decline_reward:
transaction.decline_reward = pb.decline_reward
if pb.HasField("grpc_proxy_endpoint"):
transaction.grpc_web_proxy_endpoint = Endpoint._from_proto(pb.grpc_proxy_endpoint)
transaction.associated_registered_nodes = list(pb.associated_registered_node)
return transaction