Skip to content

Commit 6042e55

Browse files
committed
Add set_node_account_id method
Signed-off-by: Angelina <aceppaluni@gmail.com>
1 parent 91d86fb commit 6042e55

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

src/hiero_sdk_python/query/query.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def __init__(self) -> None:
5959
self.node_index: int = 0
6060
self.payment_amount: Optional[Hbar] = None
6161

62+
self.node_account_ids: Optional[List[AccountId]] = None
63+
self._used_node_account_id: Optional[AccountId] = None
64+
6265
def _get_query_response(self, response: Any) -> query_pb2.Query:
6366
"""
6467
Extracts the query-specific response object from the full response.
@@ -379,3 +382,42 @@ def _is_payment_required(self) -> bool:
379382
bool: True if payment is required, False otherwise
380383
"""
381384
return True
385+
386+
def set_node_account_ids(self, node_account_ids: List[AccountId]):
387+
"""
388+
Sets the list of node account IDs the query can be sent to.
389+
390+
Args:
391+
node_account_ids (List[AccountId]): The list of node account IDs.
392+
393+
Returns:
394+
Self: Returns self for method chaining.
395+
"""
396+
self.node_account_ids = node_account_ids
397+
return self
398+
def set_node_account_id(self, node_account_id: AccountId):
399+
"""
400+
Sets a single node account ID the query will be sent to.
401+
402+
Args:
403+
node_account_id (AccountId): The node account ID.
404+
405+
Returns:
406+
Self: Returns self for method chaining.
407+
"""
408+
409+
return self.set_node_account_ids([node_account_id])
410+
411+
def _select_node_account_id(self) -> Optional[AccountId]:
412+
"""
413+
Internal method to select a node account ID to send the query to.
414+
Defaults to the first in the list.
415+
416+
Returns:
417+
Optional[AccountId]: The selected node account ID.
418+
"""
419+
if self.node_account_ids:
420+
selected = self.node_account_ids[0]
421+
self._used_node_account_id = selected
422+
return selected
423+
return None

src/hiero_sdk_python/transaction/transaction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ def __init__(self) -> None:
5959
# and ensures that the correct signatures are used when submitting transactions
6060
self._signature_map: dict[bytes, basic_types_pb2.SignatureMap] = {}
6161
self._default_transaction_fee = 2_000_000
62-
self.operator_account_id = None
62+
self.operator_account_id = None
63+
64+
self.node_account_ids: Optional[List[AccountId]] = None
65+
self._used_node_account_id: Optional[AccountId] = None
6366

6467
def _make_request(self):
6568
"""

tests/unit/test_query_nodes.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
from hiero_sdk_python.query.query import Query
3+
from hiero_sdk_python.account.account_id import AccountId
4+
5+
def test_set_single_node_account_id():
6+
q = Query()
7+
node = AccountId(0, 0, 3)
8+
9+
q.set_node_account_id(node)
10+
11+
assert q.node_account_ids == [node]
12+
assert q._used_node_account_id is None # not selected until execution
13+
14+
def test_set_multiple_node_account_ids():
15+
q = Query()
16+
nodes = [AccountId(0, 0, 3), AccountId(0, 0, 4)]
17+
18+
q.set_node_account_ids(nodes)
19+
20+
assert q.node_account_ids == nodes
21+
assert q._used_node_account_id is None
22+
23+
def test_select_node_account_id():
24+
q = Query()
25+
nodes = [AccountId(0, 0, 3), AccountId(0, 0, 4)]
26+
q.set_node_account_ids(nodes)
27+
28+
selected = q._select_node_account_id()
29+
30+
assert selected == nodes[0]
31+
assert q._used_node_account_id == nodes[0]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
from hiero_sdk_python.transaction.transaction import Transaction
3+
from hiero_sdk_python.account.account_id import AccountId
4+
5+
6+
class DummyTransaction(Transaction):
7+
"""
8+
Minimal subclass of Transaction for testing.
9+
Transaction is abstract (requires build methods), so we stub them out.
10+
"""
11+
def __init__(self):
12+
super().__init__()
13+
14+
def build_base_transaction_body(self):
15+
return None # stub
16+
17+
def _make_request(self):
18+
return None # stub
19+
20+
def _get_method(self):
21+
return None # stub
22+
23+
24+
def test_set_single_node_account_id():
25+
txn = DummyTransaction()
26+
node = AccountId(0, 0, 3)
27+
28+
txn.set_node_account_id(node)
29+
30+
assert txn.node_account_ids == [node]
31+
assert txn._used_node_account_id is None
32+
33+
34+
def test_set_multiple_node_account_ids():
35+
txn = DummyTransaction()
36+
nodes = [AccountId(0, 0, 3), AccountId(0, 0, 4)]
37+
38+
txn.set_node_account_ids(nodes)
39+
40+
assert txn.node_account_ids == nodes
41+
assert txn._used_node_account_id is None
42+
43+
44+
def test_select_node_account_id():
45+
txn = DummyTransaction()
46+
nodes = [AccountId(0, 0, 3), AccountId(0, 0, 4)]
47+
txn.set_node_account_ids(nodes)
48+
49+
selected = txn._select_node_account_id()
50+
51+
assert selected == nodes[0]
52+
assert txn._used_node_account_id == nodes[0]

0 commit comments

Comments
 (0)