Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Networking Configuration (PR) #515

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions sdk/main/include/impl/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ class Network : public BaseNetwork<Network, AccountId, Node>
*/
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.
Expand Down
15 changes: 13 additions & 2 deletions sdk/main/src/impl/Network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

#include <algorithm>
#include <cmath>
#include <fstream>
#include <filesystem>
#include <fstream>

namespace Hedera::internal
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<char>(infile), std::istreambuf_iterator<char>() });
}
Expand Down
7 changes: 4 additions & 3 deletions sdk/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ add_executable(${TEST_PROJECT_NAME}
HbarTransferTest.cc
KeyListTest.cc
LedgerIdTest.cc
NetworkUnitTests.cc
NetworkVersionInfoUnitTests.cc
NftIdTest.cc
NodeAddressTest.cc
Expand Down Expand Up @@ -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)
175 changes: 175 additions & 0 deletions sdk/tests/unit/NetworkUnitTests.cc
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <unordered_map>
#include <vector>

#include <gtest/gtest.h>

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<std::string, AccountId> networkMap;
std::vector<AccountId> 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<std::string, AccountId> networkMap;
std::vector<AccountId> 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<std::string, AccountId> networkMap;
std::vector<AccountId> 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<std::string, AccountId> 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<std::string, AccountId> networkMap;
std::vector<AccountId> 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();
}
Loading