From 1abb56dd5a347d75e67cac18befaff2c58df1c47 Mon Sep 17 00:00:00 2001 From: Deyan Zhekov Date: Thu, 28 Sep 2023 14:48:20 +0300 Subject: [PATCH 1/5] 167: Add missing methods in Client. Add new test in ClientIntegrationTest. Signed-off-by: Deyan Zhekov --- sdk/main/include/Client.h | 8 +++ sdk/main/src/Client.cc | 9 +++ .../integration/ClientIntegrationTest.cc | 69 ++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/sdk/main/include/Client.h b/sdk/main/include/Client.h index bb283a55d..0d185b794 100644 --- a/sdk/main/include/Client.h +++ b/sdk/main/include/Client.h @@ -121,6 +121,14 @@ class Client */ void close(); + /** + * Replace all nodes in this Client with a new set of nodes from the Hedera network. + * + * @param networkMap The map with string representation of node addresses with their corresponding accountId. + * @return A reference to this Client object with the newly-set operator account ID from the map. + */ + Client& setNetwork(const std::unordered_map& networkMap); + /** * Set the length of time a request sent by this Client can be processed before it times out. * diff --git a/sdk/main/src/Client.cc b/sdk/main/src/Client.cc index 9639c0748..2f07762d1 100644 --- a/sdk/main/src/Client.cc +++ b/sdk/main/src/Client.cc @@ -174,6 +174,7 @@ Client Client::forNetwork(const std::unordered_map& netw { Client client; client.mImpl->mNetwork = std::make_shared(internal::Network::forNetwork(networkMap)); + client.mImpl->mMirrorNetwork = nullptr; return client; } @@ -209,6 +210,14 @@ void Client::close() } } +//----- +Client& Client::setNetwork(const std::unordered_map& networkMap) +{ + mImpl->mNetwork = std::make_shared(internal::Network::forNetwork(networkMap)); + mImpl->mMirrorNetwork = nullptr; + return *this; +} + //----- Client& Client::setRequestTimeout(const std::chrono::duration& timeout) { diff --git a/sdk/tests/integration/ClientIntegrationTest.cc b/sdk/tests/integration/ClientIntegrationTest.cc index 4112822b3..d9ef2e009 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -17,27 +17,41 @@ * limitations under the License. * */ +#include "AccountBalance.h" +#include "AccountBalanceQuery.h" #include "AccountCreateTransaction.h" #include "AccountId.h" +#include "AccountInfo.h" +#include "AccountInfoQuery.h" +#include "BaseIntegrationTest.h" #include "Client.h" #include "ED25519PrivateKey.h" #include "Hbar.h" #include "PublicKey.h" #include "TransactionReceipt.h" +#include "TransactionRecord.h" #include "TransactionResponse.h" +#include "TransferTransaction.h" +#include "exceptions/UninitializedException.h" +#include "impl/Utilities.h" #include #include #include #include #include +#include +#include +#include using json = nlohmann::json; +using namespace std; using namespace Hedera; -class ClientIntegrationTest : public ::testing::Test +class ClientIntegrationTest : public BaseIntegrationTest { protected: + // [[nodiscard]] inline const Client& getTestClient() const { return mClient; } [[nodiscard]] inline const std::string_view& getJsonNetworkTag() const { return mJsonNetworkTag; } [[nodiscard]] inline const std::string_view& getJsonOperatorTag() const { return mJsonOperatorTag; } [[nodiscard]] inline const std::string_view& getJsonAccountIdTag() const { return mJsonAccountIdTag; } @@ -53,6 +67,8 @@ class ClientIntegrationTest : public ::testing::Test [[nodiscard]] inline const std::chrono::milliseconds getAboveMaxBackoffTime() const { return mAboveMaxBackoffTime; } private: + // Client mClient; + const std::string_view mJsonNetworkTag = "network"; const std::string_view mJsonOperatorTag = "operator"; const std::string_view mJsonAccountIdTag = "accountId"; @@ -172,4 +188,53 @@ TEST_F(ClientIntegrationTest, SetValidMaxBackoff) // When / Then EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MIN_BACKOFF)); EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MAX_BACKOFF)); -} \ No newline at end of file +} + +//----- +TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly) +{ + // Given + const std::unique_ptr myPrivateKey = ED25519PrivateKey::fromString( + "302e020100300506032b6570042204202e000363977258a41f418cf84a7df9cf1e8ae98b72de86803e64846defa43054"); + const AccountId accountId_3 = AccountId::fromString("0.0.3"); + const AccountId accountId_4 = AccountId::fromString("0.0.4"); + const AccountId accountId_5 = AccountId::fromString("0.0.5"); + + std::unordered_map testnetMap; + testnetMap.insert(std::pair("34.94.106.61:50211", accountId_3)); + testnetMap.insert(std::pair("35.237.119.55:50211", accountId_4)); + + cout << "!!! Got accountId_3 & accountId_4 !!!" << endl << endl; + + Client client = Client::forNetwork(testnetMap); + + cout << "!!! START !!!" << endl << endl; + + // Given + AccountBalance accountBalance_3; + AccountBalance accountBalance_4; + AccountBalance accountBalance_5; + + ASSERT_NO_THROW(accountBalance_3 = AccountBalanceQuery().setAccountId(accountId_3).execute(client)); + ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client)); + + cout << "Balance for Account 3: " << accountBalance_3.getBalance().toTinybars() << " tynibars." << endl; + cout << "Balance for Account 4: " << accountBalance_4.getBalance().toTinybars() << " tynibars." << endl << endl; + + // When / Then + std::unordered_map newTestnetMap; + newTestnetMap.insert(std::pair("35.237.119.55:50211", accountId_4)); + newTestnetMap.insert(std::pair("35.245.27.193:50211", accountId_5)); + + client.setNetwork(newTestnetMap); + + cout << "!!! newTestnetMap was configured !!!" << endl << endl; + + ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client)); + ASSERT_NO_THROW(accountBalance_5 = AccountBalanceQuery().setAccountId(accountId_5).execute(client)); + + cout << "Balance for Account 4: " << accountBalance_4.getBalance().toTinybars() << " tynibars." << endl; + cout << "Balance for Account 5: " << accountBalance_5.getBalance().toTinybars() << " tynibars." << endl << endl; + + cout << "!!! END !!!" << endl << endl; +} From 894a45f413f8b413446379ae8a6a374322c02b52 Mon Sep 17 00:00:00 2001 From: Deyan Zhekov Date: Thu, 5 Oct 2023 17:32:42 +0300 Subject: [PATCH 2/5] 397: Remove comments Signed-off-by: Deyan Zhekov --- sdk/tests/integration/ClientIntegrationTest.cc | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/sdk/tests/integration/ClientIntegrationTest.cc b/sdk/tests/integration/ClientIntegrationTest.cc index d9ef2e009..ef9f065d1 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -191,7 +191,7 @@ TEST_F(ClientIntegrationTest, SetValidMaxBackoff) } //----- -TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly) +TEST_F(ClientIntegrationTest, SetNetworkIsWorkingCorrectly) { // Given const std::unique_ptr myPrivateKey = ED25519PrivateKey::fromString( @@ -204,12 +204,8 @@ TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly) testnetMap.insert(std::pair("34.94.106.61:50211", accountId_3)); testnetMap.insert(std::pair("35.237.119.55:50211", accountId_4)); - cout << "!!! Got accountId_3 & accountId_4 !!!" << endl << endl; - Client client = Client::forNetwork(testnetMap); - cout << "!!! START !!!" << endl << endl; - // Given AccountBalance accountBalance_3; AccountBalance accountBalance_4; @@ -218,9 +214,6 @@ TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly) ASSERT_NO_THROW(accountBalance_3 = AccountBalanceQuery().setAccountId(accountId_3).execute(client)); ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client)); - cout << "Balance for Account 3: " << accountBalance_3.getBalance().toTinybars() << " tynibars." << endl; - cout << "Balance for Account 4: " << accountBalance_4.getBalance().toTinybars() << " tynibars." << endl << endl; - // When / Then std::unordered_map newTestnetMap; newTestnetMap.insert(std::pair("35.237.119.55:50211", accountId_4)); @@ -228,13 +221,6 @@ TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly) client.setNetwork(newTestnetMap); - cout << "!!! newTestnetMap was configured !!!" << endl << endl; - ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client)); ASSERT_NO_THROW(accountBalance_5 = AccountBalanceQuery().setAccountId(accountId_5).execute(client)); - - cout << "Balance for Account 4: " << accountBalance_4.getBalance().toTinybars() << " tynibars." << endl; - cout << "Balance for Account 5: " << accountBalance_5.getBalance().toTinybars() << " tynibars." << endl << endl; - - cout << "!!! END !!!" << endl << endl; } From 191d6506134a21971575ac68acf8da885625be1b Mon Sep 17 00:00:00 2001 From: Deyan Zhekov Date: Wed, 11 Oct 2023 19:03:34 +0300 Subject: [PATCH 3/5] 167: Changes for test SetNetworkIsWorkingCorrectly. Signed-off-by: Deyan Zhekov --- .../integration/ClientIntegrationTest.cc | 95 ++++--------------- sdk/tests/unit/ClientTest.cc | 61 ++++++++++++ 2 files changed, 78 insertions(+), 78 deletions(-) diff --git a/sdk/tests/integration/ClientIntegrationTest.cc b/sdk/tests/integration/ClientIntegrationTest.cc index ef9f065d1..995fcb14b 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -61,11 +61,6 @@ class ClientIntegrationTest : public BaseIntegrationTest [[nodiscard]] inline const AccountId& getAccountId() const { return mAccountId; } [[nodiscard]] inline const std::string getPathToJSON() const { return mFilePath.string(); } - [[nodiscard]] inline const std::chrono::milliseconds getNegativeBackoffTime() const { return mNegativeBackoffTime; } - [[nodiscard]] inline const std::chrono::milliseconds getZeroBackoffTime() const { return mZeroBackoffTime; } - [[nodiscard]] inline const std::chrono::milliseconds getBelowMinBackoffTime() const { return mBelowMinBackoffTime; } - [[nodiscard]] inline const std::chrono::milliseconds getAboveMaxBackoffTime() const { return mAboveMaxBackoffTime; } - private: // Client mClient; @@ -74,11 +69,6 @@ class ClientIntegrationTest : public BaseIntegrationTest const std::string_view mJsonAccountIdTag = "accountId"; const std::string_view mJsonPrivateKeyTag = "privateKey"; - const std::chrono::milliseconds mNegativeBackoffTime = std::chrono::milliseconds(-1); - const std::chrono::milliseconds mZeroBackoffTime = std::chrono::milliseconds(0); - const std::chrono::milliseconds mBelowMinBackoffTime = DEFAULT_MIN_BACKOFF - std::chrono::milliseconds(1); - const std::chrono::milliseconds mAboveMaxBackoffTime = DEFAULT_MAX_BACKOFF + std::chrono::milliseconds(1); - const std::string_view mAccountIdStr = "0.0.3"; const AccountId mAccountId = AccountId::fromString("0.0.3"); const std::filesystem::path mFilePath = (std::filesystem::current_path() / "local_node.json").string(); @@ -139,88 +129,37 @@ TEST_F(ClientIntegrationTest, ConnectToLocalNode) EXPECT_FALSE(newAccountId.toString().empty()); } -//----- -TEST_F(ClientIntegrationTest, SetInvalidMinBackoff) -{ - // Given - std::unordered_map networkMap; - Client client = Client::forNetwork(networkMap); - - // When / Then - EXPECT_THROW(client.setMinBackoff(getNegativeBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT - EXPECT_THROW(client.setMinBackoff(getAboveMaxBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT -} - -//----- -TEST_F(ClientIntegrationTest, SetValidMinBackoff) -{ - // Given - std::unordered_map networkMap; - Client client = Client::forNetwork(networkMap); - - // When / Then - EXPECT_NO_THROW(client.setMinBackoff(getZeroBackoffTime())); - EXPECT_NO_THROW(client.setMinBackoff(DEFAULT_MIN_BACKOFF)); - EXPECT_NO_THROW(client.setMinBackoff(DEFAULT_MAX_BACKOFF)); -} - -//----- -TEST_F(ClientIntegrationTest, SetInvalidMaxBackoff) -{ - // Given - std::unordered_map networkMap; - Client client = Client::forNetwork(networkMap); - - // When / Then - EXPECT_THROW(client.setMaxBackoff(getNegativeBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT - EXPECT_THROW(client.setMaxBackoff(getZeroBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT - EXPECT_THROW(client.setMaxBackoff(getBelowMinBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT - EXPECT_THROW(client.setMaxBackoff(getAboveMaxBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT -} - -//----- -TEST_F(ClientIntegrationTest, SetValidMaxBackoff) -{ - // Given - std::unordered_map networkMap; - Client client = Client::forNetwork(networkMap); - - // When / Then - EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MIN_BACKOFF)); - EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MAX_BACKOFF)); -} - //----- TEST_F(ClientIntegrationTest, SetNetworkIsWorkingCorrectly) { // Given - const std::unique_ptr myPrivateKey = ED25519PrivateKey::fromString( - "302e020100300506032b6570042204202e000363977258a41f418cf84a7df9cf1e8ae98b72de86803e64846defa43054"); const AccountId accountId_3 = AccountId::fromString("0.0.3"); const AccountId accountId_4 = AccountId::fromString("0.0.4"); const AccountId accountId_5 = AccountId::fromString("0.0.5"); + const AccountId accountId_6 = AccountId::fromString("0.0.6"); + const AccountId accountId_7 = AccountId::fromString("0.0.7"); - std::unordered_map testnetMap; - testnetMap.insert(std::pair("34.94.106.61:50211", accountId_3)); - testnetMap.insert(std::pair("35.237.119.55:50211", accountId_4)); - - Client client = Client::forNetwork(testnetMap); - - // Given AccountBalance accountBalance_3; AccountBalance accountBalance_4; AccountBalance accountBalance_5; + AccountBalance accountBalance_6; + AccountBalance accountBalance_7; - ASSERT_NO_THROW(accountBalance_3 = AccountBalanceQuery().setAccountId(accountId_3).execute(client)); - ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client)); + std::unordered_map networkMap; + networkMap.insert(std::pair("34.94.106.61:50211", accountId_3)); + networkMap.insert(std::pair("35.237.119.55:50211", accountId_4)); + networkMap.insert(std::pair("35.245.27.193:50211", accountId_5)); + + Client client = Client::forNetwork(networkMap); // When / Then - std::unordered_map newTestnetMap; - newTestnetMap.insert(std::pair("35.237.119.55:50211", accountId_4)); - newTestnetMap.insert(std::pair("35.245.27.193:50211", accountId_5)); + std::unordered_map newNetworkMap; + newNetworkMap.insert(std::pair("35.237.119.55:50211", accountId_6)); + newNetworkMap.insert(std::pair("35.245.27.193:50211", accountId_7)); - client.setNetwork(newTestnetMap); + client.setNetwork(newNetworkMap); - ASSERT_NO_THROW(accountBalance_4 = AccountBalanceQuery().setAccountId(accountId_4).execute(client)); - ASSERT_NO_THROW(accountBalance_5 = AccountBalanceQuery().setAccountId(accountId_5).execute(client)); + // When / Then + ASSERT_NO_THROW(accountBalance_6 = AccountBalanceQuery().setAccountId(accountId_6).execute(client)); + ASSERT_NO_THROW(accountBalance_7 = AccountBalanceQuery().setAccountId(accountId_7).execute(client)); } diff --git a/sdk/tests/unit/ClientTest.cc b/sdk/tests/unit/ClientTest.cc index 96686d7ea..d6db74f3c 100644 --- a/sdk/tests/unit/ClientTest.cc +++ b/sdk/tests/unit/ClientTest.cc @@ -42,10 +42,20 @@ class ClientTest : public ::testing::Test return mTestNetworkUpdatePeriod; } + [[nodiscard]] inline const std::chrono::milliseconds getNegativeBackoffTime() const { return mNegativeBackoffTime; } + [[nodiscard]] inline const std::chrono::milliseconds getZeroBackoffTime() const { return mZeroBackoffTime; } + [[nodiscard]] inline const std::chrono::milliseconds getBelowMinBackoffTime() const { return mBelowMinBackoffTime; } + [[nodiscard]] inline const std::chrono::milliseconds getAboveMaxBackoffTime() const { return mAboveMaxBackoffTime; } + private: const AccountId mAccountId = AccountId(10ULL); const std::unique_ptr mPrivateKey = ED25519PrivateKey::generatePrivateKey(); const std::chrono::duration mTestNetworkUpdatePeriod = std::chrono::seconds(2); + + const std::chrono::milliseconds mNegativeBackoffTime = std::chrono::milliseconds(-1); + const std::chrono::milliseconds mZeroBackoffTime = std::chrono::milliseconds(0); + const std::chrono::milliseconds mBelowMinBackoffTime = DEFAULT_MIN_BACKOFF - std::chrono::milliseconds(1); + const std::chrono::milliseconds mAboveMaxBackoffTime = DEFAULT_MAX_BACKOFF + std::chrono::milliseconds(1); }; //----- @@ -108,3 +118,54 @@ TEST_F(ClientTest, SetNetworkUpdatePeriod) // Then EXPECT_EQ(client.getNetworkUpdatePeriod(), getTestNetworkUpdatePeriod()); } + +//----- +TEST_F(ClientTest, SetInvalidMinBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_THROW(client.setMinBackoff(getNegativeBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMinBackoff(getAboveMaxBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT +} + +//----- +TEST_F(ClientTest, SetValidMinBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_NO_THROW(client.setMinBackoff(getZeroBackoffTime())); + EXPECT_NO_THROW(client.setMinBackoff(DEFAULT_MIN_BACKOFF)); + EXPECT_NO_THROW(client.setMinBackoff(DEFAULT_MAX_BACKOFF)); +} + +//----- +TEST_F(ClientTest, SetInvalidMaxBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_THROW(client.setMaxBackoff(getNegativeBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMaxBackoff(getZeroBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMaxBackoff(getBelowMinBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMaxBackoff(getAboveMaxBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT +} + +//----- +TEST_F(ClientTest, SetValidMaxBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MIN_BACKOFF)); + EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MAX_BACKOFF)); +} \ No newline at end of file From b8259a713eabcbeb554889a5e5e9a602f4690bac Mon Sep 17 00:00:00 2001 From: Deyan Zhekov Date: Wed, 11 Oct 2023 21:47:21 +0300 Subject: [PATCH 4/5] 167: Resolve comments during PR. Signed-off-by: Deyan Zhekov --- sdk/tests/integration/ClientIntegrationTest.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/sdk/tests/integration/ClientIntegrationTest.cc b/sdk/tests/integration/ClientIntegrationTest.cc index 995fcb14b..8a8ca118e 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -51,7 +51,6 @@ using namespace Hedera; class ClientIntegrationTest : public BaseIntegrationTest { protected: - // [[nodiscard]] inline const Client& getTestClient() const { return mClient; } [[nodiscard]] inline const std::string_view& getJsonNetworkTag() const { return mJsonNetworkTag; } [[nodiscard]] inline const std::string_view& getJsonOperatorTag() const { return mJsonOperatorTag; } [[nodiscard]] inline const std::string_view& getJsonAccountIdTag() const { return mJsonAccountIdTag; } @@ -62,8 +61,6 @@ class ClientIntegrationTest : public BaseIntegrationTest [[nodiscard]] inline const std::string getPathToJSON() const { return mFilePath.string(); } private: - // Client mClient; - const std::string_view mJsonNetworkTag = "network"; const std::string_view mJsonOperatorTag = "operator"; const std::string_view mJsonAccountIdTag = "accountId"; From c1f5b98eb183ac39d5bf12e84ec01d33f628e375 Mon Sep 17 00:00:00 2001 From: Deyan Zhekov Date: Wed, 11 Oct 2023 21:49:30 +0300 Subject: [PATCH 5/5] 167: Fix for failing pipeline. Signed-off-by: Deyan Zhekov --- .github/workflows/zxc-build-library.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/zxc-build-library.yaml b/.github/workflows/zxc-build-library.yaml index 2fcda8ce8..4eb38b2d4 100644 --- a/.github/workflows/zxc-build-library.yaml +++ b/.github/workflows/zxc-build-library.yaml @@ -39,7 +39,7 @@ jobs: sudo apt-get update sudo apt-get install -y --allow-downgrades \ ca-certificates \ - curl=7.81.0-1ubuntu1.13 \ + curl=7.81.0-1ubuntu1.14 \ gnupg \ lsb-release sudo mkdir -p /etc/apt/keyrings