Skip to content

Commit

Permalink
167: Fix test SetNetworkIskWorkingCorrectly. Updated class Client.
Browse files Browse the repository at this point in the history
Signed-off-by: Deyan Zhekov <[email protected]>
  • Loading branch information
deyanzz committed Sep 19, 2023
1 parent 6e38fec commit 78c7532
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
8 changes: 8 additions & 0 deletions sdk/main/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, AccountId>& networkMap);

/**
* Set the length of time a request sent by this Client can be processed before it times out.
*
Expand Down
8 changes: 8 additions & 0 deletions sdk/main/src/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ void Client::close() const
}
}

//-----
Client& Client::setNetwork(const std::unordered_map<std::string, AccountId>& networkMap)
{
mImpl->mNetwork = internal::Network::forNetwork(networkMap);
mImpl->mMirrorNetwork = nullptr;
return *this;
}

//-----
Client& Client::setRequestTimeout(const std::chrono::duration<double>& timeout)
{
Expand Down
72 changes: 35 additions & 37 deletions sdk/tests/integration/ClientIntegrationTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* limitations under the License.
*
*/
#include "AccountBalance.h"
#include "AccountBalanceQuery.h"
#include "AccountCreateTransaction.h"
#include "AccountId.h"
#include "AccountInfo.h"
Expand Down Expand Up @@ -128,54 +130,50 @@ TEST_F(ClientIntegrationTest, ConnectToLocalNode)
}

//-----
TEST_F(ClientIntegrationTest, SelectHealtyNode)
TEST_F(ClientIntegrationTest, SetNetworkIskWorkingCorrectly)
{
// Given
const std::unique_ptr<PrivateKey> 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<std::string, AccountId> networkMap;
networkMap.insert(std::pair<std::string, AccountId>("0.testnet.hedera.com:50211", accountId_3));
networkMap.insert(std::pair<std::string, AccountId>("1.testnet.hedera.com:50211", accountId_4));
std::unordered_map<std::string, AccountId> testnetMap;
testnetMap.insert(std::pair<std::string, AccountId>("34.94.106.61:50211", accountId_3));
testnetMap.insert(std::pair<std::string, AccountId>("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<std::string, AccountId> newTestnetMap;
newTestnetMap.insert(std::pair<std::string, AccountId>("35.237.119.55:50211", accountId_4));
newTestnetMap.insert(std::pair<std::string, AccountId>("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();
}

0 comments on commit 78c7532

Please sign in to comment.