Skip to content

Commit

Permalink
feat: add Client::forMirrorNetwork() to allow consensus node networ…
Browse files Browse the repository at this point in the history
…k initialization with a mirror node address book (#805)

Signed-off-by: Rob Walworth <[email protected]>
Co-authored-by: gsstoykov <[email protected]>
  • Loading branch information
rwalworth and gsstoykov authored Nov 5, 2024
1 parent 98ff911 commit 91de148
Show file tree
Hide file tree
Showing 4 changed files with 76 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
*
* 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. This will also get the address book from the mirror node and
// use it to populate the Client's consensus network.
Client client = Client::forMirrorNetwork({ "testnet.mirrornode.hedera.com:443" });
client.setOperator(operatorAccountId, operatorPrivateKey);

// 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;
}
10 changes: 10 additions & 0 deletions src/sdk/main/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ class Client
*/
[[nodiscard]] static Client forNetwork(const std::unordered_map<std::string, AccountId>& networkMap);

/**
* Construct a Client pre-configured for a specific mirror network. This will attempt to pull the network address
* book from the input mirror network and establish its consensus network with that.
*
* @param mirrorNetwork The mirror node network from which to grab the address book and initialize the Client's
* consensus network.
* @return A Client with the input mirror network and the corresponding address book consensus network
*/
[[nodiscard]] static Client forMirrorNetwork(const std::vector<std::string>& mirrorNetwork);

/**
* Construct a Client by a name. The name must be one of "mainnet", "testnet", or "previewnet", otherwise this will
* throw std::invalid_argument.
Expand Down
13 changes: 13 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 @@ -173,6 +174,18 @@ Client Client::forNetwork(const std::unordered_map<std::string, AccountId>& netw
return client;
}

//-----
Client Client::forMirrorNetwork(const std::vector<std::string>& mirrorNetwork)
{
Client client;
client.setMirrorNetwork(mirrorNetwork);
client.mImpl->mNetwork =
std::make_shared<internal::Network>(internal::Network::forNetwork(internal::Network::getNetworkFromAddressBook(
AddressBookQuery().setFileId(FileId::ADDRESS_BOOK).execute(client), internal::BaseNodeAddress::PORT_NODE_PLAIN)));

return client;
}

//-----
Client Client::forName(std::string_view name)
{
Expand Down

0 comments on commit 91de148

Please sign in to comment.