Skip to content

Commit

Permalink
feat: dynamic address book [HIP-869] (#2403)
Browse files Browse the repository at this point in the history
* feat: create node transaction (#2402)

* feat: add addressBook channel

Signed-off-by: svetoslav-nikol0v <[email protected]>

* feat: implement NodeCreateTransaction class

Signed-off-by: svetoslav-nikol0v <[email protected]>

* feat: implement ServiceEndpoint class

Signed-off-by: svetoslav-nikol0v <[email protected]>

* chore: export new classess

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: proto changes

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: transaction receipt

Signed-off-by: svetoslav-nikol0v <[email protected]>

* fix: unit tests

Signed-off-by: svetoslav-nikol0v <[email protected]>

* fix: method in node create transaction

Signed-off-by: svetoslav-nikol0v <[email protected]>

---------

Signed-off-by: svetoslav-nikol0v <[email protected]>

* fix: integration test

Signed-off-by: svetoslav-nikol0v <[email protected]>

* chore: remove .only

Signed-off-by: svetoslav-nikol0v <[email protected]>

* feat: node delete transaction (#2404)

Signed-off-by: svetoslav-nikol0v <[email protected]>

* feat: node update transaction (#2405)

* feat: node update transaction

Signed-off-by: svetoslav-nikol0v <[email protected]>

* fix: adjustments

Signed-off-by: svetoslav-nikol0v <[email protected]>

---------

Signed-off-by: svetoslav-nikol0v <[email protected]>

* update: example for HIP-869 (#2443)

* chore: formatting

Signed-off-by: Svet <[email protected]>

* add: example

Signed-off-by: Svet <[email protected]>

* chore: formatting

Signed-off-by: Svet <[email protected]>

* update: example

Signed-off-by: Svet <[email protected]>

---------

Signed-off-by: Svet <[email protected]>

* update: remove default values

Signed-off-by: Svet <[email protected]>

---------

Signed-off-by: svetoslav-nikol0v <[email protected]>
Signed-off-by: Svet <[email protected]>
  • Loading branch information
svetoslav-nikol0v authored Aug 13, 2024
1 parent 019a59e commit 82958b3
Show file tree
Hide file tree
Showing 11 changed files with 1,426 additions and 8 deletions.
95 changes: 95 additions & 0 deletions examples/create-update-delete-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
Client,
PrivateKey,
AccountId,
NodeCreateTransaction,
NodeUpdateTransaction,
NodeDeleteTransaction,
ServiceEndpoint,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

async function main() {
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required.",
);
}

const network = process.env.HEDERA_NETWORK;
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);
const client = Client.forName(network).setOperator(operatorId, operatorKey);

// Transaction parameters
const accountId = AccountId.fromString("0.0.999");
const description = "This is a description of the node.";
const newDescription = "This is new a description of the node.";
const ipAddressV4 = Uint8Array.of(127, 0, 0, 1);
const port = 50211;
const gossipEndpoint = new ServiceEndpoint()
.setIpAddressV4(ipAddressV4)
.setPort(port);
const gossipEndpoints = [gossipEndpoint];
const serviceEndpoint = new ServiceEndpoint()
.setIpAddressV4(ipAddressV4)
.setPort(port);
const serviceEndpoints = [serviceEndpoint];
const gossipCaCertificate = new Uint8Array();
const certificateHash = new Uint8Array();
const adminKey = PrivateKey.generate();

// 1. Create a new node
console.log("Creating a new node...");
const createTransaction = new NodeCreateTransaction()
.setAccountId(accountId)
.setDescription(description)
.setGossipEndpoints(gossipEndpoints)
.setServiceEndpoints(serviceEndpoints)
.setGossipCaCertificate(gossipCaCertificate)
.setCertificateHash(certificateHash)
.setAdminKey(adminKey);
const createTransactionResponse = await createTransaction.execute(client);
const createTransactionReceipt =
await createTransactionResponse.getReceipt(client);
const nodeId = createTransactionReceipt.nodeId;
console.log(
`Node create transaction status: ${createTransactionReceipt.status.toString()}`,
);
console.log(
`Node has been created successfully with node id: ${nodeId.toString()}`,
);

// 2. Update the node
console.log("Updating the node...");
const updateTransaction = new NodeUpdateTransaction()
.setNodeId(nodeId)
.setDescription(newDescription);
const updateTrasnactionResponse = await updateTransaction.execute(client);
const updateTrasnactionReceipt =
await updateTrasnactionResponse.getReceipt(client);
console.log(
`Node update transaction status: ${updateTrasnactionReceipt.status.toString()}`,
);

// 3. Delete the node
console.log("Deleting the node...");
const deleteTransaction = new NodeDeleteTransaction().setNodeId(nodeId);
const deleteTransactionResponse = await deleteTransaction.execute(client);
const deleteTransactionReceipt =
await deleteTransactionResponse.getReceipt(client);
console.log(
`Node delete transaction status: ${deleteTransactionReceipt.status.toString()}`,
);

client.close();
}

void main();
21 changes: 21 additions & 0 deletions src/channel/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export default class Channel {
* @type {?HashgraphProto.proto.UtilService}
*/
this._util = null;

/**
* @protected
* @type {?HashgraphProto.proto.AddressBookService}
*/
this._addressBook = null;
}

/**
Expand Down Expand Up @@ -230,6 +236,21 @@ export default class Channel {
return this._util;
}

/**
* @returns {HashgraphProto.proto.AddressBookService}
*/
get addressBook() {
if (this._addressBook != null) {
return this._addressBook;
}

this._addressBook = proto.AddressBookService.create(
this._createUnaryClient("AddressBookService"),
);

return this._addressBook;
}

/**
* @abstract
* @protected
Expand Down
4 changes: 4 additions & 0 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ export { default as PrecheckStatusError } from "./PrecheckStatusError.js";
export { default as ReceiptStatusError } from "./ReceiptStatusError.js";
export { default as LedgerId } from "./LedgerId.js";
export { default as TokenUpdateNftsTransaction } from "./token/TokenUpdateNftsTransaction.js";
export { default as NodeCreateTransaction } from "./node/NodeCreateTransaction.js";
export { default as ServiceEndpoint } from "./node/ServiceEndpoint.js";
export { default as NodeDeleteTransaction } from "./node/NodeDeleteTransaction.js";
export { default as NodeUpdateTransaction } from "./node/NodeUpdateTransaction.js";

/**
* @typedef {import("./client/Client.js").NetworkName} ClientNetworkName
Expand Down
Loading

0 comments on commit 82958b3

Please sign in to comment.