forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistered_node_lifecycle.py
More file actions
208 lines (167 loc) · 7.83 KB
/
Copy pathregistered_node_lifecycle.py
File metadata and controls
208 lines (167 loc) · 7.83 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
"""
Demonstrates the full lifecycle of a registered node on the Hedera network.
This example shows how to:
1. Create a registered node with BlockNodeServiceEndpoint and multiple endpoint APIs
2. Query the registered node via the mirror node REST API
3. Update the registered node's description and service endpoints
4. Associate / disassociate the registered node with a consensus node
5. Delete the registered node
NOTE: This is a privileged transaction. Regular developers do not have the required
permissions to manage registered nodes on testnet or mainnet as this operation
requires special authorization.
This example is provided to demonstrate the API for educational purposes or for use
in private network deployments where you have the necessary administrative privileges.
"""
import sys
import time
from dotenv import load_dotenv
from hiero_sdk_python import AccountId, Client, Network, PrivateKey
from hiero_sdk_python.address_book.block_node_api import BlockNodeApi
from hiero_sdk_python.address_book.block_node_service_endpoint import BlockNodeServiceEndpoint
from hiero_sdk_python.address_book.registered_node_address_book_query import RegisteredNodeAddressBookQuery
from hiero_sdk_python.exceptions import PrecheckError
from hiero_sdk_python.nodes.node_update_transaction import NodeUpdateTransaction
from hiero_sdk_python.nodes.registered_node_create_transaction import RegisteredNodeCreateTransaction
from hiero_sdk_python.nodes.registered_node_delete_transaction import RegisteredNodeDeleteTransaction
from hiero_sdk_python.nodes.registered_node_update_transaction import RegisteredNodeUpdateTransaction
from hiero_sdk_python.response_code import ResponseCode
def setup_client():
"""Initialize and set up the client with operator account."""
load_dotenv()
network = Network(network="solo")
client = Client(network)
print(f"Connecting to Hedera {network} network!")
# Account 0.0.2 is a special administrative account with
# elevated privileges for network management operations.
# The private key is intentionally public for local development.
# Note: This setup only works on solo network and will not work on testnet/mainnet.
original_operator_key = PrivateKey.from_string_der(
"302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137"
)
client.set_operator(AccountId(0, 0, 2), original_operator_key)
return client
def registered_node_lifecycle():
"""Demonstrates create, query, update, associate, disassociate, and delete of a registered node."""
client = setup_client()
# Generate an admin key for the registered node
admin_key = PrivateKey.generate_ed25519()
# ── Step 1: Create a registered node ────────────────────────────
print("\n--- Step 1: Creating registered node ---")
block_endpoint = BlockNodeServiceEndpoint(
ip_address=bytes([127, 0, 0, 1]),
port=443,
requires_tls=True,
endpoint_apis=[BlockNodeApi.STATUS, BlockNodeApi.SUBSCRIBE_STREAM],
)
try:
receipt = (
RegisteredNodeCreateTransaction()
.set_admin_key(admin_key.public_key())
.set_description("My Block Node")
.set_service_endpoints([block_endpoint])
.freeze_with(client)
.sign(admin_key)
.execute(client)
)
except (PrecheckError, ValueError) as e:
print(f"Registered node creation failed: {e}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
print(f"Registered node creation failed: {ResponseCode(receipt.status).name}")
sys.exit(1)
registered_node_id = receipt.registered_node_id
print(f"Registered node created with ID: {registered_node_id}")
# ── Step 2: Query the registered node via mirror node ───────────
print("\n--- Step 2: Querying registered node via mirror node ---")
# Wait for mirror node ingestion
time.sleep(5)
try:
address_book = RegisteredNodeAddressBookQuery().set_registered_node_id(registered_node_id).execute(client)
if len(address_book) > 0:
node = address_book[0]
print(f"Fetched registered node: {node}")
print(f" Description: {node.description}")
print(f" Endpoints: {len(node.service_endpoints)}")
else:
print("No registered nodes returned (mirror may still be ingesting)")
except RuntimeError as e:
print(f"Query failed (mirror node may not support this endpoint): {e}")
# ── Step 3: Update the registered node ──────────────────────────
print("\n--- Step 3: Updating registered node ---")
update_endpoint = BlockNodeServiceEndpoint(
domain_name="block-node.example.com",
port=443,
requires_tls=True,
endpoint_apis=[BlockNodeApi.STATUS],
)
try:
receipt = (
RegisteredNodeUpdateTransaction()
.set_registered_node_id(registered_node_id)
.set_description("My Updated Block Node")
.set_service_endpoints([block_endpoint, update_endpoint])
.freeze_with(client)
.sign(admin_key)
.execute(client)
)
except (PrecheckError, ValueError) as e:
print(f"Registered node update failed: {e}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
print(f"Registered node update failed: {ResponseCode(receipt.status).name}")
sys.exit(1)
print("Registered node updated successfully")
# ── Step 4: Associate registered node with consensus node ───────
print("\n--- Step 4: Associating registered node with consensus node 0 ---")
try:
receipt = (
NodeUpdateTransaction()
.set_node_id(0)
.add_associated_registered_node(registered_node_id)
.freeze_with(client)
.execute(client)
)
except (PrecheckError, ValueError) as e:
print(f"Association failed: {e}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
print(f"Association failed: {ResponseCode(receipt.status).name}")
sys.exit(1)
print(f"Registered node {registered_node_id} associated with consensus node 0")
# ── Step 5: Disassociate registered node from consensus node ────
print("\n--- Step 5: Disassociating registered node from consensus node 0 ---")
try:
receipt = (
NodeUpdateTransaction()
.set_node_id(0)
.clear_associated_registered_nodes()
.freeze_with(client)
.execute(client)
)
except (PrecheckError, ValueError) as e:
print(f"Disassociation failed: {e}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
print(f"Disassociation failed: {ResponseCode(receipt.status).name}")
sys.exit(1)
print(f"Registered node {registered_node_id} disassociated from consensus node 0")
# ── Step 6: Delete the registered node ──────────────────────────
print("\n--- Step 6: Deleting registered node ---")
try:
receipt = (
RegisteredNodeDeleteTransaction()
.set_registered_node_id(registered_node_id)
.freeze_with(client)
.sign(admin_key)
.execute(client)
)
except (PrecheckError, ValueError) as e:
print(f"Registered node deletion failed: {e}")
sys.exit(1)
if receipt.status != ResponseCode.SUCCESS:
print(f"Registered node deletion failed: {ResponseCode(receipt.status).name}")
sys.exit(1)
print("Registered node deleted successfully")
print("\n--- Registered node lifecycle complete! ---")
if __name__ == "__main__":
registered_node_lifecycle()