forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_create_transaction_test.py
More file actions
366 lines (267 loc) · 14.5 KB
/
Copy pathnode_create_transaction_test.py
File metadata and controls
366 lines (267 loc) · 14.5 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
Test cases for the NodeCreateTransaction class.
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.address_book.endpoint import Endpoint
from hiero_sdk_python.crypto.private_key import PrivateKey
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.nodes.node_create_transaction import (
NodeCreateParams,
NodeCreateTransaction,
)
pytestmark = pytest.mark.unit
@pytest.fixture
def node_params():
"""Fixture for node create parameters."""
return {
"account_id": AccountId(0, 0, 123),
"description": "test node",
"gossip_endpoints": [Endpoint(domain_name="test.com", port=50211)],
"service_endpoints": [Endpoint(domain_name="test1.com", port=50212)],
"gossip_ca_certificate": b"test-ca-cert",
"grpc_certificate_hash": b"test-cert-hash",
"decline_reward": True,
"admin_key": PrivateKey.generate_ed25519().public_key(),
"grpc_web_proxy_endpoint": Endpoint(domain_name="test2.com", port=50213),
}
def test_constructor_with_parameters(node_params):
"""Test creating a node create transaction with constructor parameters."""
node_create_params = NodeCreateParams(
account_id=node_params["account_id"],
description=node_params["description"],
gossip_endpoints=node_params["gossip_endpoints"],
service_endpoints=node_params["service_endpoints"],
gossip_ca_certificate=node_params["gossip_ca_certificate"],
grpc_certificate_hash=node_params["grpc_certificate_hash"],
admin_key=node_params["admin_key"],
decline_reward=node_params["decline_reward"],
grpc_web_proxy_endpoint=node_params["grpc_web_proxy_endpoint"],
)
node_tx = NodeCreateTransaction(node_create_params=node_create_params)
assert node_tx.account_id == node_params["account_id"]
assert node_tx.description == node_params["description"]
assert node_tx.gossip_endpoints == node_params["gossip_endpoints"]
assert node_tx.service_endpoints == node_params["service_endpoints"]
assert node_tx.gossip_ca_certificate == node_params["gossip_ca_certificate"]
assert node_tx.grpc_certificate_hash == node_params["grpc_certificate_hash"]
assert node_tx.admin_key == node_params["admin_key"]
assert node_tx.decline_reward == node_params["decline_reward"]
assert node_tx.grpc_web_proxy_endpoint == node_params["grpc_web_proxy_endpoint"]
def test_constructor_default_values():
"""Test that constructor sets default values correctly."""
node_tx = NodeCreateTransaction()
assert node_tx.account_id is None
assert node_tx.description is None
assert node_tx.gossip_endpoints == []
assert node_tx.service_endpoints == []
assert node_tx.gossip_ca_certificate is None
assert node_tx.grpc_certificate_hash is None
assert node_tx.admin_key is None
assert node_tx.decline_reward is None
assert node_tx.grpc_web_proxy_endpoint is None
def test_build_transaction_body_with_valid_parameters(mock_account_ids, node_params):
"""Test building a node create transaction body with valid parameters."""
operator_id, _, node_account_id, _, _ = mock_account_ids
node_create_params = NodeCreateParams(**node_params)
node_tx = NodeCreateTransaction(node_create_params=node_create_params)
# Set operator and node account IDs needed for building transaction body
node_tx.operator_account_id = operator_id
node_tx.node_account_id = node_account_id
transaction_body = node_tx.build_transaction_body()
# Verify the transaction body contains nodeCreate field
assert transaction_body.HasField("nodeCreate")
# Verify all fields are correctly set
node_create = transaction_body.nodeCreate
assert node_create.account_id == node_params["account_id"]._to_proto()
assert node_create.description == node_params["description"]
assert len(node_create.gossip_endpoint) == 1
assert node_create.gossip_endpoint[0] == node_params["gossip_endpoints"][0]._to_proto()
assert len(node_create.service_endpoint) == 1
assert node_create.service_endpoint[0] == node_params["service_endpoints"][0]._to_proto()
assert node_create.gossip_ca_certificate == node_params["gossip_ca_certificate"]
assert node_create.grpc_certificate_hash == node_params["grpc_certificate_hash"]
assert node_create.admin_key == node_params["admin_key"]._to_proto()
assert node_create.decline_reward == node_params["decline_reward"]
assert node_create.grpc_proxy_endpoint == node_params["grpc_web_proxy_endpoint"]._to_proto()
def test_build_scheduled_body(node_params):
"""Test building a schedulable node create transaction body."""
node_create_params = NodeCreateParams(**node_params)
node_tx = NodeCreateTransaction(node_create_params=node_create_params)
schedulable_body = node_tx.build_scheduled_body()
# Verify the correct type is returned
assert isinstance(schedulable_body, SchedulableTransactionBody)
# Verify the transaction was built with node create type
assert schedulable_body.HasField("nodeCreate")
# Verify fields in the schedulable body
node_create = schedulable_body.nodeCreate
assert node_create.account_id == node_params["account_id"]._to_proto()
assert node_create.description == node_params["description"]
assert len(node_create.gossip_endpoint) == 1
assert node_create.gossip_endpoint[0] == node_params["gossip_endpoints"][0]._to_proto()
assert len(node_create.service_endpoint) == 1
assert node_create.service_endpoint[0] == node_params["service_endpoints"][0]._to_proto()
assert node_create.gossip_ca_certificate == node_params["gossip_ca_certificate"]
assert node_create.grpc_certificate_hash == node_params["grpc_certificate_hash"]
assert node_create.admin_key == node_params["admin_key"]._to_proto()
assert node_create.decline_reward == node_params["decline_reward"]
assert node_create.grpc_proxy_endpoint == node_params["grpc_web_proxy_endpoint"]._to_proto()
def test_set_account_id(node_params):
"""Test setting account_id using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_account_id(node_params["account_id"])
assert node_tx.account_id == node_params["account_id"]
assert result is node_tx # Should return self for method chaining
def test_set_description(node_params):
"""Test setting description using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_description(node_params["description"])
assert node_tx.description == node_params["description"]
assert result is node_tx # Should return self for method chaining
def test_set_gossip_endpoints(node_params):
"""Test setting gossip_endpoints using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_gossip_endpoints(node_params["gossip_endpoints"])
assert node_tx.gossip_endpoints == node_params["gossip_endpoints"]
assert result is node_tx # Should return self for method chaining
def test_set_service_endpoints(node_params):
"""Test setting service_endpoints using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_service_endpoints(node_params["service_endpoints"])
assert node_tx.service_endpoints == node_params["service_endpoints"]
assert result is node_tx # Should return self for method chaining
def test_set_gossip_ca_certificate(node_params):
"""Test setting gossip_ca_certificate using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_gossip_ca_certificate(node_params["gossip_ca_certificate"])
assert node_tx.gossip_ca_certificate == node_params["gossip_ca_certificate"]
assert result is node_tx # Should return self for method chaining
def test_set_grpc_certificate_hash(node_params):
"""Test setting grpc_certificate_hash using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_grpc_certificate_hash(node_params["grpc_certificate_hash"])
assert node_tx.grpc_certificate_hash == node_params["grpc_certificate_hash"]
assert result is node_tx # Should return self for method chaining
def test_set_admin_key(node_params):
"""Test setting admin_key using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_admin_key(node_params["admin_key"])
assert node_tx.admin_key == node_params["admin_key"]
assert result is node_tx # Should return self for method chaining
def test_set_decline_reward(node_params):
"""Test setting decline_reward using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_decline_reward(node_params["decline_reward"])
assert node_tx.decline_reward == node_params["decline_reward"]
assert result is node_tx # Should return self for method chaining
def test_set_grpc_web_proxy_endpoint(node_params):
"""Test setting grpc_web_proxy_endpoint using the setter method."""
node_tx = NodeCreateTransaction()
result = node_tx.set_grpc_web_proxy_endpoint(node_params["grpc_web_proxy_endpoint"])
assert node_tx.grpc_web_proxy_endpoint == node_params["grpc_web_proxy_endpoint"]
assert result is node_tx # Should return self for method chaining
def test_method_chaining_with_all_setters(node_params):
"""Test that all setter methods support method chaining."""
node_tx = NodeCreateTransaction()
result = (
node_tx.set_account_id(node_params["account_id"])
.set_description(node_params["description"])
.set_gossip_endpoints(node_params["gossip_endpoints"])
.set_service_endpoints(node_params["service_endpoints"])
.set_gossip_ca_certificate(node_params["gossip_ca_certificate"])
.set_grpc_certificate_hash(node_params["grpc_certificate_hash"])
.set_admin_key(node_params["admin_key"])
.set_decline_reward(node_params["decline_reward"])
.set_grpc_web_proxy_endpoint(node_params["grpc_web_proxy_endpoint"])
)
assert result is node_tx
assert node_tx.account_id == node_params["account_id"]
assert node_tx.description == node_params["description"]
assert node_tx.gossip_endpoints == node_params["gossip_endpoints"]
assert node_tx.service_endpoints == node_params["service_endpoints"]
assert node_tx.gossip_ca_certificate == node_params["gossip_ca_certificate"]
assert node_tx.grpc_certificate_hash == node_params["grpc_certificate_hash"]
assert node_tx.admin_key == node_params["admin_key"]
assert node_tx.decline_reward == node_params["decline_reward"]
assert node_tx.grpc_web_proxy_endpoint == node_params["grpc_web_proxy_endpoint"]
def test_set_methods_require_not_frozen(mock_client, node_params):
"""Test that setter methods raise exception when transaction is frozen."""
node_tx = NodeCreateTransaction()
node_tx.freeze_with(mock_client)
test_cases = [
("set_account_id", node_params["account_id"]),
("set_description", node_params["description"]),
("set_gossip_endpoints", node_params["gossip_endpoints"]),
("set_service_endpoints", node_params["service_endpoints"]),
("set_gossip_ca_certificate", node_params["gossip_ca_certificate"]),
("set_grpc_certificate_hash", node_params["grpc_certificate_hash"]),
("set_admin_key", node_params["admin_key"]),
("set_decline_reward", node_params["decline_reward"]),
("set_grpc_web_proxy_endpoint", node_params["grpc_web_proxy_endpoint"]),
]
for method_name, value in test_cases:
with pytest.raises(Exception, match="Transaction is immutable; it has been frozen"):
getattr(node_tx, method_name)(value)
def test_get_method():
"""Test retrieving the gRPC method for the transaction."""
node_tx = NodeCreateTransaction()
mock_channel = MagicMock()
mock_address_book_stub = MagicMock()
mock_channel.address_book = mock_address_book_stub
method = node_tx._get_method(mock_channel)
assert method.query is None
assert method.transaction == mock_address_book_stub.createNode
def test_sign_transaction(mock_client):
"""Test signing the node create transaction with a private key."""
node_tx = NodeCreateTransaction()
node_tx.set_account_id(AccountId(0, 0, 123))
private_key = MagicMock()
private_key.sign.return_value = b"signature"
private_key.public_key().to_bytes_raw.return_value = b"public_key"
node_tx.freeze_with(mock_client)
node_tx.sign(private_key)
node_id = mock_client.network.current_node._account_id
body_bytes = node_tx._transaction_body_bytes[node_id]
assert len(node_tx._signature_map[body_bytes].sigPair) == 1
sig_pair = node_tx._signature_map[body_bytes].sigPair[0]
assert sig_pair.pubKeyPrefix == b"public_key"
assert sig_pair.ed25519 == b"signature"
def test_to_proto(mock_client):
"""Test converting the node create transaction to protobuf format after signing."""
node_tx = NodeCreateTransaction()
node_tx.set_account_id(AccountId(0, 0, 123))
private_key = MagicMock()
private_key.sign.return_value = b"signature"
private_key.public_key().to_bytes_raw.return_value = b"public_key"
node_tx.freeze_with(mock_client)
node_tx.sign(private_key)
proto = node_tx._to_proto()
assert proto.signedTransactionBytes
assert len(proto.signedTransactionBytes) > 0
def test_node_create_transaction_build_with_private_key():
"""NodeCreateTransaction should accept PrivateKey and serialize the PublicKey in the proto."""
private_key = PrivateKey.generate()
public_key = private_key.public_key()
tx = NodeCreateTransaction().set_admin_key(private_key)
body = tx._build_proto_body()
# In the proto we should have the public key not the private one
assert body.admin_key.ed25519 == public_key.to_bytes_raw()
def test_node_create_transaction_from_protobuf_with_admin_key():
"""NodeCreateTransaction should deserialize the admin_key from the proto correctly."""
private_key = PrivateKey.generate()
transaction = NodeCreateTransaction().set_admin_key(private_key)
body = transaction._build_proto_body()
transaction_body = TransactionBody()
transaction_body.nodeCreate.CopyFrom(body)
parsed_transaction = NodeCreateTransaction._from_protobuf(
transaction_body,
b"",
None,
)
assert parsed_transaction.admin_key is not None
assert parsed_transaction.admin_key.to_proto_key() == body.admin_key