|
| 1 | +package com.hedera.hashgraph.sdk.examples; |
| 2 | + |
| 3 | +import com.hedera.hashgraph.sdk.AccountId; |
| 4 | +import com.hedera.hashgraph.sdk.BlockNodeApi; |
| 5 | +import com.hedera.hashgraph.sdk.BlockNodeServiceEndpoint; |
| 6 | +import com.hedera.hashgraph.sdk.Client; |
| 7 | +import com.hedera.hashgraph.sdk.PrivateKey; |
| 8 | +import com.hedera.hashgraph.sdk.RegisteredNodeCreateTransaction; |
| 9 | +import com.hedera.hashgraph.sdk.RegisteredNodeDeleteTransaction; |
| 10 | +import com.hedera.hashgraph.sdk.RegisteredNodeUpdateTransaction; |
| 11 | +import com.hedera.hashgraph.sdk.TransactionReceipt; |
| 12 | +import com.hedera.hashgraph.sdk.TransactionResponse; |
| 13 | +import com.hedera.hashgraph.sdk.logger.LogLevel; |
| 14 | +import com.hedera.hashgraph.sdk.logger.Logger; |
| 15 | +import io.github.cdimascio.dotenv.Dotenv; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +public class RegisteredNodeLifeCycleExample { |
| 21 | + /* |
| 22 | + * See .env.sample in the examples folder root for how to specify values below |
| 23 | + * or set environment variables with the same names. |
| 24 | + */ |
| 25 | + |
| 26 | + /** |
| 27 | + * Operator's account ID. |
| 28 | + * Used to sign and pay for operations on Hedera. |
| 29 | + */ |
| 30 | + private static final AccountId OPERATOR_ID = |
| 31 | + AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID"))); |
| 32 | + |
| 33 | + /** |
| 34 | + * Operator's private key. |
| 35 | + */ |
| 36 | + private static final PrivateKey OPERATOR_KEY = |
| 37 | + PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY"))); |
| 38 | + |
| 39 | + /** |
| 40 | + * HEDERA_NETWORK defaults to testnet if not specified in dotenv file. |
| 41 | + * Network can be: localhost, testnet, previewnet or mainnet. |
| 42 | + */ |
| 43 | + private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet"); |
| 44 | + |
| 45 | + /** |
| 46 | + * SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file. |
| 47 | + * Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT. |
| 48 | + * <p> |
| 49 | + * Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL, |
| 50 | + * for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace |
| 51 | + */ |
| 52 | + private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT"); |
| 53 | + |
| 54 | + public static void main(String[] args) throws Exception { |
| 55 | + System.out.println("Registered Node Lifecycle Example Start!"); |
| 56 | + |
| 57 | + /* |
| 58 | + * Step 0: |
| 59 | + * Create and configure the SDK Client. |
| 60 | + */ |
| 61 | + Client client = ClientHelper.forName(HEDERA_NETWORK); |
| 62 | + // All generated transactions will be paid by this account and signed by this key. |
| 63 | + client.setOperator(OPERATOR_ID, OPERATOR_KEY); |
| 64 | + // Attach logger to the SDK Client. |
| 65 | + client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL))); |
| 66 | + |
| 67 | + /* |
| 68 | + * Step 1: |
| 69 | + * Generate an admin key pair and configure a BlockNodeServiceEndpoint |
| 70 | + * for use in the RegisterNodeTransaction. |
| 71 | + */ |
| 72 | + PrivateKey adminKey = PrivateKey.generateED25519(); |
| 73 | + BlockNodeServiceEndpoint initialEndpoint = new BlockNodeServiceEndpoint() |
| 74 | + .setIpAddress(new byte[]{127, 0, 0, 1}) |
| 75 | + .setPort(443) |
| 76 | + .setRequiresTls(true) |
| 77 | + .setEndpointApi(BlockNodeApi.SUBSCRIBE_STREAM); |
| 78 | + |
| 79 | + /* |
| 80 | + * Step 2: |
| 81 | + * Create Registered Node. |
| 82 | + */ |
| 83 | + RegisteredNodeCreateTransaction registeredNodeCreateTx = new RegisteredNodeCreateTransaction() |
| 84 | + .setDescription("My Block Node") |
| 85 | + .setAdminKey(adminKey) |
| 86 | + .addServiceEndpoint(initialEndpoint) |
| 87 | + .freezeWith(client) |
| 88 | + .sign(adminKey); |
| 89 | + |
| 90 | + System.out.println("Creating Registered Node..."); |
| 91 | + TransactionResponse registeredNodeCreateTxResponse = registeredNodeCreateTx.execute(client); |
| 92 | + TransactionReceipt registeredNodeCreateTxReceipt = registeredNodeCreateTxResponse.getReceipt(client); |
| 93 | + |
| 94 | + if (registeredNodeCreateTxReceipt.registeredNodeId <= 0) { |
| 95 | + throw new Exception("RegisteredNodeCreate transaction receipt was missing registeredNodeId. (Fail)"); |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * Step 3: |
| 100 | + * Execute a RegisteredNodeAddressBookQuery to verify the newly created |
| 101 | + * registered node appears in the RegisteredNodeAddressBook. |
| 102 | + */ |
| 103 | + System.out.println("Skipping registered node address book query because mirror node API is not available..."); |
| 104 | + |
| 105 | + /* |
| 106 | + * Step 4: |
| 107 | + * Update the RegisteredNode with new Block Node endpoint. |
| 108 | + */ |
| 109 | + BlockNodeServiceEndpoint updateEndpoint = new BlockNodeServiceEndpoint() |
| 110 | + .setDomainName("block-node.example.com") |
| 111 | + .setPort(443) |
| 112 | + .setRequiresTls(true) |
| 113 | + .setEndpointApi(BlockNodeApi.STATUS); |
| 114 | + |
| 115 | + RegisteredNodeUpdateTransaction registeredNodeUpdateTx = new RegisteredNodeUpdateTransaction() |
| 116 | + .setRegisteredNodeId(registeredNodeCreateTxReceipt.registeredNodeId) |
| 117 | + .setDescription("My Updated Block Node") |
| 118 | + .setServiceEndpoints(List.of(initialEndpoint, updateEndpoint)) |
| 119 | + .freezeWith(client) |
| 120 | + .sign(adminKey); |
| 121 | + |
| 122 | + System.out.println("Updating Registered Node..."); |
| 123 | + TransactionResponse registeredNodeUpdateTxResponse = registeredNodeUpdateTx.execute(client); |
| 124 | + TransactionReceipt registeredNodeUpdateTxReceipt = registeredNodeUpdateTxResponse.getReceipt(client); |
| 125 | + |
| 126 | + /* |
| 127 | + * Step 5: |
| 128 | + * Add the registeredNodeId as associatedRegisteredNodes to a Node. |
| 129 | + */ |
| 130 | + // TODO |
| 131 | + |
| 132 | + /* |
| 133 | + * Step 6: |
| 134 | + * Delete the Registered Node. |
| 135 | + */ |
| 136 | + RegisteredNodeDeleteTransaction registeredNodeDeleteTx = new RegisteredNodeDeleteTransaction() |
| 137 | + .setRegisteredNodeId(registeredNodeCreateTxReceipt.registeredNodeId) |
| 138 | + .freezeWith(client) |
| 139 | + .sign(adminKey); |
| 140 | + |
| 141 | + System.out.println("Deleting Registered Node..."); |
| 142 | + TransactionResponse registeredNodeDeleteTxResponse = registeredNodeDeleteTx.execute(client); |
| 143 | + registeredNodeDeleteTxResponse.getReceipt(client); |
| 144 | + } |
| 145 | +} |
0 commit comments