Skip to content

Commit a3cf376

Browse files
committed
chore: added check fo the nodes
Signed-off-by: Manish Dait <daitmanish88@gmail.com>
1 parent 19e5cf1 commit a3cf376

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/hiero_sdk_python/client/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ def for_network(cls, network_map: dict[str, AccountId], network_name: str | None
166166
Returns:
167167
Client: A Client instance configured with the custom network.
168168
"""
169+
if not network_map:
170+
raise ValueError("network_map cannot be empty")
171+
172+
first_node = next(iter(network_map.values()))
173+
shard = first_node.shard
174+
realm = first_node.realm
175+
176+
for account_id in network_map.values():
177+
if shard != account_id.shard or realm != account_id.realm:
178+
raise ValueError("network is not valid, all nodes must be in the same shard and realm")
179+
169180
nodes = [_Node(account_id, address, None) for address, account_id in network_map.items()]
170181
return cls(Network(network=network_name, nodes=nodes))
171182

tests/unit/client_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,28 @@ def test_for_network_with_non_hosted_network_not_downgrade_tls(network):
579579
assert str(node._address) == "127.0.0.1:50212"
580580
assert node._address._is_transport_security() is True
581581
assert client.network.is_transport_security() is False # Non hosted network.
582+
583+
584+
def test_for_network_with_empty_map_raises_error():
585+
"""Test that for_network raises ValueError when map is empty."""
586+
with pytest.raises(ValueError, match="network_map cannot be empty"):
587+
Client.for_network({})
588+
589+
590+
@pytest.mark.parametrize(
591+
"invalid_map, error_msg",
592+
[
593+
(
594+
{"127.0.0.1:50211": AccountId(0, 0, 3), "127.0.0.1:50212": AccountId(1, 0, 4)},
595+
"network is not valid, all nodes must be in the same shard and realm",
596+
),
597+
(
598+
{"127.0.0.1:50211": AccountId(0, 0, 3), "127.0.0.1:50212": AccountId(0, 1, 4)},
599+
"network is not valid, all nodes must be in the same shard and realm",
600+
),
601+
],
602+
)
603+
def test_for_network_invalid_shard_realm_raises_error(invalid_map, error_msg):
604+
"""Test that for_network catches mismatched shards or realms."""
605+
with pytest.raises(ValueError, match=error_msg):
606+
Client.for_network(invalid_map)

0 commit comments

Comments
 (0)