Skip to content

Commit

Permalink
feat: can initialize client consensus node network with mirror node a…
Browse files Browse the repository at this point in the history
…ddress book

Signed-off-by: Rob Walworth <[email protected]>
  • Loading branch information
rwalworth committed Oct 24, 2024
1 parent befc590 commit b6f4663
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sdk/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ set(GET_ACCOUNT_INFO_EXAMPLE_NAME ${PROJECT_NAME}-get-account-info-example)
set(GET_ADDRESS_BOOK_EXAMPLE_NAME ${PROJECT_NAME}-get-address-book-example)
set(GET_EXCHANGE_RATES_EXAMPLE_NAME ${PROJECT_NAME}-get-exchange-rates-example)
set(GET_FILE_CONTENTS_EXAMPLE_NAME ${PROJECT_NAME}-get-file-contents-example)
set(INITIALIZE_CLIENT_WITH_MIRROR_NODE_ADDRESS_BOOK_EXAMPLE_NAME
${PROJECT_NAME}-initialize-client-with-mirror-node-address-book-example)
set(MULTI_APP_TRANSFER_EXAMPLE_NAME ${PROJECT_NAME}-multi-app-transfer-example)
set(MULTI_SIG_OFFLINE_EXAMPLE_NAME ${PROJECT_NAME}-multi-sig-offline-example)
set(NFT_ADD_REMOVE_ALLOWANCES_EXAMPLE_NAME ${PROJECT_NAME}-nft-add-remove-allowances-example)
Expand Down Expand Up @@ -90,6 +92,7 @@ add_executable(${GET_ACCOUNT_INFO_EXAMPLE_NAME} GetAccountInfoExample.cc)
add_executable(${GET_ADDRESS_BOOK_EXAMPLE_NAME} GetAddressBookExample.cc)
add_executable(${GET_EXCHANGE_RATES_EXAMPLE_NAME} GetExchangeRatesExample.cc)
add_executable(${GET_FILE_CONTENTS_EXAMPLE_NAME} GetFileContentsExample.cc)
add_executable(${INITIALIZE_CLIENT_WITH_MIRROR_NODE_ADDRESS_BOOK_EXAMPLE_NAME} InitializeClientWithMirrorNodeAddressBookExample.cc)
add_executable(${MULTI_APP_TRANSFER_EXAMPLE_NAME} MultiAppTransferExample.cc)
add_executable(${MULTI_SIG_OFFLINE_EXAMPLE_NAME} MultiSigOfflineExample.cc)
add_executable(${NFT_ADD_REMOVE_ALLOWANCES_EXAMPLE_NAME} NftAddRemoveAllowancesExample.cc)
Expand Down Expand Up @@ -166,6 +169,7 @@ target_link_libraries(${GET_ACCOUNT_INFO_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${GET_ADDRESS_BOOK_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${GET_EXCHANGE_RATES_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${GET_FILE_CONTENTS_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${INITIALIZE_CLIENT_WITH_MIRROR_NODE_ADDRESS_BOOK_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${MULTI_APP_TRANSFER_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${MULTI_SIG_OFFLINE_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
target_link_libraries(${NFT_ADD_REMOVE_ALLOWANCES_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
Expand Down Expand Up @@ -222,6 +226,7 @@ install(TARGETS
${GET_ADDRESS_BOOK_EXAMPLE_NAME}
${GET_EXCHANGE_RATES_EXAMPLE_NAME}
${GET_FILE_CONTENTS_EXAMPLE_NAME}
${INITIALIZE_CLIENT_WITH_MIRROR_NODE_ADDRESS_BOOK_EXAMPLE_NAME}
${MULTI_APP_TRANSFER_EXAMPLE_NAME}
${MULTI_SIG_OFFLINE_EXAMPLE_NAME}
${NFT_ADD_REMOVE_ALLOWANCES_EXAMPLE_NAME}
Expand Down
5 changes: 5 additions & 0 deletions src/sdk/examples/GetAddressBookExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
* limitations under the License.
*
*/
#include "AccountCreateTransaction.h"
#include "AddressBookQuery.h"
#include "Client.h"
#include "ED25519PrivateKey.h"
#include "FileId.h"
#include "NodeAddressBook.h"
#include "TransactionReceipt.h"
#include "TransactionResponse.h"

#include <dotenv.h>
#include <iostream>

using namespace Hedera;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*-
*
* Hedera C++ SDK
*
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "AccountCreateTransaction.h"
#include "Client.h"
#include "ED25519PrivateKey.h"
#include "TransactionReceipt.h"
#include "TransactionResponse.h"

#include <dotenv.h>
#include <iostream>

using namespace Hedera;

int main(int argc, char** argv)
{
dotenv::init();
const AccountId operatorAccountId = AccountId::fromString(std::getenv("OPERATOR_ID"));
const std::shared_ptr<PrivateKey> operatorPrivateKey = ED25519PrivateKey::fromString(std::getenv("OPERATOR_KEY"));

// Initialize the client with the testnet mirror node.
Client client;
client.setOperator(operatorAccountId, operatorPrivateKey);
client.setMirrorNetwork({ "testnet.mirrornode.hedera.com:443" });

// Get the address book from the mirror node and use it to populate the Client's consensus network.
client.populateNetworkFromMirrorNodeAddressBook();

// Attempt to execute a transaction.
TransactionReceipt txReceipt =
AccountCreateTransaction().setKey(ED25519PrivateKey::generatePrivateKey()).execute(client).getReceipt(client);
std::cout << "Created account " << txReceipt.mAccountId->toString() << std::endl;

return 0;
}
6 changes: 6 additions & 0 deletions src/sdk/main/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ class Client
*/
Client& setMirrorNetwork(const std::vector<std::string>& network);

/**
* Populate this Client's consensus network by getting the address book from this Client's mirror network. This
* requires a mirror network to be manually set already.
*/
void populateNetworkFromMirrorNodeAddressBook();

/**
* Get the list of mirror nodes on this Client's network.
*
Expand Down
1 change: 1 addition & 0 deletions src/sdk/main/src/AddressBookQuery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "exceptions/MaxAttemptsExceededException.h"
#include "impl/MirrorNetwork.h"
#include "impl/MirrorNode.h"
#include "impl/Utilities.h"

#include <proto/mirror/mirror_network_service.pb.h>
#include <thread>
Expand Down
12 changes: 12 additions & 0 deletions src/sdk/main/src/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "AccountId.h"
#include "AddressBookQuery.h"
#include "Defaults.h"
#include "FileId.h"
#include "Hbar.h"
#include "Logger.h"
#include "NodeAddressBook.h"
Expand Down Expand Up @@ -695,6 +696,17 @@ Client& Client::setMirrorNetwork(const std::vector<std::string>& network)
return *this;
}

//-----
void Client::populateNetworkFromMirrorNodeAddressBook()
{
auto network =
std::make_shared<internal::Network>(internal::Network::forNetwork(internal::Network::getNetworkFromAddressBook(
AddressBookQuery().setFileId(FileId::ADDRESS_BOOK).execute(*this), internal::BaseNodeAddress::PORT_NODE_PLAIN)));

std::unique_lock lock(mImpl->mMutex);
mImpl->mNetwork = network;
}

//-----
std::vector<std::string> Client::getMirrorNetwork() const
{
Expand Down

0 comments on commit b6f4663

Please sign in to comment.