|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package com.hedera.hashgraph.sdk.examples; |
| 3 | + |
| 4 | +import com.hedera.hashgraph.sdk.AccountId; |
| 5 | +import com.hedera.hashgraph.sdk.BlockNodeApi; |
| 6 | +import com.hedera.hashgraph.sdk.BlockNodeServiceEndpoint; |
| 7 | +import com.hedera.hashgraph.sdk.Client; |
| 8 | +import com.hedera.hashgraph.sdk.NodeUpdateTransaction; |
| 9 | +import com.hedera.hashgraph.sdk.PrivateKey; |
| 10 | +import com.hedera.hashgraph.sdk.RegisteredNode; |
| 11 | +import com.hedera.hashgraph.sdk.RegisteredNodeAddressBook; |
| 12 | +import com.hedera.hashgraph.sdk.RegisteredNodeAddressBookQuery; |
| 13 | +import com.hedera.hashgraph.sdk.RegisteredNodeCreateTransaction; |
| 14 | +import com.hedera.hashgraph.sdk.RegisteredNodeDeleteTransaction; |
| 15 | +import com.hedera.hashgraph.sdk.RegisteredNodeUpdateTransaction; |
| 16 | +import com.hedera.hashgraph.sdk.TransactionReceipt; |
| 17 | +import com.hedera.hashgraph.sdk.TransactionResponse; |
| 18 | +import com.hedera.hashgraph.sdk.logger.LogLevel; |
| 19 | +import com.hedera.hashgraph.sdk.logger.Logger; |
| 20 | +import io.github.cdimascio.dotenv.Dotenv; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +public class RegisteredNodeLifeCycleExample { |
| 25 | + /* |
| 26 | + * See .env.sample in the examples folder root for how to specify values below |
| 27 | + * or set environment variables with the same names. |
| 28 | + */ |
| 29 | + |
| 30 | + /** |
| 31 | + * Operator's account ID. |
| 32 | + * Used to sign and pay for operations on Hedera. |
| 33 | + */ |
| 34 | + private static final AccountId OPERATOR_ID = |
| 35 | + AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID"))); |
| 36 | + |
| 37 | + /** |
| 38 | + * Operator's private key. |
| 39 | + */ |
| 40 | + private static final PrivateKey OPERATOR_KEY = |
| 41 | + PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY"))); |
| 42 | + |
| 43 | + /** |
| 44 | + * HEDERA_NETWORK defaults to testnet if not specified in dotenv file. |
| 45 | + * Network can be: localhost, testnet, previewnet or mainnet. |
| 46 | + */ |
| 47 | + private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet"); |
| 48 | + |
| 49 | + /** |
| 50 | + * SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file. |
| 51 | + * Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT. |
| 52 | + * <p> |
| 53 | + * Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL, |
| 54 | + * for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace |
| 55 | + */ |
| 56 | + private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT"); |
| 57 | + |
| 58 | + public static void main(String[] args) throws Exception { |
| 59 | + System.out.println("Registered Node Lifecycle Example Start!"); |
| 60 | + |
| 61 | + /* |
| 62 | + * Step 0: |
| 63 | + * Create and configure the SDK Client. |
| 64 | + */ |
| 65 | + Client client = ClientHelper.forName(HEDERA_NETWORK); |
| 66 | + // All generated transactions will be paid by this account and signed by this key. |
| 67 | + client.setOperator(OPERATOR_ID, OPERATOR_KEY); |
| 68 | + // Attach logger to the SDK Client. |
| 69 | + client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL))); |
| 70 | + |
| 71 | + /* |
| 72 | + * Step 1: |
| 73 | + * Generate an admin key pair and configure a BlockNodeServiceEndpoint |
| 74 | + * for use in the RegisterNodeTransaction. |
| 75 | + */ |
| 76 | + PrivateKey adminKey = PrivateKey.generateED25519(); |
| 77 | + BlockNodeServiceEndpoint initialEndpoint = new BlockNodeServiceEndpoint() |
| 78 | + .setIpAddress(new byte[] {127, 0, 0, 1}) |
| 79 | + .setPort(443) |
| 80 | + .setRequiresTls(true) |
| 81 | + .setEndpointApis(List.of(BlockNodeApi.SUBSCRIBE_STREAM, BlockNodeApi.STATUS)); |
| 82 | + |
| 83 | + /* |
| 84 | + * Step 2: |
| 85 | + * Create Registered Node. |
| 86 | + */ |
| 87 | + RegisteredNodeCreateTransaction registeredNodeCreateTx = new RegisteredNodeCreateTransaction() |
| 88 | + .setDescription("My Block Node") |
| 89 | + .setAdminKey(adminKey) |
| 90 | + .addServiceEndpoint(initialEndpoint) |
| 91 | + .freezeWith(client) |
| 92 | + .sign(adminKey); |
| 93 | + |
| 94 | + System.out.println("Creating Registered Node..."); |
| 95 | + TransactionResponse registeredNodeCreateTxResponse = registeredNodeCreateTx.execute(client); |
| 96 | + TransactionReceipt registeredNodeCreateTxReceipt = registeredNodeCreateTxResponse.getReceipt(client); |
| 97 | + |
| 98 | + if (registeredNodeCreateTxReceipt.registeredNodeId <= 0) { |
| 99 | + throw new Exception("RegisteredNodeCreate transaction receipt was missing registeredNodeId. (Fail)"); |
| 100 | + } |
| 101 | + |
| 102 | + long registeredNodeId = registeredNodeCreateTxReceipt.registeredNodeId; |
| 103 | + |
| 104 | + /* |
| 105 | + * Step 3: |
| 106 | + * Execute a RegisteredNodeAddressBookQuery to verify the newly created |
| 107 | + * registered node appears in the RegisteredNodeAddressBook. |
| 108 | + */ |
| 109 | + |
| 110 | + // Wait for mirror node to update |
| 111 | + Thread.sleep(5000); |
| 112 | + RegisteredNodeAddressBookQuery addressBookQuery = |
| 113 | + new RegisteredNodeAddressBookQuery().setRegisteredNodeId(registeredNodeId); |
| 114 | + |
| 115 | + System.out.println("Executing RegisteredNodeQuery...."); |
| 116 | + RegisteredNodeAddressBook addressBook = addressBookQuery.execute(client); |
| 117 | + RegisteredNode registeredNode = addressBook.registeredNodes.getFirst(); |
| 118 | + |
| 119 | + System.out.println("Successfully fetch the registered node, " + registeredNode); |
| 120 | + |
| 121 | + /* |
| 122 | + * Step 4: |
| 123 | + * Update the RegisteredNode with new Block Node endpoint. |
| 124 | + */ |
| 125 | + BlockNodeServiceEndpoint updateEndpoint = new BlockNodeServiceEndpoint() |
| 126 | + .setDomainName("block-node.example.com") |
| 127 | + .setPort(443) |
| 128 | + .setRequiresTls(true) |
| 129 | + .addEndpointApi(BlockNodeApi.STATUS); |
| 130 | + |
| 131 | + RegisteredNodeUpdateTransaction registeredNodeUpdateTx = new RegisteredNodeUpdateTransaction() |
| 132 | + .setRegisteredNodeId(registeredNodeId) |
| 133 | + .setDescription("My Updated Block Node") |
| 134 | + .setServiceEndpoints(List.of(initialEndpoint, updateEndpoint)) |
| 135 | + .freezeWith(client) |
| 136 | + .sign(adminKey); |
| 137 | + |
| 138 | + System.out.println("Updating Registered Node..."); |
| 139 | + TransactionResponse registeredNodeUpdateTxResponse = registeredNodeUpdateTx.execute(client); |
| 140 | + registeredNodeUpdateTxResponse.getReceipt(client); |
| 141 | + |
| 142 | + /* |
| 143 | + * Step 5: |
| 144 | + * Add the registeredNodeId as associatedRegisteredNodes to a Node. |
| 145 | + * NOTE: This transaction must be signed by the consensus node's admin key. |
| 146 | + * In this example, we assume the operator is the node admin. |
| 147 | + */ |
| 148 | + |
| 149 | + NodeUpdateTransaction associateTx = new NodeUpdateTransaction() |
| 150 | + .setNodeId(0) |
| 151 | + .addAssociatedRegisteredNode(registeredNodeId) |
| 152 | + .freezeWith(client); |
| 153 | + |
| 154 | + System.out.println("Associating registered node " + registeredNodeId + " with consensus node..."); |
| 155 | + TransactionResponse associateTxResponse = associateTx.execute(client); |
| 156 | + associateTxResponse.getReceipt(client); |
| 157 | + |
| 158 | + /* |
| 159 | + * Step 6: |
| 160 | + * Remove the registeredNodeId as associatedRegisteredNodes from a Node. |
| 161 | + */ |
| 162 | + |
| 163 | + NodeUpdateTransaction disassociateTx = new NodeUpdateTransaction() |
| 164 | + .setNodeId(0) |
| 165 | + .clearAssociatedRegisteredNodes() // Empty list to clear associated registeredNode |
| 166 | + .freezeWith(client); |
| 167 | + |
| 168 | + System.out.println("Disassociating registered node " + registeredNodeId + " with consensus node..."); |
| 169 | + TransactionResponse disassociatedTxResponse = disassociateTx.execute(client); |
| 170 | + disassociatedTxResponse.getReceipt(client); |
| 171 | + |
| 172 | + /* |
| 173 | + * Step 7: |
| 174 | + * Delete the Registered Node. |
| 175 | + */ |
| 176 | + System.out.println("Deleting Registered Node..."); |
| 177 | + new RegisteredNodeDeleteTransaction() |
| 178 | + .setRegisteredNodeId(registeredNodeCreateTxReceipt.registeredNodeId) |
| 179 | + .freezeWith(client) |
| 180 | + .sign(adminKey) |
| 181 | + .execute(client) |
| 182 | + .getReceipt(client); |
| 183 | + |
| 184 | + client.close(); |
| 185 | + |
| 186 | + System.out.println("Registered Node Lifecycle Example Complete!"); |
| 187 | + } |
| 188 | +} |
0 commit comments