diff --git a/sdk/main/include/impl/Network.h b/sdk/main/include/impl/Network.h index bb64c0db3..6ac3d3df0 100644 --- a/sdk/main/include/impl/Network.h +++ b/sdk/main/include/impl/Network.h @@ -120,6 +120,21 @@ class Network : public BaseNetwork */ Network& setMaxNodesPerRequest(unsigned int max); + /** + * Are certificates being verified? + * + * @return A boolean value indicating if the certificates are being verified. + */ + [[nodiscard]] bool isVerifyCertificates() const { return mVerifyCertificates; } + + /** + * Get a list of node account IDs on which to execute. This will pick 1/3 of the available nodes sorted by health and + * expected delay from the network. + * + * @return A list of AccountIds that are running nodes on which should be executed. + */ + [[nodiscard]] unsigned int getNumberOfNodesForRequest() const; + /** * Get a list of node account IDs on which to execute. This will pick 1/3 of the available nodes sorted by health and * expected delay from the network. diff --git a/sdk/main/src/impl/Network.cc b/sdk/main/src/impl/Network.cc index a12022bc6..dd828b827 100644 --- a/sdk/main/src/impl/Network.cc +++ b/sdk/main/src/impl/Network.cc @@ -26,8 +26,8 @@ #include #include -#include #include +#include namespace Hedera::internal { @@ -102,6 +102,17 @@ Network& Network::setMaxNodesPerRequest(unsigned int max) return *this; } +//----- +unsigned int Network::getNumberOfNodesForRequest() const +{ + if (mMaxNodesPerRequest > 0) + { + return mMaxNodesPerRequest; + } + + return (getNetworkInternal().size() + 3 - 1) / 3; +} + //----- Network& Network::setTransportSecurity(TLSBehavior tls) { @@ -190,7 +201,7 @@ NodeAddressBook Network::getAddressBookForLedgerId(const LedgerId& ledgerId) return {}; } - std::string buildPath = std::filesystem::current_path().string() + "/addressbook/" + ledgerId.toString()+".pb"; + std::string buildPath = std::filesystem::current_path().string() + "/addressbook/" + ledgerId.toString() + ".pb"; std::ifstream infile(buildPath, std::ios_base::binary); return NodeAddressBook::fromBytes({ std::istreambuf_iterator(infile), std::istreambuf_iterator() }); } diff --git a/sdk/tests/unit/CMakeLists.txt b/sdk/tests/unit/CMakeLists.txt index 2f2f78e91..517156e6c 100644 --- a/sdk/tests/unit/CMakeLists.txt +++ b/sdk/tests/unit/CMakeLists.txt @@ -61,6 +61,7 @@ add_executable(${TEST_PROJECT_NAME} HbarTransferTest.cc KeyListTest.cc LedgerIdTest.cc + NetworkUnitTests.cc NetworkVersionInfoUnitTests.cc NftIdTest.cc NodeAddressTest.cc @@ -133,8 +134,8 @@ gtest_discover_tests(${TEST_PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINA target_link_libraries(${TEST_PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json) file(COPY ${PROJECT_SOURCE_DIR}/addressbook/previewnet.pb - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}) + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/addressbook) file(COPY ${PROJECT_SOURCE_DIR}/addressbook/testnet.pb - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}) + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/addressbook) file(COPY ${PROJECT_SOURCE_DIR}/addressbook/mainnet.pb - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}) + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/addressbook) diff --git a/sdk/tests/unit/NetworkUnitTests.cc b/sdk/tests/unit/NetworkUnitTests.cc new file mode 100644 index 000000000..ff6ebaff5 --- /dev/null +++ b/sdk/tests/unit/NetworkUnitTests.cc @@ -0,0 +1,175 @@ +/*- + * + * Hedera C++ SDK + * + * Copyright (C) 2020 - 2022 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 "AccountId.h" +#include "impl/Network.h" +#include +#include +#include + +#include + +using namespace Hedera; + +class NetworkUnitTests : public ::testing::Test +{ +}; + +//----- +TEST_F(NetworkUnitTests, ConstructForMainnet) +{ + // Given / When + Hedera::internal::Network mainnetNetwork = Hedera::internal::Network::forMainnet(); + + // Then + std::unordered_map networkMap; + std::vector nodeAccountIds; + + EXPECT_NO_THROW(networkMap = mainnetNetwork.getNetwork()); + EXPECT_NO_THROW(nodeAccountIds = mainnetNetwork.getNodeAccountIdsForExecute()); + + EXPECT_GT(networkMap.size(), 0); + EXPECT_GT(nodeAccountIds.size(), 0); + + // Clean up + mainnetNetwork.close(); +} + +TEST_F(NetworkUnitTests, ConstructForTestnet) +{ + // Given / When + Hedera::internal::Network testnetNetwork = Hedera::internal::Network::forTestnet(); + + // Then + std::unordered_map networkMap; + std::vector nodeAccountIds; + + EXPECT_NO_THROW(networkMap = testnetNetwork.getNetwork()); + EXPECT_NO_THROW(nodeAccountIds = testnetNetwork.getNodeAccountIdsForExecute()); + + EXPECT_GT(networkMap.size(), 0); + EXPECT_GT(nodeAccountIds.size(), 0); + + // Clean up + testnetNetwork.close(); +} + +TEST_F(NetworkUnitTests, ConstructForPreviewnet) +{ + // Given / When + Hedera::internal::Network previewnetNetwork = Hedera::internal::Network::forPreviewnet(); + + // Then + std::unordered_map networkMap; + std::vector nodeAccountIds; + + EXPECT_NO_THROW(networkMap = previewnetNetwork.getNetwork()); + EXPECT_NO_THROW(nodeAccountIds = previewnetNetwork.getNodeAccountIdsForExecute()); + + EXPECT_GT(networkMap.size(), 0); + EXPECT_GT(nodeAccountIds.size(), 0); + + // Clean up + previewnetNetwork.close(); +} + +TEST_F(NetworkUnitTests, ConstructCustomNetwork) +{ + // Given + const std::unordered_map testNetwork = { + {"2.testnet.hedera.com:50211", AccountId(5ULL)}, + { "3.testnet.hedera.com:50211", AccountId(6ULL)} + }; + + // When + Hedera::internal::Network customNetwork = Hedera::internal::Network::forNetwork(testNetwork); + + // Then + std::unordered_map networkMap; + std::vector nodeAccountIds; + + EXPECT_NO_THROW(networkMap = customNetwork.getNetwork()); + EXPECT_NO_THROW(nodeAccountIds = customNetwork.getNodeAccountIdsForExecute()); + + EXPECT_GT(networkMap.size(), 0); + EXPECT_GT(nodeAccountIds.size(), 0); + + // Clean up + customNetwork.close(); +} + +TEST_F(NetworkUnitTests, GetSetLedgerIdForMainnet) +{ + // Given + Hedera::internal::Network mainnetNetwork = Hedera::internal::Network::forMainnet(); + + // When + mainnetNetwork.setLedgerId(LedgerId::TESTNET); + + // Then + EXPECT_EQ(mainnetNetwork.getLedgerId(), LedgerId::TESTNET); + + // Clean up + mainnetNetwork.close(); +} + +TEST_F(NetworkUnitTests, GetSetTransportSecurity) +{ + // Given + Hedera::internal::Network testnetNetwork = Hedera::internal::Network::forTestnet(); + + // When + testnetNetwork.setTransportSecurity(Hedera::internal::TLSBehavior::DISABLE); + + // Then + EXPECT_EQ(testnetNetwork.isTransportSecurity(), Hedera::internal::TLSBehavior::DISABLE); + + // Clean up + testnetNetwork.close(); +} + +TEST_F(NetworkUnitTests, GetSetGetMaxNodesPerRequest) +{ + // Given + Hedera::internal::Network testnetNetwork = Hedera::internal::Network::forTestnet(); + + // When + testnetNetwork.setMaxNodesPerRequest(10); + + // Then + EXPECT_EQ(testnetNetwork.getNumberOfNodesForRequest(), 10); + + // Clean up + testnetNetwork.close(); +} + +TEST_F(NetworkUnitTests, VerifyCertificates) +{ + // Given + Hedera::internal::Network testnetNetwork = Hedera::internal::Network::forTestnet(); + + // When + testnetNetwork.setVerifyCertificates(false); + + // Then + EXPECT_EQ(testnetNetwork.isVerifyCertificates(), false); + + // Clean up + testnetNetwork.close(); +} \ No newline at end of file