diff --git a/sdk/main/include/Client.h b/sdk/main/include/Client.h index 546924dfb..cd91b66c9 100644 --- a/sdk/main/include/Client.h +++ b/sdk/main/include/Client.h @@ -141,6 +141,14 @@ class Client */ void close() const; + /** + * 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 bf04ababb..8ed4e8cec 100644 --- a/sdk/main/src/Client.cc +++ b/sdk/main/src/Client.cc @@ -191,6 +191,14 @@ void Client::close() const } } +//----- +Client& Client::setNetwork(const std::unordered_map& networkMap) +{ + mImpl->mNetwork = 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 06839804d..047084253 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -17,6 +17,8 @@ * limitations under the License. * */ +#include "AccountBalance.h" +#include "AccountBalanceQuery.h" #include "AccountCreateTransaction.h" #include "AccountId.h" #include "AccountInfo.h" @@ -128,54 +130,50 @@ TEST_F(ClientIntegrationTest, ConnectToLocalNode) } //----- -TEST_F(ClientIntegrationTest, SelectHealtyNode) +TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly) { // Given const std::unique_ptr myPrivateKey = ED25519PrivateKey::fromString( "302e020100300506032b6570042204202e000363977258a41f418cf84a7df9cf1e8ae98b72de86803e64846defa43054"); - const Hbar amount(-1); - const AccountId accountId_3 = AccountId(3); - const AccountId accountId_4 = AccountId(4); + 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 networkMap; - networkMap.insert(std::pair("0.testnet.hedera.com:50211", accountId_3)); - networkMap.insert(std::pair("1.testnet.hedera.com:50211", accountId_4)); + 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(networkMap); + Client client = Client::forNetwork(testnetMap); cout << "!!! START !!!" << endl << endl; - cout << std::chrono::system_clock::now().time_since_epoch().count() << endl << endl; - for (int i = 0; i < 1000; i++) - { - try - { - cout << "START for-loop !!!" << endl; - - TransactionReceipt txReceipt; - EXPECT_NO_THROW(txReceipt = TransferTransaction() - .addHbarTransfer(myAccountId, amount) - .addHbarTransfer(newAccountId, amount.negated()) - .freezeWith(client) - .sign(myPrivateKey.get()) - .execute(client) - .getReceipt(client)); - - cout << "The transfer receipt for iteration " << (int)txReceipt.getStatus() << " is" << endl; - - AccountInfo accountInfo; - EXPECT_NO_THROW(accountInfo = AccountInfoQuery().setAccountId(newAccountId).execute(client)); - - cout << "First Account accountInfo: " << accountInfo.mBalance.toTinybars() << endl; - } - catch (const std::exception&) - { - throw std::invalid_argument("DAMN"); - } - } + // 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; - cout << std::chrono::system_clock::now().time_since_epoch().count(); } \ No newline at end of file