From de9ea0dd3312c59027bbfaff7430d3cdf906c451 Mon Sep 17 00:00:00 2001 From: Deyan Zhekov Date: Thu, 28 Sep 2023 14:48:20 +0300 Subject: [PATCH] 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 | 10 +++ .../integration/ClientIntegrationTest.cc | 66 ++++++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/sdk/main/include/Client.h b/sdk/main/include/Client.h index f16672af2..0a26fd776 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 6371ebf27..aa3125dd6 100644 --- a/sdk/main/src/Client.cc +++ b/sdk/main/src/Client.cc @@ -24,6 +24,7 @@ #include "Hbar.h" #include "PrivateKey.h" #include "PublicKey.h" +#include "exceptions/UninitializedException.h" #include "impl/MirrorNetwork.h" #include "impl/Network.h" #include "impl/ValuePtr.h" @@ -171,6 +172,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; } @@ -206,6 +208,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 ace3f6edf..047084253 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -17,28 +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; } @@ -49,6 +62,8 @@ class ClientIntegrationTest : public ::testing::Test [[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"; @@ -113,3 +128,52 @@ TEST_F(ClientIntegrationTest, ConnectToLocalNode) EXPECT_NE(client.getOperatorPublicKey(), nullptr); EXPECT_FALSE(newAccountId.toString().empty()); } + +//----- +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; +} \ No newline at end of file