diff --git a/README.md b/README.md index b4c574acf60..40a3ff49a4d 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ This will compile a runnable mirror node jar file in the `target` directory and Besides bug fixes, some features may have changed with this release which need your attention, these will be listed here. +### Added address book download capability + +The address book may be downloaded from the network using this software. + ### Addition of a REST api A REST api written in `node` is now available in this project. @@ -405,50 +409,30 @@ Use the command `docker exec -it /bin/sh` to get a shell in the You may now power down the docker image itself. -## Creating the address book file (0.0.102 file) - -The java code below was used with the java SDK in order to create the file. - -```java -package com.hedera.hashgraph.sdk.examples.advanced; - -import com.google.protobuf.InvalidProtocolBufferException; -import com.hedera.hashgraph.sdk.HederaException; -import com.hedera.hashgraph.sdk.examples.ExampleHelper; -import com.hedera.hashgraph.sdk.file.FileId; -import com.hedera.hashgraph.sdk.file.FileContentsQuery; - -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; - -public final class GetHederaAddressBook { - private GetHederaAddressBook() { } - - public static void main(String[] args) throws HederaException, InvalidProtocolBufferException { - - // Build the Hedera client using ExampleHelper class - var client = ExampleHelper.createHederaClient(); - - // Get file contents - var contents = new FileContentsQuery(client) - .setFileId(new FileId(0, 0, 102)) - .execute(); - try { - FileOutputStream fos = new FileOutputStream("0.0.102"); - fos.write(contents.getFileContents().getContents().toByteArray()); - fos.close(); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } -} +## Creating or updating the address book file (0.0.102 file) + +Set the following environment variables or add them to a `.env` file. + +```text +NODE_ADDRESS=127.0.0.1:50211 +NODE_ID=0.0.x +OPERATOR_ID=0.0.x +OPERATOR_KEY=your account's private key ``` +`NODE_ADDRESS` is the IP address/url + port of the node you wish to request the file from. +`NODE_ID` is the account number of the node (0.0.x). +`OPERATOR_ID` is your own account number on the network (0.0.x). +`OPERATOR_KEY` is your private key for the above account. + +Run the following command to update the address book at the location specified in `config.json`. + +```shell +java -Dlog4j.configurationFile=./log4j2.xml -cp mirrorNode.jar com.hedera.addressBook.NetworkAddressBook +``` + +If no errors are output, the file specified by the `addressBookFile` parameter of the `config.json` file will now contain the network's address book. + ## REST API A REST API to the database is available under `rest-api`. diff --git a/src/main/java/com/hedera/addressBook/NetworkAddressBook.java b/src/main/java/com/hedera/addressBook/NetworkAddressBook.java new file mode 100644 index 00000000000..3c2bdf2615d --- /dev/null +++ b/src/main/java/com/hedera/addressBook/NetworkAddressBook.java @@ -0,0 +1,93 @@ +package com.hedera.addressBook; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Map; + +import com.hedera.configLoader.ConfigLoader; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; + +import com.hedera.hashgraph.sdk.Client; +import com.hedera.hashgraph.sdk.HederaException; +import com.hedera.hashgraph.sdk.HederaNetworkException; +import com.hedera.hashgraph.sdk.account.AccountId; +import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey; +import com.hedera.hashgraph.sdk.file.FileId; + +import io.github.cdimascio.dotenv.Dotenv; + +import com.hedera.hashgraph.sdk.file.FileContentsQuery; + +import java.io.FileOutputStream; + +/** + * This is a utility file to read back service record file generated by Hedera node + */ +public class NetworkAddressBook { + + private static final Logger log = LogManager.getLogger("recordStream-log"); + static final Marker LOGM_EXCEPTION = MarkerManager.getMarker("EXCEPTION"); + + private static ConfigLoader configLoader = new ConfigLoader("./config/config.json"); + + static Client client; + static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); + + public static void main(String[] args) { + + String addressBookFile = configLoader.getAddressBookFile(); + + var client = createHederaClient(); + + log.info("Fecthing New address book from node {}.", dotenv.get("NODE_ADDRESS")); + + try { + var contents = new FileContentsQuery(client) + .setFileId(new FileId(0, 0, 102)) + .execute(); + + FileOutputStream fos = new FileOutputStream(addressBookFile); + fos.write(contents.getFileContents().getContents().toByteArray()); + fos.close(); + } catch (FileNotFoundException e) { + log.error(LOGM_EXCEPTION, "Address book file {} not found.", addressBookFile); + } catch (IOException e) { + log.error(LOGM_EXCEPTION, "An error occurred fetching the address book file: {} ", e.getMessage()); + } catch (HederaNetworkException e) { + log.error(LOGM_EXCEPTION, "An error occurred fetching the address book file: {} ", e.getMessage()); + } catch (HederaException e) { + log.error(LOGM_EXCEPTION, "An error occurred fetching the address book file: {} ", e.getMessage()); + } + log.info("New address book successfully saved to {}.", addressBookFile); + } + + private static Client createHederaClient() { + // To connect to a network with more nodes, add additional entries to the network map + var nodeAddress = dotenv.get("NODE_ADDRESS"); + var client = new Client(Map.of(getNodeId(), nodeAddress)); + + // Defaults the operator account ID and key such that all generated transactions will be paid for + // by this account and be signed by this key + client.setOperator(getOperatorId(), getOperatorKey()); + + return client; + } + + public static AccountId getNodeId() { + return AccountId.fromString(dotenv.get("NODE_ID")); + } + + public static AccountId getOperatorId() { + return AccountId.fromString(dotenv.get("OPERATOR_ID")); + } + + public static Ed25519PrivateKey getOperatorKey() { + return Ed25519PrivateKey.fromString(dotenv.get("OPERATOR_KEY")); + } + +} + +