Skip to content

Commit

Permalink
feat(HIP-869): Dynamic Address Book - Stage 1 - HAPI Endpoints (#1892)
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Lebedev <[email protected]>
Signed-off-by: Ivan Ivanov <[email protected]>
Co-authored-by: Ivan Ivanov <[email protected]>
  • Loading branch information
thenswan and 0xivanov authored Aug 22, 2024
1 parent ccbf657 commit 0adfe0e
Show file tree
Hide file tree
Showing 19 changed files with 1,889 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.hedera.hashgraph.sdk.examples;

import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.Endpoint;
import com.hedera.hashgraph.sdk.IPv4Address;
import com.hedera.hashgraph.sdk.IPv4AddressPart;
import com.hedera.hashgraph.sdk.NodeCreateTransaction;
import com.hedera.hashgraph.sdk.NodeDeleteTransaction;
import com.hedera.hashgraph.sdk.NodeUpdateTransaction;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.ReceiptStatusException;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.TimeoutException;

/**
* hip-869
*/
public class DynamicAddressBookExample {
// see `.env.sample` in the repository root for how to specify these values
// or set environment variables with the same names
private static final AccountId OPERATOR_ID = AccountId.fromString(
Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
private static final PrivateKey OPERATOR_KEY = PrivateKey.fromString(
Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
// HEDERA_NETWORK defaults to testnet if not specified in dotenv
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");

public static void main(String[] args)
throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// 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(OPERATOR_ID, OPERATOR_KEY);

AccountId accountId = AccountId.fromString("0.0.1999");
String description = "Hedera™ cryptocurrency";
String newDescription = "Hedera™ cryptocurrency - updated";

// Set up IPv4 address
IPv4Address ipv4Address = new IPv4Address();
ipv4Address.setHost(new IPv4AddressPart());
ipv4Address.setNetwork(new IPv4AddressPart());
Endpoint gossipEndpoint = new Endpoint();
gossipEndpoint.setAddress(ipv4Address);

// Set up service endpoint
Endpoint serviceEndpoint = new Endpoint();
serviceEndpoint.setAddress(ipv4Address);

// Generate admin key
PrivateKey adminKey = PrivateKey.generateED25519();

// Create node create transaction
NodeCreateTransaction nodeCreateTransaction = new NodeCreateTransaction()
.setAccountId(accountId)
.setDescription(description)
.setGossipCaCertificate("gossipCaCertificate".getBytes())
.setServiceEndpoints(Collections.singletonList(serviceEndpoint))
.setGossipEndpoints(Collections.singletonList(gossipEndpoint))
.setAdminKey(adminKey.getPublicKey());

try {
nodeCreateTransaction.execute(client).getReceipt(client);
} catch (Exception e){
System.out.println(e);
}

var nodeUpdateTransaction = new NodeUpdateTransaction()
.setNodeId(123)
.setAccountId(accountId)
.setDescription(newDescription)
.setGossipCaCertificate("gossipCaCertificate".getBytes())
.setServiceEndpoints(Collections.singletonList(serviceEndpoint))
.setGossipEndpoints(Collections.singletonList(gossipEndpoint))
.setAdminKey(adminKey.getPublicKey());

try {
nodeUpdateTransaction.execute(client).getReceipt(client);
} catch (Exception e){
System.out.println(e);
}

var nodeDeleteTransaction = new NodeDeleteTransaction()
.setNodeId(123);

try {
nodeDeleteTransaction.execute(client).getReceipt(client);
} catch (Exception e){
System.out.println(e);
}
}
}
38 changes: 33 additions & 5 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
* Utility class used internally by the sdk.
*/
public class Endpoint implements Cloneable {

@Nullable
IPv4Address address = null;

int port;

String domainName = "";

/**
* Constructor.
*/
Endpoint() {
public Endpoint() {
}

/**
Expand All @@ -55,7 +58,8 @@ static Endpoint fromProtobuf(ServiceEndpoint serviceEndpoint) {

return new Endpoint()
.setAddress(IPv4Address.fromProtobuf(serviceEndpoint.getIpAddressV4()))
.setPort(port);
.setPort(port)
.setDomainName(serviceEndpoint.getDomainName());
}

/**
Expand Down Expand Up @@ -99,6 +103,26 @@ public Endpoint setPort(int port) {
return this;
}

/**
* Extract the domain name.
*
* @return the domain name
*/
public String getDomainName() {
return domainName;
}

/**
* Assign the desired domain name.
*
* @param domainName the desired domain name
* @return {@code this}
*/
public Endpoint setDomainName(String domainName) {
this.domainName = domainName;
return this;
}

/**
* Create the protobuf.
*
Expand All @@ -111,14 +135,18 @@ ServiceEndpoint toProtobuf() {
builder.setIpAddressV4(address.toProtobuf());
}

builder.setDomainName(domainName);

return builder.setPort(port).build();
}

@Override
public String toString() {
return Objects.requireNonNull(address) +
":" +
port;
if (this.domainName != null && !this.domainName.isEmpty()) {
return domainName + ":" + port;
} else {
return Objects.requireNonNull(address) + ":" + port;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class IPv4Address implements Cloneable {
/**
* Constructor.
*/
IPv4Address() {
public IPv4Address() {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class IPv4AddressPart implements Cloneable {
/**
* Constructor.
*/
IPv4AddressPart() {
public IPv4AddressPart() {
}

/**
Expand Down
Loading

0 comments on commit 0adfe0e

Please sign in to comment.